source: trunk/procs/nc_put.pro @ 2

Last change on this file since 2 was 2, checked in by post_it, 17 years ago

Initial import from ~/POST_IT/

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