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

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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