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