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

Last change on this file since 232 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1;+
2;
3; @file_comments
4; Initfile for Netcdf file. define all the grid parameters through
5; an appropriate call to computegid
6;
7; @categories
8; Grid
9;
10; @param NCFILEIN {in}{required}{type=scalar string}
11; A string giving the name of the NetCdf file
12;
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
15;
16; @keyword MASKNAME {type=string}
17; A string giving the name of the variable in the file
18; that contains the land/sea mask
19;
20; @keyword MISSING_VALUE {type=scalar}
21; To define (or redefine if the attribute is
22; already existing) the missing values used with USEASMASK
23; keyword
24;
25; @keyword START1 {default=0}{type=scalar: 0 or 1}
26; Index the axis from 1 instead of 0 when using
27; /xyindex and/or /zindex
28;
29; @keyword USEASMASK {type=scalar string}
30; A string giving the name of the variable in the file
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
37;
38; @keyword ZAXISNAME {default='z', 'level', 'lev', 'depth...'}{type=scalar string}
39; A string giving the name of the variable in the file
40; that contains the [xyz]axis.
41;
42; @keyword XYINDEX {default=0}{type=scalar: 0 or 1}
43; To define the x/y axis with index instead of using
44; the values contained in X/YAXISNAME.
45; x/yaxis = keyword_set(start1) + findgen(jpi/jpj)
46; this forces key_onearth = 0
47;
48; @keyword ZINDEX {default=0}{type=scalar: 0 or 1}
49; To define the z axis with index instead of using
50; the values contained in ZAXISNAME.
51; zaxis = keyword_set(start1) + findgen(jpk)
52;
53; @keyword _EXTRA
54; Used to pass keywords to <pro>computegrid</pro> and
55; <pro>ncdf_getaxis</pro>
56;
57; @uses
58; common.pro
59;
60; @restrictions
61; Change the grid parameters (see <pro>computegrid</pro>)
62;
63; @restrictions
64; the file must contain an x and an y axis. (1 ou 2 dimensional array)
65;
66; @examples
67; IDL> initncdf,'toto.nc',glam=[-180,180]
68;
69; @history
70; Sebastien Masson (smasson\@lodyc.jussieu.fr)
71;                      8 May 2002
72;
73; @version
74; $Id$
75;
76;-
77;
78PRO initncdf, ncfilein $
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;
85  compile_opt idl2, strictarrsubs
86;
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
92    print, 'initncdf cancelled'
93    return
94  endif
95; if the file is stored on tape
96  if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null'
97;----------------------------------------------------------
98; open the file
99  cdfid = ncdf_open(ncfile)
100; what is inside the file
101  inside = ncdf_inquire(cdfid)
102;------------------------------------------------------------
103; name of all dimensions
104  namedim = strarr(inside.ndims)
105  for dimiq = 0, inside.ndims-1 do begin
106    ncdf_diminq, cdfid, dimiq, tmpname, value
107    namedim[dimiq] = strlowcase(tmpname)
108  ENDFOR
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;----------------------------------------------------------
117; find the x/yaxis
118 ncdf_getaxis, cdfid, dimidx, dimidy, xaxis, yaxis $
119               , START1 = start1, XYINDEX = xyindex, ROMSGRID = romsgrid, _extra = ex
120;----------------------------------------------------------
121; find the zaxis
122  IF keyword_set(romsgrid) THEN BEGIN
123    FOR i = 0, inside.ndims-1 DO BEGIN
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
133    ENDFOR
134    IF (where(namevar EQ 'h'))[0] NE -1 THEN BEGIN
135      ncdf_varget, cdfid, 'h', romsh
136    ENDIF ELSE romsh = -1
137  ENDIF ELSE BEGIN
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
141      print, 'initncdf: the zaxis was not found..., check the the use of ZAXISNAME keyword if you whant to find one...'
142;     stop
143    endif
144; read the zaxis
145    if zvarid NE -1 THEN ncdf_varget, cdfid, zvarid, zaxis
146  ENDELSE
147  IF keyword_set(zindex) AND keyword_set(zaxis) THEN $
148     zaxis = keyword_set(start1) + findgen(n_elements(zaxis))
149;----------------------------------------------------------
150; mask
151  IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho'
152  CASE 1 OF
153    keyword_set(maskname):BEGIN
154      mskid = (where(namevar EQ strlowcase(maskname)))[0]
155      if mskid NE -1 THEN BEGIN
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
160; in order to read only the first record
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
169; read the variable for the first record
170          ncdf_varget, cdfid, mskid, tmask, count = count
171        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
172; check if we need to applay add_offset and scale factor
173        FOR a = 0, mskinq.natts-1 DO BEGIN
174          attname = ncdf_attname(cdfid, mskid, a)
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))
185      ENDIF ELSE tmask = -1
186    END
187;..................
188    keyword_set(useasmask):BEGIN
189      mskid = (where(namevar EQ strlowcase(useasmask)))[0]
190      if mskid NE -1 THEN BEGIN
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
195; in order to read only the first record
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
204; read the variable for the first record
205          ncdf_varget, cdfid, mskid, tmask, count = count
206        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
207; check if we need to applay add_offset and scale factor
208        FOR a = 0, mskinq.natts-1 DO BEGIN
209          attname = ncdf_attname(cdfid, mskid, a)
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 $
214               ncdf_attget, cdfid, mskid, attname, missing_value
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
232            print, 'missing or nan values not found...'
233            tmask = -1
234          ENDIF
235        ENDELSE
236      ENDIF ELSE tmask = -1
237    END
238;..................
239    ELSE:tmask  = -1
240  ENDCASE
241;
242  ncdf_close, cdfid
243;
244; compute the grid
245  if NOT keyword_set(zaxis) then BEGIN
246    computegrid, xaxis = xaxis, yaxis = yaxis $
247                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
248  ENDIF ELSE BEGIN
249    computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis $
250                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
251  ENDELSE
252  IF n_elements(time) EQ 0 THEN time = 0
253  jpt = n_elements(time)
254;----------------------------------------------------------
255
256  return
257end
Note: See TracBrowser for help on using the repository browser.