source: trunk/SRC/Calendar/julday.pro @ 288

Last change on this file since 288 was 279, checked in by smasson, 17 years ago

bugfix when using double precision calendar

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