source: trunk/procs/compute_time.pro @ 203

Last change on this file since 203 was 203, checked in by pinsard, 15 years ago

remove trailing blanks

  • Property svn:keywords set to Id
File size: 10.0 KB
Line 
1;+
2;
3; compute number + array of time intervals in units of timeave between date1
4; and date2
5;
6; @param TIMEAVE {in}{required}{type=string}
7;
8; @param DATE1 {in}{required}{type=string}
9; [yy..yyy][mm][dd]-[sssss] if <n>s
10; [yy..yyy][mm][dd] if <n>d
11; [yy..yyy][mm]     if <n>m
12; [yy..yyy]         if <n>y
13;
14; @param DATE2 {in}{required}{type=string}
15; [yy..yyy][mm][dd]-[sssss] if <n>s
16; [yy..yyy][mm][dd] if <n>d
17; [yy..yyy][mm]     if <n>m
18; [yy..yyy]         if <n>y
19;
20; @keyword NEXT
21; if /NEXT, compute next date after date1, using date2 units of timeave
22;
23; @returns
24; structure of a number + array of time intervals in units of timeave between
25; date1 and date2.
26;
27; -1 in case of error
28;
29; @examples
30;
31; IDL> calendar_type='0'
32; IDL> timeave = '1mm'
33; IDL> date1 = '01_1860-1959'
34; IDL> date2 = '12_1860-1959'
35; IDL> nb_cycles=1
36; IDL> result = compute_time(timeave, date1, date2)
37; IDL> help,result,/structure
38; ** Structure <8481304>, 2 tags, length=52, data length=52, refs=1:
39;   COUNT           LONG                12
40;   SCALE           LONG      Array[12]
41;
42; IDL> print, result
43; {          12     1721424     1721454     1721484     1721514     1721544     1721574     1721604     1721634
44;     1721664     1721694     1721724     1721754
45; }
46;
47; @uses
48; <pro>common</pro>
49; <propost_it>com_eg</propost_it>
50;
51; <pro>julday</pro>
52;
53; @todo
54; add info and example on calendar_type and nb_cycles
55;
56; @history
57; - fplod 20091209T094630Z aedon.locean-ipsl.upmc.fr (Darwin)
58;
59;   * check parameters
60;
61; @version
62; $Id$
63;
64;-
65FUNCTION compute_time, timeave, date1, date2 $
66         , NEXT=next
67;
68  compile_opt idl2, strictarrsubs
69;
70@common
71@com_eg
72;
73; Return to caller if errors
74 ON_ERROR, 2
75;
76 usage='result=compute_time(timeave, date1, date2,NEXT=next)'
77;
78 nparam = N_PARAMS()
79 IF (nparam LT 2) THEN BEGIN
80    ras = report(['Incorrect number of arguments.' $
81          + '!C' $
82          + 'Usage : ' + usage])
83    return, -1
84 ENDIF
85 arg_type = size(timeave,/type)
86 IF (arg_type NE 7) THEN BEGIN
87   ras = report(['Incorrect arg type timeave' $
88          + '!C' $
89          + 'Usage : ' + usage])
90    return, -1
91 ENDIF
92 arg_type = size(date1,/type)
93 IF (arg_type NE 7) THEN BEGIN
94   ras = report(['Incorrect arg type date1' $
95          + '!C' $
96          + 'Usage : ' + usage])
97    return, -1
98 ENDIF
99 arg_type = size(date2,/type)
100 IF (arg_type NE 7) THEN BEGIN
101   ras = report(['Incorrect arg type date2' $
102          + '!C' $
103          + 'Usage : ' + usage])
104    return, -1
105 ENDIF
106
107 common_type=size(nb_cycles,/type)
108 IF ((common_type NE 2) AND (common_type NE 3)) THEN BEGIN
109   ras = report(['Incorrect common type nb_cycles' $
110          + '!C' $
111          + 'Usage : ' + usage])
112   return, -1
113 ENDIF
114
115   len1 = strlen(date1)
116   IF NOT keyword_set(NEXT) THEN BEGIN
117
118      len2 = strlen(date2)
119      mean_type = strmid(timeave, strlen(timeave)-1,1)
120      CASE mean_type OF
121         'y':BEGIN              ; year case
122            delta_time = long(strmid(timeave, 0, strlen(timeave)-1))
123            year1 = long(date1)
124            year2 = long(date2)
125            count = (year2-year1+1)*delta_time
126            IF calendar_type LE 1 THEN BEGIN
127               time = findgen(count)*360*delta_time+julday(1,1,year1)
128            ENDIF ELSE BEGIN
129               time = findgen(count)*360*delta_time+ $
130                julday(1,1,year1, ndayspm = calendar_type)
131            ENDELSE
132         END
133         'm': BEGIN              ; month case
134            CASE strmid(timeave, strlen(timeave)-2,1) OF
135               'm': BEGIN   ; mean month
136                  delta_time = long(strmid(timeave, 0, strlen(timeave)-2))
137                  month1 = long(strmid(date1, 0, 2))
138                  month2 = long(strmid(date2, 0, 2))
139                  count = (month2-month1+1)
140;                  count = (month2-month1+1)/delta_time
141                  ; time scale for hov
142                  IF calendar_type LE 1 THEN BEGIN
143                     time = lindgen(nb_cycles*count)*30*delta_time+ $
144                      julday(month1,1,01)
145                  ENDIF ELSE BEGIN
146                     time = lindgen(nb_cycles*count)*30*delta_time+ $
147                      julday(month1,1,01, ndayspm = calendar_type)
148                  ENDELSE
149
150               END
151               ELSE: BEGIN  ; month / year
152                  delta_time = long(strmid(timeave, 0, strlen(timeave)-1))
153                  month1 = long(strmid(date1, len1-2, 2))
154                  year1 = long(strmid(date1, 0, len1-2))
155                  month2 = long(strmid(date2, len2-2, 2))
156                  year2 = long(strmid(date2, 0, len2-2))
157                  count = 0
158                  im = month1
159                  iy = year1
160                  idate = long(month1+100*year1)
161                  WHILE idate LE date2 DO BEGIN
162                     idate = long(im+100*iy)
163                     count = count + 1
164                     im = im + delta_time
165                     IF im EQ 13 THEN BEGIN
166                        iy = iy+1
167                        im = 1
168                     ENDIF
169                  ENDWHILE
170                  count = count - 1
171                 ; time scale for hov
172                  IF calendar_type LE 1 THEN BEGIN
173                     time = lindgen(count)*30*delta_time+ $
174                      julday(month1,1,year1)
175                  ENDIF ELSE BEGIN
176                     time = lindgen(count)*30*delta_time+ $
177                      julday(month1,1,year1, ndayspm = calendar_type)
178                  ENDELSE
179               END
180            ENDCASE
181        END
182         'd': BEGIN              ; day case
183            delta_time = long(strmid(timeave, 0, strlen(timeave)-1))
184            day1 = long(strmid(date1, len1-2, 2))
185            month1 = long(strmid(date1, len1-4, 2))
186            year1 = long(strmid(date1, 0, len1-4))
187            day2 = long(strmid(date2, len1-2, 2))
188            month2 = long(strmid(date2, len2-4, 2))
189            year2 = long(strmid(date2, 0, len2-4))
190            count = 0
191            id = day1
192            im = month1
193            iy = year1
194            idate = long(day1+100*month1+10000*year1)
195            WHILE idate LE date2 DO BEGIN
196               idate = long(id+100*im+10000*iy)
197               count = count + 1
198               id = id + delta_time
199               idmax = daypm(im, iy)
200               IF id GT idmax THEN BEGIN
201                  im = im + 1
202                  id = id - idmax
203               ENDIF
204               IF im EQ 13 THEN BEGIN
205                  iy = iy+1
206                  im = 1
207               ENDIF
208            ENDWHILE
209            count = count - 1
210            ; time scale for hov
211            IF calendar_type LE 1 THEN BEGIN
212               time = lindgen(count)*1*delta_time+ $
213                julday(month1,1,year1)
214            ENDIF ELSE BEGIN
215               time = lindgen(count)*1*delta_time+ $
216                julday(month1,1,year1, ndayspm = calendar_type)
217            ENDELSE
218         END
219         's': BEGIN              ; seconds case
220            delta_time = long(strmid(timeave, 0, strlen(timeave)-1))
221            sec1 = long(strmid(date1, len1-5, 5))
222            day1 = long(strmid(date1, len1-8, 2))
223            month1 = long(strmid(date1, len1-10, 2))
224            year1 = long(strmid(date1, 0, len1-10))
225            sec2 = long(strmid(date2, len1-5, 5))
226            day2 = long(strmid(date2, len1-8, 2))
227            month2 = long(strmid(date2, len2-10, 2))
228            year2 = long(strmid(date2, 0, len2-10))
229            count = 0
230            is = sec1
231            id = day1
232            im = month1
233            iy = year1
234            WHILE is LE sec2 AND id LE day2 AND im LE month2 $
235             AND iy LE year2 DO BEGIN
236               count = count + 1
237               is = is + delta_time
238               IF is GT 86400 THEN BEGIN
239                  id = id + 1
240                  is = is - 86400
241               ENDIF
242               idmax = daypm(im, iy)
243               IF id EQ idmax THEN BEGIN
244                  im = im + 1
245                  id = 1
246               ENDIF
247               IF im EQ 13 THEN BEGIN
248                  iy = iy+1
249                  im = 1
250               ENDIF
251            ENDWHILE
252                                ; time scale for hov
253            IF calendar_type LE 1 THEN BEGIN
254               time = sec1/86400.+lindgen(count)*1/86400.*delta_time+ $
255                julday(month1,1,year1)
256            ENDIF ELSE BEGIN
257               time = sec1/86400.+lindgen(count)*1/86400.*delta_time+ $
258                julday(month1,1,year1, ndayspm = calendar_type)
259            ENDELSE
260         END
261         ELSE: BEGIN
262            print, '   **** compute_time not ready for timeave = ', timeave
263            count = -1
264            time = 0
265         END
266      ENDCASE
267      IF date1 EQ date2 THEN count = 1
268      timearr = {count: count, scale: time}
269      return, timearr
270
271   ENDIF ELSE BEGIN
272      ; next date after date1, using date2 units of timeave
273      dmax = 30
274      print, '   **** ASSUMES DMAX = 30'
275      CASE strmid(timeave, strlen(timeave)-1, 1) OF
276         'd': BEGIN
277            ndays = long(strmid(timeave, 0, strlen(timeave)-1))
278            day1 = long(strmid(date1, len1-2, 2))
279            month1 = long(strmid(date1, len1-4, 2))
280            year1 = long(strmid(date1, 0, len1-4))
281            iadd = 1
282            day2 = day1
283            month2 = month1
284            year2 = year1
285            WHILE iadd LE date2 DO BEGIN
286               day2 = day2+1
287               IF day2 GT dmax THEN BEGIN
288                  month2 = month2 + 1
289                  day2 = 1
290                  IF month2 GT 12 THEN BEGIN
291                     year2 = year2+1
292                     month2 = 1
293                  ENDIF
294               ENDIF
295               iadd = iadd + 1
296            ENDWHILE
297            IF year2 GE 10 THEN BEGIN
298               nextdate = string(year2)
299            ENDIF ELSE BEGIN
300               nextdate = string(format = '(I2.2)',year2)
301            ENDELSE
302            nextdate = nextdate+string(format = '(I2.2)', month2)+string(format = '(I2.2)', day2)
303         END
304         ELSE: BEGIN
305            print, '   **** next time not ready for timeave = ', timeave
306            nextdate = -1
307         END
308      ENDCASE
309      return, nextdate
310   ENDELSE
311
312END
Note: See TracBrowser for help on using the repository browser.