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