[33] | 1 | ;+ |
---|
| 2 | ; |
---|
[142] | 3 | ; @file_comments |
---|
[172] | 4 | ; Initfile for Netcdf file. define all the grid parameters through |
---|
| 5 | ; an appropriate call to computegid |
---|
[33] | 6 | ; |
---|
[142] | 7 | ; @categories |
---|
[172] | 8 | ; Grid |
---|
[231] | 9 | ; |
---|
[172] | 10 | ; @param NCFILEIN {in}{required}{type=scalar string} |
---|
[142] | 11 | ; A string giving the name of the NetCdf file |
---|
[33] | 12 | ; |
---|
[172] | 13 | ; @keyword INVMASK {default=0}{type=scalar: 0 or 1} |
---|
| 14 | ; Inverse the land/sea mask (that should have 0/1 values for land/sea): mask = 1-mask |
---|
[33] | 15 | ; |
---|
[163] | 16 | ; @keyword MASKNAME {type=string} |
---|
[231] | 17 | ; A string giving the name of the variable in the file |
---|
[142] | 18 | ; that contains the land/sea mask |
---|
[33] | 19 | ; |
---|
[172] | 20 | ; @keyword MISSING_VALUE {type=scalar} |
---|
[163] | 21 | ; To define (or redefine if the attribute is |
---|
[142] | 22 | ; already existing) the missing values used with USEASMASK |
---|
| 23 | ; keyword |
---|
[33] | 24 | ; |
---|
[172] | 25 | ; @keyword START1 {default=0}{type=scalar: 0 or 1} |
---|
[142] | 26 | ; Index the axis from 1 instead of 0 when using |
---|
[221] | 27 | ; /xyindex and/or /zindex |
---|
[33] | 28 | ; |
---|
[172] | 29 | ; @keyword USEASMASK {type=scalar string} |
---|
[231] | 30 | ; A string giving the name of the variable in the file |
---|
[142] | 31 | ; that will be used to build the land/sea mask. In this case the |
---|
| 32 | ; mask is based on the first record (if record dimension |
---|
| 33 | ; exists). The mask is build according to : |
---|
| 34 | ; 1 the keyword missing_value if existing |
---|
| 35 | ; 2 the attribute 'missing_value' if existing |
---|
| 36 | ; 3 NaN values if existing |
---|
[33] | 37 | ; |
---|
[172] | 38 | ; @keyword ZAXISNAME {default='z', 'level', 'lev', 'depth...'}{type=scalar string} |
---|
[231] | 39 | ; A string giving the name of the variable in the file |
---|
| 40 | ; that contains the [xyz]axis. |
---|
[33] | 41 | ; |
---|
[172] | 42 | ; @keyword XYINDEX {default=0}{type=scalar: 0 or 1} |
---|
[142] | 43 | ; To define the x/y axis with index instead of using |
---|
[231] | 44 | ; the values contained in X/YAXISNAME. |
---|
| 45 | ; x/yaxis = keyword_set(start1) + findgen(jpi/jpj) |
---|
[142] | 46 | ; this forces key_onearth = 0 |
---|
[33] | 47 | ; |
---|
[172] | 48 | ; @keyword ZINDEX {default=0}{type=scalar: 0 or 1} |
---|
[142] | 49 | ; To define the z axis with index instead of using |
---|
[231] | 50 | ; the values contained in ZAXISNAME. |
---|
| 51 | ; zaxis = keyword_set(start1) + findgen(jpk) |
---|
| 52 | ; |
---|
[142] | 53 | ; @keyword _EXTRA |
---|
[232] | 54 | ; Used to pass keywords to <pro>computegrid</pro> and |
---|
| 55 | ; <pro>ncdf_getaxis</pro> |
---|
[33] | 56 | ; |
---|
[142] | 57 | ; @uses |
---|
| 58 | ; common.pro |
---|
[33] | 59 | ; |
---|
[142] | 60 | ; @restrictions |
---|
[231] | 61 | ; Change the grid parameters (see <pro>computegrid</pro>) |
---|
[33] | 62 | ; |
---|
[142] | 63 | ; @restrictions |
---|
[226] | 64 | ; the file must contain an x and an y axis. (1 ou 2 dimensional array) |
---|
[33] | 65 | ; |
---|
[142] | 66 | ; @examples |
---|
[231] | 67 | ; IDL> initncdf,'toto.nc',glam=[-180,180] |
---|
[33] | 68 | ; |
---|
[142] | 69 | ; @history |
---|
[157] | 70 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[142] | 71 | ; 8 May 2002 |
---|
[33] | 72 | ; |
---|
[142] | 73 | ; @version |
---|
| 74 | ; $Id$ |
---|
[33] | 75 | ; |
---|
| 76 | ;- |
---|
[231] | 77 | ; |
---|
[221] | 78 | PRO initncdf, ncfilein $ |
---|
[33] | 79 | , ZAXISNAME = zaxisname, MASKNAME = maskname $ |
---|
| 80 | , INVMASK = invmask, USEASMASK = useasmask $ |
---|
| 81 | , MISSING_VALUE = missing_value, START1 = start1 $ |
---|
| 82 | , XYINDEX = xyindex, ZINDEX = zindex $ |
---|
| 83 | , _EXTRA = ex |
---|
| 84 | ; |
---|
[114] | 85 | compile_opt idl2, strictarrsubs |
---|
| 86 | ; |
---|
[33] | 87 | @common |
---|
| 88 | ;---------------------------------------------------------- |
---|
| 89 | ; check the name of the file |
---|
| 90 | ncfile = isafile(FILENAME = ncfilein, IODIRECTORY = iodir, _extra = ex) |
---|
| 91 | if size(ncfile, /type) NE 7 then BEGIN |
---|
[236] | 92 | ras = report( 'initncdf cancelled') |
---|
[33] | 93 | return |
---|
| 94 | endif |
---|
| 95 | ; if the file is stored on tape |
---|
[231] | 96 | if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null' |
---|
[33] | 97 | ;---------------------------------------------------------- |
---|
| 98 | ; open the file |
---|
| 99 | cdfid = ncdf_open(ncfile) |
---|
| 100 | ; what is inside the file |
---|
| 101 | inside = ncdf_inquire(cdfid) |
---|
[221] | 102 | ;------------------------------------------------------------ |
---|
| 103 | ; name of all dimensions |
---|
| 104 | namedim = strarr(inside.ndims) |
---|
| 105 | for dimiq = 0, inside.ndims-1 do begin |
---|
[231] | 106 | ncdf_diminq, cdfid, dimiq, tmpname, value |
---|
[221] | 107 | namedim[dimiq] = strlowcase(tmpname) |
---|
| 108 | ENDFOR |
---|
[33] | 109 | ;---------------------------------------------------------- |
---|
| 110 | ; name of the variables |
---|
| 111 | namevar = strarr(inside.nvars) |
---|
| 112 | for varid = 0, inside.nvars-1 do begin |
---|
| 113 | invar = ncdf_varinq(cdfid, varid) |
---|
| 114 | namevar[varid] = strlowcase(invar.name) |
---|
| 115 | ENDFOR |
---|
| 116 | ;---------------------------------------------------------- |
---|
[221] | 117 | ; find the x/yaxis |
---|
| 118 | ncdf_getaxis, cdfid, dimidx, dimidy, xaxis, yaxis $ |
---|
| 119 | , START1 = start1, XYINDEX = xyindex, ROMSGRID = romsgrid, _extra = ex |
---|
[33] | 120 | ;---------------------------------------------------------- |
---|
| 121 | ; find the zaxis |
---|
[172] | 122 | IF keyword_set(romsgrid) THEN BEGIN |
---|
[231] | 123 | FOR i = 0, inside.ndims-1 DO BEGIN |
---|
[172] | 124 | ncdf_diminq, cdfid, i, name, size |
---|
| 125 | CASE strlowcase(name) OF |
---|
| 126 | 's_rho':zaxis = reverse(indgen(size)) |
---|
| 127 | 's_u':zaxis = reverse(indgen(size)) |
---|
| 128 | 's_v':zaxis = reverse(indgen(size)) |
---|
| 129 | 's_psi':zaxis = reverse(indgen(size)) |
---|
| 130 | 's_w':zaxis = reverse(indgen(size-1)) |
---|
| 131 | ELSE: |
---|
| 132 | ENDCASE |
---|
[231] | 133 | ENDFOR |
---|
[172] | 134 | IF (where(namevar EQ 'h'))[0] NE -1 THEN BEGIN |
---|
| 135 | ncdf_varget, cdfid, 'h', romsh |
---|
| 136 | ENDIF ELSE romsh = -1 |
---|
[231] | 137 | ENDIF ELSE BEGIN |
---|
[172] | 138 | if keyword_set(zaxisname) then zaxisname = strlowcase(zaxisname) ELSE zaxisname = 'z' |
---|
| 139 | zvarid = (where(namevar EQ 'nav_lev' or namevar EQ zaxisname OR namevar EQ 'level' OR namevar EQ 'lev' OR strmid(namevar, 0, 5) EQ 'depth'))[0] |
---|
| 140 | if zvarid EQ -1 AND inside.ndims GT 3 then begin |
---|
[236] | 141 | ras = report( 'initncdf: the zaxis was not found..., check the the use of ZAXISNAME keyword if you whant to find one...') |
---|
[33] | 142 | ; stop |
---|
[172] | 143 | endif |
---|
[33] | 144 | ; read the zaxis |
---|
[172] | 145 | if zvarid NE -1 THEN ncdf_varget, cdfid, zvarid, zaxis |
---|
[231] | 146 | ENDELSE |
---|
[172] | 147 | IF keyword_set(zindex) AND keyword_set(zaxis) THEN $ |
---|
| 148 | zaxis = keyword_set(start1) + findgen(n_elements(zaxis)) |
---|
[33] | 149 | ;---------------------------------------------------------- |
---|
| 150 | ; mask |
---|
[172] | 151 | IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho' |
---|
[33] | 152 | CASE 1 OF |
---|
| 153 | keyword_set(maskname):BEGIN |
---|
| 154 | mskid = (where(namevar EQ strlowcase(maskname)))[0] |
---|
[231] | 155 | if mskid NE -1 THEN BEGIN |
---|
[33] | 156 | mskinq = ncdf_varinq(cdfid, mskid) |
---|
| 157 | ; is the mask variable containing the record dimension? |
---|
| 158 | withrcd = (where(mskinq.dim EQ inside.recdim))[0] |
---|
| 159 | IF withrcd NE -1 THEN BEGIN |
---|
[231] | 160 | ; in order to read only the first record |
---|
[33] | 161 | ; we need to get the size of each dimension |
---|
| 162 | count = replicate(1L, mskinq.ndims) |
---|
| 163 | FOR d = 0, mskinq.ndims -1 DO BEGIN |
---|
| 164 | IF d NE withrcd THEN BEGIN |
---|
| 165 | ncdf_diminq, cdfid, mskinq.dim[d], name, size |
---|
| 166 | count[d] = size |
---|
| 167 | ENDIF |
---|
| 168 | ENDFOR |
---|
[231] | 169 | ; read the variable for the first record |
---|
[33] | 170 | ncdf_varget, cdfid, mskid, tmask, count = count |
---|
| 171 | ENDIF ELSE ncdf_varget, cdfid, mskid, tmask |
---|
[231] | 172 | ; check if we need to applay add_offset and scale factor |
---|
[33] | 173 | FOR a = 0, mskinq.natts-1 DO BEGIN |
---|
[231] | 174 | attname = ncdf_attname(cdfid, mskid, a) |
---|
[33] | 175 | CASE strlowcase(attname) OF |
---|
| 176 | 'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset |
---|
| 177 | 'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor |
---|
| 178 | ELSE: |
---|
| 179 | ENDCASE |
---|
| 180 | ENDFOR |
---|
| 181 | IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor |
---|
| 182 | IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset |
---|
| 183 | if keyword_set(invmask) then tmask = 1-tmask |
---|
| 184 | tmask = byte(round(tmask)) |
---|
[231] | 185 | ENDIF ELSE tmask = -1 |
---|
[33] | 186 | END |
---|
| 187 | ;.................. |
---|
| 188 | keyword_set(useasmask):BEGIN |
---|
| 189 | mskid = (where(namevar EQ strlowcase(useasmask)))[0] |
---|
[231] | 190 | if mskid NE -1 THEN BEGIN |
---|
[33] | 191 | mskinq = ncdf_varinq(cdfid, mskid) |
---|
| 192 | ; is the mask variable containing the record dimension? |
---|
| 193 | withrcd = (where(mskinq.dim EQ inside.recdim))[0] |
---|
| 194 | IF withrcd NE -1 THEN BEGIN |
---|
[231] | 195 | ; in order to read only the first record |
---|
[33] | 196 | ; we need to get the size of each dimension |
---|
| 197 | count = replicate(1L, mskinq.ndims) |
---|
| 198 | FOR d = 0, mskinq.ndims -1 DO BEGIN |
---|
| 199 | IF d NE withrcd THEN BEGIN |
---|
| 200 | ncdf_diminq, cdfid, mskinq.dim[d], name, size |
---|
| 201 | count[d] = size |
---|
| 202 | ENDIF |
---|
| 203 | ENDFOR |
---|
[231] | 204 | ; read the variable for the first record |
---|
[33] | 205 | ncdf_varget, cdfid, mskid, tmask, count = count |
---|
| 206 | ENDIF ELSE ncdf_varget, cdfid, mskid, tmask |
---|
[231] | 207 | ; check if we need to applay add_offset and scale factor |
---|
[33] | 208 | FOR a = 0, mskinq.natts-1 DO BEGIN |
---|
[231] | 209 | attname = ncdf_attname(cdfid, mskid, a) |
---|
[33] | 210 | CASE strlowcase(attname) OF |
---|
| 211 | 'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset |
---|
| 212 | 'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor |
---|
| 213 | 'missing_value':IF n_elements(missing_value) EQ 0 THEN $ |
---|
[172] | 214 | ncdf_attget, cdfid, mskid, attname, missing_value |
---|
[33] | 215 | ELSE: |
---|
| 216 | ENDCASE |
---|
| 217 | ENDFOR |
---|
| 218 | IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor |
---|
| 219 | IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset |
---|
| 220 | IF n_elements(missing_value) NE 0 THEN BEGIN |
---|
| 221 | ; we have to take care of the float accuracy |
---|
| 222 | CASE 1 OF |
---|
| 223 | missing_value GE 1.e6:tmask = tmask LT (missing_value-10) |
---|
| 224 | missing_value LE -1.e6:tmask = tmask GT (missing_value-10) |
---|
| 225 | abs(missing_value) LE 1.e-6:tmask = abs(tmask) GT 1.e-6 |
---|
| 226 | ELSE:tmask = tmask NE missing_value |
---|
| 227 | ENDCASE |
---|
| 228 | if keyword_set(invmask) then tmask = 1-tmask |
---|
| 229 | ENDIF ELSE BEGIN |
---|
| 230 | tmask = finite(tmask) |
---|
| 231 | IF min(tmask) EQ 1 THEN BEGIN |
---|
[236] | 232 | ras = report( 'missing or nan values not found...') |
---|
[231] | 233 | tmask = -1 |
---|
[33] | 234 | ENDIF |
---|
| 235 | ENDELSE |
---|
[231] | 236 | ENDIF ELSE tmask = -1 |
---|
[33] | 237 | END |
---|
| 238 | ;.................. |
---|
| 239 | ELSE:tmask = -1 |
---|
| 240 | ENDCASE |
---|
[172] | 241 | ; |
---|
[33] | 242 | ncdf_close, cdfid |
---|
| 243 | ; |
---|
| 244 | ; compute the grid |
---|
[231] | 245 | if NOT keyword_set(zaxis) then BEGIN |
---|
[33] | 246 | computegrid, xaxis = xaxis, yaxis = yaxis $ |
---|
[231] | 247 | , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex |
---|
| 248 | ENDIF ELSE BEGIN |
---|
[33] | 249 | computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis $ |
---|
[172] | 250 | , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex |
---|
[231] | 251 | ENDELSE |
---|
[33] | 252 | IF n_elements(time) EQ 0 THEN time = 0 |
---|
[231] | 253 | jpt = n_elements(time) |
---|
[33] | 254 | ;---------------------------------------------------------- |
---|
| 255 | |
---|
| 256 | return |
---|
| 257 | end |
---|