source: trunk/SRC/Calendar/caldat.pro @ 261

Last change on this file since 261 was 261, checked in by pinsard, 17 years ago

keywords in uppercase in headers of pro files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1;+
2;
3; @file_comments
4; Return the calendar date and time given julian date.
5; This is the inverse of the function
6; <pro>julday</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 JULIAN {in}{required} {type=long integer}
14; contains the Julian Day Number (which begins at noon) of the
15; specified calendar date.
16;
17; @param MONTH {out} {type=integer}
18; Number of the desired month (1 = January, ..., 12 = December).
19;
20; @param DAY {out} {type=integer}
21; Number of day of the month.
22;
23; @param YEAR {out} {type=integer}
24; Number of the desired year.
25;
26; @param HOUR {out} {type=integer}
27; Hour of the day
28;
29; @param MINUTE {out} {type=integer}
30; Minute of the day
31;
32; @param SECOND {out} {type=float}
33; Second (and fractions) of the day.
34;
35; @keyword NDAYSPM {type=integer} {default=30}
36; To use a calendar with fixed number of days per month.
37; see also the use of key_caltype (variable of the common file cm_4cal)
38;
39; @uses
40; cm_4cal
41;
42; @restrictions
43; Accuracy using IEEE double precision numbers is approximately 1/10000th of a
44; second.
45;
46; @history
47; Translated from "Numerical Recipies in C", by William H. Press,
48; Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling.
49; Cambridge University Press, 1988 (second printing).
50;
51; DMS, July 1992.
52; DMS, April 1996, Added HOUR, MINUTE and SECOND keyword
53; AB, 7 December 1997, Generalized to handle array input.
54;
55; Eric Guilyardi, June 1999
56; Added key_work ndayspm for fixed number of days per months
57;
58; AB, 3 January 2000, Make seconds output as DOUBLE in array output.
59;
60; Sebastien Masson, May 2006, add different calendar with key_caltype
61; (variable of the common file cm_4cal)
62;
63; @version
64; $Id$
65;
66;-
67;
68PRO caldat, julian, month, day, year, hour, minute, second, NDAYSPM = ndayspm
69;
70  compile_opt idl2, strictarrsubs
71;
72@cm_4cal
73;
74  ON_ERROR, 2                   ; Return to caller if errors
75
76  IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg'
77  if keyword_set(ndayspm) then key_caltype = '360d'
78
79  nParam = N_PARAMS()
80  IF (nParam LT 1) THEN ras = report('Incorrect number of arguments.')
81
82  CASE key_caltype OF
83    'greg':BEGIN
84
85      min_julian = -1095
86      max_julian = 1827933925
87      minn = MIN(julian, MAX = maxx)
88      IF (minn LT min_julian) OR (maxx GT max_julian) THEN $
89        ras = report('Value of Julian date is out of allowed range.')
90
91      igreg = 2299161L                   ;Beginning of Gregorian calendar
92      julLong = FLOOR(julian + 0.5d)     ;Better be long
93      minJul = MIN(julLong)
94
95      IF (minJul GE igreg) THEN BEGIN ; all are Gregorian
96        jalpha = LONG(((julLong - 1867216L) - 0.25d) / 36524.25d)
97        ja = julLong + 1L + jalpha - long(0.25d * jalpha)
98      ENDIF ELSE BEGIN
99        ja = julLong
100        gregChange = WHERE(julLong ge igreg, ngreg)
101        IF (ngreg GT 0) THEN BEGIN
102          jalpha = long(((julLong[gregChange] - 1867216L) - 0.25d) / 36524.25d)
103          ja[gregChange] = julLong[gregChange] + 1L + jalpha - long(0.25d * jalpha)
104        ENDIF
105      ENDELSE
106      jalpha = -1               ; clear memory
107
108      jb = TEMPORARY(ja) + 1524L
109      jc = long(6680d + ((jb-2439870L)-122.1d0)/365.25d)
110      jd = long(365d * jc + (0.25d * jc))
111      je = long((jb - jd) / 30.6001d)
112
113      day = TEMPORARY(jb) - TEMPORARY(jd) - long(30.6001d * je)
114      month = TEMPORARY(je) - 1L
115      month = ((TEMPORARY(month) - 1L) MOD 12L) + 1L
116      year = TEMPORARY(jc) - 4715L
117      year = TEMPORARY(year) - (month GT 2)
118      year = year - (year LE 0)
119
120    END
121    '360d':BEGIN
122
123      IF keyword_set(ndayspm) THEN BEGIN
124        IF ndayspm EQ 1 THEN ndayspm = 30
125      ENDIF ELSE ndayspm = 30
126
127      ndayspm = long(ndayspm)
128      julLong = FLOOR(julian + 0.5d)     ;Better be long
129      year = julLong/(12*ndayspm)+1
130      month = (julLong-(12*ndayspm)*(year-1))/ndayspm+1
131      day = julLong-(12*ndayspm)*(year-1)-ndayspm*(month-1)
132      WHILE total(day LT 1) GT 0 DO BEGIN
133        tochange = where(day LT 1)
134        month[tochange] = month[tochange]-1
135        day[tochange] = day[tochange]+ndayspm
136      ENDWHILE
137      WHILE total(month LT 1) GT 0 DO BEGIN
138        tochange = where(month LT 1)
139        year[tochange] = year[tochange]-1
140        month[tochange] = month[tochange]+12
141      ENDWHILE
142; year 0 does not exist...
143      neg = where(year LT 0)
144      IF neg[0] NE -1 THEN year[neg] = year[neg]-1
145    END
146    'noleap':BEGIN
147
148      julLong = FLOOR(julian + 0.5d)     ;Better be long
149      year = julLong/365 + 1
150      day = julLong MOD 365L
151;
152      zero = where(day EQ 0)
153;
154      month = 1 + (day GT 31) + (day GT 59) + (day GT 90) + (day GT 120) $
155              + (day GT 151) + (day GT 181) + (day GT 212) + (day GT 243) $
156              + (day GT 273) + (day GT 304) + (day GT 334)
157      month = long(month)
158;
159      day = day - 31L * (day GT 31) - 28L * (day GT 59) - 31L * (day GT 90) $
160              - 30L * (day GT 120) - 31L * (day GT 151) - 30L * (day GT 181) $
161              - 31L * (day GT 212) - 31L * (day GT 243) - 30L * (day GT 273) $
162              - 31L * (day GT 304) - 30L * (day GT 334)
163;
164      IF zero[0] NE -1 THEN BEGIN
165        year[zero] = year[zero]-1
166        month[zero] = 12L
167        day[zero] = 31L
168      ENDIF
169;
170    END
171    ELSE:BEGIN
172      ng = report('only 3 types of calendar are accepted: greg, 360d and noleap')
173      return
174    END
175  ENDCASE
176;
177  zero = where(year ge 600000L, cnt)
178  IF cnt NE 0 THEN year[zero] = year[zero]-654321L
179;
180; see if we need to do hours, minutes, seconds
181  IF (nParam GT 4) THEN BEGIN
182    fraction = julian + 0.5d - TEMPORARY(julLong)
183    hour = floor(fraction * 24d)
184    fraction = TEMPORARY(fraction) - hour/24d
185    minute = floor(fraction*1440d)
186    second = (TEMPORARY(fraction) - minute/1440d) * 86400d
187  ENDIF
188
189; if julian is an array, reform all output to correct dimensions
190  IF (SIZE(julian, /N_DIMENSION) GT 0) THEN BEGIN
191    dimensions = SIZE(julian, /DIMENSION)
192    month = REFORM(month, dimensions, /overwrite)
193    day = REFORM(day, dimensions, /overwrite)
194    year = REFORM(year, dimensions, /overwrite)
195    IF (nParam GT 4) THEN BEGIN
196      hour = REFORM(hour, dimensions, /overwrite)
197      minute = REFORM(minute, dimensions, /overwrite)
198      second = REFORM(second, dimensions, /overwrite)
199    ENDIF
200  ENDIF
201;
202  return
203
204END
Note: See TracBrowser for help on using the repository browser.