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