source: trunk/procs/search_time_file.pro

Last change on this file was 230, checked in by ericg, 14 years ago

Add difference with other exp in cmd.exp for ensembles <expid>#<ensbl_code>-<expid2>

  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1;+
2;
3; look for time serie file with date min =< cmd.date1 and date max => cmd.spec
4;
5; @param CMD {in}{required}{type=structure}
6; command line of window
7;
8; @param NCDF_DB {in}{required}{type=string}
9; <location>:<path> or just <path>
10;
11; @param SUFFIX {in}{required}{type=string}
12;
13; @param DELTA_T1 {out}{type=integer}
14;
15; @param DATE1 {out}{type=string}
16; [yy..yyy][mm][dd]-[sssss] if <n>s
17; [yy..yyy][mm][dd] if <n>d
18; [yy..yyy][mm]     if <n>m
19; [yy..yyy]         if <n>y
20;
21; @param DATE2 {out}{type=string}
22; [yy..yyy][mm][dd]-[sssss] if <n>s
23; [yy..yyy][mm][dd] if <n>d
24; [yy..yyy][mm]     if <n>m
25; [yy..yyy]         if <n>y
26;
27; @param TIMAVEF {out}{type=string}
28;
29; output : data file date1 and date2
30;
31; @examples
32;
33; IDL> cmd={timave:'1m',date1:'199201',spec:'-',exp:'HH',var:'temp_1'}
34; IDL> ncdf_db='local:./'
35; IDL> suffix=''
36; IDL> search_time_file, cmd, ncdf_db, suffix, date1, date2, delta_t1, timavef
37; IDL> print,  date1, date2,delta_t1,timavef
38;
39; @uses
40; <pro>common</pro>
41; <propost_it>com_eg</propost_it>
42;
43; @todo
44; get rid of spawn
45;
46; error handling
47; realistic examples
48;
49; @history
50;
51; - fplod 20091210T150954Z aedon.locean-ipsl.upmc.fr (Darwin)
52;
53;   * check parameters
54;
55; @version
56; $Id$
57;
58;-
59PRO search_time_file, cmd, ncdf_db, suffix, date1, date2, delta_t1, timavef
60;
61  compile_opt idl2, strictarrsubs
62;
63@common
64@com_eg
65;
66; Return to caller if errors
67 ON_ERROR, 2
68
69   IF debug_w THEN BEGIN
70    info = report('enter ...')
71    print, '       cmd at top of proc = ', cmd
72    print, '       suffix at top of proc = ', suffix
73   ENDIF
74;
75 usage='search_time_file, cmd, ncdf_db, suffix, date1, date2, delta_t1, timavef'
76;
77 nparam = N_PARAMS()
78 IF (nparam LT 7) THEN BEGIN
79   ras = report(['Incorrect number of arguments.' $
80          + '!C' $
81          + 'Usage : ' + usage])
82   stop
83 ENDIF
84
85 arg_type = size(cmd,/type)
86 IF (arg_type NE 8) THEN BEGIN
87   ras = report(['Incorrect arg type cmd' $
88          + '!C' $
89          + 'Usage : ' + usage])
90   stop
91 ENDIF
92 arg_struct_tags=TAG_NAMES(cmd)
93 tag=WHERE(STRMATCH(arg_struct_tags, 'TIMAVE'))
94 IF (tag EQ -1) THEN BEGIN
95   ras = report(['Incorrect arg tag TIMAVE cmd' $
96          + '!C' $
97          + 'Usage : ' + usage])
98   stop
99 ENDIF
100 arg_type = size(cmd.timave,/type)
101 IF (arg_type NE 7) THEN BEGIN
102   ras = report(['Incorrect arg type cmd.timave' $
103          + '!C' $
104          + 'Usage : ' + usage])
105   stop
106 ENDIF
107 tag=WHERE(STRMATCH(arg_struct_tags, 'DATE1'))
108 IF (tag EQ -1) THEN BEGIN
109   ras = report(['Incorrect arg tag DATE1 cmd' $
110          + '!C' $
111          + 'Usage : ' + usage])
112   stop
113 ENDIF
114 arg_type = size(cmd.date1,/type)
115 IF (arg_type NE 7) THEN BEGIN
116   ras = report(['Incorrect arg type cmd.date1' $
117          + '!C' $
118          + 'Usage : ' + usage])
119   stop
120 ENDIF
121 tag=WHERE(STRMATCH(arg_struct_tags, 'SPEC'))
122 IF (tag EQ -1) THEN BEGIN
123   ras = report(['Incorrect arg tag SPEC cmd' $
124          + '!C' $
125          + 'Usage : ' + usage])
126   stop
127 ENDIF
128 arg_type = size(cmd.spec,/type)
129 IF (arg_type NE 7) THEN BEGIN
130   ras = report(['Incorrect arg type cmd.spec' $
131          + '!C' $
132          + 'Usage : ' + usage])
133   stop
134 ENDIF
135 tag=WHERE(STRMATCH(arg_struct_tags, 'EXP'))
136 IF (tag EQ -1) THEN BEGIN
137   ras = report(['Incorrect arg tag EXP cmd' $
138          + '!C' $
139          + 'Usage : ' + usage])
140   stop
141 ENDIF
142
143 arg_type = size(cmd.exp,/type)
144 IF (arg_type NE 7) THEN BEGIN
145   ras = report(['Incorrect arg type cmd.exp' $
146          + '!C' $
147          + 'Usage : ' + usage])
148   stop
149 ENDIF
150
151 tag=WHERE(STRMATCH(arg_struct_tags, 'VAR'))
152 IF (tag EQ -1) THEN BEGIN
153   ras = report(['Incorrect arg tag VAR cmd' $
154          + '!C' $
155          + 'Usage : ' + usage])
156   stop
157 ENDIF
158
159 arg_type = size(cmd.var,/type)
160 IF (arg_type NE 7) THEN BEGIN
161   ras = report(['Incorrect arg type cmd.grid' $
162          + '!C' $
163          + 'Usage : ' + usage])
164   stop
165 ENDIF
166
167 arg_type = size(ncdf_db,/type)
168 IF (arg_type NE 7) THEN BEGIN
169   ras = report(['Incorrect arg type ncdf_db' $
170          + '!C' $
171          + 'Usage : ' + usage])
172   stop
173 ENDIF
174
175 arg_type = size(suffix,/type)
176 IF (arg_type NE 7) THEN BEGIN
177   ras = report(['Incorrect arg type suffix' $
178          + '!C' $
179          + 'Usage : ' + usage])
180   stop
181 ENDIF
182
183   timavef = cmd.timave
184   date1 = cmd.date1
185   date2 = cmd.spec
186
187   found = 1
188   delta_t1 = 0
189   suff_var = ''
190
191   IF strpos(cmd.timave, 'mm') NE -1 THEN BEGIN
192    meanm = 'yes'
193   ENDIF ELSE BEGIN
194    meanm = 'no'
195   ENDELSE
196
197   CASE meanm OF
198
199      'no': BEGIN ; No mean month case
200
201         file_name_root = cmd.exp+'_'+cmd.timave+'_'
202
203         ; local or remote DB
204         machine = (strsplit(ncdf_db, ':', /EXTRACT))[0]
205         CASE machine OF
206            'local': BEGIN
207               ; field name added (1 variable)
208               variab = '_'+cmd.var
209               print, '    Looking for '+(strsplit(ncdf_db, ':', /EXTRACT))[1]+file_name_root+'*'+suffix+'.nc ...'
210               spawn, 'ls '+(strsplit(ncdf_db, ':', /EXTRACT))[1]+file_name_root+'*'+suffix+'.nc', line2
211               line = line2
212            END
213            ELSE: BEGIN
214               spawn, 'grep -i " '+machine+' " '+hom_def+'remote/remote.def', rem_line
215               nrem = n_elements(rem_line)
216               IF nrem EQ 1 THEN BEGIN
217                  rem_line = strcompress(strtrim(rem_line[0], 2))
218                  argvar = strsplit(rem_line, ' ', /EXTRACT)
219                  remote_name = argvar[1]
220                  remote_login = argvar[2]
221                  print, '    rsh '+remote_login+'@'+remote_name+' ls '+$
222                   (strsplit(ncdf_db, ':', /EXTRACT))[1]+file_name_root+'*'+suffix+'.nc'
223                  print, ''
224                  spawn, 'rsh '+remote_login+'@'+remote_name+' ls '+$
225                   (strsplit(ncdf_db, ':', /EXTRACT))[1]+file_name_root+'*'+suffix+'.nc', line
226               ENDIF ELSE BEGIN
227                  print, ' *** search_file : '+machine+' unknown in config/remote'
228               ENDELSE
229            END
230         ENDCASE
231
232         nfiles = n_elements(line)
233
234         print, '     -> ', strtrim(string(nfiles), 2), ' file(s) found for date1 '+date1+' and date2 '+date2+':'
235         print, '      ', line
236         print, ' '
237
238         ; identify right file
239         ; if ensemble, date1 is set by cmd.date1
240
241         il = 0
242         found = 0
243         delta_1 = 0L
244         WHILE il LE nfiles-1 AND found EQ 0 DO BEGIN
245            file = line[il]
246              ; remove root directory if exists
247            IF strpos(file, '/') NE -1 THEN BEGIN
248               argvar = strsplit(file, '/', /EXTRACT)
249               n = n_elements(argvar)
250               file = argvar[n-1]
251            ENDIF ELSE BEGIN
252               print, '  *** File missing or'
253               print, '  *** Wrong definition for database: ', file
254               stop
255            ENDELSE
256            IF debug_w THEN BEGIN
257               print, '    file = ', file
258            ENDIF
259            argvar = strsplit(file, '_', /EXTRACT)
260            dat1 = argvar[2]
261            dat2 = argvar[3]
262           
263            IF debug_w THEN BEGIN
264               print, '    dat1, date1, dat2, date2 ', dat1,' ', date1,' ', dat2,' ', date2
265            ENDIF
266           
267            IF ensbl_code NE '' AND toggle_ensbl_diff EQ 0 THEN BEGIN
268               IF  ensbl_lt NE 0 THEN BEGIN ; ensemble case
269                  IF (long(dat1) EQ long(date1)) THEN BEGIN ; only look for initial date
270                     found = 1
271                     delta_t1 = ensbl_lt-1 ; (index set by lead time <date1>LT<n>)
272                     date1 = dat1
273                     date2 = dat2
274                     cmd.date1 = date1
275                     cmd.spec = date2
276                     var_file = strpos(file, cmd.var)
277                  ENDIF
278               ENDIF
279            ENDIF ELSE BEGIN ; general case
280                   
281               IF strpos(dat1, '_') EQ -1 AND strpos(dat2, '_') EQ -1 AND $
282                strpos(dat1, '-') EQ -1 AND strpos(dat2, '-') EQ -1 THEN BEGIN
283                  IF (long(dat1) LE long(date1) AND long(dat2) GE long(date2)) THEN BEGIN
284                     IF debug_w THEN BEGIN 
285                        print, ' il, file found: ',  il,  file
286                     ENDIF
287                     found = 1
288                     delta_t2 = compute_time(cmd.timave, dat1, date1)
289                     delta_t1 = delta_t2.count-1
290                     date1 = dat1
291                     date2 = dat2
292                     var_file = strpos(file, cmd.var)
293                  ENDIF   
294               ENDIF ELSE BEGIN
295                  found = 1
296                  nfiles = 1
297                  delta_t1 = 0
298               ENDELSE
299            ENDELSE
300           
301            il = il+1
302         ENDWHILE
303      END
304      'yes': BEGIN ; Mean month case
305         CASE strmid(cmd.timave, strlen(cmd.timave)-2, 1) OF
306            'm': BEGIN          ; mean month case
307               nmonths = long(strmid(cmd.timave, 0,strlen(cmd.timave)-2))
308               date1 = '01'
309               ndate = string(long(12/nmonths), format = '(I2.2)')
310               date2 = ndate+'_'+strmid(cmd.date1, 3, strlen(cmd.date1)-3)
311               timavef = strmid(cmd.timave, 0,strlen(cmd.timave)-2)+'mm'
312            END
313            ELSE:
314         ENDCASE
315         delta_t1 = 0
316         nfiles = 1
317         found = 1
318      END
319      ELSE:
320   ENDCASE
321
322   IF nfiles EQ 0 OR found EQ 0 THEN BEGIN
323    date1 = '???'
324    date2 = '???'
325   ENDIF
326
327END
Note: See TracBrowser for help on using the repository browser.