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

Last change on this file since 231 was 231, 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 <pro>ncdf_getaxis</pro>
55;
56; @uses
57; common.pro
58;
59; @restrictions
60; Change the grid parameters (see <pro>computegrid</pro>)
61;
62; @restrictions
63; the file must contain an x and an y axis. (1 ou 2 dimensional array)
64;
65; @examples
66; IDL> initncdf,'toto.nc',glam=[-180,180]
67;
68; @history
69; Sebastien Masson (smasson\@lodyc.jussieu.fr)
70;                      8 May 2002
71;
72; @version
73; $Id$
74;
75;-
76;
77PRO initncdf, ncfilein $
78              , ZAXISNAME = zaxisname, MASKNAME = maskname $
79              , INVMASK = invmask, USEASMASK = useasmask $
80              , MISSING_VALUE = missing_value, START1 = start1 $
81              , XYINDEX = xyindex, ZINDEX = zindex $
82              , _EXTRA = ex
83;
84  compile_opt idl2, strictarrsubs
85;
86@common
87;----------------------------------------------------------
88; check the name of the file
89  ncfile = isafile(FILENAME = ncfilein, IODIRECTORY = iodir, _extra = ex)
90  if size(ncfile, /type) NE 7 then BEGIN
91    print, 'initncdf cancelled'
92    return
93  endif
94; if the file is stored on tape
95  if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null'
96;----------------------------------------------------------
97; open the file
98  cdfid = ncdf_open(ncfile)
99; what is inside the file
100  inside = ncdf_inquire(cdfid)
101;------------------------------------------------------------
102; name of all dimensions
103  namedim = strarr(inside.ndims)
104  for dimiq = 0, inside.ndims-1 do begin
105    ncdf_diminq, cdfid, dimiq, tmpname, value
106    namedim[dimiq] = strlowcase(tmpname)
107  ENDFOR
108;----------------------------------------------------------
109; name of the variables
110  namevar = strarr(inside.nvars)
111  for varid = 0, inside.nvars-1 do begin
112    invar = ncdf_varinq(cdfid, varid)
113    namevar[varid] = strlowcase(invar.name)
114  ENDFOR
115;----------------------------------------------------------
116; find the x/yaxis
117 ncdf_getaxis, cdfid, dimidx, dimidy, xaxis, yaxis $
118               , START1 = start1, XYINDEX = xyindex, ROMSGRID = romsgrid, _extra = ex
119;----------------------------------------------------------
120; find the zaxis
121  IF keyword_set(romsgrid) THEN BEGIN
122    FOR i = 0, inside.ndims-1 DO BEGIN
123      ncdf_diminq, cdfid, i, name, size
124      CASE strlowcase(name) OF
125        's_rho':zaxis = reverse(indgen(size))
126        's_u':zaxis = reverse(indgen(size))
127        's_v':zaxis = reverse(indgen(size))
128        's_psi':zaxis = reverse(indgen(size))
129        's_w':zaxis = reverse(indgen(size-1))
130        ELSE:
131      ENDCASE
132    ENDFOR
133    IF (where(namevar EQ 'h'))[0] NE -1 THEN BEGIN
134      ncdf_varget, cdfid, 'h', romsh
135    ENDIF ELSE romsh = -1
136  ENDIF ELSE BEGIN
137    if keyword_set(zaxisname) then zaxisname = strlowcase(zaxisname) ELSE zaxisname = 'z'
138    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]
139    if zvarid EQ -1 AND inside.ndims GT 3 then begin
140      print, 'initncdf: the zaxis was not found..., check the the use of ZAXISNAME keyword if you whant to find one...'
141;     stop
142    endif
143; read the zaxis
144    if zvarid NE -1 THEN ncdf_varget, cdfid, zvarid, zaxis
145  ENDELSE
146  IF keyword_set(zindex) AND keyword_set(zaxis) THEN $
147     zaxis = keyword_set(start1) + findgen(n_elements(zaxis))
148;----------------------------------------------------------
149; mask
150  IF NOT (keyword_set(maskname) OR keyword_set(useasmask)) AND keyword_set(romsgrid) THEN maskname = 'mask_rho'
151  CASE 1 OF
152    keyword_set(maskname):BEGIN
153      mskid = (where(namevar EQ strlowcase(maskname)))[0]
154      if mskid NE -1 THEN BEGIN
155        mskinq = ncdf_varinq(cdfid, mskid)
156; is the mask variable containing the record dimension?
157        withrcd = (where(mskinq.dim EQ inside.recdim))[0]
158        IF withrcd NE -1 THEN BEGIN
159; in order to read only the first record
160; we need to get the size of each dimension
161          count = replicate(1L, mskinq.ndims)
162          FOR d = 0, mskinq.ndims -1 DO BEGIN
163            IF d NE withrcd THEN BEGIN
164              ncdf_diminq, cdfid, mskinq.dim[d], name, size
165              count[d] = size
166            ENDIF
167          ENDFOR
168; read the variable for the first record
169          ncdf_varget, cdfid, mskid, tmask, count = count
170        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
171; check if we need to applay add_offset and scale factor
172        FOR a = 0, mskinq.natts-1 DO BEGIN
173          attname = ncdf_attname(cdfid, mskid, a)
174          CASE strlowcase(attname) OF
175            'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset
176            'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor
177            ELSE:
178          ENDCASE
179        ENDFOR
180        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
181        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
182        if keyword_set(invmask) then tmask = 1-tmask
183        tmask = byte(round(tmask))
184      ENDIF ELSE tmask = -1
185    END
186;..................
187    keyword_set(useasmask):BEGIN
188      mskid = (where(namevar EQ strlowcase(useasmask)))[0]
189      if mskid NE -1 THEN BEGIN
190        mskinq = ncdf_varinq(cdfid, mskid)
191; is the mask variable containing the record dimension?
192        withrcd = (where(mskinq.dim EQ inside.recdim))[0]
193        IF withrcd NE -1 THEN BEGIN
194; in order to read only the first record
195; we need to get the size of each dimension
196          count = replicate(1L, mskinq.ndims)
197          FOR d = 0, mskinq.ndims -1 DO BEGIN
198            IF d NE withrcd THEN BEGIN
199              ncdf_diminq, cdfid, mskinq.dim[d], name, size
200              count[d] = size
201            ENDIF
202          ENDFOR
203; read the variable for the first record
204          ncdf_varget, cdfid, mskid, tmask, count = count
205        ENDIF ELSE ncdf_varget, cdfid, mskid, tmask
206; check if we need to applay add_offset and scale factor
207        FOR a = 0, mskinq.natts-1 DO BEGIN
208          attname = ncdf_attname(cdfid, mskid, a)
209          CASE strlowcase(attname) OF
210            'add_offset':ncdf_attget, cdfid, mskid, attname, add_offset
211            'scale_factor':ncdf_attget, cdfid, mskid, attname, scale_factor
212            'missing_value':IF n_elements(missing_value) EQ 0 THEN $
213               ncdf_attget, cdfid, mskid, attname, missing_value
214            ELSE:
215          ENDCASE
216        ENDFOR
217        IF n_elements(scale_factor) NE 0 THEN tmask = tmask*scale_factor
218        IF n_elements(add_offset) NE 0 THEN tmask = tmask+add_offset
219        IF n_elements(missing_value) NE 0 THEN BEGIN
220; we have to take care of the float accuracy
221          CASE 1 OF
222            missing_value GE 1.e6:tmask = tmask LT (missing_value-10)
223            missing_value LE -1.e6:tmask = tmask GT (missing_value-10)
224            abs(missing_value) LE 1.e-6:tmask = abs(tmask) GT 1.e-6
225            ELSE:tmask = tmask NE missing_value
226          ENDCASE
227          if keyword_set(invmask) then tmask = 1-tmask
228        ENDIF ELSE BEGIN
229          tmask = finite(tmask)
230          IF min(tmask) EQ 1 THEN BEGIN
231            print, 'missing or nan values not found...'
232            tmask = -1
233          ENDIF
234        ENDELSE
235      ENDIF ELSE tmask = -1
236    END
237;..................
238    ELSE:tmask  = -1
239  ENDCASE
240;
241  ncdf_close, cdfid
242;
243; compute the grid
244  if NOT keyword_set(zaxis) then BEGIN
245    computegrid, xaxis = xaxis, yaxis = yaxis $
246                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
247  ENDIF ELSE BEGIN
248    computegrid, xaxis = xaxis, yaxis = yaxis, zaxis = zaxis $
249                 , mask = tmask, onearth = 1b - keyword_set(xyindex), ROMSH = romsh, _EXTRA = ex
250  ENDELSE
251  IF n_elements(time) EQ 0 THEN time = 0
252  jpt = n_elements(time)
253;----------------------------------------------------------
254
255  return
256end
Note: See TracBrowser for help on using the repository browser.