source: trunk/procs/nc_put.pro

Last change on this file was 206, checked in by pinsard, 14 years ago

homegenize ELSE BEGIN ... ENDELSE

  • Property svn:keywords set to Id
File size: 11.6 KB
Line 
1;+
2;
3; @param FLD {in}{required}{type=structure}
4; field
5; fdl.data, fld.units, fld.short_name, fld.long_name, fld.missing_value, fld.direc
6; with fld.direc= data type (x,xy,xyzt,yt,etc...)
7;
8; @param FILE_NAME {in}{required}{type=string}
9;
10; @keyword NCDF_DB {required}{type=string}
11; <location>:<path> or just <path>
12;
13; @keyword X {type=structure}
14; attributes of X direction
15; x.name, x.units, x.long_name, x.data
16;
17; @keyword Y {type=structure}
18; attributes of Y direction
19; y.name, y.units, y.long_name, y.data
20;
21; @keyword Z {type=structure}
22; attributes of Z direction
23; z.name, z.units, z.long_name, z.data
24;
25; @keyword T {type=structure}
26; attributes of T direction
27; t.name, t.units, t.long_name, t.data, t.calendar, t.origin
28;
29; @keyword GLOBAL {type=structure}
30; global attributes: conventions, title, origin
31;
32; @examples
33; IDL> fld={data:1.,  units:'', short_name:'', long_name:'', missing_value:'', direc:''}
34; IDL> file_name='ginette'
35; IDL> ncdf_db='local:./'
36; IDL> nc_put, fld, file_name,NCDF_DB=ncdf_db
37;
38; @uses
39; <pro>common</pro>
40; <propost_it>com_eg</propost_it>
41;
42; @todo
43; why ncfd_db is keyword here (argument elsewhere) ?
44;
45; check required keyword type
46;
47; error handling
48;
49; @history
50;
51; - fplod 20091210T132915Z aedon.locean-ipsl.upmc.fr (Darwin)
52;
53;   * check parameters
54;
55; @version
56; $Id$
57;
58;-
59PRO nc_put, fld, file_name $
60    , NCDF_DB=ncdf_db $
61    , X=x $
62    , Y=y $
63    , Z=z $
64    , T=t $
65    , GLOBAL=global
66;
67  compile_opt idl2, strictarrsubs
68;
69@common
70@com_eg
71;
72   IF debug_w THEN BEGIN
73    info = report('enter ...')
74   ENDIF
75;
76; Return to caller if errors
77 ON_ERROR, 2
78;
79 usage='nc_put, fld, file_name'
80;
81 nparam = N_PARAMS()
82 IF (nparam LT 2) THEN BEGIN
83    ras = report(['Incorrect number of arguments.' $
84          + '!C' $
85          + 'Usage : ' + usage])
86    stop
87 ENDIF
88 arg_type = size(fld,/type)
89 IF (arg_type NE 8) THEN BEGIN
90   ras = report(['Incorrect arg type fld' $
91          + '!C' $
92          + 'Usage : ' + usage])
93    stop
94 ENDIF
95
96 arg_struct_tags=TAG_NAMES(fld)
97
98 tag=WHERE(STRMATCH(arg_struct_tags, 'DATA'))
99 IF (tag EQ -1) THEN BEGIN
100   ras = report(['Incorrect arg tag DATA fld' $
101          + '!C' $
102          + 'Usage : ' + usage])
103    stop
104 ENDIF
105 arg_type = size(fld.data,/type)
106 IF (arg_type NE 4) THEN BEGIN
107   ras = report(['Incorrect arg type fld.data' $
108          + '!C' $
109          + 'Usage : ' + usage])
110    stop
111 ENDIF
112
113 tag=WHERE(STRMATCH(arg_struct_tags, 'UNITS'))
114 IF (tag EQ -1) THEN BEGIN
115   ras = report(['Incorrect arg tag UNITS fld' $
116          + '!C' $
117          + 'Usage : ' + usage])
118    stop
119 ENDIF
120 arg_type = size(fld.units,/type)
121 IF (arg_type NE 7) THEN BEGIN
122   ras = report(['Incorrect arg type fld.units' $
123          + '!C' $
124          + 'Usage : ' + usage])
125    stop
126 ENDIF
127
128 tag=WHERE(STRMATCH(arg_struct_tags, 'SHORT_NAME'))
129 IF (tag EQ -1) THEN BEGIN
130   ras = report(['Incorrect arg tag SHORT_NAME fld' $
131          + '!C' $
132          + 'Usage : ' + usage])
133    stop
134 ENDIF
135 arg_type = size(fld.short_name,/type)
136 IF (arg_type NE 7) THEN BEGIN
137   ras = report(['Incorrect arg type fld.short_name' $
138          + '!C' $
139          + 'Usage : ' + usage])
140    stop
141 ENDIF
142
143 tag=WHERE(STRMATCH(arg_struct_tags, 'LONG_NAME'))
144 IF (tag EQ -1) THEN BEGIN
145   ras = report(['Incorrect arg tag LONG_NAME fld' $
146          + '!C' $
147          + 'Usage : ' + usage])
148    stop
149 ENDIF
150 arg_type = size(fld.long_name,/type)
151 IF (arg_type NE 7) THEN BEGIN
152   ras = report(['Incorrect arg type fld.long_name' $
153          + '!C' $
154          + 'Usage : ' + usage])
155    stop
156 ENDIF
157
158 tag=WHERE(STRMATCH(arg_struct_tags, 'MISSING_VALUE'))
159 IF (tag EQ -1) THEN BEGIN
160   ras = report(['Incorrect arg tag MISSING_VALUE fld' $
161          + '!C' $
162          + 'Usage : ' + usage])
163    stop
164 ENDIF
165 arg_type = size(fld.missing_value,/type)
166 IF (arg_type NE 7) THEN BEGIN
167   ras = report(['Incorrect arg type fld.missing_value' $
168          + '!C' $
169          + 'Usage : ' + usage])
170    stop
171 ENDIF
172
173 tag=WHERE(STRMATCH(arg_struct_tags, 'DIREC'))
174 IF (tag EQ -1) THEN BEGIN
175   ras = report(['Incorrect arg tag DIREC fld' $
176          + '!C' $
177          + 'Usage : ' + usage])
178    stop
179 ENDIF
180 arg_type = size(fld.direc,/type)
181 IF (arg_type NE 7) THEN BEGIN
182   ras = report(['Incorrect arg type fld.direc' $
183          + '!C' $
184          + 'Usage : ' + usage])
185    stop
186 ENDIF
187
188 arg_type = size(file_name,/type)
189 IF (arg_type NE 7) THEN BEGIN
190   ras = report(['Incorrect arg type file_name' $
191          + '!C' $
192          + 'Usage : ' + usage])
193    stop
194 ENDIF
195
196
197   dim = size(fld.data)
198   dim_check = 0
199
200   type = fld.direc
201
202; open netCDF file
203
204   cdfid = ncdf_create(ncdf_db+file_name, /clobber)
205   IF debug_w THEN BEGIN
206    print, '    type,cdfid = ', type, cdfid
207   ENDIF
208   ncdf_control, cdfid, /fill
209
210; grid
211
212   jpi=1
213   jpj=1
214   jpk=1
215
216   xpos = strpos(type, 'x')
217   ypos = strpos(type, 'y')
218   zpos = strpos(type, 'z')
219   tpos = strpos(type, 't')
220
221   IF xpos NE -1 THEN BEGIN
222      dim_check = dim_check+1
223      jpi = dim[xpos+1]
224   ENDIF
225   xid = ncdf_dimdef(cdfid, 'lon', jpi)
226
227   IF ypos NE -1 THEN BEGIN
228      dim_check = dim_check+1
229      jpj = dim[ypos+1]
230   ENDIF
231   yid = ncdf_dimdef(cdfid, 'lat', jpj)
232
233   IF zpos NE -1 THEN BEGIN
234      dim_check = dim_check+1
235      jpk = dim[zpos+1]
236   ENDIF
237   zid = ncdf_dimdef(cdfid, 'depth', jpk)
238
239   IF tpos NE -1 THEN BEGIN
240      dim_check = dim_check+1
241   ENDIF
242   tid = ncdf_dimdef(cdfid, 'time', /UNLIMITED)
243
244   IF debug_w THEN BEGIN
245    print, '    dim check ', dim_check
246   ENDIF
247
248   IF dim_check NE dim[0] THEN BEGIN
249      print, ' ERROR data size / type mismatch ', type, dim[0]
250   ENDIF
251   swx = 0
252   swy = 0
253   swz = 0
254   swt = 0
255
256   ; horizontal zonal axis
257   IF debug_w THEN BEGIN
258    print, '    building zonal axis, xid: ', xid
259   ENDIF
260;   IF strpos(type, 'x') NE -1 AND keyword_set(X) THEN BEGIN
261      IF strpos (type, 'xy') NE -1 THEN BEGIN
262         lonid = ncdf_vardef(cdfid,'lon', [xid, yid], /FLOAT)
263      ENDIF ELSE BEGIN
264         lonid = ncdf_vardef(cdfid,'nav_lon', [xid], /FLOAT)
265      ENDELSE
266      NCDF_ATTPUT, cdfid, lonid, 'units', x.units
267      NCDF_ATTPUT, cdfid, lonid, 'long_name', x.long_name
268      NCDF_ATTPUT, cdfid, lonid, 'valid_min', min(x.data), /float
269      NCDF_ATTPUT, cdfid, lonid, 'valid_max', max(x.data), /float
270      swx = 1
271;   ENDIF
272
273   ; horizontal meridional axis
274   IF debug_w THEN BEGIN
275    print, '    building Y axis, yid: ', yid
276   ENDIF
277   IF strpos (type, 'xy') NE -1 THEN BEGIN
278      latid = ncdf_vardef(cdfid,'lat', [xid, yid], /FLOAT)
279   ENDIF ELSE BEGIN
280      latid = ncdf_vardef(cdfid,'nav_lat', [yid], /FLOAT)
281   ENDELSE
282   NCDF_ATTPUT, cdfid, latid, 'units', y.units
283   NCDF_ATTPUT, cdfid, latid, 'long_name', y.long_name
284   NCDF_ATTPUT, cdfid, latid, 'valid_min', min(y.data), /float
285   NCDF_ATTPUT, cdfid, latid, 'valid_max', max(y.data), /float
286   swy = 1
287
288   ; vertical axis
289   IF debug_w THEN BEGIN
290    print, '    building Z axis, zid: ', zid
291   ENDIF
292   IF zpos NE -1 AND keyword_set(Z) THEN BEGIN
293      vertid = ncdf_vardef(cdfid,z.name, [zid], /FLOAT)
294      NCDF_ATTPUT, cdfid, vertid, 'units', z.units
295      NCDF_ATTPUT, cdfid, vertid, 'long_name', z.long_name
296      NCDF_ATTPUT, cdfid, vertid, 'valid_min', min(z.data), /float
297      NCDF_ATTPUT, cdfid, vertid, 'valid_max', max(z.data), /float
298      swz = 1
299   ENDIF
300
301   ; time axis
302   IF debug_w THEN BEGIN
303    print, '    building T axis, tid: ', tid
304   ENDIF
305   IF tpos NE -1 AND keyword_set(T) THEN BEGIN
306      mid = NCDF_VARDEF(cdfid, 'time', [tid], /long)
307      NCDF_ATTPUT, cdfid, mid, 'units', t.units
308      NCDF_ATTPUT, cdfid, mid, 'calendar', t.calendar
309      NCDF_ATTPUT, cdfid, mid, 'origin', t.origin
310      NCDF_ATTPUT, cdfid, mid, 'long_name', t.long_name
311      NCDF_ATTPUT, cdfid, mid, 'short_name', 'Time'
312      NCDF_ATTPUT, cdfid, mid, 'valid_min', min(t.data), /long
313      NCDF_ATTPUT, cdfid, mid, 'valid_max', max(t.data), /long
314      time_data = t.data
315      swt = 1
316   ENDIF ELSE BEGIN
317      mid = NCDF_VARDEF(cdfid, 'time', [tid], /long)
318      NCDF_ATTPUT, cdfid, mid, 'units', "days since 1991-03-01 00:00:00"
319      time_data = 1.
320      swt = 1
321      type = type+'t'
322      CASE type OF
323          'xt':   fld.data = reform(fld.data, jpi, 1)
324          'yt':   fld.data = reform(fld.data, jpj, 1)
325          'zt':   fld.data = reform(fld.data, jpk, 1)
326          'xyt':  fld.data = reform(fld.data, jpi, jpj, 1)
327          'xzt':  fld.data = reform(fld.data, jpi, jpk, 1)
328          'yzt':  fld.data = reform(fld.data, jpj, jpk, 1)
329          'xyzt': fld.data = reform(fld.data, jpi, jpj, jpk, 1)
330      ENDCASE
331   ENDELSE
332
333   IF debug_w THEN BEGIN
334    print, '    test 1 fld.short_name = ', fld.short_name
335    print, '    swx,y,z,t = ', swx, swy, swz, swt
336   ENDIF
337
338; field attributes
339
340   IF debug_w THEN BEGIN
341    print, '    type, size fld.data ', type, size(fld.data)
342   ENDIF
343
344   varn = strcompress(fld.short_name, /remove_all)
345
346   CASE type OF
347      'x': fldid = NCDF_VARDEF(cdfid,varn, [xid], /FLOAT)
348      'y': fldid = NCDF_VARDEF(cdfid,varn, [yid], /FLOAT)
349      'z': fldid = NCDF_VARDEF(cdfid,varn, [zid], /FLOAT)
350      't': fldid = NCDF_VARDEF(cdfid,varn, [tid], /FLOAT)
351      'xy': fldid = NCDF_VARDEF(cdfid,varn, [xid, yid], /FLOAT)
352      'xz': fldid = NCDF_VARDEF(cdfid,varn, [xid, zid], /FLOAT)
353      'yz': fldid = NCDF_VARDEF(cdfid,varn, [yid, zid], /FLOAT)
354      'xyz': fldid = NCDF_VARDEF(cdfid,varn, [xid, yid, zid], /FLOAT)
355      'xyt': fldid = NCDF_VARDEF(cdfid,varn, [xid, yid, tid], /FLOAT)
356      'xzt': fldid = NCDF_VARDEF(cdfid,varn, [xid, zid, tid], /FLOAT)
357      'yzt':  fldid = NCDF_VARDEF(cdfid,varn, [xid, yid, zid, tid], /FLOAT)
358      'xyzt': fldid = NCDF_VARDEF(cdfid,varn, [xid, yid, zid, tid], /FLOAT)
359      ELSE:
360   ENDCASE
361   CASE type OF
362      'xyzt':  fld.data = reform(fld.data,jpi,jpj,jpk,jpt)
363      ELSE:
364   ENDCASE
365   NCDF_ATTPUT, cdfid, fldid, 'units', fld.units
366   NCDF_ATTPUT, cdfid, fldid, 'long_name', fld.long_name
367   NCDF_ATTPUT, cdfid, fldid, 'short_name', varn
368   NCDF_ATTPUT, cdfid, fldid, 'missing_value', fld.missing_value
369   NCDF_ATTPUT, cdfid, fldid, 'valid_min', min(fld.data[where (fld.data NE fld.missing_value)]), /float
370   NCDF_ATTPUT, cdfid, fldid, 'valid_max', max(fld.data[where (fld.data NE fld.missing_value)]), /float
371
372
373; global attributes
374
375   NCDF_ATTPUT, cdfid, /GLOBAL, 'Conventions', global.conventions
376   NCDF_ATTPUT, cdfid, /GLOBAL, 'Title', global.title
377   NCDF_ATTPUT, cdfid, /GLOBAL, 'Origin', global.origin
378   NCDF_ATTPUT, cdfid, /GLOBAL, 'Software', global.software
379
380; put file in data mode
381
382   NCDF_CONTROL, cdfid, /ENDEF
383
384;
385; put grid data
386;
387   print, ' '
388   print, '    Writing '+varn+' to netCDF file ',ncdf_db+file_name
389
390
391;   stop
392
393   IF swx EQ 1 THEN BEGIN
394      print, '       writing lon, size =', size(x.data)
395      NCDF_VARPUT, cdfid, lonid, x.data
396   END
397   IF swy EQ 1 THEN BEGIN
398      print, '       writing lat, size =', size(y.data)
399      NCDF_VARPUT, cdfid, latid, y.data
400   END
401   IF swz EQ 1 THEN BEGIN
402      print, '       writing depth, size =', size(z.data)
403      NCDF_VARPUT, cdfid, vertid, z.data
404   END
405   IF swt EQ 1 THEN BEGIN
406      print, '       writing time, size=', size (time_data)
407      NCDF_VARPUT, cdfid, mid, time_data
408   END
409
410; put field data
411
412    print, '       Field legend = ',fld.long_name
413    print, '       Field dimensions = ',size(fld.data)
414    print, '       Field min/max/unit = ',min(fld.data[where (fld.data NE fld.missing_value)]), max(fld.data[where (fld.data NE fld.missing_value)]), '   ', fld.units
415    print, '       Global attributes= Conventions:  ', global.conventions
416    print, '                           Title:        ', global.title
417    print, '                           Origin:       ', global.origin
418
419;    stop
420
421    NCDF_VARPUT, cdfid, fldid, fld.data
422
423; close file
424
425    NCDF_CLOSE, cdfid
426   IF debug_w THEN BEGIN
427    info = report('leaving ...')
428   ENDIF
429
430END
Note: See TracBrowser for help on using the repository browser.