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

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

improvements/corrections of some *.pro headers

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