1 | ;+ |
---|
2 | ; |
---|
3 | ; Reading any netcdf file |
---|
4 | ; |
---|
5 | ; @param FILE_NAME {in}{required}{type=string} |
---|
6 | ; |
---|
7 | ; @param VAR_NAME {in}{required}{type=string} |
---|
8 | ; variable name |
---|
9 | ; |
---|
10 | ; @param NCDF_DB {in}{required}{type=string} |
---|
11 | ; <location>:<path> or just <path> |
---|
12 | ; |
---|
13 | ; @keyword BOXZOOM |
---|
14 | ; |
---|
15 | ; @keyword TIME_1 |
---|
16 | ; |
---|
17 | ; @keyword TIME_2 |
---|
18 | ; |
---|
19 | ; @keyword ALL_DATA |
---|
20 | ; |
---|
21 | ; @keyword NO_MEAN |
---|
22 | ; |
---|
23 | ; @returns |
---|
24 | ; structure |
---|
25 | ; -1 in case of error |
---|
26 | ; |
---|
27 | ; @examples |
---|
28 | ; |
---|
29 | ; IDL> file_name='ginette' |
---|
30 | ; IDL> var_name='temp_1' |
---|
31 | ; IDL> ncdf_db='local:./' |
---|
32 | ; IDL> vert_type='z' |
---|
33 | ; IDL> result=nc_read(file_name, var_name, ncdf_db) |
---|
34 | ; IDL> help, result,/structure |
---|
35 | ; IDL> print, result |
---|
36 | ; |
---|
37 | ; @uses |
---|
38 | ; <pro>common</pro> |
---|
39 | ; <propost_it>com_eg</propost_it> |
---|
40 | ; |
---|
41 | ; <pro>ncdf_getatt</pro> |
---|
42 | ; |
---|
43 | ; @todo |
---|
44 | ; |
---|
45 | ; explication sur vert_type |
---|
46 | ; |
---|
47 | ; realistic example |
---|
48 | ; |
---|
49 | ; @history |
---|
50 | ; |
---|
51 | ; - fplod 20091210T135516Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
52 | ; |
---|
53 | ; * check parameters |
---|
54 | ; |
---|
55 | ; @version |
---|
56 | ; $Id$ |
---|
57 | ; |
---|
58 | ;- |
---|
59 | FUNCTION nc_read, file_name, var_name, ncdf_db $ |
---|
60 | , BOXZOOM=boxzoom $ |
---|
61 | , TIME_1=time_1 $ |
---|
62 | , TIME_2=time_2 $ |
---|
63 | , ALL_DATA=all_data $ |
---|
64 | , NO_MEAN=no_mean |
---|
65 | ; |
---|
66 | compile_opt idl2, strictarrsubs |
---|
67 | ; |
---|
68 | @common |
---|
69 | @com_eg |
---|
70 | ; |
---|
71 | IF debug_w THEN BEGIN |
---|
72 | info = report('enter ...') |
---|
73 | ENDIF |
---|
74 | ; |
---|
75 | ; Return to caller if errors |
---|
76 | ;++ ON_ERROR, 2 |
---|
77 | ; |
---|
78 | usage='result=nc_read(file_name, var_name, ncdf_db '$ |
---|
79 | + ', BOXZOOM=boxzoom' $ |
---|
80 | + ', TIME_1=time_1' $ |
---|
81 | + ', TIME_2=time_2' $ |
---|
82 | + ', ALL_DATA=all_data' $ |
---|
83 | + ', NO_MEAN=no_mean)' |
---|
84 | |
---|
85 | nparam = N_PARAMS() |
---|
86 | IF (nparam LT 3) THEN BEGIN |
---|
87 | ras = report(['Incorrect number of arguments.' $ |
---|
88 | + '!C' $ |
---|
89 | + 'Usage : ' + usage]) |
---|
90 | return, -1 |
---|
91 | ENDIF |
---|
92 | |
---|
93 | arg_type = size(file_name,/type) |
---|
94 | IF (arg_type NE 7) THEN BEGIN |
---|
95 | ras = report(['Incorrect arg type file_name' $ |
---|
96 | + '!C' $ |
---|
97 | + 'Usage : ' + usage]) |
---|
98 | return, -1 |
---|
99 | ENDIF |
---|
100 | |
---|
101 | arg_type = size(var_name,/type) |
---|
102 | IF (arg_type NE 7) THEN BEGIN |
---|
103 | ras = report(['Incorrect arg type var_name' $ |
---|
104 | + '!C' $ |
---|
105 | + 'Usage : ' + usage]) |
---|
106 | return, -1 |
---|
107 | ENDIF |
---|
108 | |
---|
109 | arg_type = size(ncdf_db,/type) |
---|
110 | IF (arg_type NE 7) THEN BEGIN |
---|
111 | ras = report(['Incorrect arg type ncdf_db' $ |
---|
112 | + '!C' $ |
---|
113 | + 'Usage : ' + usage]) |
---|
114 | return, -1 |
---|
115 | ENDIF |
---|
116 | |
---|
117 | common_type=size(vert_type,/type) |
---|
118 | IF (common_type NE 7) THEN BEGIN |
---|
119 | ras = report(['Incorrect common type vert_type' $ |
---|
120 | + '!C' $ |
---|
121 | + 'Usage : ' + usage]) |
---|
122 | return, -1 |
---|
123 | ENDIF |
---|
124 | |
---|
125 | IF NOT keyword_set(TIME_1) THEN BEGIN |
---|
126 | time_1 = 1 |
---|
127 | ENDIF |
---|
128 | IF NOT keyword_set(TIME_2) THEN BEGIN |
---|
129 | time_2 = time_1 |
---|
130 | ENDIF |
---|
131 | IF debug_w THEN BEGIN |
---|
132 | print, ' Before read_ncdf, key_yreverse=', key_yreverse |
---|
133 | ENDIF |
---|
134 | |
---|
135 | ; decide which subdomain of data to read |
---|
136 | IF debug_w THEN BEGIN |
---|
137 | print, ' keyword_set(ALL_DATA) : ', keyword_set(ALL_DATA) |
---|
138 | ENDIF |
---|
139 | IF keyword_set(ALL_DATA) THEN BEGIN |
---|
140 | tout = 1 |
---|
141 | ENDIF ELSE tout = 0 |
---|
142 | CASE vert_type OF |
---|
143 | 'z' : zindex = 0 |
---|
144 | 'level' : zindex = 1 |
---|
145 | ELSE: zindex = 0 |
---|
146 | ENDCASE |
---|
147 | |
---|
148 | ; local directory |
---|
149 | IF strpos(ncdf_db, ':') GE 1 THEN BEGIN |
---|
150 | directory = (strsplit(ncdf_db, ':', /EXTRACT))[1] |
---|
151 | ENDIF ELSE BEGIN |
---|
152 | directory = ncdf_db |
---|
153 | ENDELSE |
---|
154 | |
---|
155 | ; Find the variable's attribute |
---|
156 | ncdf_getatt, directory+file_name, var_name, add_offset = add_offset, scale_factor = scale_factor, $ |
---|
157 | missing_value = missing_value, units = units, calendar = calendar, long_name = long_name |
---|
158 | |
---|
159 | ; By default units for the Z axis are meters |
---|
160 | IF n_elements(gdept) GT 1 THEN BEGIN |
---|
161 | IF name_level NE '-' THEN BEGIN |
---|
162 | ncdf_getatt, directory+file_name, name_level, units = units_depth |
---|
163 | ENDIF ELSE BEGIN |
---|
164 | units_depth = 'm' |
---|
165 | ENDELSE |
---|
166 | ENDIF ELSE units_depth = '' |
---|
167 | |
---|
168 | ; Check consistency between time_2 computed from param in cmdline and the max number |
---|
169 | ; of time steps found in the file. |
---|
170 | jpt_max = find_jptmax(file_name, IODIRECTORY = directory) |
---|
171 | IF time_2-time_1+1 GT jpt_max AND jpt_max NE -1 THEN BEGIN |
---|
172 | time_2 = time_1+jpt_max-1 |
---|
173 | ENDIF |
---|
174 | |
---|
175 | ; Read the data with a call to read_ncdf |
---|
176 | IF debug_w THEN BEGIN |
---|
177 | print, ' ' |
---|
178 | print, ' Reading raw data from ', file_name |
---|
179 | IF keyword_set(boxzoom) THEN BEGIN |
---|
180 | print, ' boxzoom before=', boxzoom |
---|
181 | ENDIF |
---|
182 | ENDIF |
---|
183 | |
---|
184 | lec_data = read_ncdf(var_name, time_1-1, time_2-1, BOXZOOM = boxzoom, FILENAME = directory+file_name, $ |
---|
185 | /TIMESTEP, TOUT = tout, /NOSTRUCT, /CONT_NOFILL, GRID = vargrid, $ |
---|
186 | ZINVAR = zinvar, ZINDEX = zindex) |
---|
187 | field_dim = (size(lec_data))[0] |
---|
188 | IF field_dim LE 0 THEN BEGIN |
---|
189 | print, ' Something wrong happened in read_ncdf ' |
---|
190 | return, -1 |
---|
191 | ENDIF |
---|
192 | |
---|
193 | IF debug_w THEN BEGIN |
---|
194 | print, ' ' |
---|
195 | IF keyword_set(boxzoom) THEN BEGIN |
---|
196 | print, ' boxzoom after=', boxzoom |
---|
197 | ENDIF |
---|
198 | print, ' firstxt,firstyt,firstzt=', firstxt, firstyt, firstzt |
---|
199 | print, ' lastxt,lastyt,lastzt=', lastxt, lastyt, lastzt |
---|
200 | print, ' zinvar=', zinvar |
---|
201 | print, ' jpt=', jpt |
---|
202 | print, ' key_shift=', key_shift |
---|
203 | print, ' After read_ncdf, key_yreverse=',key_yreverse |
---|
204 | print, ' ' |
---|
205 | ENDIF |
---|
206 | |
---|
207 | ; Average data along vertical if needed and update some features |
---|
208 | ; needed for plt (data_direc, name_suff) |
---|
209 | name_suff = '' |
---|
210 | data_direc = '' |
---|
211 | update_data, TAB = lec_data, VNAME = var_name, BOXZOOM = boxzoom, ZUNITS = units_depth, $ |
---|
212 | ZINVAR = zinvar, SUFFIX = name_suff, D_DIREC = data_direc, $ |
---|
213 | TIME_1 = time_1, TIME_2 = time_2, NO_MEAN = no_mean |
---|
214 | field_dim = (size(lec_data))[0] |
---|
215 | |
---|
216 | ; Field attributes |
---|
217 | field = {name: '', data: lec_data, legend: '', units: '', origin: '', dim: 0, direc: data_direc} |
---|
218 | field.name = var_name |
---|
219 | field.origin = directory+file_name |
---|
220 | field.legend = long_name+name_suff |
---|
221 | field.units = units |
---|
222 | field.dim = field_dim |
---|
223 | |
---|
224 | ; replace NaN with valmask |
---|
225 | ; idx_t = where (~finite(field.data)) |
---|
226 | ; IF idx_t[0] NE -1 THEN BEGIN |
---|
227 | ; field.data(idx_t) = valmask |
---|
228 | ; ENDIF |
---|
229 | |
---|
230 | ; get valmask (might need valmask = float(string(valmask)) |
---|
231 | valmask = 1.e20 |
---|
232 | IF size(missing_value, /TYPE) EQ 4 OR size(missing_value, /TYPE) EQ 5 THEN BEGIN |
---|
233 | valmask = missing_value |
---|
234 | ; ensure valmask is positive |
---|
235 | IF valmask LT 0 THEN BEGIN |
---|
236 | print, ' *** Warning valmask is negative - changing sign: ', valmask |
---|
237 | idx_t = where (field.data EQ valmask) |
---|
238 | IF idx_t[0] NE -1 THEN BEGIN |
---|
239 | field.data[idx_t] = -valmask |
---|
240 | ENDIF |
---|
241 | valmask = -valmask |
---|
242 | idx_t=0 ; free memory |
---|
243 | ENDIF |
---|
244 | ENDIF |
---|
245 | |
---|
246 | IF debug_w THEN BEGIN |
---|
247 | print, ' valmask=',valmask |
---|
248 | ENDIF |
---|
249 | |
---|
250 | ; if pseudo 3d mask, read mask file and set masked points to valmask |
---|
251 | ; still to do |
---|
252 | |
---|
253 | ; min/max |
---|
254 | |
---|
255 | chardim = ' - dim = ' |
---|
256 | FOR i = 1, (size(field.data))[0] DO BEGIN |
---|
257 | chardim = chardim+string((size(field.data))[i], format = '(I5)') |
---|
258 | ENDFOR |
---|
259 | |
---|
260 | idx_valmsk = (where (field.data EQ valmask)) |
---|
261 | idx_novalmsk = (where (field.data NE valmask)) |
---|
262 | minf = idx_valmsk[0] EQ -1 ? min(field.data) : min(field.data[idx_novalmsk]) |
---|
263 | maxf = idx_valmsk[0] EQ -1 ? max(field.data) : max(field.data[idx_novalmsk]) |
---|
264 | |
---|
265 | print, ' = ',field.legend, ' [min/max = ',minf , maxf,' ', field.units,' - masked values = ',valmask, chardim, ']' |
---|
266 | |
---|
267 | IF debug_w THEN BEGIN |
---|
268 | info = report('leaving ...') |
---|
269 | ENDIF |
---|
270 | |
---|
271 | return, field |
---|
272 | |
---|
273 | END |
---|