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

Last change on this file since 133 was 133, checked in by navarro, 18 years ago

english and nicer header (1)

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