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