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

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

replace some print by some report in some .pro (continuation) + improvements/corrections of some *.pro headers

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