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