source: trunk/ToBeReviewed/INIT/initncdf.pro @ 69

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

debug + new xxx

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