1 | subroutine get_iddim(infid,latid,latlength,lonid,lonlength, & |
---|
2 | altid,altlength,timid,timelength,lmdflag ) |
---|
3 | |
---|
4 | implicit none |
---|
5 | |
---|
6 | include "netcdf.inc" ! NetCDF definitions |
---|
7 | |
---|
8 | ! arguments |
---|
9 | integer infid ! NetCDF input file ID |
---|
10 | integer lonid,latid,altid,timid |
---|
11 | integer lonlength ! # of grid points along longitude |
---|
12 | integer latlength ! # of grid points along latitude |
---|
13 | integer altlength ! # of grid point along altitude (of input datasets) |
---|
14 | integer timelength ! # of points along time |
---|
15 | logical lmdflag ! true=LMD, false=CAM |
---|
16 | |
---|
17 | !local |
---|
18 | integer tmpdimid ! temporarily store a dimension ID |
---|
19 | integer ierr ! NetCDF routines return code |
---|
20 | |
---|
21 | ! latitude |
---|
22 | ierr=NF_INQ_DIMID(infid,"latitude",tmpdimid) |
---|
23 | if (ierr.ne.NF_NOERR) then |
---|
24 | write(*,*) "Could not get latitude dimension ID" |
---|
25 | write(*,*) " looking for lat dimension instead... " |
---|
26 | ierr=NF_INQ_DIMID(infid,"lat",tmpdimid) |
---|
27 | if (ierr.ne.NF_NOERR) then |
---|
28 | stop "Error: Failed to get lat dimension ID" |
---|
29 | else |
---|
30 | ierr=NF_INQ_VARID(infid,"lat",latid) |
---|
31 | if (ierr.ne.NF_NOERR) then |
---|
32 | stop "Error: Failed to get lat ID" |
---|
33 | else |
---|
34 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,latlength) |
---|
35 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get lat length" |
---|
36 | endif |
---|
37 | endif |
---|
38 | else |
---|
39 | ierr=NF_INQ_VARID(infid,"latitude",latid) |
---|
40 | if (ierr.ne.NF_NOERR) then |
---|
41 | stop "Error: Failed to get latitude ID" |
---|
42 | else |
---|
43 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,latlength) |
---|
44 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get latitude length" |
---|
45 | endif |
---|
46 | endif |
---|
47 | |
---|
48 | ! longitude |
---|
49 | ierr=NF_INQ_DIMID(infid,"longitude",tmpdimid) |
---|
50 | if (ierr.ne.NF_NOERR) then |
---|
51 | write(*,*) "Could not get longitude dimension ID" |
---|
52 | write(*,*) " looking for lon dimension instead... " |
---|
53 | ierr=NF_INQ_DIMID(infid,"lon",tmpdimid) |
---|
54 | if (ierr.ne.NF_NOERR) then |
---|
55 | stop "Error: Failed to get lon dimension ID" |
---|
56 | else |
---|
57 | ierr=NF_INQ_VARID(infid,"lon",lonid) |
---|
58 | if (ierr.ne.NF_NOERR) then |
---|
59 | stop "Error: Failed to get lon ID" |
---|
60 | else |
---|
61 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,lonlength) |
---|
62 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get lon length" |
---|
63 | endif |
---|
64 | endif |
---|
65 | else |
---|
66 | ierr=NF_INQ_VARID(infid,"longitude",lonid) |
---|
67 | if (ierr.ne.NF_NOERR) then |
---|
68 | stop "Error: Failed to get longitude ID" |
---|
69 | else |
---|
70 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,lonlength) |
---|
71 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get longitude length" |
---|
72 | endif |
---|
73 | endif |
---|
74 | |
---|
75 | lmdflag=.true. |
---|
76 | ! altitude : pressure levels |
---|
77 | ierr=NF_INQ_DIMID(infid,"altitude",tmpdimid) |
---|
78 | if (ierr.ne.NF_NOERR) then |
---|
79 | write(*,*) "Could not get altitude dimension ID" |
---|
80 | write(*,*) " looking for presnivs dimension instead... " |
---|
81 | ierr=NF_INQ_DIMID(infid,"presnivs",tmpdimid) |
---|
82 | if (ierr.ne.NF_NOERR) then |
---|
83 | write(*,*) "Could not get presnivs dimension ID" |
---|
84 | write(*,*) " looking for lev dimension instead... " |
---|
85 | ierr=NF_INQ_DIMID(infid,"lev",tmpdimid) |
---|
86 | if (ierr.ne.NF_NOERR) then |
---|
87 | stop "Error: Failed to get lev dimension ID" |
---|
88 | else |
---|
89 | ierr=NF_INQ_VARID(infid,"lev",altid) |
---|
90 | if (ierr.ne.NF_NOERR) then |
---|
91 | stop "Error: Failed to get lev ID" |
---|
92 | else |
---|
93 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,altlength) |
---|
94 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get lev length" |
---|
95 | lmdflag=.false. |
---|
96 | endif |
---|
97 | endif |
---|
98 | else |
---|
99 | ierr=NF_INQ_VARID(infid,"presnivs",altid) |
---|
100 | if (ierr.ne.NF_NOERR) then |
---|
101 | stop "Error: Failed to get presnivs ID" |
---|
102 | else |
---|
103 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,altlength) |
---|
104 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get presnivs length" |
---|
105 | endif |
---|
106 | endif |
---|
107 | else |
---|
108 | ierr=NF_INQ_VARID(infid,"altitude",altid) |
---|
109 | if (ierr.ne.NF_NOERR) then |
---|
110 | stop "Error: Failed to get altitude ID" |
---|
111 | else |
---|
112 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,altlength) |
---|
113 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get altitude length" |
---|
114 | endif |
---|
115 | endif |
---|
116 | |
---|
117 | ! time |
---|
118 | ierr=NF_INQ_DIMID(infid,"Time",tmpdimid) |
---|
119 | if (ierr.ne.NF_NOERR) then |
---|
120 | write(*,*) "Could not get Time dimension ID" |
---|
121 | write(*,*) " looking for time dimension instead... " |
---|
122 | ierr=NF_INQ_DIMID(infid,"time",tmpdimid) |
---|
123 | if (ierr.ne.NF_NOERR) then |
---|
124 | write(*,*) "Could not get time dimension ID" |
---|
125 | write(*,*) " looking for time_counter dimension instead... " |
---|
126 | ierr=NF_INQ_DIMID(infid,"time_counter",tmpdimid) |
---|
127 | if (ierr.ne.NF_NOERR) then |
---|
128 | stop "Error: Failed to get time_counter dimension ID" |
---|
129 | else |
---|
130 | ierr=NF_INQ_VARID(infid,"time_counter",timid) |
---|
131 | if (ierr.ne.NF_NOERR) then |
---|
132 | stop "Error: Failed to get time_counter ID" |
---|
133 | else |
---|
134 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,timelength) |
---|
135 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get time_counter length" |
---|
136 | endif |
---|
137 | endif |
---|
138 | else |
---|
139 | ierr=NF_INQ_VARID(infid,"time",timid) |
---|
140 | if (ierr.ne.NF_NOERR) then |
---|
141 | stop "Error: Failed to get time ID" |
---|
142 | else |
---|
143 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,timelength) |
---|
144 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get time length" |
---|
145 | endif |
---|
146 | endif |
---|
147 | else |
---|
148 | ierr=NF_INQ_VARID(infid,"Time",timid) |
---|
149 | if (ierr.ne.NF_NOERR) then |
---|
150 | stop "Error: Failed to get Time ID" |
---|
151 | else |
---|
152 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,timelength) |
---|
153 | if (ierr.ne.NF_NOERR) stop "Error: Failed to get Time length" |
---|
154 | endif |
---|
155 | endif |
---|
156 | |
---|
157 | return |
---|
158 | end |
---|
159 | |
---|
160 | !=========================================================================== |
---|
161 | |
---|
162 | subroutine get_var2d(infid,dim1,dim2,text,var,ierr1,ierr2) |
---|
163 | |
---|
164 | implicit none |
---|
165 | |
---|
166 | include "netcdf.inc" ! NetCDF definitions |
---|
167 | |
---|
168 | ! arguments |
---|
169 | integer :: infid ! NetCDF input file ID |
---|
170 | integer :: dim1,dim2 ! dim length of the 3D variable |
---|
171 | character (len=64) :: text ! name of variable to read |
---|
172 | real,dimension(dim1,dim2) :: var ! variable to read |
---|
173 | integer :: ierr1,ierr2 ! NetCDF routines return code |
---|
174 | |
---|
175 | ! local |
---|
176 | integer tmpvarid ! temporarily store a variable ID |
---|
177 | |
---|
178 | ierr1=NF_INQ_VARID(infid,trim(text),tmpvarid) |
---|
179 | if (ierr1.ne.NF_NOERR) then |
---|
180 | write(*,*) "Could not get ID for ",trim(text) |
---|
181 | else |
---|
182 | write(*,*) "ID ok for ",trim(text) |
---|
183 | ierr2=NF_GET_VAR_REAL(infid,tmpvarid,var) |
---|
184 | endif |
---|
185 | |
---|
186 | return |
---|
187 | end |
---|
188 | |
---|
189 | !=========================================================================== |
---|
190 | |
---|
191 | subroutine get_var3d(infid,dim1,dim2,dim3,text,var,ierr1,ierr2) |
---|
192 | |
---|
193 | implicit none |
---|
194 | |
---|
195 | include "netcdf.inc" ! NetCDF definitions |
---|
196 | |
---|
197 | ! arguments |
---|
198 | integer :: infid ! NetCDF input file ID |
---|
199 | integer :: dim1,dim2,dim3 ! dim length of the 3D variable |
---|
200 | character (len=64) :: text ! name of variable to read |
---|
201 | real,dimension(dim1,dim2,dim3) :: var ! variable to read |
---|
202 | integer :: ierr1,ierr2 ! NetCDF routines return code |
---|
203 | |
---|
204 | ! local |
---|
205 | integer tmpvarid ! temporarily store a variable ID |
---|
206 | |
---|
207 | ierr1=NF_INQ_VARID(infid,trim(text),tmpvarid) |
---|
208 | if (ierr1.ne.NF_NOERR) then |
---|
209 | write(*,*) "Could not get ID for ",trim(text) |
---|
210 | else |
---|
211 | write(*,*) "ID ok for ",trim(text) |
---|
212 | ierr2=NF_GET_VAR_REAL(infid,tmpvarid,var) |
---|
213 | endif |
---|
214 | |
---|
215 | return |
---|
216 | end |
---|
217 | |
---|
218 | !=========================================================================== |
---|
219 | |
---|
220 | subroutine get_var4d(infid,dim1,dim2,dim3,dim4,text,var,ierr1,ierr2) |
---|
221 | |
---|
222 | implicit none |
---|
223 | |
---|
224 | include "netcdf.inc" ! NetCDF definitions |
---|
225 | |
---|
226 | ! arguments |
---|
227 | integer :: infid ! NetCDF input file ID |
---|
228 | integer :: dim1,dim2,dim3,dim4 ! dim length of the 4D variable |
---|
229 | character (len=64) :: text ! name of variable to read |
---|
230 | real,dimension(dim1,dim2,dim3,dim4) :: var ! variable to read |
---|
231 | integer :: ierr1,ierr2 ! NetCDF routines return code |
---|
232 | |
---|
233 | ! local |
---|
234 | integer tmpvarid ! temporarily store a variable ID |
---|
235 | |
---|
236 | ierr1=NF_INQ_VARID(infid,trim(text),tmpvarid) |
---|
237 | if (ierr1.ne.NF_NOERR) then |
---|
238 | write(*,*) "Could not get ID for ",trim(text) |
---|
239 | else |
---|
240 | write(*,*) "ID ok for ",trim(text) |
---|
241 | ierr2=NF_GET_VAR_REAL(infid,tmpvarid,var) |
---|
242 | endif |
---|
243 | |
---|
244 | return |
---|
245 | end |
---|
246 | |
---|
247 | !=========================================================================== |
---|
248 | |
---|
249 | subroutine write_dim(outfid,dim1,dim2,dim3,dim4,lon,lat,plev,time,& |
---|
250 | lon_dimid,lat_dimid,alt_dimid,time_dimid) |
---|
251 | |
---|
252 | implicit none |
---|
253 | |
---|
254 | include "netcdf.inc" ! NetCDF definitions |
---|
255 | |
---|
256 | ! arguments |
---|
257 | integer :: outfid ! NetCDF output file ID |
---|
258 | integer :: dim1,dim2,dim3,dim4 ! dim length of the 4D variable |
---|
259 | real,dimension(dim1) :: lon ! longitude |
---|
260 | real,dimension(dim2) :: lat ! latitude |
---|
261 | real,dimension(dim3) :: plev ! Pressure levels (Pa) |
---|
262 | real,dimension(dim4) :: time ! time |
---|
263 | integer lon_dimid,lat_dimid,alt_dimid,time_dimid ! NetCDF dimension IDs |
---|
264 | |
---|
265 | ! local |
---|
266 | integer lon_varid,lat_varid,alt_varid,time_varid |
---|
267 | character (len=64) :: text ! to store some text |
---|
268 | integer ierr ! NetCDF routines return code |
---|
269 | |
---|
270 | |
---|
271 | !------------ |
---|
272 | ! longitude |
---|
273 | !------------ |
---|
274 | |
---|
275 | ierr=NF_DEF_DIM(outfid,"longitude",dim1,lon_dimid) |
---|
276 | if (ierr.ne.NF_NOERR) stop "Error: Could not define longitude dimension" |
---|
277 | |
---|
278 | ierr=NF_DEF_VAR(outfid,"longitude",NF_REAL,1,lon_dimid,lon_varid) |
---|
279 | if (ierr.ne.NF_NOERR) stop "Error: Could not define longitude variable" |
---|
280 | |
---|
281 | ! longitude attributes |
---|
282 | text='east longitude' |
---|
283 | ierr=NF_PUT_ATT_TEXT(outfid,lon_varid,'long_name',len_trim(text),text) |
---|
284 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for longitude" |
---|
285 | |
---|
286 | text='degrees_east' |
---|
287 | ierr=NF_PUT_ATT_TEXT(outfid,lon_varid,'units',len_trim(text),text) |
---|
288 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for longitude" |
---|
289 | |
---|
290 | !------------ |
---|
291 | ! latitude |
---|
292 | !------------ |
---|
293 | |
---|
294 | ierr=NF_DEF_DIM(outfid,"latitude",dim2,lat_dimid) |
---|
295 | if (ierr.ne.NF_NOERR) stop "Error: Could not define latitude dimension" |
---|
296 | |
---|
297 | ierr=NF_DEF_VAR(outfid,"latitude",NF_REAL,1,lat_dimid,lat_varid) |
---|
298 | if (ierr.ne.NF_NOERR) stop "Error: Could not define latitude variable" |
---|
299 | |
---|
300 | ! latitude attributes |
---|
301 | text='north latitude' |
---|
302 | ierr=NF_PUT_ATT_TEXT(outfid,lat_varid,'long_name',len_trim(text),text) |
---|
303 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for latitude" |
---|
304 | |
---|
305 | text='degrees_north' |
---|
306 | ierr=NF_PUT_ATT_TEXT(outfid,lat_varid,'units',len_trim(text),text) |
---|
307 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for latitude" |
---|
308 | |
---|
309 | !------------ |
---|
310 | ! pressure |
---|
311 | !------------ |
---|
312 | |
---|
313 | ierr=NF_DEF_DIM(outfid,"presnivs",dim3,alt_dimid) |
---|
314 | if (ierr.ne.NF_NOERR) stop "Error: Could not define presnivs dimension" |
---|
315 | |
---|
316 | ierr=NF_DEF_VAR(outfid,"presnivs",NF_REAL,1,alt_dimid,alt_varid) |
---|
317 | if (ierr.ne.NF_NOERR) stop "Error: Could not define presnivs variable" |
---|
318 | |
---|
319 | !presnivs attributes |
---|
320 | text='Pressure levels' |
---|
321 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'long_name',len_trim(text),text) |
---|
322 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for presnivs (p levels)" |
---|
323 | |
---|
324 | text='Pa' |
---|
325 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'units',len_trim(text),text) |
---|
326 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for presnivs (p levels)" |
---|
327 | |
---|
328 | text='down' |
---|
329 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'positive',len_trim(text),text) |
---|
330 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing positive for presnivs (p levels)" |
---|
331 | |
---|
332 | !------------ |
---|
333 | ! time |
---|
334 | !------------ |
---|
335 | |
---|
336 | ierr=NF_DEF_DIM(outfid,"Time",dim4,time_dimid) |
---|
337 | if (ierr.ne.NF_NOERR) stop "Error: Could not define time dimension" |
---|
338 | |
---|
339 | ierr=NF_DEF_VAR(outfid,"Time",NF_REAL,1,time_dimid,time_varid) |
---|
340 | if (ierr.ne.NF_NOERR) stop "Error: Could not define Time variable" |
---|
341 | |
---|
342 | ! time attributes |
---|
343 | text='Time' |
---|
344 | ierr=NF_PUT_ATT_TEXT(outfid,time_varid,'long_name',len_trim(text),text) |
---|
345 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for Time" |
---|
346 | |
---|
347 | text='days since 0000-01-1 00:00:00' |
---|
348 | ierr=NF_PUT_ATT_TEXT(outfid,time_varid,'units',len_trim(text),text) |
---|
349 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for Time" |
---|
350 | |
---|
351 | print*,"End of dim definitions" |
---|
352 | |
---|
353 | !------------ |
---|
354 | ! Switch out of NetCDF define mode |
---|
355 | !------------ |
---|
356 | ierr=NF_ENDDEF(outfid) |
---|
357 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
358 | |
---|
359 | ! Write longitude |
---|
360 | ierr=NF_PUT_VAR_REAL(outfid,lon_varid,lon) |
---|
361 | if (ierr.ne.NF_NOERR) stop "Error: Could not write longitude data to output file" |
---|
362 | |
---|
363 | ! Write latitude |
---|
364 | ierr=NF_PUT_VAR_REAL(outfid,lat_varid,lat) |
---|
365 | if (ierr.ne.NF_NOERR) stop "Error: Could not write latitude data to output file" |
---|
366 | |
---|
367 | ! Write pressure |
---|
368 | ierr=NF_PUT_VAR_REAL(outfid,alt_varid,plev) |
---|
369 | if (ierr.ne.NF_NOERR) stop "Error: Could not write presnivs data to output file" |
---|
370 | |
---|
371 | ! Write time |
---|
372 | ierr=NF_PUT_VAR_REAL(outfid,time_varid,time) |
---|
373 | if (ierr.ne.NF_NOERR) stop "Error: Could not write Time data to output file" |
---|
374 | |
---|
375 | print*,"Writing dim OK" |
---|
376 | |
---|
377 | return |
---|
378 | end |
---|
379 | |
---|
380 | |
---|
381 | !=========================================================================== |
---|
382 | |
---|
383 | subroutine write_var1d(outfid,datashape,dim1,& |
---|
384 | name,lgname,units,miss_val,var) |
---|
385 | |
---|
386 | implicit none |
---|
387 | |
---|
388 | include "netcdf.inc" ! NetCDF definitions |
---|
389 | |
---|
390 | ! arguments |
---|
391 | integer :: outfid ! NetCDF output file ID |
---|
392 | integer :: dim1 ! dim length of the 1D variable |
---|
393 | integer :: datashape ! shape of datasets |
---|
394 | character (len=10) :: name ! name of variable |
---|
395 | character (len=20) :: lgname ! long name of variable |
---|
396 | character (len=10) :: units ! unit of variable |
---|
397 | real :: miss_val ! "missing value" to specify missing data |
---|
398 | real,dimension(dim1) :: var ! variable to read |
---|
399 | |
---|
400 | ! local |
---|
401 | character (len=64) :: text ! to store some text |
---|
402 | integer ierr ! NetCDF routines return code |
---|
403 | integer varid |
---|
404 | |
---|
405 | !------------ |
---|
406 | ! Define variable |
---|
407 | !------------ |
---|
408 | |
---|
409 | ierr=NF_REDEF(outfid) |
---|
410 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
411 | |
---|
412 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,1,datashape,varid) |
---|
413 | if (ierr.ne.NF_NOERR) then |
---|
414 | write(*,*) "Error: Could not define variable : ",name |
---|
415 | stop |
---|
416 | endif |
---|
417 | |
---|
418 | ! attributes |
---|
419 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
420 | if (ierr.ne.NF_NOERR) then |
---|
421 | write(*,*) "Error: Problem writing long_name for ",name |
---|
422 | stop |
---|
423 | endif |
---|
424 | |
---|
425 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
426 | if (ierr.ne.NF_NOERR) then |
---|
427 | write(*,*) "Error: Problem writing units for ",name |
---|
428 | stop |
---|
429 | endif |
---|
430 | |
---|
431 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
432 | if (ierr.ne.NF_NOERR) then |
---|
433 | write(*,*) "Error: failed to write missing_value for ",name |
---|
434 | stop |
---|
435 | endif |
---|
436 | |
---|
437 | print*,"End of var def : ",name |
---|
438 | |
---|
439 | !------------ |
---|
440 | ! Switch out of NetCDF define mode |
---|
441 | !------------ |
---|
442 | ierr=NF_ENDDEF(outfid) |
---|
443 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
444 | |
---|
445 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
446 | if (ierr.ne.NF_NOERR) then |
---|
447 | write(*,*) "Error: Could not write var ",name |
---|
448 | stop |
---|
449 | endif |
---|
450 | |
---|
451 | print*,"Writing var OK : ",name |
---|
452 | |
---|
453 | return |
---|
454 | end |
---|
455 | |
---|
456 | !=========================================================================== |
---|
457 | |
---|
458 | subroutine write_var2d(outfid,datashape,dim1,dim2,& |
---|
459 | name,lgname,units,miss_val,var) |
---|
460 | |
---|
461 | implicit none |
---|
462 | |
---|
463 | include "netcdf.inc" ! NetCDF definitions |
---|
464 | |
---|
465 | ! arguments |
---|
466 | integer :: outfid ! NetCDF output file ID |
---|
467 | integer :: dim1,dim2 ! dim length of the 2D variable |
---|
468 | integer,dimension(2) :: datashape ! shape of datasets |
---|
469 | character (len=10) :: name ! name of variable |
---|
470 | character (len=20) :: lgname ! long name of variable |
---|
471 | character (len=10) :: units ! unit of variable |
---|
472 | real :: miss_val ! "missing value" to specify missing data |
---|
473 | real,dimension(dim1,dim2) :: var ! variable to read |
---|
474 | |
---|
475 | ! local |
---|
476 | character (len=64) :: text ! to store some text |
---|
477 | integer ierr ! NetCDF routines return code |
---|
478 | integer varid |
---|
479 | |
---|
480 | !------------ |
---|
481 | ! Define variable |
---|
482 | !------------ |
---|
483 | |
---|
484 | ierr=NF_REDEF(outfid) |
---|
485 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
486 | |
---|
487 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,2,datashape,varid) |
---|
488 | if (ierr.ne.NF_NOERR) then |
---|
489 | write(*,*) "Error: Could not define variable : ",name |
---|
490 | stop |
---|
491 | endif |
---|
492 | |
---|
493 | ! attributes |
---|
494 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
495 | if (ierr.ne.NF_NOERR) then |
---|
496 | write(*,*) "Error: Problem writing long_name for ",name |
---|
497 | stop |
---|
498 | endif |
---|
499 | |
---|
500 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
501 | if (ierr.ne.NF_NOERR) then |
---|
502 | write(*,*) "Error: Problem writing units for ",name |
---|
503 | stop |
---|
504 | endif |
---|
505 | |
---|
506 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
507 | if (ierr.ne.NF_NOERR) then |
---|
508 | write(*,*) "Error: failed to write missing_value for ",name |
---|
509 | stop |
---|
510 | endif |
---|
511 | |
---|
512 | print*,"End of var def : ",name |
---|
513 | |
---|
514 | !------------ |
---|
515 | ! Switch out of NetCDF define mode |
---|
516 | !------------ |
---|
517 | ierr=NF_ENDDEF(outfid) |
---|
518 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
519 | |
---|
520 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
521 | if (ierr.ne.NF_NOERR) then |
---|
522 | write(*,*) "Error: Could not write var ",name |
---|
523 | stop |
---|
524 | endif |
---|
525 | |
---|
526 | print*,"Writing var OK : ",name |
---|
527 | |
---|
528 | return |
---|
529 | end |
---|
530 | |
---|
531 | !=========================================================================== |
---|
532 | |
---|
533 | subroutine write_var3d(outfid,datashape,dim1,dim2,dim3,& |
---|
534 | name,lgname,units,miss_val,var) |
---|
535 | |
---|
536 | implicit none |
---|
537 | |
---|
538 | include "netcdf.inc" ! NetCDF definitions |
---|
539 | |
---|
540 | ! arguments |
---|
541 | integer :: outfid ! NetCDF output file ID |
---|
542 | integer :: dim1,dim2,dim3 ! dim length of the 3D variable |
---|
543 | integer,dimension(3) :: datashape ! shape of datasets |
---|
544 | character (len=10) :: name ! name of variable |
---|
545 | character (len=20) :: lgname ! long name of variable |
---|
546 | character (len=10) :: units ! unit of variable |
---|
547 | real :: miss_val ! "missing value" to specify missing data |
---|
548 | real,dimension(dim1,dim2,dim3) :: var ! variable to read |
---|
549 | |
---|
550 | ! local |
---|
551 | character (len=64) :: text ! to store some text |
---|
552 | integer ierr ! NetCDF routines return code |
---|
553 | integer varid |
---|
554 | |
---|
555 | !------------ |
---|
556 | ! Define variable |
---|
557 | !------------ |
---|
558 | |
---|
559 | ierr=NF_REDEF(outfid) |
---|
560 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
561 | |
---|
562 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,3,datashape,varid) |
---|
563 | if (ierr.ne.NF_NOERR) then |
---|
564 | write(*,*) "Error: Could not define variable : ",name |
---|
565 | stop |
---|
566 | endif |
---|
567 | |
---|
568 | ! attributes |
---|
569 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
570 | if (ierr.ne.NF_NOERR) then |
---|
571 | write(*,*) "Error: Problem writing long_name for ",name |
---|
572 | stop |
---|
573 | endif |
---|
574 | |
---|
575 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
576 | if (ierr.ne.NF_NOERR) then |
---|
577 | write(*,*) "Error: Problem writing units for ",name |
---|
578 | stop |
---|
579 | endif |
---|
580 | |
---|
581 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
582 | if (ierr.ne.NF_NOERR) then |
---|
583 | write(*,*) "Error: failed to write missing_value for ",name |
---|
584 | stop |
---|
585 | endif |
---|
586 | |
---|
587 | print*,"End of var def : ",name |
---|
588 | |
---|
589 | !------------ |
---|
590 | ! Switch out of NetCDF define mode |
---|
591 | !------------ |
---|
592 | ierr=NF_ENDDEF(outfid) |
---|
593 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
594 | |
---|
595 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
596 | if (ierr.ne.NF_NOERR) then |
---|
597 | write(*,*) "Error: Could not write var ",name |
---|
598 | stop |
---|
599 | endif |
---|
600 | |
---|
601 | print*,"Writing var OK : ",name |
---|
602 | |
---|
603 | return |
---|
604 | end |
---|
605 | |
---|
606 | !=========================================================================== |
---|
607 | |
---|
608 | subroutine write_var4d(outfid,datashape,dim1,dim2,dim3,dim4,& |
---|
609 | name,lgname,units,miss_val,var) |
---|
610 | |
---|
611 | implicit none |
---|
612 | |
---|
613 | include "netcdf.inc" ! NetCDF definitions |
---|
614 | |
---|
615 | ! arguments |
---|
616 | integer :: outfid ! NetCDF output file ID |
---|
617 | integer :: dim1,dim2,dim3,dim4 ! dim length of the 4D variable |
---|
618 | integer,dimension(4) :: datashape ! shape of datasets |
---|
619 | character (len=10) :: name ! name of variable |
---|
620 | character (len=20) :: lgname ! long name of variable |
---|
621 | character (len=10) :: units ! unit of variable |
---|
622 | real :: miss_val ! "missing value" to specify missing data |
---|
623 | real,dimension(dim1,dim2,dim3,dim4) :: var ! variable to read |
---|
624 | |
---|
625 | ! local |
---|
626 | character (len=64) :: text ! to store some text |
---|
627 | integer ierr ! NetCDF routines return code |
---|
628 | integer varid |
---|
629 | |
---|
630 | !------------ |
---|
631 | ! Define variable |
---|
632 | !------------ |
---|
633 | |
---|
634 | ierr=NF_REDEF(outfid) |
---|
635 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
636 | |
---|
637 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,4,datashape,varid) |
---|
638 | if (ierr.ne.NF_NOERR) then |
---|
639 | write(*,*) "Error: Could not define variable : ",name |
---|
640 | stop |
---|
641 | endif |
---|
642 | |
---|
643 | ! attributes |
---|
644 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
645 | if (ierr.ne.NF_NOERR) then |
---|
646 | write(*,*) "Error: Problem writing long_name for ",name |
---|
647 | stop |
---|
648 | endif |
---|
649 | |
---|
650 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
651 | if (ierr.ne.NF_NOERR) then |
---|
652 | write(*,*) "Error: Problem writing units for ",name |
---|
653 | stop |
---|
654 | endif |
---|
655 | |
---|
656 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
657 | if (ierr.ne.NF_NOERR) then |
---|
658 | write(*,*) "Error: failed to write missing_value for ",name |
---|
659 | stop |
---|
660 | endif |
---|
661 | |
---|
662 | print*,"End of var def : ",name |
---|
663 | |
---|
664 | !------------ |
---|
665 | ! Switch out of NetCDF define mode |
---|
666 | !------------ |
---|
667 | ierr=NF_ENDDEF(outfid) |
---|
668 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
669 | |
---|
670 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
671 | if (ierr.ne.NF_NOERR) then |
---|
672 | write(*,*) "Error: Could not write var ",name |
---|
673 | stop |
---|
674 | endif |
---|
675 | |
---|
676 | print*,"Writing var OK : ",name |
---|
677 | |
---|
678 | return |
---|
679 | end |
---|
680 | |
---|
681 | |
---|