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} |
---|
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 |
---|
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 redifine 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 |
---|
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 |
---|
41 | ; A string giving the name of the variable in the file |
---|
42 | ; that contains the [xyz]axis. |
---|
43 | ; for X axis, default name must be 'x', 'longitude', 'nav_lon' or 'lon'. |
---|
44 | ; |
---|
45 | ; @keyword YAXISNAME |
---|
46 | ; A string giving the name of the variable in the file |
---|
47 | ; that contains the [xyz]axis. |
---|
48 | ; for Y axis, default name must be 'y', 'latitude', 'nav_lat' or 'lat'. |
---|
49 | ; |
---|
50 | ; @keyword ZAXISNAME |
---|
51 | ; A string giving the name of the variable in the file |
---|
52 | ; that contains the [xyz]axis. |
---|
53 | ; for Z axis, default name must be 'z', 'level', 'lev', 'depth...' |
---|
54 | ; |
---|
55 | ; @keyword XYINDEX |
---|
56 | ; To define the x/y axis with index instead of using |
---|
57 | ; the values contained in X/YAXISNAME. |
---|
58 | ; x/yaxis = keyword_set(start1) + findgen(jpi/jpj) |
---|
59 | ; this forces key_onearth = 0 |
---|
60 | ; |
---|
61 | ; @keyword ZINDEX |
---|
62 | ; To define the z axis with index instead of using |
---|
63 | ; the values contained in ZAXISNAME. |
---|
64 | ; zaxis = keyword_set(start1) + findgen(jpk) |
---|
65 | ; |
---|
66 | ; @keyword _EXTRA |
---|
67 | ; Used to pass your keywords/ |
---|
68 | ; |
---|
69 | ; @uses |
---|
70 | ; common.pro |
---|
71 | ; |
---|
72 | ; @restrictions |
---|
73 | ; Change the grid parameters of the common.pro |
---|
74 | ; |
---|
75 | ; @restrictions |
---|
76 | ; the file must contain an x and an y axis. (1 ou 2 dimentional array) |
---|
77 | ; |
---|
78 | ; @examples |
---|
79 | ; IDL> initncdf,'toto.nc',glam=[-180,180] |
---|
80 | ; |
---|
81 | ; @history |
---|
82 | ; Sebastien Masson (smasson@lodyc.jussieu.fr) |
---|
83 | ; 8 May 2002 |
---|
84 | ; |
---|
85 | ; @version |
---|
86 | ; $Id$ |
---|
87 | ; |
---|
88 | ;- |
---|
89 | ;------------------------------------------------------------ |
---|
90 | ;------------------------------------------------------------ |
---|
91 | ;------------------------------------------------------------ |
---|
92 | PRO initncdf, ncfilein, XAXISNAME = xaxisname, YAXISNAME = yaxisname $ |
---|
93 | , ZAXISNAME = zaxisname, MASKNAME = maskname $ |
---|
94 | , INVMASK = invmask, USEASMASK = useasmask $ |
---|
95 | , MISSING_VALUE = missing_value, START1 = start1 $ |
---|
96 | , XYINDEX = xyindex, ZINDEX = zindex $ |
---|
97 | , _EXTRA = ex |
---|
98 | ; |
---|
99 | ; |
---|
100 | compile_opt idl2, strictarrsubs |
---|
101 | ; |
---|
102 | @common |
---|
103 | ;---------------------------------------------------------- |
---|
104 | ; check the name of the file |
---|
105 | ncfile = isafile(FILENAME = ncfilein, IODIRECTORY = iodir, _extra = ex) |
---|
106 | if size(ncfile, /type) NE 7 then BEGIN |
---|
107 | print, 'initncdf cancelled' |
---|
108 | return |
---|
109 | endif |
---|
110 | ; if the file is stored on tape |
---|
111 | if !version.os_family EQ 'unix' then spawn, 'file '+ncfile+' > /dev/null' |
---|
112 | ;---------------------------------------------------------- |
---|
113 | ; open the file |
---|
114 | cdfid = ncdf_open(ncfile) |
---|
115 | ; what is inside the file |
---|
116 | inside = ncdf_inquire(cdfid) |
---|
117 | ;---------------------------------------------------------- |
---|
118 | ; name of the variables |
---|
119 | namevar = strarr(inside.nvars) |
---|
120 | for varid = 0, inside.nvars-1 do begin |
---|
121 | invar = ncdf_varinq(cdfid, varid) |
---|
122 | namevar[varid] = strlowcase(invar.name) |
---|
123 | ENDFOR |
---|
124 | ;---------------------------------------------------------- |
---|
125 | ; find the xaxis |
---|
126 | if keyword_set(xaxisname) then xaxisname = strlowcase(xaxisname) ELSE xaxisname = 'x' |
---|
127 | xvarid = where(namevar EQ xaxisname OR namevar EQ 'longitude' $ |
---|
128 | OR namevar EQ 'nav_lon' OR namevar EQ 'lon') |
---|
129 | xvarid = xvarid[0] |
---|
130 | if xvarid EQ -1 then begin |
---|
131 | print, 'the xaxis was not found, check the use of XAXISNAME keyword' |
---|
132 | stop |
---|
133 | endif |
---|
134 | ; get the size of xaxis |
---|
135 | xinq = ncdf_varinq(cdfid, xvarid) |
---|
136 | ncdf_diminq, cdfid, xinq.dim[0], blabla, jpifromx |
---|
137 | ; should we read or compute the xaxis? |
---|
138 | IF NOT keyword_set(xyindex) THEN BEGIN |
---|
139 | ; read the xaxis |
---|
140 | ncdf_varget, cdfid, xvarid, xaxis |
---|
141 | ; make sure of the shape of xaxis |
---|
142 | IF xinq.ndims GE 2 THEN BEGIN |
---|
143 | ncdf_diminq, cdfid, xinq.dim[1], blabla, jpjfromx |
---|
144 | xaxis = reform(xaxis, jpifromx, jpjfromx, /over) |
---|
145 | ENDIF |
---|
146 | ENDIF ELSE xaxis = keyword_set(start1) + findgen(jpifromx) |
---|
147 | ;---------------------------------------------------------- |
---|
148 | ; find the yaxis |
---|
149 | if keyword_set(yaxisname) then yaxisname = strlowcase(yaxisname) ELSE yaxisname = 'y' |
---|
150 | yvarid = where(namevar EQ yaxisname OR namevar EQ 'latitude' OR namevar EQ 'nav_lat' OR namevar EQ 'lat') |
---|
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 |
---|
296 | end |
---|