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

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

modification of headers : mainly blanks around = sign for keywords in declaration of function and pro

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