source: trunk/SRC/ReadWrite/read_grads.pro @ 130

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

improvements of ReadWrite/?*.pro header

  • Property svn:keywords set to Id
File size: 11.4 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5; @file_comments
6; reading grads file (except "data type station" or "grib")
7; from the grads control file even if there is multiple data files.
8;
9; @categories reading function
10;
11; @param var {in}{required}
12; the variable name
13;
14; @param date1 {in}{required}
15; date of the beginning (yyyymmdd if TIMESTEP is not activate)
16;
17; @param date2 {in}{optional}
18; last date. Optionnal, if not scpecified date2=date1
19;
20; @keyword FILENAME
21; the grads control file name: 'xxxx.ctl'
22;
23; @file_comments
24; keyword GLAMBOUNDARY (via computegrid.pro) a 2 elements vector,
25; {lon1,lon2], giving the longitude boundaries that should be
26; used to visualize the data.
27;         lon2 > lon1
28;         lon2 - lon1 eq 360
29; key_shift will be automatically defined according to GLAMBOUNDARY.
30;
31; @keyword TIMESTEP
32; to specify that the dates are time steps instead of true calendar.
33;
34; @file_comments
35; keyword IODIRECTORY
36; a string giving the name of iodirectory
37; (see isafile.pro for all possibilities).
38; default value is common variable iodir
39;
40; @todo
41;---------------
42; NOT yet available
43;---------------
44;
45;       BOX a 4 or 6 elements 1d array, [lon1,lon2,lat1,lat2, depth1,
46;       depth2], that specifies the area where data must be read
47;
48;       EVERYTHING
49;
50;       NOSTRUCTURE
51;
52; @returns
53; an array
54;
55; @uses common
56;
57; @restriction
58; define all the grid parameters (defined in common.pro)
59; associated to the data.
60;
61; @restrictions
62; this function call the procedure scanfile that use the
63; unix commands grep and sed
64;
65; @examples
66; IDL> a=read_grads('sst',19900101,19900131,filename='outputs.ctl')
67; IDL> plt, a
68;
69; @history Sebastien Masson (smasson\@lodyc.jussieu.fr)
70;
71; @version $Id$
72;
73;-
74;------------------------------------------------------------
75;------------------------------------------------------------
76;------------------------------------------------------------
77
78FUNCTION read_grads, var, date1, date2, FILENAME = filename, BOX=box, TIMESTEP = timestep, EVERYTHING = everything, NOSTRUCT = nostruct, _EXTRA = ex
79;---------------------------------------------------------
80;
81  compile_opt idl2, strictarrsubs
82;
83@cm_4mesh
84@cm_4data
85@cm_4cal
86  IF NOT keyword_set(key_forgetold) THEN BEGIN
87@updatenew
88  ENDIF
89;------------------------------------------------------------
90; we find the filename.
91;------------------------------------------------------------
92   filename = isafile(FILENAME = filename, IODIRECTORY = iodir, _EXTRA = ex)
93   if size(filename, /type) NE 7 then $
94    return, report('read_ncdf cancelled')
95;------------------------------------------------------------
96; we scan the control file called filename
97;------------------------------------------------------------
98   scanctl, filename, filesname, jpt1file, varsname, varslev, swapbytes, bigendian, littleendian, f77sequential, fileheader, theader, xyheader, VARFMT = varfmt, _EXTRA = ex
99   if n_elements(varfmt) EQ 0 then varfmt = 'float'
100;------------------------
101;------------------------
102; check date1 and date2 and found the starting index "t1" and the
103; ending index "t2" that corresponds to the time series specified by
104; date1 and date2 for the time axis defined in the .ctl file.
105;------------------------
106;------------------------
107   if n_elements(date1) EQ 0 then begin
108      t0 = 0
109      t1 = 0
110   ENDIF
111   if n_elements(date2) EQ 0 then date2 = date1
112   if keyword_set(timestep) then BEGIN
113      if date1 GT date2 then begin
114         print, 'date2 must be larger than date1'
115         return, -1
116      endif
117      t1 = 0 > long(date1) < (jpt-1)
118      t2 = t1 > long(date2) < (jpt-1)
119   ENDIF ELSE BEGIN
120      jdate1 = date2jul(date1, /grads) < time[jpt-1]
121      jdate2 = time[0] > date2jul(date2, /grads)
122      if jdate1 GT jdate2 then begin
123         print, 'date2 must be larger than date1'
124         return, -1
125      endif
126      t1 = (where(time GE jdate1))[0]
127      tmp = where(time LE jdate2, t2)
128      t2 = t2-1
129   ENDELSE
130   if t2 LT t1 then begin
131      print, 'There is no date between date1 and date2'
132      return, -1
133   endif
134   jpt2read = t2-t1+1
135;------------------------
136;------------------------
137; index of the variable
138;------------------------
139;------------------------
140   varid = where(strlowcase(varsname) EQ strlowcase(var))
141   varid = varid[0]
142   if varid EQ -1 then begin
143      print, var+' not found in the variable liste of '+filename
144      return,  -1
145   ENDIF
146   varname = var
147   if varslev[varid] EQ 1 then res = fltarr(jpi, jpj, jpt2read, /nozero) $
148   ELSE res = fltarr(jpi, jpj, varslev[varid], jpt2read, /nozero)
149;------------------------
150;------------------------
151; find the first file to be read according to the lile list,, the
152; number of time step in each file and t1 and t2
153;------------------------
154;------------------------
155   indf2read = t1/jpt1file
156   startread = t1-indf2read*jpt1file
157   alreadyread = 0
158readagain:
159   jpt2read1file = min([jpt1file-startread, jpt2read])
160   f2read = filesname[indf2read]
161;------------------------
162;------------------------
163; opening
164;------------------------
165;------------------------;
166; check the existance of the file
167   f2read = isafile(filename = f2read, iodirectory = iodir, _EXTRA = ex)
168; if the file is stored on tape
169   if !version.os_family EQ 'unix' then spawn, 'file '+f2read+' > /dev/null'
170; open the file
171   openr, unit, f2read, /get_lun, error=err $
172    , swap_if_little_endian = bigendian $
173    , swap_if_big_endian = littleendian $
174    , swap_endian = swapbytes
175   if err ne 0 then begin
176      print,!err_string
177      return, -1
178   endif
179;------------------------
180   case varfmt of
181      'byte':fmtsz = 1l
182      'uint':fmtsz = 2l
183      'int':fmtsz = 2l
184      'long':fmtsz = 4l
185      'float':fmtsz = 4l
186   endcase
187;------------------------
188; check its size
189   addf77sec = long(4*2*f77sequential)
190   xyblocsize = xyheader + addf77sec*(xyheader NE 0) + jpi*jpj*fmtsz +addf77sec
191   nxybloc = long(total(varslev))
192   filesize = (fileheader + addf77sec*(fileheader NE 0)) $
193    +(theader+addf77sec*(theader NE 0) + nxybloc*xyblocsize)*jpt1file
194   infof2read=fstat(unit)
195   if infof2read.size NE filesize then begin
196      print, 'According to '+filename+' the file size must be '+strtrim(filesize, 1)+' instead of '+strtrim(infof2read.size, 1)
197      print, 'jpi: '+strtrim(jpi, 2)
198      print, 'jpj: '+strtrim(jpj, 2)
199      print, 'jpt: '+strtrim(jpt, 2)
200      print, 'format size in byte: '+strtrim(fmtsz, 2)
201      print, 'number of xy arrays: '+strtrim(nxybloc, 2)
202      return, -1
203   endif
204;------------------------
205;------------------------
206; reading
207;------------------------
208;------------------------;
209;------------------------;
210; loop on the time steps to be read in one file
211   for i = 0, jpt2read1file-1 do begin
212; computing the offset
213      offset = (fileheader + addf77sec*(fileheader NE 0)) $
214       +(theader+addf77sec*(theader NE 0) + nxybloc*xyblocsize)*(startread+i) $
215       +theader+addf77sec*(theader NE 0)
216      if varid NE 0 THEN $
217       offset = offset + long(total(varslev[0:varid-1]))*xyblocsize
218; if there is only one level
219      IF varslev[varid] EQ 1 then begin
220         case varfmt of
221            'byte':a=assoc(unit, bytarr(jpi,jpj,/nozero), offset+4*f77sequential)
222            'uint':a=assoc(unit,uintarr(jpi,jpj,/nozero), offset+4*f77sequential)
223            'int':a=assoc(unit,  intarr(jpi,jpj,/nozero), offset+4*f77sequential)
224            'long':a=assoc(unit, lonarr(jpi,jpj,/nozero), offset+4*f77sequential)
225            'float':a=assoc(unit,fltarr(jpi,jpj,/nozero), offset+4*f77sequential)
226         endcase
227         res[*, *, i+alreadyread]=a[0]
228      ENDIF ELSE BEGIN ; more than 1 level to be read
229         if f77sequential then BEGIN ; sequential access
230            case varfmt of
231               'byte':a=assoc(unit, bytarr(jpi*jpj+8, varslev[varid],/nozero), offset)
232               'uint':a=assoc(unit,uintarr(jpi*jpj+4, varslev[varid],/nozero), offset)
233               'int':a=assoc(unit,  intarr(jpi*jpj+4, varslev[varid],/nozero), offset)
234               'long':a=assoc(unit, lonarr(jpi*jpj+2, varslev[varid],/nozero), offset)
235               'float':a=assoc(unit,fltarr(jpi*jpj+2, varslev[varid],/nozero), offset)
236            endcase
237            tmp = a[0]
238            case varfmt OF ; we cut the headers and tailers of f77 write
239               'byte': tmp = tmp[4:jpi*jpj+3, *]
240               'uint': tmp = tmp[2:jpi*jpj+1, *]
241               'int':  tmp = tmp[2:jpi*jpj+1, *]
242               'long': tmp = tmp[1:jpi*jpj+0, *]
243               'float':tmp = tmp[1:jpi*jpj+0, *]
244            endcase
245            if keyword_set(key_zreverse) then res[*, *, *, i+alreadyread]=reverse(reform(tmp,  jpi, jpj, varslev[varid], /over), 3) ELSE res[*, *, *, i+alreadyread]=reform(tmp,  jpi, jpj, varslev[varid], /over)
246         ENDIF ELSE BEGIN  ; direct acces
247            case varfmt of
248               'byte':a=assoc(unit, bytarr(jpi,jpj,varslev[varid],/nozero),offset)
249               'uint':a=assoc(unit,uintarr(jpi,jpj,varslev[varid],/nozero),offset)
250               'int':a=assoc(unit,  intarr(jpi,jpj,varslev[varid],/nozero),offset)
251               'long':a=assoc(unit, lonarr(jpi,jpj,varslev[varid],/nozero),offset)
252               'float':a=assoc(unit,fltarr(jpi,jpj,varslev[varid],/nozero),offset)
253            endcase
254            if keyword_set(key_zreverse) then res[*, *, *, i+alreadyread]=reverse(a[0], 3) ELSE res[*, *, *, i+alreadyread]=a[0]
255         ENDELSE
256      ENDELSE
257   endfor
258;------------------------
259; close the file
260   free_lun,unit
261   close,unit
262;------------------------
263;------------------------
264; do we need to read a new file to complete the time series ?
265;------------------------
266;------------------------
267   if jpt2read1file NE jpt2read then BEGIN
268      indf2read = indf2read+1
269      startread = 0
270      alreadyread = alreadyread+jpt2read1file
271      jpt2read = jpt2read-jpt2read1file
272      GOTO, readagain
273   ENDIF
274;------------------------
275;------------------------
276; post processing
277;------------------------
278;------------------------
279   if keyword_set(key_yreverse) then res = reverse(res, 2)
280   if keyword_set(key_shift) then begin
281      case (size(res))[0] of
282         2:res = shift(res, key_shift, 0)
283         3:res = shift(res, key_shift, 0, 0)
284         4:res = shift(res, key_shift, 0, 0, 0)
285      endcase
286   endif
287;------------------------
288;   mask
289;   IF varslev[varid] EQ 1 then begin
290;      if abs(valmask) LE 1e5 then notgood = where(res[*, *, 0] EQ valmask) $
291;      ELSE notgood = where(abs(res[*, *, 0]) GE abs(valmask)/10)
292;      if notgood[0] NE -1 then tmask[notgood] = 0b
293;   ENDIF ELSE BEGIN
294;      if abs(valmask) LE 1e5 then notgood = where(res[*, *, *, 0] EQ valmask) $
295;      ELSE notgood = where(abs(res[*, *, *, 0]) GE abs(valmask)/10)
296;      if notgood[0] NE -1 then tmask[notgood] = 0b
297;   ENDELSE
298   if abs(valmask) LE 1e5 then notgood = where(res EQ valmask) $
299   ELSE notgood = where(abs(res) GE abs(valmask)/10)
300   if notgood[0] NE -1 THEN res[notgood] = !values.f_nan
301;   valmask = 1e20
302;   if abs(valmask) LE 1e5 then notgood = where(res EQ valmask) $
303;   ELSE notgood = where(abs(res) GE abs(valmask)/10)
304;   if notgood[0] NE -1 THEN res[notgood] = 1e20
305;   valmask = 1e20
306   triangles_list = triangule()
307;------------------------
308; subdomain extration
309
310;------------------------
311; time aguments
312;------------------------
313   time = time[t1:t2]
314   jpt = t2-t1+1
315   if keyword_set(timestep) then vardate = strtrim(time[0], 2) $
316   ELSE vardate = date2string(vairdate(time[0]))
317;------------------------
318   @updateold
319;------------------------
320
321   return, res
322end
Note: See TracBrowser for help on using the repository browser.