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

Last change on this file since 172 was 172, checked in by smasson, 18 years ago

bugfix + manage roms outputs

  • Property svn:keywords set to Id
File size: 12.1 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 XAXISNAME {default='x', 'longitude', 'nav_lon', 'lon', 'lon_rho' or 'NbLongitudes'}{type=scalar string}
42; A string giving the name of the variable in the file
43; that contains the [xyz]axis.
44;     
45; @keyword YAXISNAME {default='y', 'latitude', 'nav_lat','lat', 'lat_rho' or 'NbLatitudes'}{type=scalar string}
46; A string giving the name of the variable in the file
47; that contains the [xyz]axis.
48;
49; @keyword ZAXISNAME {default='z', 'level', 'lev', 'depth...'}{type=scalar string}
50; A string giving the name of the variable in the file
51; that contains the [xyz]axis.
52;
53; @keyword XYINDEX {default=0}{type=scalar: 0 or 1}
54; To define the x/y axis with index instead of using
55; the values contained in X/YAXISNAME.
56; x/yaxis = keyword_set(start1) + findgen(jpi/jpj)
57; this forces key_onearth = 0
58;
59; @keyword ZINDEX {default=0}{type=scalar: 0 or 1}
60; To define the z axis with index instead of using
61; the values contained in ZAXISNAME.
62; zaxis = keyword_set(start1) + findgen(jpk)
63;
64; @keyword _EXTRA
65; Used to pass keywords to computegrid
66;
67; @uses
68; common.pro
69;
70; @restrictions
71; Change the grid parameters (see computegrid)
72;
73; @restrictions
74; the file must contain an x and an y axis. (1 ou 2 dimentional array)
75;
76; @examples
77;  IDL> initncdf,'toto.nc',glam=[-180,180]
78;
79; @history
80; Sebastien Masson (smasson\@lodyc.jussieu.fr)
81;                      8 May 2002
82;
83; @version
84; $Id$
85;
86;-
87;------------------------------------------------------------
88;------------------------------------------------------------
89;------------------------------------------------------------
90PRO initncdf, ncfilein, XAXISNAME = xaxisname, YAXISNAME = yaxisname $
91              , ZAXISNAME = zaxisname, MASKNAME = maskname $
92              , INVMASK = invmask, USEASMASK = useasmask $
93              , MISSING_VALUE = missing_value, START1 = start1 $
94              , XYINDEX = xyindex, ZINDEX = zindex $
95              , _EXTRA = ex
96;
97;
98  compile_opt idl2, strictarrsubs
99;
100@common
101;----------------------------------------------------------
102; check the name of the file
103  ncfile = isafile(FILENAME = ncfilein, IODIRECTORY = iodir, _extra = ex)
104  if size(ncfile, /type) NE 7 then BEGIN
105    print, 'initncdf cancelled'
106    return
107  endif
108; if the file is stored on tape
109  if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null'
110;----------------------------------------------------------
111; open the file
112  cdfid = ncdf_open(ncfile)
113; what is inside the file
114  inside = ncdf_inquire(cdfid)
115;----------------------------------------------------------
116; name of the variables
117  namevar = strarr(inside.nvars)
118  for varid = 0, inside.nvars-1 do begin
119    invar = ncdf_varinq(cdfid, varid)
120    namevar[varid] = strlowcase(invar.name)
121  ENDFOR
122;----------------------------------------------------------
123; find the xaxis
124  if keyword_set(xaxisname) then xaxisname = strlowcase(xaxisname) ELSE xaxisname = 'x'
125  xvarid = (where(namevar EQ xaxisname OR namevar EQ 'longitude' $
126                  OR namevar EQ 'nav_lon' OR namevar EQ 'lon' $
127                  OR namevar EQ 'lon_rho' OR namevar EQ 'NbLongitudes'))[0]
128  romsgrid = strmid(namevar[xvarid], 0, 4) EQ 'lon_'
129  if xvarid EQ -1 then begin
130    print, 'the xaxis was not found, check the use of XAXISNAME keyword'
131    stop
132  endif
133; get the size of xaxis
134  xinq = ncdf_varinq(cdfid, xvarid)
135  ncdf_diminq, cdfid, xinq.dim[0], blabla, jpifromx
136; should we read or compute the xaxis?
137  IF NOT keyword_set(xyindex) THEN BEGIN
138; read the xaxis
139    ncdf_varget, cdfid, xvarid, xaxis
140; make sure of the shape of xaxis
141    IF xinq.ndims GE 2 THEN BEGIN
142      ncdf_diminq, cdfid, xinq.dim[1], blabla, jpjfromx
143      xaxis = reform(xaxis, jpifromx, jpjfromx, /over)
144    ENDIF
145  ENDIF ELSE xaxis = keyword_set(start1) + findgen(jpifromx)
146;----------------------------------------------------------
147; find the yaxis
148  if keyword_set(yaxisname) then yaxisname = strlowcase(yaxisname) ELSE yaxisname = 'y'
149  yvarid = (where(namevar EQ yaxisname OR namevar EQ 'latitude' $
150                  OR namevar EQ 'nav_lat' OR namevar EQ 'lat' $
151                  OR namevar EQ 'lat_rho' OR namevar EQ 'NbLatitudes'))[0]
152  yvarid = yvarid[0]
153  if yvarid EQ -1 then begin
154    print, 'the yaxis was not found, check the use of YAXISNAME keyword'
155    stop
156  endif
157; get the size of yaxis and check it is ok with the values found for x
158  yinq = ncdf_varinq(cdfid, yvarid)
159  IF xinq.ndims GE 2 THEN BEGIN
160    ncdf_diminq, cdfid, yinq.dim[0], blabla, jpifromy
161    ncdf_diminq, cdfid, yinq.dim[1], blabla, jpjfromy
162    IF jpifromy NE jpifromx THEN BEGIN
163      print, 'xaxis and y axis do not have the same x dimension...'
164    ENDIF
165  ENDIF ELSE ncdf_diminq, cdfid, yinq.dim[0], blabla, jpjfromy
166  IF n_elements(jpjfromx) NE 0 THEN BEGIN
167    IF jpjfromy NE jpjfromx THEN BEGIN
168      print, 'xaxis and y axis do not have the same y dimension...'
169    ENDIF   
170  ENDIF
171; should we read or compute the xaxis?
172  IF NOT keyword_set(xyindex) THEN BEGIN
173; read the yaxis
174    ncdf_varget, cdfid, yvarid, yaxis
175; make sure of the shape of xaxis
176    IF xinq.ndims GE 2 THEN yaxis = reform(yaxis, jpifromy, jpjfromy, /over)
177  ENDIF ELSE yaxis = keyword_set(start1) + findgen(jpjfromy)
178;----------------------------------------------------------
179; find the zaxis
180  IF keyword_set(romsgrid) THEN BEGIN
181    FOR i = 0, inside.ndims-1 DO BEGIN
182      ncdf_diminq, cdfid, i, name, size
183      CASE strlowcase(name) OF
184        's_rho':zaxis = reverse(indgen(size))
185        's_u':zaxis = reverse(indgen(size))
186        's_v':zaxis = reverse(indgen(size))
187        's_psi':zaxis = reverse(indgen(size))
188        's_w':zaxis = reverse(indgen(size-1))
189        ELSE:
190      ENDCASE
191    ENDFOR
192    IF (where(namevar EQ 'h'))[0] NE -1 THEN BEGIN
193      ncdf_varget, cdfid, 'h', romsh
194    ENDIF ELSE romsh = -1
195  ENDIF ELSE BEGIN
196    if keyword_set(zaxisname) then zaxisname = strlowcase(zaxisname) ELSE zaxisname = 'z'
197    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]
198    if zvarid EQ -1 AND inside.ndims GT 3 then begin
199      print, 'initncdf: the zaxis was not found..., check the the use of ZAXISNAME keyword if you whant to find one...'
200;     stop
201    endif
202; read the zaxis
203    if zvarid NE -1 THEN ncdf_varget, cdfid, zvarid, zaxis
204  ENDELSE
205  IF keyword_set(zindex) AND keyword_set(zaxis) THEN $
206     zaxis = keyword_set(start1) + findgen(n_elements(zaxis))
207;----------------------------------------------------------
208; mask
209  IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho'
210  CASE 1 OF
211    keyword_set(maskname):BEGIN
212      mskid = (where(namevar EQ strlowcase(maskname)))[0]
213      if mskid NE -1 THEN BEGIN
214        mskinq = ncdf_varinq(cdfid, mskid)
215; is the mask variable containing the record dimension?
216        withrcd = (where(mskinq.dim EQ inside.recdim))[0]
217        IF withrcd NE -1 THEN BEGIN
218; in order to read only the first record         
219; we need to get the size of each dimension
220          count = replicate(1L, mskinq.ndims)
221          FOR d = 0, mskinq.ndims -1 DO BEGIN
222            IF d NE withrcd THEN BEGIN
223              ncdf_diminq, cdfid, mskinq.dim[d], name, size
224              count[d] = size
225            ENDIF
226          ENDFOR
227; read the variable for the first record         
228          ncdf_varget, cdfid, mskid, tmask, count = count
229        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
230; check if we need to applay add_offset and scale factor         
231        FOR a = 0, mskinq.natts-1 DO BEGIN
232          attname = ncdf_attname(cdfid, mskid, a)     
233          CASE strlowcase(attname) OF
234            'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset
235            'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor
236            ELSE:
237          ENDCASE
238        ENDFOR
239        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
240        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
241        if keyword_set(invmask) then tmask = 1-tmask
242        tmask = byte(round(tmask))
243      ENDIF ELSE tmask = -1       
244    END
245;..................
246    keyword_set(useasmask):BEGIN
247      mskid = (where(namevar EQ strlowcase(useasmask)))[0]
248      if mskid NE -1 THEN BEGIN
249        mskinq = ncdf_varinq(cdfid, mskid)
250; is the mask variable containing the record dimension?
251        withrcd = (where(mskinq.dim EQ inside.recdim))[0]
252        IF withrcd NE -1 THEN BEGIN
253; in order to read only the first record         
254; we need to get the size of each dimension
255          count = replicate(1L, mskinq.ndims)
256          FOR d = 0, mskinq.ndims -1 DO BEGIN
257            IF d NE withrcd THEN BEGIN
258              ncdf_diminq, cdfid, mskinq.dim[d], name, size
259              count[d] = size
260            ENDIF
261          ENDFOR
262; read the variable for the first record       
263          ncdf_varget, cdfid, mskid, tmask, count = count
264        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
265; check if we need to applay add_offset and scale factor       
266        FOR a = 0, mskinq.natts-1 DO BEGIN
267          attname = ncdf_attname(cdfid, mskid, a)     
268          CASE strlowcase(attname) OF
269            'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset
270            'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor
271            'missing_value':IF n_elements(missing_value) EQ 0 THEN $
272               ncdf_attget, cdfid, mskid, attname, missing_value
273            ELSE:
274          ENDCASE
275        ENDFOR
276        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
277        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
278        IF n_elements(missing_value) NE 0 THEN BEGIN
279; we have to take care of the float accuracy
280          CASE 1 OF
281            missing_value GE 1.e6:tmask = tmask LT (missing_value-10)
282            missing_value LE -1.e6:tmask = tmask GT (missing_value-10)
283            abs(missing_value) LE 1.e-6:tmask = abs(tmask) GT 1.e-6
284            ELSE:tmask = tmask NE missing_value
285          ENDCASE
286          if keyword_set(invmask) then tmask = 1-tmask
287        ENDIF ELSE BEGIN
288          tmask = finite(tmask)
289          IF min(tmask) EQ 1 THEN BEGIN
290            print, 'missing or nan values not found...'
291            tmask = -1         
292          ENDIF
293        ENDELSE
294      ENDIF ELSE tmask = -1       
295    END
296;..................
297    ELSE:tmask  = -1
298  ENDCASE
299;
300  ncdf_close, cdfid
301;
302; compute the grid
303  if NOT keyword_set(zaxis) then BEGIN
304    computegrid, xaxis = xaxis, yaxis = yaxis $
305                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
306  ENDIF ELSE BEGIN
307    computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis $
308                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
309  ENDELSE
310  IF n_elements(time) EQ 0 THEN time = 0
311  jpt = n_elements(time)
312;----------------------------------------------------------
313
314  return
315end
Note: See TracBrowser for help on using the repository browser.