source: trunk/SRC/ReadWrite/scanctl.pro @ 72

Last change on this file since 72 was 44, checked in by pinsard, 18 years ago

upgrade of LECTURE/ReadWrite according to cerbere.lodyc.jussieu.fr: /usr/home/smasson/SAXO_RD/ : files

  • Property svn:executable set to *
File size: 16.3 KB
Line 
1;       GLAMBOUNDARY:a 2 elements vector, {lon1,lon2], the longitute
2;       boundaries that should be used to visualize the data.
3;         lon2 > lon1
4;         lon2 - lon1 le 360
5;       key_shift will be defined according to GLAMBOUNDARY.
6PRO scanctl, filename, filesname, jpt1file, varsname, varslev, swapbytes, bigendian, littleendian, f77sequential, fileheader, theader, xyheader, VARFMT = varfmt, _EXTRA = ex
7@common
8
9   time1 = systime(1)           ; for key_performance
10
11;------------------------
12; DTYPE
13;------------------------
14   spawn, '\grep -i "^DTYPE" '+filename, notgood
15   if keyword_set(notgood) then begin
16      print, 'This program is not adapted to data type station or grib. Sorry...'
17      stop
18   endif
19;------------------------
20; UNDEF, define valmask
21;------------------------
22   spawn, '\grep -i "^UNDEF" '+filename, valmask
23   valmask = strtrim(valmask, 2)
24   valmask = strsplit(valmask[0],/extract)
25   valmask = float(valmask[1])
26;------------------------
27; Headers
28;------------------------
29   spawn, '\grep -i "^FILEHEADER" '+filename, fileheader
30   fileheader = strtrim(fileheader, 2)
31   if keyword_set(fileheader) then BEGIN
32      fileheader = strsplit(fileheader[0],/extract)
33      fileheader = long(fileheader[1])
34   ENDIF ELSE fileheader = 0L
35   spawn, '\grep -i "^THEADER" '+filename, theader
36   theader = strtrim(theader, 2)
37   if keyword_set(theader) then BEGIN
38      theader = strsplit(theader[0],/extract)
39      theader = long(theader[1])
40   ENDIF ELSE theader = 0L
41   spawn, '\grep -i "^XYHEADER" '+filename, xyheader
42   xyheader = strtrim(xyheader, 2)
43   if keyword_set(xyheader) then BEGIN
44      xyheader = strsplit(xyheader[0],/extract)
45      xyheader = long(xyheader[1])
46   ENDIF ELSE xyheader = 0L
47;------------------------
48;------------------------
49; find the x axis
50;------------------------
51;------------------------
52   spawn, '\sed -n -e ''/^#/d'' -e ''/^[Xx][Dd][Ee][Ff]/,/^[Yy][Dd][Ee][Ff]/p'' '+filename, xdef
53   if xdef[0] EQ '' then BEGIN
54      print, 'Bad definition of xdef or ydef'
55      stop
56   ENDIF
57   xdef = xdef[0:n_elements(xdef)-2]
58   if n_elements(xdef) NE 1 then begin
59      xdef = [byte(xdef), replicate(byte(' '),1,n_elements(xdef))]
60      xdef = xdef[where(xdef NE 0)]
61      xdef = string(xdef)
62   endif
63   xdef = strtrim(xdef[0], 2)
64   xdef = strsplit(xdef,/extract)
65   jpi = long(xdef[1])
66   case strupcase(xdef[2]) of
67      'LINEAR':xaxis = float(xdef[3])+findgen(jpi)*float(xdef[4])
68      'LEVELS':xaxis = float(xdef[3:n_elements(xdef)-1])
69   ENDCASE
70;------------------------
71;------------------------
72; find the y axis
73;------------------------
74;------------------------
75   spawn, '\sed -n -e ''/^#/d'' -e ''/^[Yy][Dd][Ee][Ff]/,/^[Zz][Dd][Ee][Ff]/p'' '+filename, ydef
76   if ydef[0] EQ '' then BEGIN
77      print, 'Bad definition of ydef or zdef'
78      stop
79   ENDIF
80   ydef = ydef[0:n_elements(ydef)-2]
81   if n_elements(ydef) NE 1 then begin
82      ydef = [byte(ydef), replicate(byte(' '),1,n_elements(ydef))]
83      ydef = ydef[where(ydef NE 0)]
84      ydef = string(ydef)
85   endif
86   ydef = strtrim(ydef[0], 2)
87   ydef = strsplit(ydef,/extract)
88   jpj = long(ydef[1])
89   case strupcase(ydef[2]) of
90      'LINEAR':yaxis = float(ydef[3])+findgen(jpj)*float(ydef[4])
91      'LEVELS':yaxis = float(ydef[3:n_elements(ydef)-1])
92   'GAUST62':BEGIN & print, 'Not yet coded...' & stop & END
93   'GAUSR15':BEGIN & print, 'Not yet coded...' & stop & END
94   'GAUSR20':BEGIN & print, 'Not yet coded...' & stop & END
95   'GAUSR30':BEGIN & print, 'Not yet coded...' & stop & END
96   'GAUSR40':BEGIN & print, 'Not yet coded...' & stop & END
97   ELSE:BEGIN & print, 'Not yet coded...' & stop & END
98   endcase
99;------------------------
100;------------------------
101; find the z axis
102;------------------------
103;------------------------
104   spawn, '\sed -n -e ''/^#/d'' -e ''/^[Zz][Dd][Ee][Ff]/,/^[Tt][Dd][Ee][Ff]/p'' '+filename, zdef
105   if zdef[0] EQ '' then BEGIN
106      print, 'Bad definition of zdef or tdef'
107      stop
108   ENDIF
109   zdef = zdef[0:n_elements(zdef)-2]
110   if n_elements(zdef) NE 1 then begin
111      zdef = [byte(zdef), replicate(byte(' '),1,n_elements(zdef))]
112      zdef = zdef[where(zdef NE 0)]
113      zdef = string(zdef)
114   endif
115   zdef = strtrim(zdef[0], 2)
116   zdef = strsplit(zdef,/extract)
117   jpk = long(zdef[1])
118   case strupcase(zdef[2]) of
119      'LINEAR':zaxis = float(zdef[3])+findgen(jpk)*float(zdef[4])
120      'LEVELS':zaxis = float(zdef[3:n_elements(zdef)-1])
121   ENDCASE
122;------------------------
123;------------------------
124; compute the grid
125;------------------------
126;------------------------
127   computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis, _EXTRA = ex
128   domdef
129;------------------------
130;------------------------
131; find the time axis
132;------------------------
133;------------------------
134   spawn, '\grep -i "^TDEF" '+filename, timedef
135   timedef = strupcase(strtrim(timedef, 2))
136   timedef = strsplit(timedef[0],/extract)
137   jpt = long(timedef[1])
138;------------------------
139; initial date: y0, m0, d0, h0, mn0
140;             -> julian day of IDL: julady(m0, d0, y0, h0, mn0, 00)
141;------------------------
142   t0 = timedef[3]
143   monthsname = string(format='(C(CMOA))',31*(indgen(12)))
144   case 1 OF
145; h[h]:mmZd[d]mmmyy[yy]
146      strpos(t0, ':') NE -1:BEGIN
147         pp = strpos(t0, ':')
148         h0 = long(strmid(t0, 0, pp))
149         mn0 = long(strmid(t0, pp+1, 2))
150         pp = strpos(t0, 'Z')
151         dd = byte(strmid(t0, pp+2, 1)) LT byte('A')
152         d0 = long(strmid(t0, pp+1, 1+dd))
153         m0 = (where(monthsname EQ strmid(t0, pp+2+dd, 3)))[0]+1
154         y0 = long(strmid(t0, pp+5+dd))
155      END
156; m[m]Zd[d]mmmyy[yy]
157      strpos(t0, 'Z') NE -1:BEGIN
158         h0 = 0+12
159         pp = strpos(t0, 'Z')
160         mn0 = long(strmid(t0, 0, pp))
161         dd = byte(strmid(t0, pp+2, 1)) LT byte('A')
162         d0 = long(strmid(t0, pp+1, 1+dd))
163         m0 = (where(monthsname EQ strmid(t0, pp+2+dd, 3)))[0]+1
164         y0 = long(strmid(t0, pp+5+dd))
165      END
166; d[d]mmmyy[yy]
167      (byte(strmid(t0, 0, 1)) LT byte('A'))[0]:BEGIN
168         h0 = 0+12
169         mn0 = 0
170         dd = byte(strmid(t0, 1, 1)) LT byte('A')
171         d0 = long(strmid(t0, 0, 1+dd))
172         m0 = (where(monthsname EQ strmid(t0, 1+dd, 3)))[0]+1
173         y0 = long(strmid(t0, 4+dd))
174      END
175; mmmyy[yy]
176      ELSE:BEGIN
177         h0 = 0+12
178         mn0 = 0
179         d0 = 1
180         m0 = (where(monthsname EQ strmid(t0, 0, 3)))[0]+1
181         y0 = long(strmid(t0, 3))
182      END
183   ENDCASE
184; if y0 is a two digit integer -> between 1950 and 2049
185   case 1 of
186      y0 LE 49:y0 = 2000+y0
187      y0 LE 99:y0 = 1900+y0
188      ELSE:
189   ENDCASE
190;------------------------
191; increment date and definition of the calendar with IDL julian days
192;------------------------
193   tstep = timedef[4]
194   tsval = long(strmid(tstep,0, strlen(tstep)-2))
195   case strlowcase(strmid(tstep, 1, /reverse)) of
196      'mn':time = julday(m0, d0, y0, h0, mn0+lindgen(jpt)*tsval, 0)
197      'hr':time = julday(m0, d0, y0, h0+lindgen(jpt)*tsval, mn0, 0)
198      'dy':time = julday(m0, d0+lindgen(jpt)*tsval, y0, h0, mn0, 0)
199      'mo':time = julday(m0+lindgen(jpt)*tsval, d0, y0, h0, mn0, 0)
200      'yr':time = julday(m0, d0, y0+lindgen(jpt)*tsval, h0, mn0, 0)
201   ENDCASE                      ;
202; shit the calendar to correspond to the time step.
203   case strlowcase(strmid(tstep, 1, /reverse)) of
204      'dy':time = long(time)
205      'mo':time = long(time)+14L
206      'yr':time = long(time)+365L/2
207      ELSE:
208   endcase
209;------------------------
210; OPTIONS
211;------------------------
212   spawn, '\grep -i "^OPTIONS" '+filename, options
213   options = strtrim(options, 2)
214   options = strlowcase(options[0])
215   key_yreverse = strpos(options, 'yrev') NE -1
216   key_zreverse = strpos(options, 'zrev') EQ -1
217   multifiles = strpos(options, 'template') NE -1
218   f77sequential = strpos(options, 'sequential') NE -1
219   swapbytes = strpos(options, 'byteswapped') NE -1
220   bigendian = strpos(options, 'big_endian') NE -1
221   littleendian = strpos(options, 'little_endian') NE -1
222   cray = strpos(options, 'cray_32bit_ieee') NE -1
223IF cray THEN BEGIN & print, 'cray_32bit_ieee; Not yet coded...' & stop & ENDIF
224   cal365 = strpos(options, '365_day_calendar') NE -1
225IF cal365 THEN BEGIN & print, '365_day_calenda; Not yet coded...' & stop & ENDIF
226;------------------------
227;------------------------
228; building the filesname
229;------------------------
230;------------------------
231   spawn, '\grep -i "^DSET" '+filename, files
232   files = strtrim(files[0], 2)
233   files = strsplit(files,/extract)
234   if n_elements(files) NE 2 then begin
235      print, 'Bad definition of the filename. There shoud be 2 elements:'
236      print, 'DEST and 1 filename (that may define many files)'
237      stop
238   endif
239   files = files[1]
240;   files = strmid(files[0], strpos(files[0],' ', /reverse_search)+1)
241   filesname = files
242   if keyword_set(multifiles) then begin
243; minutes
244      if (stregex(files,'%i?n2'))[0] NE -1 then begin
245         filetsep = 'mn'
246         mnend = long(mn0+jpt-1)
247         tmp = strarr(hend-h0+1)
248         for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?n2',/extract,/regex), string(mn0+i, format = '(i2.2)'))
249         filesname = strjoin(tmp, '!@#$')
250      endif
251; hours
252      if (stregex(files,'%i?[hf][123]'))[0] NE -1 then begin
253         filetsep = 'hr'
254         case strlowcase(strmid(tstep, 1, /reverse)) of
255            'mn':hend = long(h0+(jpt+mn0-1-1)/60.)
256            'hr':hend = long(h0+jpt-1)
257         endcase
258         tmp = strarr(hend-h0+1)
259         case 1 of
260            stregex(files,'%i?h1') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?h1',/extract,/regex), strtrim(h0+i, 1))
261            stregex(files,'%i?h2') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?h2',/extract,/regex), string(h0+i, format = '(i2.2)'))
262            stregex(files,'%f2') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%f2',/extract,/regex), string(h0+i, format = '(i3.2)'))
263            stregex(files,'%i?[hf]3') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?[hf]3',/extract,/regex), string(h0+i, format = '(i3.3)'))
264         endcase
265         filesname = strjoin(tmp, '!@#$')
266      endif
267; days
268      if (stregex(files,'%i?d[12]'))[0] NE -1 then begin
269         filetsep = 'dy'
270         case strlowcase(strmid(tstep, 1, /reverse)) of
271            'mn':dend = long(d0+(jpt+mn0-1-1)/1440.)
272            'hr':dend = long(d0+(jpt+h0-1-1)/24.)
273            'dy':dend = long(d0+jpt-1)
274         endcase
275         tmp = strarr(dend-d0+1)
276         case 1 of
277            stregex(files,'%i?d1') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?d1',/extract,/regex), strtrim(d0+i, 1))
278            stregex(files,'%i?d2') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?d2',/extract,/regex), string(d0+i, format = '(i2.2)'))
279         endcase
280         filesname = strjoin(tmp, '!@#$')
281      endif
282; months
283      if (stregex(files,'%i?m[12c]'))[0] NE -1 then begin
284         filetsep = 'mo'
285         tmp = strarr(12)
286         case 1 of
287            stregex(files,'%i?m1') NE -1:for i = 1, 12 do tmp[i-1] = strjoin(strsplit(filesname,'%i?m1',/extract,/regex), strtrim(i, 1))
288            stregex(files,'%i?m2') NE -1:for i = 1, 12 do tmp[i-1] = strjoin(strsplit(filesname,'%i?m2',/extract,/regex), string(i, format = '(i2.2)'))
289            stregex(files,'%i?mc') NE -1:for i = 1, 12 do tmp[i-1] = strjoin(strsplit(filesname,'%i?mc',/extract,/regex), monthsname[i-1])
290         endcase
291         filesname = strjoin(tmp, '!@#$')
292      endif
293; years
294      if (stregex(files,'%i?y[24]'))[0] NE -1 then begin
295         case strlowcase(strmid(tstep, 1, /reverse)) of
296            'dy':yend = long(y0+(jpt+d0-1-1)/365.)
297            'mo':yend = long(y0+(jpt+m0-1-1)/12.)
298            'yr':yend = long(y0+jpt-1)
299            ELSE:yend = y0
300         endcase
301         tmp = strarr(yend-y0+1)
302         case 1 of
303            stregex(files,'%i?y2') NE -1:for i = 0, n_elements(tmp)-1 do tmp[i] = strjoin(strsplit(filesname,'%i?y2',/extract,/regex), string((y0+i)-100*((y0+i)/100), format = '(i2.2)'))
304            stregex(files,'%i?y4') NE -1:for i = 0, n_elements(tmp)-1  do tmp[i] = strjoin(strsplit(filesname,'%i?y]4',/extract,/regex), string(y0+i, format = '(i4.4)'))
305         endcase
306         filesname = strjoin(tmp, '!@#$')
307      endif
308      filesname = strsplit(filesname, '!@#$', /extract)
309;
310; time step unit of each file:
311;
312      case 1 of
313         (stregex(files,'%i?n2'))[0] NE -1:filetsep = 'mn'
314         (stregex(files,'%i?[hf][123]'))[0] NE -1:filetsep = 'hr'
315         (stregex(files,'%i?d[12]'))[0] NE -1:filetsep = 'dy'
316         (stregex(files,'%i?m[12c]'))[0] NE -1: filetsep = 'mo'
317         (stregex(files,'%i?y[24]'))[0] NE -1:filetsep = 'yr'
318      ENDCASE
319;
320; number of time steps for each files
321;
322      case strlowcase(strmid(tstep, 1, /reverse)) of
323         'mn':BEGIN
324            case filetsep of
325               'yr':jpt1file = 60L*24L*365L
326               'mo':jpt1file = 60L*24L*30L
327               'dy':jpt1file = 60L*24L
328               'hr':jpt1file = 60L
329               'mn':jpt1file = 1L
330            endcase
331         END
332         'hr':BEGIN
333            case filetsep of
334               'yr':jpt1file = 24L*365L
335               'mo':jpt1file = 24L*30L
336               'dy':jpt1file = 24L
337               'hr':jpt1file = 1L
338            endcase
339         END
340         'dy':BEGIN
341            case filetsep of
342               'yr':jpt1file = 365L
343               'mo':jpt1file = 30L
344               'dy':jpt1file = 1L
345            endcase
346         END
347         'mo':BEGIN
348            case filetsep of
349               'yr':jpt1file = 12L
350               'mo':jpt1file = 1L
351            endcase
352         END
353         'yr':jpt1file = 1L
354      endcase
355;
356; number of files
357;
358      nof = ceil(jpt/(1.*jpt1file))
359      filesname = filesname[0:nof-1]
360   ENDIF ELSE BEGIN
361      nof = 1
362      jpt1file = jpt
363   ENDELSE
364; first character ^
365   if stregex(files,'^\^') GE 0 THEN BEGIN
366      iodir = strmid(filename,0,strpos(filename,'/',/reverse_search)+1)
367      for i = 0, nof-1 do filesname[i] = iodir+strmid(filesname[i], 1)
368   ENDIF
369;------------------------
370;------------------------
371; extracting the variables
372;------------------------
373;------------------------
374   spawn, '\grep -i "^VARS" '+filename, nvars
375   nvars = strtrim(nvars, 2)
376   nvars = strsplit(nvars[0],/extract)
377   nvars = long(nvars[1])
378   spawn, '\sed -n -e ''/^#/d'' -e ''/^[Vv][Aa][Rr][Ss]/,/^[Ee][Nn][Dd][Vv][Aa][Rr][Ss]/p'' '+filename, varlist
379   if n_elements(varlist) LE 2  then begin
380      print, 'No lines between vars and endvars???'
381      stop
382   endif
383   varlist = varlist[1:n_elements(varlist)-2]
384   if n_elements(varlist) NE nvars  then begin
385      print, 'Number of variables indicated by VARS ('+strtrim(nvars, 1)+') differs from number of lines (without ''#'' at the beginning) located between VARS and ENDVARS: '+strtrim(n_elements(varlist), 1)
386      stop
387   ENDIF
388   varsname = strarr(nvars)
389   varsdes = strarr(nvars)
390   varslev = lonarr(nvars)
391   for i = 0, nvars-1 do BEGIN
392      varlist[i] = strtrim(varlist[i], 2)
393      tmp = strsplit(varlist[i],/extract)
394      if strmid(tmp[2], 0, 2) EQ '-1' then BEGIN
395         case long(strmid(tmp[2], 3, 2)) of
396            10:BEGIN
397               print, 'Special data formats, units = -1,10... Not yet coded...'
398               stop
399            END
400            20:BEGIN
401               print, 'Special data formats, units = -1,20... Not yet coded...'
402               stop
403            END
404            30:BEGIN
405               print, 'Special data formats, units = -1,30... Not yet coded...'
406               stop
407            END
408            40:BEGIN
409               case long(strmid(tmp[2], 6)) of
410                  1:varfmt = 'byte'
411                  2:varfmt = 'uint'
412                  -2:varfmt = 'int'
413                  4:varfmt = 'long'
414                  ELSE:BEGIN
415                     print, 'Bad definition of the special data formats: '
416                     print,long(strmid(tmp[2],6))+' should be equal to 1, 2, -2 or 4'
417                     stop
418                  END
419               endcase
420            END
421            ELSE:BEGIN
422               print, 'Special data formats, units = -1, ... Not yet coded...'
423               stop
424            END
425         endcase
426      endif
427      varsname[i] = tmp[0]
428      varsdes[i] = strjoin(tmp[3:n_elements(tmp)-1], ' ')
429      varslev[i] = long(tmp[1])
430   ENDFOR
431   varslev = 1 > varslev
432
433;
434   ccmeshparameters.filename = filename
435   ccmeshparameters.filename = 'Grads'
436;
437   IF keyword_set(key_performance) EQ 1 THEN print, 'time scanctl', systime(1)-time1
438
439   return
440end
441;
442;
443
Note: See TracBrowser for help on using the repository browser.