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 | ; <pro>cm_4cal</pro> |
---|
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 | ; CT, Nov 2006: For Hour/Min/Sec, tweak the input to make sure hours |
---|
63 | ; and minutes are correct. Restrict hours to 0-23 & min to 0-59. |
---|
64 | ; |
---|
65 | ; @version |
---|
66 | ; $Id$ |
---|
67 | ; |
---|
68 | ;- |
---|
69 | PRO caldat, julian, month, day, year, hour, minute, second, NDAYSPM=ndayspm |
---|
70 | ; |
---|
71 | compile_opt idl2, strictarrsubs |
---|
72 | ; |
---|
73 | @cm_4cal |
---|
74 | ; |
---|
75 | ON_ERROR, 2 ; Return to caller if errors |
---|
76 | |
---|
77 | IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' |
---|
78 | if keyword_set(ndayspm) then key_caltype = '360d' |
---|
79 | |
---|
80 | nParam = N_PARAMS() |
---|
81 | IF (nParam LT 1) THEN ras = report('Incorrect number of arguments.') |
---|
82 | |
---|
83 | CASE key_caltype OF |
---|
84 | 'greg':BEGIN |
---|
85 | |
---|
86 | min_julian = -1095 |
---|
87 | max_julian = 1827933925 |
---|
88 | minn = MIN(julian, MAX = maxx) |
---|
89 | IF (minn LT min_julian) OR (maxx GT max_julian) THEN $ |
---|
90 | ras = report('Value of Julian date is out of allowed range.') |
---|
91 | |
---|
92 | igreg = 2299161L ;Beginning of Gregorian calendar |
---|
93 | julLong = FLOOR(julian + 0.5d) ;Better be long |
---|
94 | minJul = MIN(julLong) |
---|
95 | |
---|
96 | IF (minJul GE igreg) THEN BEGIN ; all are Gregorian |
---|
97 | jalpha = LONG(((julLong - 1867216L) - 0.25d) / 36524.25d) |
---|
98 | ja = julLong + 1L + jalpha - long(0.25d * jalpha) |
---|
99 | ENDIF ELSE BEGIN |
---|
100 | ja = julLong |
---|
101 | gregChange = WHERE(julLong ge igreg, ngreg) |
---|
102 | IF (ngreg GT 0) THEN BEGIN |
---|
103 | jalpha = long(((julLong[gregChange] - 1867216L) - 0.25d) / 36524.25d) |
---|
104 | ja[gregChange] = julLong[gregChange] + 1L + jalpha - long(0.25d * jalpha) |
---|
105 | ENDIF |
---|
106 | ENDELSE |
---|
107 | jalpha = -1 ; clear memory |
---|
108 | |
---|
109 | jb = TEMPORARY(ja) + 1524L |
---|
110 | jc = long(6680d + ((jb-2439870L)-122.1d0)/365.25d) |
---|
111 | jd = long(365d * jc + (0.25d * jc)) |
---|
112 | je = long((jb - jd) / 30.6001d) |
---|
113 | |
---|
114 | day = TEMPORARY(jb) - TEMPORARY(jd) - long(30.6001d * je) |
---|
115 | month = TEMPORARY(je) - 1L |
---|
116 | month = ((TEMPORARY(month) - 1L) MOD 12L) + 1L |
---|
117 | year = TEMPORARY(jc) - 4715L |
---|
118 | year = TEMPORARY(year) - (month GT 2) |
---|
119 | year = year - (year LE 0) |
---|
120 | |
---|
121 | END |
---|
122 | '360d':BEGIN |
---|
123 | |
---|
124 | IF keyword_set(ndayspm) THEN BEGIN |
---|
125 | IF ndayspm EQ 1 THEN ndayspm = 30 |
---|
126 | ENDIF ELSE ndayspm = 30 |
---|
127 | |
---|
128 | ndayspm = long(ndayspm) |
---|
129 | julLong = FLOOR(julian + 0.5d) ;Better be long |
---|
130 | year = julLong/(12*ndayspm)+1 |
---|
131 | month = (julLong-(12*ndayspm)*(year-1))/ndayspm+1 |
---|
132 | day = julLong-(12*ndayspm)*(year-1)-ndayspm*(month-1) |
---|
133 | WHILE total(day LT 1) GT 0 DO BEGIN |
---|
134 | tochange = where(day LT 1) |
---|
135 | month[tochange] = month[tochange]-1 |
---|
136 | day[tochange] = day[tochange]+ndayspm |
---|
137 | ENDWHILE |
---|
138 | WHILE total(month LT 1) GT 0 DO BEGIN |
---|
139 | tochange = where(month LT 1) |
---|
140 | year[tochange] = year[tochange]-1 |
---|
141 | month[tochange] = month[tochange]+12 |
---|
142 | ENDWHILE |
---|
143 | ; year 0 does not exist... |
---|
144 | neg = where(year LT 0) |
---|
145 | IF neg[0] NE -1 THEN year[neg] = year[neg]-1 |
---|
146 | END |
---|
147 | 'noleap':BEGIN |
---|
148 | |
---|
149 | julLong = FLOOR(julian + 0.5d) ;Better be long |
---|
150 | year = julLong/365 + 1 |
---|
151 | day = julLong MOD 365L |
---|
152 | ; |
---|
153 | zero = where(day EQ 0) |
---|
154 | ; |
---|
155 | month = 1 + (day GT 31) + (day GT 59) + (day GT 90) + (day GT 120) $ |
---|
156 | + (day GT 151) + (day GT 181) + (day GT 212) + (day GT 243) $ |
---|
157 | + (day GT 273) + (day GT 304) + (day GT 334) |
---|
158 | month = long(month) |
---|
159 | ; |
---|
160 | day = day - 31L * (day GT 31) - 28L * (day GT 59) - 31L * (day GT 90) $ |
---|
161 | - 30L * (day GT 120) - 31L * (day GT 151) - 30L * (day GT 181) $ |
---|
162 | - 31L * (day GT 212) - 31L * (day GT 243) - 30L * (day GT 273) $ |
---|
163 | - 31L * (day GT 304) - 30L * (day GT 334) |
---|
164 | ; |
---|
165 | IF zero[0] NE -1 THEN BEGIN |
---|
166 | year[zero] = year[zero]-1 |
---|
167 | month[zero] = 12L |
---|
168 | day[zero] = 31L |
---|
169 | ENDIF |
---|
170 | ; |
---|
171 | END |
---|
172 | ELSE:BEGIN |
---|
173 | ng = report('only 3 types of calendar are accepted: greg, 360d and noleap') |
---|
174 | return |
---|
175 | END |
---|
176 | ENDCASE |
---|
177 | ; |
---|
178 | zero = where(year ge 600000L, cnt) |
---|
179 | IF cnt NE 0 THEN year[zero] = year[zero]-654321L |
---|
180 | ; |
---|
181 | ; see if we need to do hours, minutes, seconds |
---|
182 | IF (nParam GT 4) THEN BEGIN |
---|
183 | fraction = julian + 0.5d - julLong |
---|
184 | eps = 1d-12 > 1d-12*ABS(Temporary(julLong)) |
---|
185 | hour = 0 > Floor(fraction * 24d + eps) < 23 |
---|
186 | fraction -= hour/24d |
---|
187 | minute = 0 > Floor(fraction*1440d + eps) < 59 |
---|
188 | second = 0 > (TEMPORARY(fraction) - minute/1440d)*86400d |
---|
189 | ENDIF |
---|
190 | |
---|
191 | ; if julian is an array, reform all output to correct dimensions |
---|
192 | IF (SIZE(julian, /N_DIMENSION) GT 0) THEN BEGIN |
---|
193 | dimensions = SIZE(julian, /DIMENSION) |
---|
194 | month = REFORM(month, dimensions, /overwrite) |
---|
195 | day = REFORM(day, dimensions, /overwrite) |
---|
196 | year = REFORM(year, dimensions, /overwrite) |
---|
197 | IF (nParam GT 4) THEN BEGIN |
---|
198 | hour = REFORM(hour, dimensions, /overwrite) |
---|
199 | minute = REFORM(minute, dimensions, /overwrite) |
---|
200 | second = REFORM(second, dimensions, /overwrite) |
---|
201 | ENDIF |
---|
202 | ENDIF |
---|
203 | ; |
---|
204 | return |
---|
205 | |
---|
206 | END |
---|