source: trunk/SRC/ToBeReviewed/INIT/initncdf.pro @ 221

Last change on this file since 221 was 221, checked in by smasson, 17 years ago

add x/ydimname and cleaning to solve ticket:61

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