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

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

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • 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, FILENAME = filename, BOX=box, TIMESTEP = timestep, EVERYTHING = everything, NOSTRUCT = nostruct, _EXTRA = ex
62;
63  compile_opt idl2, strictarrsubs
64;
65@cm_4mesh
66@cm_4data
67@cm_4cal
68  IF NOT keyword_set(key_forgetold) THEN BEGIN
69@updatenew
70  ENDIF
71;------------------------------------------------------------
72; we find the filename.
73;------------------------------------------------------------
74   filename = isafile(FILENAME = filename, IODIRECTORY = iodir, _EXTRA = ex)
75   if size(filename, /type) NE 7 then $
76    return, report('read_ncdf cancelled')
77;------------------------------------------------------------
78; we scan the control file called filename
79;------------------------------------------------------------
80   scanctl, filename, filesname, jpt1file, varsname, varslev, swapbytes, bigendian, littleendian, f77sequential, fileheader, theader, xyheader, VARFMT = varfmt, _EXTRA = ex
81   if n_elements(varfmt) EQ 0 then varfmt = 'float'
82;------------------------
83;------------------------
84; check date1 and date2 and found the starting index "t1" and the
85; ending index "t2" that corresponds to the time series specified by
86; date1 and date2 for the time axis defined in the .ctl file.
87;------------------------
88;------------------------
89   if n_elements(date1) EQ 0 then begin
90      t0 = 0
91      t1 = 0
92   ENDIF
93   if n_elements(date2) EQ 0 then date2 = date1
94   if keyword_set(timestep) then BEGIN
95      if date1 GT date2 then begin
96         ras = report( 'date2 must be larger than date1')
97         return, -1
98      endif
99      t1 = 0 > long(date1) < (jpt-1)
100      t2 = t1 > long(date2) < (jpt-1)
101   ENDIF ELSE BEGIN
102      jdate1 = date2jul(date1, /grads) < time[jpt-1]
103      jdate2 = time[0] > date2jul(date2, /grads)
104      if jdate1 GT jdate2 then begin
105         ras = report('date2 must be larger than date1')
106         return, -1
107      endif
108      t1 = (where(time GE jdate1))[0]
109      tmp = where(time LE jdate2, t2)
110      t2 = t2-1
111   ENDELSE
112   if t2 LT t1 then begin
113      ras = report('There is no date between date1 and date2')
114      return, -1
115   endif
116   jpt2read = t2-t1+1
117;------------------------
118;------------------------
119; index of the variable
120;------------------------
121;------------------------
122   varid = where(strlowcase(varsname) EQ strlowcase(var))
123   varid = varid[0]
124   if varid EQ -1 then begin
125      ras = report(var+' not found in the variable list of '+filename)
126      return,  -1
127   ENDIF
128   varname = var
129   if varslev[varid] EQ 1 then res = fltarr(jpi, jpj, jpt2read, /nozero) $
130   ELSE res = fltarr(jpi, jpj, varslev[varid], jpt2read, /nozero)
131;------------------------
132;------------------------
133; find the first file to be read according to the file list, the
134; number of time step in each file and t1 and t2
135;------------------------
136;------------------------
137   indf2read = t1/jpt1file
138   startread = t1-indf2read*jpt1file
139   alreadyread = 0
140readagain:
141   jpt2read1file = min([jpt1file-startread, jpt2read])
142   f2read = filesname[indf2read]
143;------------------------
144;------------------------
145; opening
146;------------------------
147;------------------------;
148; check the existence of the file
149   f2read = isafile(filename = f2read, iodirectory = iodir, _EXTRA = ex)
150; if the file is stored on tape
151   if !version.os_family EQ 'unix' then spawn, 'file '+f2read+' > /dev/null'
152; open the file
153   openr, unit, f2read, /get_lun, error=err $
154    , swap_if_little_endian = bigendian $
155    , swap_if_big_endian = littleendian $
156    , swap_endian = swapbytes
157   if err ne 0 then begin
158      ras = report(!err_string)
159      return, -1
160   endif
161;------------------------
162   case varfmt of
163      'byte':fmtsz = 1l
164      'uint':fmtsz = 2l
165      'int':fmtsz = 2l
166      'long':fmtsz = 4l
167      'float':fmtsz = 4l
168   endcase
169;------------------------
170; check its size
171   addf77sec = long(4*2*f77sequential)
172   xyblocsize = xyheader + addf77sec*(xyheader NE 0) + jpi*jpj*fmtsz +addf77sec
173   nxybloc = long(total(varslev))
174   filesize = (fileheader + addf77sec*(fileheader NE 0)) $
175    +(theader+addf77sec*(theader NE 0) + nxybloc*xyblocsize)*jpt1file
176   infof2read=fstat(unit)
177   if infof2read.size NE filesize then begin
178      ras = report(['According to '+filename+' the file size must be '+strtrim(filesize, 1)+' instead of '+strtrim(infof2read.size, 1), $
179      'jpi: '+strtrim(jpi, 2), $
180      'jpj: '+strtrim(jpj, 2), $
181      'jpt: '+strtrim(jpt, 2), $
182      'format size in byte: '+strtrim(fmtsz, 2), $
183      'number of xy arrays: '+strtrim(nxybloc, 2)])
184      return, -1
185   endif
186;------------------------
187;------------------------
188; reading
189;------------------------
190;------------------------;
191;------------------------;
192; loop on the time steps to be read in one file
193   for i = 0, jpt2read1file-1 do begin
194; computing the offset
195      offset = (fileheader + addf77sec*(fileheader NE 0)) $
196       +(theader+addf77sec*(theader NE 0) + nxybloc*xyblocsize)*(startread+i) $
197       +theader+addf77sec*(theader NE 0)
198      if varid NE 0 THEN $
199       offset = offset + long(total(varslev[0:varid-1]))*xyblocsize
200; if there is only one level
201      IF varslev[varid] EQ 1 then begin
202         case varfmt of
203            'byte':a=assoc(unit, bytarr(jpi,jpj,/nozero), offset+4*f77sequential)
204            'uint':a=assoc(unit,uintarr(jpi,jpj,/nozero), offset+4*f77sequential)
205            'int':a=assoc(unit,  intarr(jpi,jpj,/nozero), offset+4*f77sequential)
206            'long':a=assoc(unit, lonarr(jpi,jpj,/nozero), offset+4*f77sequential)
207            'float':a=assoc(unit,fltarr(jpi,jpj,/nozero), offset+4*f77sequential)
208         endcase
209         res[*, *, i+alreadyread]=a[0]
210      ENDIF ELSE BEGIN ; more than 1 level to be read
211         if f77sequential then BEGIN ; sequential access
212            case varfmt of
213               'byte':a=assoc(unit, bytarr(jpi*jpj+8, varslev[varid],/nozero), offset)
214               'uint':a=assoc(unit,uintarr(jpi*jpj+4, varslev[varid],/nozero), offset)
215               'int':a=assoc(unit,  intarr(jpi*jpj+4, varslev[varid],/nozero), offset)
216               'long':a=assoc(unit, lonarr(jpi*jpj+2, varslev[varid],/nozero), offset)
217               'float':a=assoc(unit,fltarr(jpi*jpj+2, varslev[varid],/nozero), offset)
218            endcase
219            tmp = a[0]
220            case varfmt OF ; we cut the headers and tailers of f77 write
221               'byte': tmp = tmp[4:jpi*jpj+3, *]
222               'uint': tmp = tmp[2:jpi*jpj+1, *]
223               'int':  tmp = tmp[2:jpi*jpj+1, *]
224               'long': tmp = tmp[1:jpi*jpj+0, *]
225               'float':tmp = tmp[1:jpi*jpj+0, *]
226            endcase
227            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)
228         ENDIF ELSE BEGIN  ; direct acces
229            case varfmt of
230               'byte':a=assoc(unit, bytarr(jpi,jpj,varslev[varid],/nozero),offset)
231               'uint':a=assoc(unit,uintarr(jpi,jpj,varslev[varid],/nozero),offset)
232               'int':a=assoc(unit,  intarr(jpi,jpj,varslev[varid],/nozero),offset)
233               'long':a=assoc(unit, lonarr(jpi,jpj,varslev[varid],/nozero),offset)
234               'float':a=assoc(unit,fltarr(jpi,jpj,varslev[varid],/nozero),offset)
235            endcase
236            if keyword_set(key_zreverse) then res[*, *, *, i+alreadyread]=reverse(a[0], 3) ELSE res[*, *, *, i+alreadyread]=a[0]
237         ENDELSE
238      ENDELSE
239   endfor
240;------------------------
241; close the file
242   free_lun,unit
243   close,unit
244;------------------------
245;------------------------
246; do we need to read a new file to complete the time series ?
247;------------------------
248;------------------------
249   if jpt2read1file NE jpt2read then BEGIN
250      indf2read = indf2read+1
251      startread = 0
252      alreadyread = alreadyread+jpt2read1file
253      jpt2read = jpt2read-jpt2read1file
254      GOTO, readagain
255   ENDIF
256;------------------------
257;------------------------
258; post processing
259;------------------------
260;------------------------
261   if keyword_set(key_yreverse) then res = reverse(res, 2)
262   if keyword_set(key_shift) then begin
263      case (size(res))[0] of
264         2:res = shift(res, key_shift, 0)
265         3:res = shift(res, key_shift, 0, 0)
266         4:res = shift(res, key_shift, 0, 0, 0)
267      endcase
268   endif
269;------------------------
270;   mask
271;   IF varslev[varid] EQ 1 then begin
272;      if abs(valmask) LE 1e5 then notgood = where(res[*, *, 0] EQ valmask) $
273;      ELSE notgood = where(abs(res[*, *, 0]) GE abs(valmask)/10)
274;      if notgood[0] NE -1 then tmask[notgood] = 0b
275;   ENDIF ELSE BEGIN
276;      if abs(valmask) LE 1e5 then notgood = where(res[*, *, *, 0] EQ valmask) $
277;      ELSE notgood = where(abs(res[*, *, *, 0]) GE abs(valmask)/10)
278;      if notgood[0] NE -1 then tmask[notgood] = 0b
279;   ENDELSE
280   if abs(valmask) LE 1e5 then notgood = where(res EQ valmask) $
281   ELSE notgood = where(abs(res) GE abs(valmask)/10)
282   if notgood[0] NE -1 THEN res[notgood] = !values.f_nan
283;   valmask = 1e20
284;   if abs(valmask) LE 1e5 then notgood = where(res EQ valmask) $
285;   ELSE notgood = where(abs(res) GE abs(valmask)/10)
286;   if notgood[0] NE -1 THEN res[notgood] = 1e20
287;   valmask = 1e20
288   triangles_list = triangule()
289;------------------------
290; subdomain extraction
291
292;------------------------
293; time arguments
294;------------------------
295   time = time[t1:t2]
296   jpt = t2-t1+1
297   if keyword_set(timestep) then vardate = strtrim(time[0], 2) $
298   ELSE vardate = date2string(vairdate(time[0]))
299;------------------------
300   @updateold
301;------------------------
302
303   return, res
304end
Note: See TracBrowser for help on using the repository browser.