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

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

clean div, curl, grad + minors bugfix

  • Property svn:keywords set to Id
File size: 10.9 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5;
6; @file_comments
7; Initfile for Netcdf file. define all the grid parameters
8;
9; @categories
10;
11;
12; @param NCFILEIN {in}{required}{type=string}
13; A string giving the name of the NetCdf file
14;
15; @keyword INVMASK
16; To inverse the mask: mask = 1-mask
17;
18; @keyword MASKNAME {type=string}
19; A string giving the name of the variable in the file
20; that contains the land/sea mask
21;
22; @keyword MISSING_VALUE
23; To define (or redefine if the attribute is
24; already existing) the missing values used with USEASMASK
25; keyword
26;
27; @keyword START1
28; Index the axis from 1 instead of 0 when using
29; /xyindex and/or zindex
30;
31; @keyword USEASMASK {type=string}
32; A string giving the name of the variable in the file
33; that will be used to build the land/sea mask. In this case the
34; mask is based on the first record (if record dimension
35; exists). The mask is build according to :
36;    1 the keyword missing_value if existing
37;    2 the attribute 'missing_value' if existing
38;    3 NaN values if existing
39;
40; @keyword XAXISNAME {default='x', 'longitude', 'nav_lon' or 'lon'}{type=string}
41; A string giving the name of the variable in the file
42; that contains the [xyz]axis.
43;     
44; @keyword YAXISNAME {default='y', 'latitude', 'nav_lat' or 'lat'}{type=string}
45; A string giving the name of the variable in the file
46; that contains the [xyz]axis.
47;
48; @keyword ZAXISNAME {default='z', 'level', 'lev', 'depth...'}{type=string}
49; A string giving the name of the variable in the file
50; that contains the [xyz]axis.
51;
52; @keyword XYINDEX
53; To define the x/y axis with index instead of using
54; the values contained in X/YAXISNAME.
55; x/yaxis = keyword_set(start1) + findgen(jpi/jpj)
56; this forces key_onearth = 0
57;
58; @keyword ZINDEX
59; To define the z axis with index instead of using
60; the values contained in ZAXISNAME.
61; zaxis = keyword_set(start1) + findgen(jpk)
62;
63; @keyword _EXTRA
64; Used to pass your keywords/
65;
66; @uses
67; common.pro
68;
69; @restrictions
70; Change the grid parameters of the common.pro
71;
72; @restrictions
73; the file must contain an x and an y axis. (1 ou 2 dimentional array)
74;
75; @examples
76;  IDL> initncdf,'toto.nc',glam=[-180,180]
77;
78; @history
79; Sebastien Masson (smasson\@lodyc.jussieu.fr)
80;                      8 May 2002
81;
82; @version
83; $Id$
84;
85;-
86;------------------------------------------------------------
87;------------------------------------------------------------
88;------------------------------------------------------------
89PRO initncdf, ncfilein, XAXISNAME = xaxisname, YAXISNAME = yaxisname $
90              , ZAXISNAME = zaxisname, MASKNAME = maskname $
91              , INVMASK = invmask, USEASMASK = useasmask $
92              , MISSING_VALUE = missing_value, START1 = start1 $
93              , XYINDEX = xyindex, ZINDEX = zindex $
94              , _EXTRA = ex
95;
96;
97  compile_opt idl2, strictarrsubs
98;
99@common
100;----------------------------------------------------------
101; check the name of the file
102  ncfile = isafile(FILENAME = ncfilein, IODIRECTORY = iodir, _extra = ex)
103  if size(ncfile, /type) NE 7 then BEGIN
104    print, 'initncdf cancelled'
105    return
106  endif
107; if the file is stored on tape
108  if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null'
109;----------------------------------------------------------
110; open the file
111  cdfid = ncdf_open(ncfile)
112; what is inside the file
113  inside = ncdf_inquire(cdfid)
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 xaxis
123  if keyword_set(xaxisname) then xaxisname = strlowcase(xaxisname) ELSE xaxisname = 'x'
124  xvarid = where(namevar EQ xaxisname OR namevar EQ 'longitude' $
125                 OR namevar EQ 'nav_lon' OR namevar EQ 'lon' $
126                 OR namevar EQ 'NbLongitudes')
127  xvarid = xvarid[0]
128  if xvarid EQ -1 then begin
129    print, 'the xaxis was not found, check the use of XAXISNAME keyword'
130    stop
131  endif
132; get the size of xaxis
133  xinq = ncdf_varinq(cdfid, xvarid)
134  ncdf_diminq, cdfid, xinq.dim[0], blabla, jpifromx
135; should we read or compute the xaxis?
136  IF NOT keyword_set(xyindex) THEN BEGIN
137; read the xaxis
138    ncdf_varget, cdfid, xvarid, xaxis
139; make sure of the shape of xaxis
140    IF xinq.ndims GE 2 THEN BEGIN
141      ncdf_diminq, cdfid, xinq.dim[1], blabla, jpjfromx
142      xaxis = reform(xaxis, jpifromx, jpjfromx, /over)
143    ENDIF
144  ENDIF ELSE xaxis = keyword_set(start1) + findgen(jpifromx)
145;----------------------------------------------------------
146; find the yaxis
147  if keyword_set(yaxisname) then yaxisname = strlowcase(yaxisname) ELSE yaxisname = 'y'
148  yvarid = where(namevar EQ yaxisname OR namevar EQ 'latitude' $
149                 OR namevar EQ 'nav_lat' OR namevar EQ 'lat' $
150                 OR namevar EQ 'NbLatitudes')
151  yvarid = yvarid[0]
152  if yvarid EQ -1 then begin
153    print, 'the yaxis was not found, check the use of YAXISNAME keyword'
154    stop
155  endif
156; get the size of yaxis and check it is ok with the values found for x
157  yinq = ncdf_varinq(cdfid, yvarid)
158  IF xinq.ndims GE 2 THEN BEGIN
159    ncdf_diminq, cdfid, yinq.dim[0], blabla, jpifromy
160    ncdf_diminq, cdfid, yinq.dim[1], blabla, jpjfromy
161    IF jpifromy NE jpifromx THEN BEGIN
162      print, 'xaxis and y axis do not have the same x dimension...'
163    ENDIF
164  ENDIF ELSE ncdf_diminq, cdfid, yinq.dim[0], blabla, jpjfromy
165  IF n_elements(jpjfromx) NE 0 THEN BEGIN
166    IF jpjfromy NE jpjfromx THEN BEGIN
167      print, 'xaxis and y axis do not have the same y dimension...'
168    ENDIF   
169  ENDIF
170; should we read or compute the xaxis?
171  IF NOT keyword_set(xyindex) THEN BEGIN
172; read the yaxis
173    ncdf_varget, cdfid, yvarid, yaxis
174; make sure of the shape of xaxis
175    IF xinq.ndims GE 2 THEN yaxis = reform(yaxis, jpifromy, jpjfromy, /over)
176  ENDIF ELSE yaxis = keyword_set(start1) + findgen(jpjfromy)
177;----------------------------------------------------------
178; find the zaxis
179  if keyword_set(zaxisname) then zaxisname = strlowcase(zaxisname) ELSE zaxisname = 'z'
180  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')
181  zvarid = zvarid[0]
182  if zvarid EQ -1 AND inside.ndims GT 3 then begin
183    print, 'initncdf: the zaxis was not found..., check the the use of ZAXISNAME keyword if you whant to find one...'
184;     stop
185  endif
186; read the zaxis
187  if zvarid NE -1 THEN ncdf_varget, cdfid, zvarid, zaxis
188  IF keyword_set(zindex) THEN $
189    zaxis = keyword_set(start1) + findgen(n_elements(zaxis))
190;----------------------------------------------------------
191; mask
192  CASE 1 OF
193    keyword_set(maskname):BEGIN
194      mskid = (where(namevar EQ strlowcase(maskname)))[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            ELSE:
219          ENDCASE
220        ENDFOR
221        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
222        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
223        if keyword_set(invmask) then tmask = 1-tmask
224        tmask = byte(round(tmask))
225      ENDIF ELSE tmask = -1       
226    END
227;..................
228    keyword_set(useasmask):BEGIN
229      mskid = (where(namevar EQ strlowcase(useasmask)))[0]
230      if mskid NE -1 THEN BEGIN
231        mskinq = ncdf_varinq(cdfid, mskid)
232; is the mask variable containing the record dimension?
233        withrcd = (where(mskinq.dim EQ inside.recdim))[0]
234        IF withrcd NE -1 THEN BEGIN
235; in order to read only the first record         
236; we need to get the size of each dimension
237          count = replicate(1L, mskinq.ndims)
238          FOR d = 0, mskinq.ndims -1 DO BEGIN
239            IF d NE withrcd THEN BEGIN
240              ncdf_diminq, cdfid, mskinq.dim[d], name, size
241              count[d] = size
242            ENDIF
243          ENDFOR
244; read the variable for the first record       
245          ncdf_varget, cdfid, mskid, tmask, count = count
246        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
247; check if we need to applay add_offset and scale factor       
248        FOR a = 0, mskinq.natts-1 DO BEGIN
249          attname = ncdf_attname(cdfid, mskid, a)     
250          CASE strlowcase(attname) OF
251            'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset
252            'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor
253            'missing_value':IF n_elements(missing_value) EQ 0 THEN $
254              ncdf_attget, cdfid, mskid, attname, missing_value
255            ELSE:
256          ENDCASE
257        ENDFOR
258        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
259        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
260        IF n_elements(missing_value) NE 0 THEN BEGIN
261; we have to take care of the float accuracy
262          CASE 1 OF
263            missing_value GE 1.e6:tmask = tmask LT (missing_value-10)
264            missing_value LE -1.e6:tmask = tmask GT (missing_value-10)
265            abs(missing_value) LE 1.e-6:tmask = abs(tmask) GT 1.e-6
266            ELSE:tmask = tmask NE missing_value
267          ENDCASE
268          if keyword_set(invmask) then tmask = 1-tmask
269        ENDIF ELSE BEGIN
270          tmask = finite(tmask)
271          IF min(tmask) EQ 1 THEN BEGIN
272            print, 'missing or nan values not found...'
273            tmask = -1         
274          ENDIF
275        ENDELSE
276      ENDIF ELSE tmask = -1       
277    END
278;..................
279    ELSE:tmask  = -1
280  ENDCASE
281  ncdf_close, cdfid
282;
283; compute the grid
284  if zvarid EQ -1 then BEGIN
285    computegrid, xaxis = xaxis, yaxis = yaxis $
286      , mask = tmask, onearth = 1b - keyword_set(xyindex), _EXTRA = ex
287  ENDIF ELSE BEGIN
288    computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis $
289      , mask = tmask, onearth = 1b - keyword_set(xyindex), _EXTRA = ex
290  ENDELSE
291  IF n_elements(time) EQ 0 THEN time = 0
292  jpt = n_elements(time)
293;----------------------------------------------------------
294
295  return
296end
Note: See TracBrowser for help on using the repository browser.