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

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

improvements/corrections of some *.pro headers + replace some message by some report

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