[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[133] | 3 | ; @file_comments |
---|
[136] | 4 | ; Calculate the Julian Day Number for a given month, day, and year. |
---|
| 5 | ; This is the inverse of the library function CALDAT. |
---|
[226] | 6 | ; 3 calendars are available according to the value of key_caltype |
---|
[137] | 7 | ; (variable of the common file cm_4cal): 'greg', '360d', 'noleap' |
---|
[9] | 8 | ; |
---|
[133] | 9 | ; @categories Calendar |
---|
[2] | 10 | ; |
---|
[226] | 11 | ; @param MONTH {in}{required} |
---|
| 12 | ; Number of the desired month (1 = January, ..., 12 = December). |
---|
[136] | 13 | ; Can be scalar or array |
---|
[2] | 14 | ; |
---|
[226] | 15 | ; @param DAY {in}{required} |
---|
[136] | 16 | ; Number of day of the month.Can be scalar or array |
---|
[2] | 17 | ; |
---|
[226] | 18 | ; @param YEARin {in}{required} |
---|
[137] | 19 | ; Number of the desired year.Year parameters must be valid |
---|
[136] | 20 | ; values from the civil calendar. Years B.C.E. are represented |
---|
[137] | 21 | ; as negative integers. Years in the common era are represented |
---|
| 22 | ; as positive integers. In particular, note that there is no |
---|
[136] | 23 | ; year 0 in the civil calendar. 1 B.C.E. (-1) is followed by |
---|
[226] | 24 | ; 1 C.E. (1). |
---|
| 25 | ; Change: However for climatological year, we do accept the year |
---|
[137] | 26 | ; O but we change it for year 654321L (the same trick is done in |
---|
| 27 | ; caldat so caldat, julday(1,1,0) gives you back Jan 1st of year 0) |
---|
| 28 | ; Can be scalar or array |
---|
[2] | 29 | ; |
---|
[226] | 30 | ; @param HOUR {in}{optional}{default=12} |
---|
[136] | 31 | ; Number of the hour of the day. Can be scalar or array |
---|
[2] | 32 | ; |
---|
[137] | 33 | ; @param MINUTE {in}{optional}{default=0} |
---|
[136] | 34 | ; Number of the minute of the hour. Can be scalar or array |
---|
[2] | 35 | ; |
---|
[137] | 36 | ; @param SECOND {in}{optional}{default=0} |
---|
[136] | 37 | ; Number of the second of the minute. Can be scalar or array |
---|
[2] | 38 | ; |
---|
[226] | 39 | ; @restrictions |
---|
[136] | 40 | ; The Result will have the same dimensions as the smallest array, or |
---|
| 41 | ; will be a scalar if all arguments are scalars. |
---|
[226] | 42 | ; |
---|
| 43 | ; @keyword NDAYSPM {default=30} |
---|
[137] | 44 | ; To use a calendar with fixed number of days per months. |
---|
| 45 | ; see also the use of key_caltype (variable of the common file cm_4cal) |
---|
[9] | 46 | ; |
---|
[226] | 47 | ; @returns |
---|
[136] | 48 | ; the Julian Day Number (which begins at noon) of the specified calendar date. |
---|
[226] | 49 | ; If Hour, Minute, and Second are not specified, then the result will be a |
---|
| 50 | ; long integer, otherwise the result is a double precision floating point |
---|
[136] | 51 | ; number. |
---|
[2] | 52 | ; |
---|
[133] | 53 | ; @uses cm_4cal |
---|
[2] | 54 | ; |
---|
[226] | 55 | ; @restrictions |
---|
[136] | 56 | ; Accuracy using IEEE double precision numbers is approximately |
---|
| 57 | ; 1/10000th of a second, with higher accuracy for smaller (earlier) |
---|
| 58 | ; Julian dates. |
---|
[2] | 59 | ; |
---|
[226] | 60 | ; @history |
---|
[137] | 61 | ; Translated from "Numerical Recipies in C", by William H. Press, |
---|
[136] | 62 | ; Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. |
---|
| 63 | ; Cambridge University Press, 1988 (second printing). |
---|
[2] | 64 | ; |
---|
[136] | 65 | ; AB, September, 1988 |
---|
| 66 | ; DMS, April, 1995, Added time of day. |
---|
[2] | 67 | ; |
---|
[136] | 68 | ; Eric Guilyardi, June 1999 |
---|
[137] | 69 | ; Added key_work ndayspm for fixed number of days per months |
---|
[9] | 70 | ; |
---|
[137] | 71 | ; CT, April 2000, Now accepts vectors or scalars. |
---|
| 72 | ; |
---|
[136] | 73 | ; Sebastien Masson, Aug. 2003 |
---|
| 74 | ; fix bug for negative and large values of month values |
---|
| 75 | ; eg. julday(349,1,1970) |
---|
[9] | 76 | ; |
---|
[226] | 77 | ; Sebastien Masson, May 2006, add different calendat with key_caltype |
---|
[137] | 78 | ; (variable of the common file cm_4cal) |
---|
[133] | 79 | ; |
---|
[226] | 80 | ; @version |
---|
| 81 | ; $Id$ |
---|
[2] | 82 | ;- |
---|
| 83 | ; |
---|
[163] | 84 | function julday, MONTH, DAY, YEARin, Hour, Minute, Second, NDAYSPM = ndayspm |
---|
[9] | 85 | ;------------------------------------------------------------ |
---|
| 86 | @cm_4cal |
---|
| 87 | ;------------------------------------------------------------ |
---|
[2] | 88 | |
---|
[9] | 89 | COMPILE_OPT idl2 |
---|
[2] | 90 | |
---|
[9] | 91 | ON_ERROR, 2 ; Return to caller if errors |
---|
[2] | 92 | |
---|
[9] | 93 | IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' |
---|
| 94 | if keyword_set(ndayspm) then key_caltype = '360d' |
---|
| 95 | ; |
---|
[69] | 96 | YEAR = long(yearin) |
---|
| 97 | zero = where(year EQ 0, cnt) |
---|
| 98 | IF cnt NE 0 THEN YEAR[zero] = 654321L |
---|
| 99 | ; |
---|
[9] | 100 | CASE key_caltype OF |
---|
| 101 | 'greg':BEGIN |
---|
| 102 | |
---|
[136] | 103 | ; Gregorian Calender was adopted on Oct. 15, 1582 |
---|
[9] | 104 | ; skipping from Oct. 4, 1582 to Oct. 15, 1582 |
---|
| 105 | GREG = 2299171L ; incorrect Julian day for Oct. 25, 1582 |
---|
[2] | 106 | |
---|
| 107 | ; Process the input, if all are missing, use todays date. |
---|
| 108 | NP = n_params() |
---|
[9] | 109 | IF (np EQ 0) THEN RETURN, SYSTIME(/JULIAN) |
---|
| 110 | IF (np LT 3) THEN MESSAGE, 'Incorrect number of arguments.' |
---|
| 111 | |
---|
| 112 | ; Find the dimensions of the Result: |
---|
| 113 | ; 1. Find all of the input arguments that are arrays (ignore scalars) |
---|
| 114 | ; 2. Out of the arrays, find the smallest number of elements |
---|
| 115 | ; 3. Find the dimensions of the smallest array |
---|
| 116 | |
---|
| 117 | ; Step 1: find all array arguments |
---|
| 118 | nDims = [SIZE(month, /N_DIMENSIONS), SIZE(day, /N_DIMENSIONS), $ |
---|
| 119 | SIZE(year, /N_DIMENSIONS), SIZE(hour, /N_DIMENSIONS), $ |
---|
| 120 | SIZE(minute, /N_DIMENSIONS), SIZE(second, /N_DIMENSIONS)] |
---|
| 121 | arrays = WHERE(nDims GE 1) |
---|
| 122 | |
---|
| 123 | nJulian = 1L ; assume everything is a scalar |
---|
| 124 | IF (arrays[0] GE 0) THEN BEGIN |
---|
| 125 | ; Step 2: find the smallest number of elements |
---|
| 126 | nElement = [N_ELEMENTS(month), N_ELEMENTS(day), $ |
---|
| 127 | N_ELEMENTS(year), N_ELEMENTS(hour), $ |
---|
| 128 | N_ELEMENTS(minute), N_ELEMENTS(second)] |
---|
| 129 | nJulian = MIN(nElement[arrays], whichVar) |
---|
| 130 | ; step 3: find dimensions of the smallest array |
---|
| 131 | CASE arrays[whichVar] OF |
---|
| 132 | 0: julianDims = SIZE(month, /DIMENSIONS) |
---|
| 133 | 1: julianDims = SIZE(day, /DIMENSIONS) |
---|
| 134 | 2: julianDims = SIZE(year, /DIMENSIONS) |
---|
| 135 | 3: julianDims = SIZE(hour, /DIMENSIONS) |
---|
| 136 | 4: julianDims = SIZE(minute, /DIMENSIONS) |
---|
| 137 | 5: julianDims = SIZE(second, /DIMENSIONS) |
---|
| 138 | ENDCASE |
---|
| 139 | ENDIF |
---|
| 140 | |
---|
| 141 | d_Second = 0d ; defaults |
---|
| 142 | d_Minute = 0d |
---|
| 143 | d_Hour = 0d |
---|
| 144 | ; convert all Arguments to appropriate array size & type |
---|
| 145 | SWITCH np OF ; use switch so we fall thru all arguments... |
---|
| 146 | 6: d_Second = (SIZE(second, /N_DIMENSIONS) GT 0) ? $ |
---|
| 147 | second[0:nJulian-1] : second |
---|
| 148 | 5: d_Minute = (SIZE(minute, /N_DIMENSIONS) GT 0) ? $ |
---|
| 149 | minute[0:nJulian-1] : minute |
---|
| 150 | 4: d_Hour = (SIZE(hour, /N_DIMENSIONS) GT 0) ? $ |
---|
| 151 | hour[0:nJulian-1] : hour |
---|
| 152 | 3: BEGIN ; convert m,d,y to type LONG |
---|
| 153 | L_MONTH = (SIZE(month, /N_DIMENSIONS) GT 0) ? $ |
---|
| 154 | LONG(month[0:nJulian-1]) : LONG(month) |
---|
| 155 | L_DAY = (SIZE(day, /N_DIMENSIONS) GT 0) ? $ |
---|
| 156 | LONG(day[0:nJulian-1]) : LONG(day) |
---|
| 157 | L_YEAR = (SIZE(year, /N_DIMENSIONS) GT 0) ? $ |
---|
| 158 | LONG(year[0:nJulian-1]) : LONG(year) |
---|
| 159 | END |
---|
| 160 | ENDSWITCH |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | min_calendar = -4716 |
---|
| 164 | max_calendar = 5000000 |
---|
| 165 | minn = MIN(l_year, MAX = maxx) |
---|
| 166 | IF (minn LT min_calendar) OR (maxx GT max_calendar) THEN MESSAGE, $ |
---|
| 167 | 'Value of Julian date is out of allowed range.' |
---|
| 168 | ; change to accept year 0 |
---|
| 169 | ; if (MAX(L_YEAR eq 0) NE 0) then message, $ |
---|
[136] | 170 | ; 'There is no year zero in the civil calendar.' |
---|
[2] | 171 | ; |
---|
[9] | 172 | ; by seb Aug 2003 |
---|
[69] | 173 | |
---|
[9] | 174 | tochange = where(L_MONTH LT 0) |
---|
| 175 | IF tochange[0] NE -1 THEN BEGIN |
---|
| 176 | L_YEAR[tochange] = L_YEAR[tochange]+L_MONTH[tochange]/12-1 |
---|
| 177 | L_MONTH[tochange] = 12 + L_MONTH[tochange] MOD 12 |
---|
| 178 | ENDIF |
---|
[69] | 179 | |
---|
[9] | 180 | tochange = where(L_MONTH GT 12) |
---|
| 181 | IF tochange[0] NE -1 THEN BEGIN |
---|
| 182 | L_YEAR[tochange] = L_YEAR[tochange]+L_MONTH[tochange]/12 |
---|
| 183 | L_MONTH[tochange] = L_MONTH[tochange] MOD 12 |
---|
| 184 | ENDIF |
---|
| 185 | ; by seb Aug 2003 - end |
---|
[2] | 186 | ; |
---|
[9] | 187 | ; |
---|
| 188 | bc = (L_YEAR LT 0) |
---|
| 189 | L_YEAR = TEMPORARY(L_YEAR) + TEMPORARY(bc) |
---|
| 190 | inJanFeb = (L_MONTH LE 2) |
---|
| 191 | JY = L_YEAR - inJanFeb |
---|
| 192 | JM = L_MONTH + (1b + 12b*TEMPORARY(inJanFeb)) |
---|
[2] | 193 | |
---|
[9] | 194 | JUL = floor(365.25d * JY) + floor(30.6001d*TEMPORARY(JM)) + L_DAY + 1720995L |
---|
| 195 | |
---|
[136] | 196 | ; Test whether to change to Gregorian Calendar. |
---|
[9] | 197 | IF (MIN(JUL) GE GREG) THEN BEGIN ; change all dates |
---|
| 198 | JA = long(0.01d * TEMPORARY(JY)) |
---|
| 199 | JUL = TEMPORARY(JUL) + 2L - JA + long(0.25d * JA) |
---|
| 200 | ENDIF ELSE BEGIN |
---|
| 201 | gregChange = WHERE(JUL ge GREG, ngreg) |
---|
| 202 | IF (ngreg GT 0) THEN BEGIN |
---|
| 203 | JA = long(0.01d * JY[gregChange]) |
---|
| 204 | JUL[gregChange] = JUL[gregChange] + 2L - JA + long(0.25d * JA) |
---|
| 205 | ENDIF |
---|
| 206 | ENDELSE |
---|
[2] | 207 | |
---|
| 208 | |
---|
[9] | 209 | ; hour,minute,second? |
---|
| 210 | IF (np GT 3) THEN BEGIN ; yes, compute the fractional Julian date |
---|
| 211 | ; Add a small offset so we get the hours, minutes, & seconds back correctly |
---|
| 212 | ; if we convert the Julian dates back. This offset is proportional to the |
---|
| 213 | ; Julian date, so small dates (a long, long time ago) will be "more" accurate. |
---|
| 214 | eps = (MACHAR(/DOUBLE)).eps |
---|
| 215 | eps = eps*ABS(jul) > eps |
---|
[136] | 216 | ; For Hours, divide by 24, then subtract 0.5, in case we have unsigned integers. |
---|
[9] | 217 | jul = TEMPORARY(JUL) + ( (TEMPORARY(d_Hour)/24d - 0.5d) + $ |
---|
| 218 | TEMPORARY(d_Minute)/1440d + TEMPORARY(d_Second)/86400d + eps ) |
---|
| 219 | ENDIF |
---|
[2] | 220 | |
---|
[9] | 221 | ; check to see if we need to reform vector to array of correct dimensions |
---|
| 222 | IF (N_ELEMENTS(julianDims) GT 1) THEN $ |
---|
| 223 | JUL = REFORM(TEMPORARY(JUL), julianDims) |
---|
| 224 | |
---|
| 225 | RETURN, jul |
---|
| 226 | |
---|
[226] | 227 | END |
---|
[9] | 228 | '360d':BEGIN |
---|
[2] | 229 | ; |
---|
| 230 | ; Fixed number of days per month (default=30) : |
---|
| 231 | ; |
---|
[9] | 232 | IF keyword_set(ndayspm) THEN BEGIN |
---|
| 233 | IF ndayspm EQ 1 THEN ndayspm = 30 |
---|
| 234 | ENDIF ELSE ndayspm = 30 |
---|
[2] | 235 | |
---|
| 236 | L_MONTH = LONG(MONTH) |
---|
| 237 | L_DAY = LONG(DAY) |
---|
[9] | 238 | L_YEAR = LONG(YEAR) |
---|
[2] | 239 | |
---|
[9] | 240 | neg = where(L_YEAR LT 0) |
---|
| 241 | IF neg[0] NE -1 THEN L_YEAR[neg] = L_YEAR[neg]+1 |
---|
| 242 | |
---|
[226] | 243 | JUL = ((L_YEAR-1)*12 + (L_MONTH-1))* ndayspm + L_DAY |
---|
[2] | 244 | if n_elements(Hour) + n_elements(Minute) + n_elements(Second) eq 0 then $ |
---|
[9] | 245 | return, JUL |
---|
[205] | 246 | if n_elements(Hour) eq 0 then Hour = 12 |
---|
[2] | 247 | if n_elements(Minute) eq 0 then Minute = 0 |
---|
| 248 | if n_elements(Second) eq 0 then Second = 0 |
---|
[226] | 249 | |
---|
[205] | 250 | IF total([hour NE 12, minute NE 0, second NE 0]) EQ 0 THEN return, JUL ELSE $ |
---|
| 251 | return, JUL + (Hour / 24.0d0 - 0.5d) + (Minute/1440.0d0) + (Second / 86400.0d0) |
---|
[2] | 252 | |
---|
[226] | 253 | END |
---|
[9] | 254 | 'noleap':BEGIN |
---|
[69] | 255 | |
---|
| 256 | L_MONTH = LONG(MONTH) |
---|
| 257 | L_DAY = LONG(DAY) |
---|
| 258 | L_YEAR = LONG(YEAR) |
---|
| 259 | ; |
---|
| 260 | tochange = where(L_MONTH LT 0) |
---|
| 261 | IF tochange[0] NE -1 THEN BEGIN |
---|
| 262 | L_YEAR[tochange] = L_YEAR[tochange]+L_MONTH[tochange]/12-1 |
---|
| 263 | L_MONTH[tochange] = 12 + L_MONTH[tochange] MOD 12 |
---|
| 264 | ENDIF |
---|
| 265 | ; |
---|
| 266 | tochange = where(L_MONTH GT 12) |
---|
| 267 | IF tochange[0] NE -1 THEN BEGIN |
---|
| 268 | L_YEAR[tochange] = L_YEAR[tochange]+L_MONTH[tochange]/12 |
---|
| 269 | L_MONTH[tochange] = L_MONTH[tochange] MOD 12 |
---|
| 270 | ENDIF |
---|
| 271 | ; |
---|
| 272 | L_YEAR = L_YEAR - 1 |
---|
| 273 | ; |
---|
| 274 | daysyear = long(total([0, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30], /cumulative)) |
---|
| 275 | |
---|
[205] | 276 | JUL = 365*L_YEAR + daysyear[L_MONTH] + L_DAY |
---|
| 277 | if n_elements(Hour) + n_elements(Minute) + n_elements(Second) eq 0 then $ |
---|
| 278 | return, JUL |
---|
| 279 | if n_elements(Hour) eq 0 then Hour = 12 |
---|
| 280 | if n_elements(Minute) eq 0 then Minute = 0 |
---|
| 281 | if n_elements(Second) eq 0 then Second = 0 |
---|
[226] | 282 | |
---|
[205] | 283 | IF total([hour NE 12, minute NE 0, second NE 0]) EQ 0 THEN return, JUL ELSE $ |
---|
| 284 | return, JUL + (Hour / 24.0d0 - 0.5d) + (Minute/1440.0d0) + (Second / 86400.0d0) |
---|
[69] | 285 | |
---|
[226] | 286 | END |
---|
[9] | 287 | ELSE:return, report('only 3 types of calendar are accepted: greg, 360d and noleap') |
---|
| 288 | ENDCASE |
---|
| 289 | |
---|
| 290 | END |
---|