[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[133] | 3 | ; @file_comments |
---|
[137] | 4 | ; Return the calendar date and time given julian date. |
---|
[318] | 5 | ; This is the inverse of the function <pro>julday</pro>. |
---|
[231] | 6 | ; 3 calendars are available according to the value of key_caltype |
---|
[137] | 7 | ; (variable of the common file cm_4cal): 'greg', '360d', 'noleap' |
---|
| 8 | ; |
---|
[261] | 9 | ; @categories |
---|
| 10 | ; Calendar |
---|
[2] | 11 | ; |
---|
[327] | 12 | ; @param julian {in}{required} {type=long integer or double-precision floating-point} |
---|
[136] | 13 | ; contains the Julian Day Number (which begins at noon) of the |
---|
[231] | 14 | ; specified calendar date. |
---|
[2] | 15 | ; |
---|
[327] | 16 | ; @param month {out} {type=longword integer} |
---|
[136] | 17 | ; Number of the desired month (1 = January, ..., 12 = December). |
---|
[2] | 18 | ; |
---|
[327] | 19 | ; @param day {out} {type=longword integer} |
---|
[136] | 20 | ; Number of day of the month. |
---|
[2] | 21 | ; |
---|
[327] | 22 | ; @param year {out} {type=longword integer} |
---|
[136] | 23 | ; Number of the desired year. |
---|
[2] | 24 | ; |
---|
[327] | 25 | ; @param hour {out} {type=longword integer} |
---|
[136] | 26 | ; Hour of the day |
---|
[2] | 27 | ; |
---|
[327] | 28 | ; @param minute {out} {type=longword integer} |
---|
[136] | 29 | ; Minute of the day |
---|
[2] | 30 | ; |
---|
[327] | 31 | ; @param second {out} {type=double-precision floating-point} |
---|
[136] | 32 | ; Second (and fractions) of the day. |
---|
[9] | 33 | ; |
---|
[231] | 34 | ; @keyword NDAYSPM {type=integer} {default=30} |
---|
| 35 | ; To use a calendar with fixed number of days per month. |
---|
[137] | 36 | ; see also the use of key_caltype (variable of the common file cm_4cal) |
---|
[9] | 37 | ; |
---|
[238] | 38 | ; @uses |
---|
| 39 | ; cm_4cal |
---|
[133] | 40 | ; |
---|
[231] | 41 | ; @restrictions |
---|
| 42 | ; Accuracy using IEEE double precision numbers is approximately 1/10000th of a |
---|
[136] | 43 | ; second. |
---|
[133] | 44 | ; |
---|
[231] | 45 | ; @history |
---|
[292] | 46 | ; Translated from "Numerical Recipes in C", by William H. Press, |
---|
[136] | 47 | ; Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. |
---|
| 48 | ; Cambridge University Press, 1988 (second printing). |
---|
[2] | 49 | ; |
---|
[136] | 50 | ; DMS, July 1992. |
---|
| 51 | ; DMS, April 1996, Added HOUR, MINUTE and SECOND keyword |
---|
| 52 | ; AB, 7 December 1997, Generalized to handle array input. |
---|
[2] | 53 | ; |
---|
[136] | 54 | ; Eric Guilyardi, June 1999 |
---|
| 55 | ; Added key_work ndayspm for fixed number of days per months |
---|
[9] | 56 | ; |
---|
[136] | 57 | ; AB, 3 January 2000, Make seconds output as DOUBLE in array output. |
---|
[9] | 58 | ; |
---|
[231] | 59 | ; Sebastien Masson, May 2006, add different calendar with key_caltype |
---|
[137] | 60 | ; (variable of the common file cm_4cal) |
---|
| 61 | ; |
---|
[231] | 62 | ; @version |
---|
| 63 | ; $Id$ |
---|
[238] | 64 | ; |
---|
[2] | 65 | ;- |
---|
[327] | 66 | PRO caldat, julian, month, day, year, hour, minute, second, NDAYSPM=ndayspm |
---|
[231] | 67 | ; |
---|
[238] | 68 | compile_opt idl2, strictarrsubs |
---|
| 69 | ; |
---|
[9] | 70 | @cm_4cal |
---|
[231] | 71 | ; |
---|
[9] | 72 | ON_ERROR, 2 ; Return to caller if errors |
---|
[2] | 73 | |
---|
[9] | 74 | IF n_elements(key_caltype) EQ 0 THEN key_caltype = 'greg' |
---|
| 75 | if keyword_set(ndayspm) then key_caltype = '360d' |
---|
[205] | 76 | |
---|
| 77 | nParam = N_PARAMS() |
---|
[242] | 78 | IF (nParam LT 1) THEN ras = report('Incorrect number of arguments.') |
---|
[205] | 79 | |
---|
[9] | 80 | CASE key_caltype OF |
---|
| 81 | 'greg':BEGIN |
---|
[2] | 82 | |
---|
[9] | 83 | min_julian = -1095 |
---|
| 84 | max_julian = 1827933925 |
---|
| 85 | minn = MIN(julian, MAX = maxx) |
---|
[242] | 86 | IF (minn LT min_julian) OR (maxx GT max_julian) THEN $ |
---|
| 87 | ras = report('Value of Julian date is out of allowed range.') |
---|
[2] | 88 | |
---|
[9] | 89 | igreg = 2299161L ;Beginning of Gregorian calendar |
---|
| 90 | julLong = FLOOR(julian + 0.5d) ;Better be long |
---|
| 91 | minJul = MIN(julLong) |
---|
[2] | 92 | |
---|
[9] | 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 |
---|
[2] | 105 | |
---|
[9] | 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) |
---|
[2] | 110 | |
---|
[9] | 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 | |
---|
[231] | 118 | END |
---|
[9] | 119 | '360d':BEGIN |
---|
[231] | 120 | |
---|
[9] | 121 | IF keyword_set(ndayspm) THEN BEGIN |
---|
| 122 | IF ndayspm EQ 1 THEN ndayspm = 30 |
---|
| 123 | ENDIF ELSE ndayspm = 30 |
---|
[2] | 124 | |
---|
[9] | 125 | ndayspm = long(ndayspm) |
---|
[205] | 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) |
---|
[9] | 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 |
---|
[231] | 143 | END |
---|
[9] | 144 | 'noleap':BEGIN |
---|
[69] | 145 | |
---|
[205] | 146 | julLong = FLOOR(julian + 0.5d) ;Better be long |
---|
| 147 | year = julLong/365 + 1 |
---|
| 148 | day = julLong MOD 365L |
---|
[69] | 149 | ; |
---|
[205] | 150 | zero = where(day EQ 0) |
---|
[69] | 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) |
---|
[231] | 156 | ; |
---|
[69] | 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 |
---|
[231] | 166 | ENDIF |
---|
[205] | 167 | ; |
---|
[231] | 168 | END |
---|
[9] | 169 | ELSE:BEGIN |
---|
| 170 | ng = report('only 3 types of calendar are accepted: greg, 360d and noleap') |
---|
| 171 | return |
---|
| 172 | END |
---|
| 173 | ENDCASE |
---|
| 174 | ; |
---|
[69] | 175 | zero = where(year ge 600000L, cnt) |
---|
| 176 | IF cnt NE 0 THEN year[zero] = year[zero]-654321L |
---|
| 177 | ; |
---|
[205] | 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 |
---|
[231] | 186 | |
---|
[205] | 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 | ; |
---|
[9] | 200 | return |
---|
[2] | 201 | |
---|
[9] | 202 | END |
---|