source: trunk/SRC/ToBeReviewed/CALENDRIER/julday.pro @ 76

Last change on this file since 76 was 69, checked in by smasson, 18 years ago

debug + new xxx

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