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,missing,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 | real :: missing ! missing value |
---|
232 | integer :: ierr1,ierr2,miss ! NetCDF routines return code |
---|
233 | |
---|
234 | ! local |
---|
235 | integer tmpvarid ! temporarily store a variable ID |
---|
236 | |
---|
237 | ierr1=NF_INQ_VARID(infid,trim(text),tmpvarid) |
---|
238 | if (ierr1.ne.NF_NOERR) then |
---|
239 | write(*,*) "Could not get ID for ",trim(text) |
---|
240 | else |
---|
241 | write(*,*) "ID ok for ",trim(text) |
---|
242 | ierr2=NF_GET_VAR_REAL(infid,tmpvarid,var) |
---|
243 | miss=NF_GET_ATT_REAL(infid,tmpvarid,"missing_value",missing) |
---|
244 | endif |
---|
245 | |
---|
246 | return |
---|
247 | end |
---|
248 | |
---|
249 | !=========================================================================== |
---|
250 | |
---|
251 | subroutine write_dim(outfid,dim1,dim2,dim3,dim4,lon,lat,plev,time,& |
---|
252 | lon_dimid,lat_dimid,alt_dimid,time_dimid) |
---|
253 | |
---|
254 | implicit none |
---|
255 | |
---|
256 | include "netcdf.inc" ! NetCDF definitions |
---|
257 | |
---|
258 | ! arguments |
---|
259 | integer :: outfid ! NetCDF output file ID |
---|
260 | integer :: dim1,dim2,dim3,dim4 ! dim length of the 4D variable |
---|
261 | real,dimension(dim1) :: lon ! longitude |
---|
262 | real,dimension(dim2) :: lat ! latitude |
---|
263 | real,dimension(dim3) :: plev ! Pressure levels (Pa) |
---|
264 | real,dimension(dim4) :: time ! time |
---|
265 | integer lon_dimid,lat_dimid,alt_dimid,time_dimid ! NetCDF dimension IDs |
---|
266 | |
---|
267 | ! local |
---|
268 | integer lon_varid,lat_varid,alt_varid,time_varid |
---|
269 | character (len=64) :: text ! to store some text |
---|
270 | integer ierr ! NetCDF routines return code |
---|
271 | |
---|
272 | |
---|
273 | !------------ |
---|
274 | ! longitude |
---|
275 | !------------ |
---|
276 | |
---|
277 | ierr=NF_DEF_DIM(outfid,"longitude",dim1,lon_dimid) |
---|
278 | if (ierr.ne.NF_NOERR) stop "Error: Could not define longitude dimension" |
---|
279 | |
---|
280 | ierr=NF_DEF_VAR(outfid,"longitude",NF_REAL,1,lon_dimid,lon_varid) |
---|
281 | if (ierr.ne.NF_NOERR) stop "Error: Could not define longitude variable" |
---|
282 | |
---|
283 | ! longitude attributes |
---|
284 | text='east longitude' |
---|
285 | ierr=NF_PUT_ATT_TEXT(outfid,lon_varid,'long_name',len_trim(text),text) |
---|
286 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for longitude" |
---|
287 | |
---|
288 | text='degrees_east' |
---|
289 | ierr=NF_PUT_ATT_TEXT(outfid,lon_varid,'units',len_trim(text),text) |
---|
290 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for longitude" |
---|
291 | |
---|
292 | !------------ |
---|
293 | ! latitude |
---|
294 | !------------ |
---|
295 | |
---|
296 | ierr=NF_DEF_DIM(outfid,"latitude",dim2,lat_dimid) |
---|
297 | if (ierr.ne.NF_NOERR) stop "Error: Could not define latitude dimension" |
---|
298 | |
---|
299 | ierr=NF_DEF_VAR(outfid,"latitude",NF_REAL,1,lat_dimid,lat_varid) |
---|
300 | if (ierr.ne.NF_NOERR) stop "Error: Could not define latitude variable" |
---|
301 | |
---|
302 | ! latitude attributes |
---|
303 | text='north latitude' |
---|
304 | ierr=NF_PUT_ATT_TEXT(outfid,lat_varid,'long_name',len_trim(text),text) |
---|
305 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for latitude" |
---|
306 | |
---|
307 | text='degrees_north' |
---|
308 | ierr=NF_PUT_ATT_TEXT(outfid,lat_varid,'units',len_trim(text),text) |
---|
309 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for latitude" |
---|
310 | |
---|
311 | !------------ |
---|
312 | ! pressure |
---|
313 | !------------ |
---|
314 | |
---|
315 | ierr=NF_DEF_DIM(outfid,"presnivs",dim3,alt_dimid) |
---|
316 | if (ierr.ne.NF_NOERR) stop "Error: Could not define presnivs dimension" |
---|
317 | |
---|
318 | ierr=NF_DEF_VAR(outfid,"presnivs",NF_REAL,1,alt_dimid,alt_varid) |
---|
319 | if (ierr.ne.NF_NOERR) stop "Error: Could not define presnivs variable" |
---|
320 | |
---|
321 | !presnivs attributes |
---|
322 | text='Pressure levels' |
---|
323 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'long_name',len_trim(text),text) |
---|
324 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for presnivs (p levels)" |
---|
325 | |
---|
326 | text='Pa' |
---|
327 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'units',len_trim(text),text) |
---|
328 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for presnivs (p levels)" |
---|
329 | |
---|
330 | text='down' |
---|
331 | ierr=NF_PUT_ATT_TEXT(outfid,alt_varid,'positive',len_trim(text),text) |
---|
332 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing positive for presnivs (p levels)" |
---|
333 | |
---|
334 | !------------ |
---|
335 | ! time |
---|
336 | !------------ |
---|
337 | |
---|
338 | ierr=NF_DEF_DIM(outfid,"Time",dim4,time_dimid) |
---|
339 | if (ierr.ne.NF_NOERR) stop "Error: Could not define time dimension" |
---|
340 | |
---|
341 | ierr=NF_DEF_VAR(outfid,"Time",NF_REAL,1,time_dimid,time_varid) |
---|
342 | if (ierr.ne.NF_NOERR) stop "Error: Could not define Time variable" |
---|
343 | |
---|
344 | ! time attributes |
---|
345 | text='Time' |
---|
346 | ierr=NF_PUT_ATT_TEXT(outfid,time_varid,'long_name',len_trim(text),text) |
---|
347 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing long_name for Time" |
---|
348 | |
---|
349 | text='days since 0000-01-1 00:00:00' |
---|
350 | ierr=NF_PUT_ATT_TEXT(outfid,time_varid,'units',len_trim(text),text) |
---|
351 | if (ierr.ne.NF_NOERR) stop "Error: Problem writing units for Time" |
---|
352 | |
---|
353 | print*,"End of dim definitions" |
---|
354 | |
---|
355 | !------------ |
---|
356 | ! Switch out of NetCDF define mode |
---|
357 | !------------ |
---|
358 | ierr=NF_ENDDEF(outfid) |
---|
359 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
360 | |
---|
361 | ! Write longitude |
---|
362 | ierr=NF_PUT_VAR_REAL(outfid,lon_varid,lon) |
---|
363 | if (ierr.ne.NF_NOERR) stop "Error: Could not write longitude data to output file" |
---|
364 | |
---|
365 | ! Write latitude |
---|
366 | ierr=NF_PUT_VAR_REAL(outfid,lat_varid,lat) |
---|
367 | if (ierr.ne.NF_NOERR) stop "Error: Could not write latitude data to output file" |
---|
368 | |
---|
369 | ! Write pressure |
---|
370 | ierr=NF_PUT_VAR_REAL(outfid,alt_varid,plev) |
---|
371 | if (ierr.ne.NF_NOERR) stop "Error: Could not write presnivs data to output file" |
---|
372 | |
---|
373 | ! Write time |
---|
374 | ierr=NF_PUT_VAR_REAL(outfid,time_varid,time) |
---|
375 | if (ierr.ne.NF_NOERR) stop "Error: Could not write Time data to output file" |
---|
376 | |
---|
377 | print*,"Writing dim OK" |
---|
378 | |
---|
379 | return |
---|
380 | end |
---|
381 | |
---|
382 | |
---|
383 | !=========================================================================== |
---|
384 | |
---|
385 | subroutine write_var1d(outfid,datashape,dim1,& |
---|
386 | name,lgname,units,miss_val,var) |
---|
387 | |
---|
388 | implicit none |
---|
389 | |
---|
390 | include "netcdf.inc" ! NetCDF definitions |
---|
391 | |
---|
392 | ! arguments |
---|
393 | integer :: outfid ! NetCDF output file ID |
---|
394 | integer :: dim1 ! dim length of the 1D variable |
---|
395 | integer :: datashape ! shape of datasets |
---|
396 | character (len=10) :: name ! name of variable |
---|
397 | character (len=20) :: lgname ! long name of variable |
---|
398 | character (len=10) :: units ! unit of variable |
---|
399 | real :: miss_val ! "missing value" to specify missing data |
---|
400 | real,dimension(dim1) :: var ! variable to read |
---|
401 | |
---|
402 | ! local |
---|
403 | character (len=64) :: text ! to store some text |
---|
404 | integer ierr ! NetCDF routines return code |
---|
405 | integer varid |
---|
406 | |
---|
407 | !------------ |
---|
408 | ! Define variable |
---|
409 | !------------ |
---|
410 | |
---|
411 | ierr=NF_REDEF(outfid) |
---|
412 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
413 | |
---|
414 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,1,datashape,varid) |
---|
415 | if (ierr.ne.NF_NOERR) then |
---|
416 | write(*,*) "Error: Could not define variable : ",name |
---|
417 | stop |
---|
418 | endif |
---|
419 | |
---|
420 | ! attributes |
---|
421 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
422 | if (ierr.ne.NF_NOERR) then |
---|
423 | write(*,*) "Error: Problem writing long_name for ",name |
---|
424 | stop |
---|
425 | endif |
---|
426 | |
---|
427 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
428 | if (ierr.ne.NF_NOERR) then |
---|
429 | write(*,*) "Error: Problem writing units for ",name |
---|
430 | stop |
---|
431 | endif |
---|
432 | |
---|
433 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
434 | if (ierr.ne.NF_NOERR) then |
---|
435 | write(*,*) "Error: failed to write missing_value for ",name |
---|
436 | stop |
---|
437 | endif |
---|
438 | |
---|
439 | print*,"End of var def : ",name |
---|
440 | |
---|
441 | !------------ |
---|
442 | ! Switch out of NetCDF define mode |
---|
443 | !------------ |
---|
444 | ierr=NF_ENDDEF(outfid) |
---|
445 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
446 | |
---|
447 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
448 | if (ierr.ne.NF_NOERR) then |
---|
449 | write(*,*) "Error: Could not write var ",name |
---|
450 | stop |
---|
451 | endif |
---|
452 | |
---|
453 | print*,"Writing var OK : ",name |
---|
454 | |
---|
455 | return |
---|
456 | end |
---|
457 | |
---|
458 | !=========================================================================== |
---|
459 | |
---|
460 | subroutine write_var2d(outfid,datashape,dim1,dim2,& |
---|
461 | name,lgname,units,miss_val,var) |
---|
462 | |
---|
463 | implicit none |
---|
464 | |
---|
465 | include "netcdf.inc" ! NetCDF definitions |
---|
466 | |
---|
467 | ! arguments |
---|
468 | integer :: outfid ! NetCDF output file ID |
---|
469 | integer :: dim1,dim2 ! dim length of the 2D variable |
---|
470 | integer,dimension(2) :: datashape ! shape of datasets |
---|
471 | character (len=10) :: name ! name of variable |
---|
472 | character (len=20) :: lgname ! long name of variable |
---|
473 | character (len=10) :: units ! unit of variable |
---|
474 | real :: miss_val ! "missing value" to specify missing data |
---|
475 | real,dimension(dim1,dim2) :: var ! variable to read |
---|
476 | |
---|
477 | ! local |
---|
478 | character (len=64) :: text ! to store some text |
---|
479 | integer ierr ! NetCDF routines return code |
---|
480 | integer varid |
---|
481 | |
---|
482 | !------------ |
---|
483 | ! Define variable |
---|
484 | !------------ |
---|
485 | |
---|
486 | ierr=NF_REDEF(outfid) |
---|
487 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
488 | |
---|
489 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,2,datashape,varid) |
---|
490 | if (ierr.ne.NF_NOERR) then |
---|
491 | write(*,*) "Error: Could not define variable : ",name |
---|
492 | stop |
---|
493 | endif |
---|
494 | |
---|
495 | ! attributes |
---|
496 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
497 | if (ierr.ne.NF_NOERR) then |
---|
498 | write(*,*) "Error: Problem writing long_name for ",name |
---|
499 | stop |
---|
500 | endif |
---|
501 | |
---|
502 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
503 | if (ierr.ne.NF_NOERR) then |
---|
504 | write(*,*) "Error: Problem writing units for ",name |
---|
505 | stop |
---|
506 | endif |
---|
507 | |
---|
508 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
509 | if (ierr.ne.NF_NOERR) then |
---|
510 | write(*,*) "Error: failed to write missing_value for ",name |
---|
511 | stop |
---|
512 | endif |
---|
513 | |
---|
514 | print*,"End of var def : ",name |
---|
515 | |
---|
516 | !------------ |
---|
517 | ! Switch out of NetCDF define mode |
---|
518 | !------------ |
---|
519 | ierr=NF_ENDDEF(outfid) |
---|
520 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
521 | |
---|
522 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
523 | if (ierr.ne.NF_NOERR) then |
---|
524 | write(*,*) "Error: Could not write var ",name |
---|
525 | stop |
---|
526 | endif |
---|
527 | |
---|
528 | print*,"Writing var OK : ",name |
---|
529 | |
---|
530 | return |
---|
531 | end |
---|
532 | |
---|
533 | !=========================================================================== |
---|
534 | |
---|
535 | subroutine write_var3d(outfid,datashape,dim1,dim2,dim3,& |
---|
536 | name,lgname,units,miss_val,var) |
---|
537 | |
---|
538 | implicit none |
---|
539 | |
---|
540 | include "netcdf.inc" ! NetCDF definitions |
---|
541 | |
---|
542 | ! arguments |
---|
543 | integer :: outfid ! NetCDF output file ID |
---|
544 | integer :: dim1,dim2,dim3 ! dim length of the 3D variable |
---|
545 | integer,dimension(3) :: datashape ! shape of datasets |
---|
546 | character (len=10) :: name ! name of variable |
---|
547 | character (len=20) :: lgname ! long name of variable |
---|
548 | character (len=10) :: units ! unit of variable |
---|
549 | real :: miss_val ! "missing value" to specify missing data |
---|
550 | real,dimension(dim1,dim2,dim3) :: var ! variable to read |
---|
551 | |
---|
552 | ! local |
---|
553 | character (len=64) :: text ! to store some text |
---|
554 | integer ierr ! NetCDF routines return code |
---|
555 | integer varid |
---|
556 | |
---|
557 | !------------ |
---|
558 | ! Define variable |
---|
559 | !------------ |
---|
560 | |
---|
561 | ierr=NF_REDEF(outfid) |
---|
562 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
563 | |
---|
564 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,3,datashape,varid) |
---|
565 | if (ierr.ne.NF_NOERR) then |
---|
566 | write(*,*) "Error: Could not define variable : ",name |
---|
567 | stop |
---|
568 | endif |
---|
569 | |
---|
570 | ! attributes |
---|
571 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
572 | if (ierr.ne.NF_NOERR) then |
---|
573 | write(*,*) "Error: Problem writing long_name for ",name |
---|
574 | stop |
---|
575 | endif |
---|
576 | |
---|
577 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
578 | if (ierr.ne.NF_NOERR) then |
---|
579 | write(*,*) "Error: Problem writing units for ",name |
---|
580 | stop |
---|
581 | endif |
---|
582 | |
---|
583 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
584 | if (ierr.ne.NF_NOERR) then |
---|
585 | write(*,*) "Error: failed to write missing_value for ",name |
---|
586 | stop |
---|
587 | endif |
---|
588 | |
---|
589 | print*,"End of var def : ",name |
---|
590 | |
---|
591 | !------------ |
---|
592 | ! Switch out of NetCDF define mode |
---|
593 | !------------ |
---|
594 | ierr=NF_ENDDEF(outfid) |
---|
595 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
596 | |
---|
597 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
598 | if (ierr.ne.NF_NOERR) then |
---|
599 | write(*,*) "Error: Could not write var ",name |
---|
600 | stop |
---|
601 | endif |
---|
602 | |
---|
603 | print*,"Writing var OK : ",name |
---|
604 | |
---|
605 | return |
---|
606 | end |
---|
607 | |
---|
608 | !=========================================================================== |
---|
609 | |
---|
610 | subroutine write_var4d(outfid,datashape,dim1,dim2,dim3,dim4,& |
---|
611 | name,lgname,units,miss_val,var) |
---|
612 | |
---|
613 | implicit none |
---|
614 | |
---|
615 | include "netcdf.inc" ! NetCDF definitions |
---|
616 | |
---|
617 | ! arguments |
---|
618 | integer :: outfid ! NetCDF output file ID |
---|
619 | integer :: dim1,dim2,dim3,dim4 ! dim length of the 4D variable |
---|
620 | integer,dimension(4) :: datashape ! shape of datasets |
---|
621 | character (len=10) :: name ! name of variable |
---|
622 | character (len=20) :: lgname ! long name of variable |
---|
623 | character (len=10) :: units ! unit of variable |
---|
624 | real :: miss_val ! "missing value" to specify missing data |
---|
625 | real,dimension(dim1,dim2,dim3,dim4) :: var ! variable to read |
---|
626 | |
---|
627 | ! local |
---|
628 | character (len=64) :: text ! to store some text |
---|
629 | integer ierr ! NetCDF routines return code |
---|
630 | integer varid |
---|
631 | |
---|
632 | !------------ |
---|
633 | ! Define variable |
---|
634 | !------------ |
---|
635 | |
---|
636 | ierr=NF_REDEF(outfid) |
---|
637 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch into define mode" |
---|
638 | |
---|
639 | ierr=NF_DEF_VAR(outfid,trim(name),NF_REAL,4,datashape,varid) |
---|
640 | if (ierr.ne.NF_NOERR) then |
---|
641 | write(*,*) "Error: Could not define variable : ",name |
---|
642 | stop |
---|
643 | endif |
---|
644 | |
---|
645 | ! attributes |
---|
646 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'long_name',len_trim(lgname),lgname) |
---|
647 | if (ierr.ne.NF_NOERR) then |
---|
648 | write(*,*) "Error: Problem writing long_name for ",name |
---|
649 | stop |
---|
650 | endif |
---|
651 | |
---|
652 | ierr=NF_PUT_ATT_TEXT(outfid,varid,'units',len_trim(units),units) |
---|
653 | if (ierr.ne.NF_NOERR) then |
---|
654 | write(*,*) "Error: Problem writing units for ",name |
---|
655 | stop |
---|
656 | endif |
---|
657 | |
---|
658 | ierr=NF_PUT_ATT_REAL(outfid,varid,'missing_value',NF_REAL,1,miss_val) |
---|
659 | if (ierr.ne.NF_NOERR) then |
---|
660 | write(*,*) "Error: failed to write missing_value for ",name |
---|
661 | stop |
---|
662 | endif |
---|
663 | |
---|
664 | print*,"End of var def : ",name |
---|
665 | |
---|
666 | !------------ |
---|
667 | ! Switch out of NetCDF define mode |
---|
668 | !------------ |
---|
669 | ierr=NF_ENDDEF(outfid) |
---|
670 | if (ierr.ne.NF_NOERR) stop "Error: Could not switch out of define mode" |
---|
671 | |
---|
672 | ierr=NF_PUT_VAR_REAL(outfid,varid,var) |
---|
673 | if (ierr.ne.NF_NOERR) then |
---|
674 | write(*,*) "Error: Could not write var ",name |
---|
675 | stop |
---|
676 | endif |
---|
677 | |
---|
678 | print*,"Writing var OK : ",name |
---|
679 | |
---|
680 | return |
---|
681 | end |
---|
682 | |
---|
683 | |
---|