source: trunk/procs/nc_put.pro @ 13

Last change on this file since 13 was 13, checked in by kolasinski, 16 years ago

Add some debug_w in nc_build and nc_put

File size: 7.4 KB
Line 
1PRO nc_put, fld, file_name, NCDF_DB = ncdf_db, X = x, Y = y, Z = z, T = t, GLOBAL = global
2
3@common
4@com_eg
5
6; fld = structure
7; fdl.data, fld.units, fld.short_name, fld.long_name, fld.missing_value, fld.direc
8
9; fld.direc= data type (x,xy,xyzt,yt,etc...)
10; X,Y,Z,T = structures containing attributes for each direction :
11;     x.name, x.units, x.long_name, x.data (+t.calendar and t.origin for time)
12; Global = global attributes: conventions, title, origin
13   IF debug_w THEN print, ' '
14   IF debug_w THEN print, '  ENTER nc_put...'
15
16   dim = size(fld.data)
17   dim_check = 0
18
19   type = fld.direc
20
21; open netCDF file
22
23   cdfid = ncdf_create(ncdf_db+file_name, /clobber)
24   IF debug_w THEN print, '    type,cdfid = ', type, cdfid
25   ncdf_control, cdfid, /fill
26
27; grid
28
29   jpi=1
30   jpj=1
31   jpk=1
32
33   xpos = strpos(type, 'x')
34   ypos = strpos(type, 'y')
35   zpos = strpos(type, 'z')
36   tpos = strpos(type, 't')
37
38   IF xpos NE -1 THEN BEGIN
39      dim_check = dim_check+1
40      jpi = dim(xpos+1)
41   ENDIF
42   xid = ncdf_dimdef(cdfid, 'lon', jpi)
43       
44   IF ypos NE -1 THEN BEGIN
45      dim_check = dim_check+1
46      jpj = dim(ypos+1)
47   ENDIF
48   yid = ncdf_dimdef(cdfid, 'lat', jpj)
49
50   IF zpos NE -1 THEN BEGIN
51      dim_check = dim_check+1
52      jpk = dim(zpos+1)
53   ENDIF
54   zid = ncdf_dimdef(cdfid, 'depth', jpk)
55
56   IF tpos NE -1 THEN BEGIN
57      dim_check = dim_check+1
58   ENDIF
59   tid = ncdf_dimdef(cdfid, 'time', /UNLIMITED)
60
61   
62   IF dim_check NE dim(0) THEN BEGIN
63      print,  ' ERROR data size / type mismatch ', type, dim(0)
64   ENDIF
65   swx = 0
66   swy = 0
67   swz = 0
68   swt = 0
69
70   ; horizontal zonal axis
71;   IF strpos(type, 'x') NE -1 AND keyword_set(X) THEN BEGIN
72      IF strpos (type, 'xy') NE -1 THEN BEGIN
73         lonid = ncdf_vardef(cdfid,'lon', [xid, yid], /FLOAT)
74      ENDIF ELSE lonid = ncdf_vardef(cdfid,'nav_lon', [xid], /FLOAT)
75      NCDF_ATTPUT, cdfid, lonid, 'units', x.units
76      NCDF_ATTPUT, cdfid, lonid, 'long_name', x.long_name
77      NCDF_ATTPUT, cdfid, lonid, 'valid_min', min(x.data), /float
78      NCDF_ATTPUT, cdfid, lonid, 'valid_max', max(x.data), /float
79      swx = 1
80;   ENDIF
81
82   ; horizontal meridional axis
83;   IF strpos(type, 'y') NE -1 AND keyword_set(Y) THEN BEGIN
84      IF strpos (type, 'xy') NE -1 THEN BEGIN
85         latid = ncdf_vardef(cdfid,'lat', [xid, yid], /FLOAT)
86      ENDIF ELSE latid = ncdf_vardef(cdfid,'nav_lat', [yid], /FLOAT)
87      NCDF_ATTPUT, cdfid, latid, 'units', y.units
88      NCDF_ATTPUT, cdfid, latid, 'long_name', y.long_name
89      NCDF_ATTPUT, cdfid, latid, 'valid_min', min(y.data), /float
90      NCDF_ATTPUT, cdfid, latid, 'valid_max', max(y.data), /float
91      swy = 1
92;   ENDIF
93
94   ; vertical axis
95      IF zpos NE -1 AND keyword_set(Z) THEN BEGIN
96         vertid = ncdf_vardef(cdfid,z.name, [zid], /FLOAT)
97         NCDF_ATTPUT, cdfid, vertid, 'units', z.units
98         NCDF_ATTPUT, cdfid, vertid, 'long_name', z.long_name
99         NCDF_ATTPUT, cdfid, vertid, 'valid_min', min(z.data), /float
100         NCDF_ATTPUT, cdfid, vertid, 'valid_max', max(z.data), /float
101         swz = 1
102      ENDIF
103   
104   ; time axis
105   IF tpos NE -1 AND keyword_set(T) THEN BEGIN
106      mid = NCDF_VARDEF(cdfid, 'time', [tid], /long)
107      NCDF_ATTPUT, cdfid, mid, 'units', t.units
108      NCDF_ATTPUT, cdfid, mid, 'calendar', t.calendar
109      NCDF_ATTPUT, cdfid, mid, 'origin', t.origin
110      NCDF_ATTPUT, cdfid, mid, 'long_name', t.long_name
111      NCDF_ATTPUT, cdfid, mid, 'short_name', 'Time'
112      NCDF_ATTPUT, cdfid, mid, 'valid_min', min(t.data), /long
113      NCDF_ATTPUT, cdfid, mid, 'valid_max', max(t.data), /long
114      time_data = t.data
115      swt = 1
116   ENDIF ELSE BEGIN
117      mid = NCDF_VARDEF(cdfid, 'time', [tid], /long)
118      NCDF_ATTPUT, cdfid, mid, 'units', "days since 1991-03-01 00:00:00"
119      time_data = 1.
120      swt = 1
121      type = type+'t'
122      CASE type OF
123          'xt':   fld.data = reform(fld.data, jpi, 1)
124          'yt':   fld.data = reform(fld.data, jpj, 1)
125          'zt':   fld.data = reform(fld.data, jpk, 1)
126          'xyt':  fld.data = reform(fld.data, jpi, jpj, 1)
127          'xzt':  fld.data = reform(fld.data, jpi, jpk, 1)
128          'yzt':  fld.data = reform(fld.data, jpj, jpk, 1)
129          'xyzt': fld.data = reform(fld.data, jpi, jpj, jpk, 1)
130      ENDCASE 
131   ENDELSE
132
133   IF debug_w THEN print, '    test 1 fld.short_name  = ', fld.short_name
134   IF debug_w THEN print, '    swx,y,z,t  = ', swx, swy, swz, swt
135
136; field attributes
137
138   CASE type OF
139      'x': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid], /FLOAT)
140      'y': fldid = NCDF_VARDEF(cdfid,fld.short_name, [yid], /FLOAT)
141      'z': fldid = NCDF_VARDEF(cdfid,fld.short_name, [zid], /FLOAT)
142      't': fldid = NCDF_VARDEF(cdfid,fld.short_name, [tid], /FLOAT)
143      'xy': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, yid], /FLOAT)
144      'xz': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, zid], /FLOAT)
145      'yz': fldid = NCDF_VARDEF(cdfid,fld.short_name, [yid, zid], /FLOAT)
146      'xyz': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, yid, zid], /FLOAT)
147      'xyt': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, yid, tid], /FLOAT)
148      'xzt': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, zid, tid], /FLOAT)
149      'yzt':  fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, yid, zid, tid], /FLOAT)
150      'xyzt': fldid = NCDF_VARDEF(cdfid,fld.short_name, [xid, yid, zid, tid], /FLOAT)
151      ELSE:
152   ENDCASE
153   CASE type OF
154      'xyzt':  fld.data = reform(fld.data,jpi,jpj,jpk,jpt)
155      ELSE:
156   ENDCASE
157   NCDF_ATTPUT, cdfid, fldid, 'units', fld.units
158   NCDF_ATTPUT, cdfid, fldid, 'long_name', fld.long_name
159   NCDF_ATTPUT, cdfid, fldid, 'short_name', fld.short_name
160   NCDF_ATTPUT, cdfid, fldid, 'missing_value', fld.missing_value
161   NCDF_ATTPUT, cdfid, fldid, 'valid_min', min(fld.data(where (fld.data NE fld.missing_value))), /float
162   NCDF_ATTPUT, cdfid, fldid, 'valid_max', max(fld.data(where (fld.data NE fld.missing_value))), /float
163   
164   
165; global attributes
166
167   NCDF_ATTPUT, cdfid, /GLOBAL, 'Conventions', global.conventions
168   NCDF_ATTPUT, cdfid, /GLOBAL, 'Title', global.title
169   NCDF_ATTPUT, cdfid, /GLOBAL, 'Origin', global.origin
170   NCDF_ATTPUT, cdfid, /GLOBAL, 'Software', global.software
171
172; put file in data mode
173
174   NCDF_CONTROL, cdfid, /ENDEF   
175   
176;
177; put grid data
178;
179   print, ' '
180   print, '    Writing '+fld.short_name+' to netCDF file ',ncdf_db+file_name
181   
182
183;   stop
184
185   IF swx EQ 1 THEN BEGIN
186      print, '       writing lon, size =', size(x.data)
187      NCDF_VARPUT, cdfid, lonid, x.data
188   END
189   IF swy EQ 1 THEN BEGIN
190      print, '       writing lat, size =', size(y.data)
191      NCDF_VARPUT, cdfid, latid, y.data
192   END
193   IF swz EQ 1 THEN BEGIN
194      print, '       writing depth, size =', size(z.data)
195      NCDF_VARPUT, cdfid, vertid, z.data
196   END
197   IF swt EQ 1 THEN BEGIN
198      print, '       writing time, size=', size (time_data)
199      NCDF_VARPUT, cdfid, mid, time_data
200   END
201
202; put field data
203   
204    print, '       Field legend       =  ',fld.long_name
205    print, '       Field dimensions   =  ',size(fld.data)
206    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
207    print, '       Global attributes=  Conventions:  ', global.conventions
208    print, '                           Title:        ', global.title
209    print, '                           Origin:       ', global.origin
210
211;    stop
212
213    NCDF_VARPUT, cdfid, fldid, fld.data
214   
215; close file
216
217    NCDF_CLOSE, cdfid
218    IF debug_w THEN print, '  ...EXIT nc_put'
219
220END
Note: See TracBrowser for help on using the repository browser.