1 | program streamfunction |
---|
2 | |
---|
3 | ! ---------------------------------------------------------------------------- |
---|
4 | ! Program to calculate the stream function and the total angular momentum |
---|
5 | ! from stats and diagfi files |
---|
6 | ! input : diagfi.nc / concat.nc / stats.nc kind of files |
---|
7 | ! author: F. Gonzalez-Galindo |
---|
8 | ! ---------------------------------------------------------------------------- |
---|
9 | |
---|
10 | implicit none |
---|
11 | |
---|
12 | include "netcdf.inc" ! NetCDF definitions |
---|
13 | |
---|
14 | character (len=128) :: infile ! input file name (diagfi.nc or stats.nc format) |
---|
15 | character (len=128) :: infile2 ! second input file (may be needed for 'aire' and 'cv') |
---|
16 | character (len=64) :: text ! to store some text |
---|
17 | |
---|
18 | character (len=50), dimension(:), allocatable :: var |
---|
19 | ! var(): name(s) of variable(s) that will be concatenated |
---|
20 | character (len=100) :: filename |
---|
21 | ! filename(): output file name |
---|
22 | integer :: nid,ierr,miss |
---|
23 | ! nid: [netcdf] file ID # |
---|
24 | ! ierr: [netcdf] subroutine returned error code |
---|
25 | ! miss: [netcdf] subroutine returned error code |
---|
26 | integer :: tmpvarid |
---|
27 | ! varid: temporary store a variable ID # |
---|
28 | integer tmpdimid ! temporarily store a dimension ID |
---|
29 | integer infid ! NetCDF input file ID (of diagfi.nc or stats.nc format) |
---|
30 | integer infid2 ! NetCDF input file which contains 'phisini' dataset (diagfi.nc) |
---|
31 | integer nbvarinfile ! # of variables in input file |
---|
32 | real, dimension(:), allocatable:: lat,lon,alt,time |
---|
33 | ! lat(): array, stores latitude coordinates |
---|
34 | ! lon(): array, stores longitude coordinates |
---|
35 | ! alt(): array, stores altitude coordinates |
---|
36 | ! time(): array, stores time coordinates |
---|
37 | integer :: ilat,ilon,ialt,it,i |
---|
38 | ! ilat: index for loops on latitude |
---|
39 | ! ilon: index for loops on longitude |
---|
40 | ! ialt: index for loops on altitude |
---|
41 | ! it: # index for loops on time |
---|
42 | ! i: index for loops |
---|
43 | integer :: nout,latdimout,londimout,altdimout,timedimout,lonvarout,timevarout |
---|
44 | integer :: psivarout,momvarout,uvarout,vvarout,rhovarout,tempvarout |
---|
45 | integer :: psvarout, phisinitvarout |
---|
46 | ! nout: [netcdf] output file ID |
---|
47 | ! latdimout: [netcdf] output latitude (dimension) ID |
---|
48 | ! londimout: [netcdf] output longitude (dimension) ID |
---|
49 | ! altdimout: [netcdf] output altitude (dimension) ID |
---|
50 | ! timedimout: [netcdf] output time (dimension) ID |
---|
51 | ! lonvarout: [netcdf] ID of output "Time" variable |
---|
52 | integer lonlength ! # of grid points along longitude |
---|
53 | integer latlength ! # of grid points along latitude |
---|
54 | integer altlength ! # of grid point along altitude (of input datasets) |
---|
55 | integer timelength ! # of points along time |
---|
56 | real,dimension(:),allocatable :: aps,bps ! hybrid vertical coordinates |
---|
57 | real,dimension(:),allocatable :: sigma ! sigma levels |
---|
58 | real,dimension(:),allocatable :: lon_fake ! Fake longitude to be written |
---|
59 | real,dimension(:,:),allocatable :: aire ! Area of the box |
---|
60 | real,dimension(:,:),allocatable :: cv ! Conversion between natural and covariant v |
---|
61 | real,dimension(:,:),allocatable :: phisinit ! Ground geopotential |
---|
62 | real,dimension(:,:,:),allocatable :: ps ! GCM surface pressure |
---|
63 | real,dimension(:,:,:,:),allocatable :: press ! GCM atmospheric pressure |
---|
64 | real,dimension(:,:,:,:),allocatable :: temp ! GCM atmospheric temperature |
---|
65 | real,dimension(:,:,:,:),allocatable :: u ! GCM zonal wind |
---|
66 | real,dimension(:,:,:,:),allocatable :: v ! GCM meridional wind |
---|
67 | real,dimension(:,:,:,:),allocatable :: rho ! GCM atmospheric density |
---|
68 | real,dimension(:,:,:),allocatable :: vcont ! Covariant meridional wind |
---|
69 | real,dimension(:,:),allocatable :: vcontcum ! Zonal mean covariant meridional wind |
---|
70 | real,dimension(:,:,:),allocatable :: plev ! Pressure from hybrid coordinates |
---|
71 | real,dimension(:,:,:),allocatable :: mass ! Mass in a grid box |
---|
72 | real,dimension(:,:,:),allocatable :: dmass ! Mass variation |
---|
73 | real,dimension(:,:),allocatable :: psi ! Stream function |
---|
74 | real,dimension(:,:,:),allocatable :: mom ! Angular momentum |
---|
75 | real,dimension(:,:),allocatable :: momave ! Zonally averaged angular momentum |
---|
76 | real,dimension(:,:,:),allocatable :: ucum ! Temporally averaged zonal wind |
---|
77 | real,dimension(:,:,:),allocatable :: vcum ! Temporally averaged meridional wind |
---|
78 | real,dimension(:,:,:),allocatable :: rhocum ! Temporally averaged density |
---|
79 | real,dimension(:,:,:),allocatable :: tempcum ! Temporally averaged zonal wind |
---|
80 | real,dimension(:,:),allocatable :: pscum ! Temporally averaged zonal wind |
---|
81 | real,dimension(:,:),allocatable :: uzm ! Zonally averaged zonal wind |
---|
82 | real,dimension(:,:),allocatable :: vzm ! Zonally averaged meridional wind |
---|
83 | real,dimension(:,:),allocatable :: rhozm ! Zonally averaged density |
---|
84 | real,dimension(:,:),allocatable :: tempzm ! Zonally averaged temperature |
---|
85 | real,dimension(:),allocatable :: pszm ! Zonally averaged surface pressure |
---|
86 | real,dimension(:),allocatable :: phisinitzm ! Zonally averaged ground geopotential |
---|
87 | |
---|
88 | !Parameters to calculate the stream function |
---|
89 | real :: g0 ! exact mean gravity at radius=3396.km (Lemoine et al. 2001) |
---|
90 | data g0 /3.7257964/ |
---|
91 | real :: a0 |
---|
92 | data a0 /3396000/ ! radius=3396.km |
---|
93 | real :: daysec |
---|
94 | data daysec /88775/ ! Duration of day=88775 s |
---|
95 | real :: omega |
---|
96 | real :: Rgas ! gas constant |
---|
97 | data Rgas /190./ |
---|
98 | |
---|
99 | #ifdef MARS |
---|
100 | data g0 /3.7257964/ |
---|
101 | data a0 /3396000/ ! radius=3396.km |
---|
102 | data daysec /88775/ ! Duration of day=88775 s |
---|
103 | data Rgas /190./ |
---|
104 | #endif |
---|
105 | |
---|
106 | #ifdef JUPITER |
---|
107 | data g0 /24.79/ |
---|
108 | data a0 /69911000./ |
---|
109 | data daysec /35733./ |
---|
110 | data Rgas /3745.276/ |
---|
111 | #endif |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | !=============================================================================== |
---|
116 | ! 1.1 Input file |
---|
117 | !=============================================================================== |
---|
118 | |
---|
119 | write(*,*) "" |
---|
120 | write(*,*) " Program valid for diagfi.nc, concatnc.nc and stats.nc files" |
---|
121 | write(*,*) "Enter input file name:" |
---|
122 | |
---|
123 | read(*,'(a128)') infile |
---|
124 | write(*,*) "" |
---|
125 | |
---|
126 | ! open input file |
---|
127 | |
---|
128 | ierr = NF_OPEN(infile,NF_NOWRITE,infid) |
---|
129 | if (ierr.ne.NF_NOERR) then |
---|
130 | write(*,*) 'ERROR: Pb opening input file' |
---|
131 | stop "" |
---|
132 | endif |
---|
133 | |
---|
134 | !=============================================================================== |
---|
135 | ! 1.2 Get grids in lon,lat,alt,time, |
---|
136 | ! as well as hybrid coordinates aps() and bps() (or sigma levels sigma()) |
---|
137 | ! aire() and cv() from input file |
---|
138 | !=============================================================================== |
---|
139 | |
---|
140 | ! 1.2.1 longitude, latitude, altitude and time |
---|
141 | |
---|
142 | ! latitude |
---|
143 | ierr=NF_INQ_DIMID(infid,"latitude",tmpdimid) |
---|
144 | if (ierr.ne.NF_NOERR) then |
---|
145 | stop "Error: Failed to get latitude dimension ID" |
---|
146 | else |
---|
147 | ierr=NF_INQ_VARID(infid,"latitude",tmpvarid) |
---|
148 | if (ierr.ne.NF_NOERR) then |
---|
149 | stop "Error: Failed to get latitude ID" |
---|
150 | else |
---|
151 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,latlength) |
---|
152 | if (ierr.ne.NF_NOERR) then |
---|
153 | stop "Error: Failed to get latitude length" |
---|
154 | else |
---|
155 | allocate(lat(latlength)) |
---|
156 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,lat) |
---|
157 | if (ierr.ne.NF_NOERR) then |
---|
158 | stop "Error: Failed reading latitude" |
---|
159 | endif |
---|
160 | endif |
---|
161 | endif |
---|
162 | endif |
---|
163 | |
---|
164 | ! longitude |
---|
165 | ierr=NF_INQ_DIMID(infid,"longitude",tmpdimid) |
---|
166 | if (ierr.ne.NF_NOERR) then |
---|
167 | stop "Error: Failed to get longitude dimension ID" |
---|
168 | else |
---|
169 | ierr=NF_INQ_VARID(infid,"longitude",tmpvarid) |
---|
170 | if (ierr.ne.NF_NOERR) then |
---|
171 | stop "Error: Failed to get longitude ID" |
---|
172 | else |
---|
173 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,lonlength) |
---|
174 | if (ierr.ne.NF_NOERR) then |
---|
175 | stop "Error: Failed to get longitude length" |
---|
176 | else |
---|
177 | allocate(lon(lonlength)) |
---|
178 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,lon) |
---|
179 | if (ierr.ne.NF_NOERR) then |
---|
180 | stop "Error: Failed reading longitude" |
---|
181 | endif |
---|
182 | endif |
---|
183 | endif |
---|
184 | endif |
---|
185 | |
---|
186 | ! time |
---|
187 | ierr=NF_INQ_DIMID(infid,"Time",tmpdimid) |
---|
188 | if (ierr.ne.NF_NOERR) then |
---|
189 | stop "Error: Failed to get Time dimension ID" |
---|
190 | else |
---|
191 | ierr=NF_INQ_VARID(infid,"Time",tmpvarid) |
---|
192 | if (ierr.ne.NF_NOERR) then |
---|
193 | stop "Error: Failed to get Time ID" |
---|
194 | else |
---|
195 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,timelength) |
---|
196 | if (ierr.ne.NF_NOERR) then |
---|
197 | stop "Error: Failed to get Time length" |
---|
198 | else |
---|
199 | allocate(time(timelength)) |
---|
200 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,time) |
---|
201 | if (ierr.ne.NF_NOERR) then |
---|
202 | stop "Error: Failed reading Time" |
---|
203 | endif |
---|
204 | endif |
---|
205 | endif |
---|
206 | endif |
---|
207 | |
---|
208 | ! altlength |
---|
209 | ierr=NF_INQ_DIMID(infid,"altitude",tmpdimid) |
---|
210 | if (ierr.ne.NF_NOERR) then |
---|
211 | stop "Error: Failed to get altitude dimension ID" |
---|
212 | else |
---|
213 | ierr=NF_INQ_VARID(infid,"altitude",tmpvarid) |
---|
214 | if (ierr.ne.NF_NOERR) then |
---|
215 | stop "Error: Failed to get altitude length" |
---|
216 | else |
---|
217 | ierr=NF_INQ_DIMLEN(infid,tmpdimid,altlength) |
---|
218 | if (ierr.ne.NF_NOERR) then |
---|
219 | stop "Error: Failed to get altitude length" |
---|
220 | else |
---|
221 | allocate(alt(altlength)) |
---|
222 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,alt) |
---|
223 | if (ierr.ne.NF_NOERR) then |
---|
224 | stop "Error: Failed reading Altitude" |
---|
225 | endif |
---|
226 | endif |
---|
227 | endif |
---|
228 | endif |
---|
229 | |
---|
230 | ! 1.2.2 Get hybrid coordinates |
---|
231 | |
---|
232 | ! look for hybrid coordinates |
---|
233 | |
---|
234 | ! hybrid coordinate aps |
---|
235 | ierr=NF_INQ_VARID(infid,"aps",tmpvarid) |
---|
236 | if (ierr.ne.NF_NOERR) then |
---|
237 | stop "Error: Failed to get aps ID" |
---|
238 | else |
---|
239 | allocate(aps(altlength)) |
---|
240 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) |
---|
241 | if (ierr.ne.NF_NOERR) then |
---|
242 | stop "Error: Failed reading aps" |
---|
243 | endif |
---|
244 | endif |
---|
245 | |
---|
246 | ! hybrid coordinate bps |
---|
247 | ierr=NF_INQ_VARID(infid,"bps",tmpvarid) |
---|
248 | if (ierr.ne.NF_NOERR) then |
---|
249 | stop "Error: Failed to get bps ID" |
---|
250 | else |
---|
251 | allocate(bps(altlength)) |
---|
252 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) |
---|
253 | if (ierr.ne.NF_NOERR) then |
---|
254 | stop "Error: Failed reading bps" |
---|
255 | endif |
---|
256 | endif |
---|
257 | |
---|
258 | !surface pressure |
---|
259 | ierr=NF_INQ_VARID(infid,"ps",tmpvarid) |
---|
260 | if (ierr.ne.NF_NOERR) then |
---|
261 | stop "Error: Failed to get ps ID" |
---|
262 | else |
---|
263 | allocate(ps(lonlength,latlength,timelength)) |
---|
264 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,ps) |
---|
265 | if (ierr.ne.NF_NOERR) then |
---|
266 | stop "Error: Failed reading ps" |
---|
267 | endif |
---|
268 | endif |
---|
269 | |
---|
270 | |
---|
271 | !=============================================================================== |
---|
272 | ! 1.3 Reads variables in input file |
---|
273 | !=============================================================================== |
---|
274 | |
---|
275 | ierr=NF_INQ_NVARS(infid,nbvarinfile) |
---|
276 | if (ierr.ne.NF_NOERR) then |
---|
277 | write(*,*) 'ERROR: Failed geting number of variables from file' |
---|
278 | stop |
---|
279 | endif |
---|
280 | |
---|
281 | !1.3.1 Zonal wind, meridional wind |
---|
282 | |
---|
283 | !Zonal wind |
---|
284 | ierr=NF_INQ_VARID(infid,"u",tmpvarid) |
---|
285 | if (ierr.ne.NF_NOERR) then |
---|
286 | stop "Error: Failed to get u ID" |
---|
287 | else |
---|
288 | allocate(u(lonlength,latlength,altlength,timelength)) |
---|
289 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,u) |
---|
290 | if (ierr.ne.NF_NOERR) then |
---|
291 | stop "Error: Failed reading zonal wind" |
---|
292 | endif |
---|
293 | endif |
---|
294 | |
---|
295 | !Meridional wind |
---|
296 | ierr=NF_INQ_VARID(infid,"v",tmpvarid) |
---|
297 | if (ierr.ne.NF_NOERR) then |
---|
298 | stop "Error: Failed to get v ID" |
---|
299 | else |
---|
300 | allocate(v(lonlength,latlength,altlength,timelength)) |
---|
301 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,v) |
---|
302 | if (ierr.ne.NF_NOERR) then |
---|
303 | stop "Error: Failed reading zonal wind" |
---|
304 | endif |
---|
305 | endif |
---|
306 | |
---|
307 | |
---|
308 | ! 1.3.2 aire |
---|
309 | |
---|
310 | allocate(aire(lonlength,latlength)) |
---|
311 | ! look for 'aire' in current file |
---|
312 | ierr=NF_INQ_VARID(infid,"aire",tmpvarid) |
---|
313 | if (ierr.ne.NF_NOERR) then |
---|
314 | write(*,*) "Warning: Failed to get aire ID from file ",trim(infile) |
---|
315 | infile2="diagfi.nc" |
---|
316 | write(*,*) " Trying file ",trim(infile2) |
---|
317 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
318 | if (ierr.ne.NF_NOERR) then |
---|
319 | infile2="diagfi1.nc" |
---|
320 | write(*,*) " Trying file ",trim(infile2) |
---|
321 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
322 | if (ierr.ne.NF_NOERR) then |
---|
323 | write(*,*) "Problem: Could not find/open these files" |
---|
324 | stop "Might as well stop here" |
---|
325 | endif |
---|
326 | endif |
---|
327 | |
---|
328 | ! Get ID for aire |
---|
329 | ierr=NF_INQ_VARID(infid2,"aire",tmpvarid) |
---|
330 | if (ierr.ne.NF_NOERR) then |
---|
331 | stop "Error: Failed to get aire ID" |
---|
332 | endif |
---|
333 | ! Get aire |
---|
334 | ierr=NF_GET_VAR_REAL(infid2,tmpvarid,aire) |
---|
335 | if (ierr.ne.NF_NOERR) then |
---|
336 | stop "Error: Failed reading aire" |
---|
337 | endif |
---|
338 | ! Close file |
---|
339 | write(*,*) 'OK, got aire' |
---|
340 | ierr=NF_CLOSE(infid2) |
---|
341 | else |
---|
342 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,aire) |
---|
343 | if (ierr.ne.NF_NOERR) then |
---|
344 | stop "Error: Failed reading aire" |
---|
345 | endif |
---|
346 | endif |
---|
347 | |
---|
348 | |
---|
349 | ! 1.3.3 phisinit |
---|
350 | |
---|
351 | allocate(phisinit(lonlength,latlength)) |
---|
352 | ! look for '' in current file |
---|
353 | ierr=NF_INQ_VARID(infid,"phisinit",tmpvarid) |
---|
354 | if (ierr.ne.NF_NOERR) then |
---|
355 | write(*,*) "Warning: Failed to get phisinit ID from file ",trim(infile) |
---|
356 | infile2="diagfi.nc" |
---|
357 | write(*,*) " Trying file ",trim(infile2) |
---|
358 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
359 | if (ierr.ne.NF_NOERR) then |
---|
360 | infile2="diagfi1.nc" |
---|
361 | write(*,*) " Trying file ",trim(infile2) |
---|
362 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
363 | if (ierr.ne.NF_NOERR) then |
---|
364 | write(*,*) "Problem: Could not find/open these files" |
---|
365 | stop "Might as well stop here" |
---|
366 | endif |
---|
367 | endif |
---|
368 | |
---|
369 | |
---|
370 | ! Get ID for phisinit |
---|
371 | ierr=NF_INQ_VARID(infid2,"phisinit",tmpvarid) |
---|
372 | if (ierr.ne.NF_NOERR) then |
---|
373 | stop "Error: Failed to get phisinit ID" |
---|
374 | endif |
---|
375 | ! Get phisinit |
---|
376 | ierr=NF_GET_VAR_REAL(infid2,tmpvarid,phisinit) |
---|
377 | if (ierr.ne.NF_NOERR) then |
---|
378 | stop "Error: Failed reading phisinit" |
---|
379 | endif |
---|
380 | ! Close file |
---|
381 | write(*,*) 'OK, got phisinit' |
---|
382 | ierr=NF_CLOSE(infid2) |
---|
383 | else |
---|
384 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,phisinit) |
---|
385 | if (ierr.ne.NF_NOERR) then |
---|
386 | stop "Error: Failed reading phisinit" |
---|
387 | endif |
---|
388 | endif |
---|
389 | |
---|
390 | |
---|
391 | ! 1.3.4 cv |
---|
392 | |
---|
393 | allocate(cv(lonlength,latlength)) |
---|
394 | ! look for 'cv' in current file |
---|
395 | ierr=NF_INQ_VARID(infid,"cv",tmpvarid) |
---|
396 | if (ierr.ne.NF_NOERR) then |
---|
397 | write(*,*) "Warning: Failed to get cv ID from file ",trim(infile) |
---|
398 | infile2="diagfi.nc" |
---|
399 | write(*,*) " Trying file ",trim(infile2) |
---|
400 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
401 | if (ierr.ne.NF_NOERR) then |
---|
402 | infile2="diagfi1.nc" |
---|
403 | write(*,*) " Trying file ",trim(infile2) |
---|
404 | ierr=NF_OPEN(infile2,NF_NOWRITE,infid2) |
---|
405 | if (ierr.ne.NF_NOERR) then |
---|
406 | write(*,*) "Problem: Could not find/open these files" |
---|
407 | stop "Might as well stop here" |
---|
408 | endif |
---|
409 | endif |
---|
410 | |
---|
411 | |
---|
412 | ! Get ID for cv |
---|
413 | ierr=NF_INQ_VARID(infid2,"cv",tmpvarid) |
---|
414 | if (ierr.ne.NF_NOERR) then |
---|
415 | stop "Error: Failed to get cv ID" |
---|
416 | endif |
---|
417 | ! Get cv |
---|
418 | ierr=NF_GET_VAR_REAL(infid2,tmpvarid,cv) |
---|
419 | if (ierr.ne.NF_NOERR) then |
---|
420 | stop "Error: Failed reading cv" |
---|
421 | endif |
---|
422 | ! Close file |
---|
423 | write(*,*) 'OK, got cv' |
---|
424 | ierr=NF_CLOSE(infid2) |
---|
425 | else |
---|
426 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,cv) |
---|
427 | if (ierr.ne.NF_NOERR) then |
---|
428 | stop "Error: Failed reading cv" |
---|
429 | endif |
---|
430 | endif |
---|
431 | |
---|
432 | |
---|
433 | !Other variables: rho, temp |
---|
434 | |
---|
435 | ierr=NF_INQ_VARID(infid,"temp",tmpvarid) |
---|
436 | if (ierr.ne.NF_NOERR) then |
---|
437 | stop "Error: Failed to get temp ID" |
---|
438 | else |
---|
439 | allocate(temp(lonlength,latlength,altlength,timelength)) |
---|
440 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,temp) |
---|
441 | if (ierr.ne.NF_NOERR) then |
---|
442 | stop "Error: Failed reading temperature" |
---|
443 | endif |
---|
444 | endif |
---|
445 | |
---|
446 | ierr=NF_INQ_VARID(infid,"rho",tmpvarid) |
---|
447 | if (ierr.ne.NF_NOERR) then |
---|
448 | write(*,*) "Error: Failed to get rho ID" |
---|
449 | write(*,*) "Let's compute it from temperature" |
---|
450 | allocate(rho(lonlength,latlength,altlength,timelength)) |
---|
451 | |
---|
452 | do it=1,timelength |
---|
453 | do ilat=1,latlength |
---|
454 | do ilon=1,lonlength |
---|
455 | do ialt=1,altlength |
---|
456 | rho(ilon,ilat,ialt,it)= (aps(ialt)+bps(ialt)*ps(ilon,ilat,it))/(Rgas*temp(ilon,ilat,ialt,it)) |
---|
457 | enddo |
---|
458 | enddo |
---|
459 | enddo |
---|
460 | enddo |
---|
461 | else |
---|
462 | allocate(rho(lonlength,latlength,altlength,timelength)) |
---|
463 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,rho) |
---|
464 | if (ierr.ne.NF_NOERR) then |
---|
465 | stop "Error: Failed reading mass density" |
---|
466 | endif |
---|
467 | endif |
---|
468 | |
---|
469 | !============================================================================== |
---|
470 | ! 1.4. Output file name |
---|
471 | !============================================================================== |
---|
472 | filename=infile(1:len_trim(infile)-3)//"_stream.nc" |
---|
473 | write(*,*)'The output file has name:' |
---|
474 | write(*,*) filename |
---|
475 | |
---|
476 | |
---|
477 | !============================================================================== |
---|
478 | ! 2.1. Temporal averages |
---|
479 | !============================================================================== |
---|
480 | |
---|
481 | allocate(ucum(lonlength,latlength,altlength)) !Time mean zonal wind |
---|
482 | allocate(vcum(lonlength,latlength,altlength)) !Time mean meridional wind |
---|
483 | allocate(tempcum(lonlength,latlength,altlength)) !Time mean temperature |
---|
484 | allocate(rhocum(lonlength,latlength,altlength)) !Time mean density |
---|
485 | allocate(pscum(lonlength,latlength)) !Time mean surface pressure |
---|
486 | do ilat=1,latlength |
---|
487 | do ilon=1,lonlength |
---|
488 | pscum(ilon,ilat)=0. |
---|
489 | do ialt=1,altlength |
---|
490 | ucum(ilon,ilat,ialt)=0. |
---|
491 | vcum(ilon,ilat,ialt)=0. |
---|
492 | tempcum(ilon,ilat,ialt)=0. |
---|
493 | rhocum(ilon,ilat,ialt)=0. |
---|
494 | do it=1,timelength |
---|
495 | ucum(ilon,ilat,ialt)=ucum(ilon,ilat,ialt)+u(ilon,ilat,ialt,it) |
---|
496 | vcum(ilon,ilat,ialt)=vcum(ilon,ilat,ialt)+v(ilon,ilat,ialt,it) |
---|
497 | tempcum(ilon,ilat,ialt)=tempcum(ilon,ilat,ialt)+temp(ilon,ilat,ialt,it) |
---|
498 | rhocum(ilon,ilat,ialt)=rhocum(ilon,ilat,ialt)+rho(ilon,ilat,ialt,it) |
---|
499 | enddo |
---|
500 | ucum(ilon,ilat,ialt)=ucum(ilon,ilat,ialt)/timelength |
---|
501 | vcum(ilon,ilat,ialt)=vcum(ilon,ilat,ialt)/timelength |
---|
502 | tempcum(ilon,ilat,ialt)=tempcum(ilon,ilat,ialt)/timelength |
---|
503 | rhocum(ilon,ilat,ialt)=rhocum(ilon,ilat,ialt)/timelength |
---|
504 | enddo |
---|
505 | do it=1,timelength |
---|
506 | pscum(ilon,ilat)=pscum(ilon,ilat)+ps(ilon,ilat,it) |
---|
507 | enddo |
---|
508 | pscum(ilon,ilat)=pscum(ilon,ilat)/timelength |
---|
509 | enddo |
---|
510 | enddo |
---|
511 | |
---|
512 | !============================================================================== |
---|
513 | ! 2.2. Calculation of the stream function |
---|
514 | !============================================================================== |
---|
515 | |
---|
516 | !Contravariant meridional wind |
---|
517 | allocate(vcont(lonlength,latlength,altlength)) |
---|
518 | do ilon=1,lonlength |
---|
519 | do ialt=1,altlength |
---|
520 | do ilat=1,latlength-1 |
---|
521 | vcont(ilon,ilat,ialt)=0.5*& |
---|
522 | (vcum(ilon,ilat,ialt)+vcum(ilon,ilat+1,ialt))/cv(ilon,ilat) |
---|
523 | enddo |
---|
524 | vcont(ilon,latlength,ialt)=0.!vcont(ilon,latlength-1,ialt) |
---|
525 | enddo |
---|
526 | enddo |
---|
527 | |
---|
528 | !Pressure from hybrid levels |
---|
529 | allocate(Plev(lonlength,latlength,altlength)) |
---|
530 | do ilon=1,lonlength |
---|
531 | do ilat=1,latlength |
---|
532 | do ialt=1,altlength |
---|
533 | Plev(ilon,ilat,ialt)=aps(ialt)+bps(ialt)*pscum(ilon,ilat) |
---|
534 | enddo |
---|
535 | enddo |
---|
536 | enddo |
---|
537 | |
---|
538 | !Mass in the box |
---|
539 | allocate(mass(lonlength,latlength,altlength)) |
---|
540 | do ilon=1,lonlength |
---|
541 | do ilat=1,latlength |
---|
542 | do ialt=1,altlength |
---|
543 | mass(ilon,ilat,ialt)=aire(ilon,ilat)*& |
---|
544 | Plev(ilon,ilat,ialt)/g0 |
---|
545 | enddo |
---|
546 | enddo |
---|
547 | enddo |
---|
548 | |
---|
549 | !Mass variation in the box |
---|
550 | allocate(dmass(lonlength,latlength,altlength)) |
---|
551 | do ilon=1,lonlength |
---|
552 | do ilat=1,latlength |
---|
553 | do ialt=1,altlength-1 |
---|
554 | dmass(ilon,ilat,ialt)=mass(ilon,ilat,ialt)-& |
---|
555 | mass(ilon,ilat,ialt+1) |
---|
556 | enddo |
---|
557 | dmass(ilon,ilat,altlength)=0.!dmass(ilon,ilat,altlength-1) |
---|
558 | enddo |
---|
559 | enddo |
---|
560 | |
---|
561 | !Stream function |
---|
562 | allocate(psi(latlength,altlength)) |
---|
563 | allocate(vcontcum(latlength,altlength)) |
---|
564 | do ilat=1,latlength-1 |
---|
565 | psi(ilat,altlength)=0. |
---|
566 | do ialt=altlength-1,1,-1 |
---|
567 | psi(ilat,ialt)=psi(ilat,ialt+1) |
---|
568 | vcontcum(ilat,ialt)=0. |
---|
569 | do ilon=1,lonlength |
---|
570 | vcontcum(ilat,ialt)=vcontcum(ilat,ialt)+vcont(ilon,ilat,ialt)/lonlength |
---|
571 | psi(ilat,ialt)=psi(ilat,ialt)-(vcont(ilon,ilat,ialt)& |
---|
572 | *(dmass(ilon,ilat,ialt)+dmass(ilon,ilat+1,ialt))) |
---|
573 | enddo |
---|
574 | psi(latlength,ialt)=0.!psi(latlength-1,ialt) |
---|
575 | enddo |
---|
576 | enddo |
---|
577 | |
---|
578 | !============================================================================== |
---|
579 | ! 2.3. Calculation of the total angular momentum |
---|
580 | !============================================================================== |
---|
581 | |
---|
582 | !Rotation speed |
---|
583 | omega=2.*3.141592/daysec |
---|
584 | |
---|
585 | !Angular momentum |
---|
586 | allocate(mom(lonlength,latlength,altlength)) |
---|
587 | allocate(momave(latlength,altlength)) |
---|
588 | do ilat=1,latlength |
---|
589 | do ialt=1,altlength |
---|
590 | momave(ilat,ialt)=0. |
---|
591 | do ilon=1,lonlength |
---|
592 | ! mom(ilon,ilat,ialt,it)=dmass(ilon,ilat,ialt,it)*a0*& |
---|
593 | mom(ilon,ilat,ialt)=a0*cos(lat(ilat)*3.141592/180.)*& |
---|
594 | (omega*a0*cos(lat(ilat)*3.141592/180.)+ucum(ilon,ilat,ialt)) |
---|
595 | |
---|
596 | momave(ilat,ialt)=momave(ilat,ialt)+mom(ilon,ilat,ialt) |
---|
597 | enddo |
---|
598 | momave(ilat,ialt)=momave(ilat,ialt)/lonlength |
---|
599 | enddo |
---|
600 | enddo |
---|
601 | |
---|
602 | !============================================================================== |
---|
603 | ! 2.4. Zonal mean winds, temperature and density |
---|
604 | !============================================================================== |
---|
605 | |
---|
606 | allocate(uzm(latlength,altlength)) !Zonal mean zonal wind |
---|
607 | allocate(vzm(latlength,altlength)) !Zonal mean meridional wind |
---|
608 | allocate(tempzm(latlength,altlength)) !Zonal mean temperature |
---|
609 | allocate(rhozm(latlength,altlength)) !Zonal mean density |
---|
610 | allocate(pszm(latlength)) !Zonal mean surface pressure |
---|
611 | allocate(phisinitzm(latlength)) !Zonal mean ground geopotential |
---|
612 | do ilat=1,latlength |
---|
613 | phisinitzm(ilat)=0. |
---|
614 | pszm(ilat)=0. |
---|
615 | do ialt=1,altlength |
---|
616 | uzm(ilat,ialt)=0. |
---|
617 | vzm(ilat,ialt)=0. |
---|
618 | tempzm(ilat,ialt)=0. |
---|
619 | rhozm(ilat,ialt)=0. |
---|
620 | do ilon=1,lonlength |
---|
621 | uzm(ilat,ialt)=uzm(ilat,ialt)+ucum(ilon,ilat,ialt) |
---|
622 | vzm(ilat,ialt)=vzm(ilat,ialt)+vcum(ilon,ilat,ialt) |
---|
623 | tempzm(ilat,ialt)=tempzm(ilat,ialt)+tempcum(ilon,ilat,ialt) |
---|
624 | rhozm(ilat,ialt)=rhozm(ilat,ialt)+rhocum(ilon,ilat,ialt) |
---|
625 | enddo |
---|
626 | uzm(ilat,ialt)=uzm(ilat,ialt)/lonlength |
---|
627 | vzm(ilat,ialt)=vzm(ilat,ialt)/lonlength |
---|
628 | tempzm(ilat,ialt)=tempzm(ilat,ialt)/lonlength |
---|
629 | rhozm(ilat,ialt)=rhozm(ilat,ialt)/lonlength |
---|
630 | enddo |
---|
631 | do ilon=1,lonlength |
---|
632 | pszm(ilat)=pszm(ilat)+pscum(ilon,ilat) |
---|
633 | phisinitzm(ilat)=phisinitzm(ilat)+phisinit(ilon,ilat) |
---|
634 | enddo |
---|
635 | pszm(ilat)=pszm(ilat)/lonlength |
---|
636 | phisinitzm(ilat)=phisinitzm(ilat)/lonlength |
---|
637 | enddo |
---|
638 | |
---|
639 | |
---|
640 | !============================================================================== |
---|
641 | ! 2.3. Recalculation of angular momentum using zonally averaged wind |
---|
642 | !============================================================================== |
---|
643 | |
---|
644 | do ilat=1,latlength |
---|
645 | do ialt=1,altlength |
---|
646 | momave(ilat,ialt)=a0*cos(lat(ilat)*3.141592/180.)*& |
---|
647 | (omega*a0*cos(lat(ilat)*3.141592/180.)+uzm(ilat,ialt)) |
---|
648 | enddo |
---|
649 | enddo |
---|
650 | |
---|
651 | |
---|
652 | !============================================================================== |
---|
653 | ! 3.1 Dimensions in output file |
---|
654 | !============================================================================== |
---|
655 | |
---|
656 | ! 3.1.1 Initialize output file's lat,lon,alt and time dimensions |
---|
657 | call initiate (filename,lat,alt,time,nout,& |
---|
658 | latdimout,londimout,altdimout,timedimout,lonvarout,timevarout) |
---|
659 | ! Initialize output file's aps,bps variables |
---|
660 | call init2(infid,lonlength,latlength,altlength,& |
---|
661 | nout,londimout,latdimout,altdimout) |
---|
662 | |
---|
663 | ! 3.1.2 New longitude dimension/value in output file |
---|
664 | allocate(lon_fake(1)) |
---|
665 | lon_fake(1)=0. |
---|
666 | #ifdef NC_DOUBLE |
---|
667 | ierr= NF_PUT_VARA_DOUBLE(nout,lonvarout,1,1,lon_fake) |
---|
668 | #else |
---|
669 | ierr= NF_PUT_VARA_REAL(nout,lonvarout,1,1,lon_fake) |
---|
670 | #endif |
---|
671 | |
---|
672 | ! 3.1.3 New time dimension/value in output file |
---|
673 | #ifdef NC_DOUBLE |
---|
674 | ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,1,1,lon_fake) |
---|
675 | #else |
---|
676 | ierr= NF_PUT_VARA_REAL(nout,timevarout,1,1,lon_fake) |
---|
677 | #endif |
---|
678 | |
---|
679 | |
---|
680 | !============================================================================== |
---|
681 | ! 3.2 Write variables |
---|
682 | !============================================================================== |
---|
683 | |
---|
684 | !Define the dimensions of the variables to be written |
---|
685 | |
---|
686 | !!3.2.1 Stream function |
---|
687 | call def_var(nout,"psi","Stream function","",4,& |
---|
688 | (/londimout,latdimout,altdimout,timedimout/),psivarout,ierr) |
---|
689 | if (ierr.ne.NF_NOERR) then |
---|
690 | write(*,*) 'Error, could not define variable psi' |
---|
691 | stop "" |
---|
692 | endif |
---|
693 | |
---|
694 | !Write in the output file |
---|
695 | ierr=NF_PUT_VAR_REAL(nout,psivarout,psi) |
---|
696 | if (ierr.ne.NF_NOERR) then |
---|
697 | write(*,*)'Error, Failed to write variable psi' |
---|
698 | stop |
---|
699 | endif |
---|
700 | |
---|
701 | !3.2.2 Momentum |
---|
702 | call def_var(nout,"momave","Angular momentum","",4,& |
---|
703 | (/londimout,latdimout,altdimout,timedimout/),momvarout,ierr) |
---|
704 | if (ierr.ne.NF_NOERR) then |
---|
705 | write(*,*) 'Error, could not define variable momave' |
---|
706 | stop "" |
---|
707 | endif |
---|
708 | |
---|
709 | !Write in the output file |
---|
710 | ierr=NF_PUT_VAR_REAL(nout,momvarout,momave) |
---|
711 | if (ierr.ne.NF_NOERR) then |
---|
712 | write(*,*)'Error, Failed to write variable momave' |
---|
713 | stop |
---|
714 | endif |
---|
715 | |
---|
716 | |
---|
717 | !3.2.3 Zonal mean zonal wind |
---|
718 | call def_var(nout,"u","Zonal mean zonal wind","m/s",4,& |
---|
719 | (/londimout,latdimout,altdimout,timedimout/),uvarout,ierr) |
---|
720 | if (ierr.ne.NF_NOERR) then |
---|
721 | write(*,*) 'Error, could not define variable u' |
---|
722 | stop "" |
---|
723 | endif |
---|
724 | |
---|
725 | !Write in the output file |
---|
726 | ierr=NF_PUT_VAR_REAL(nout,uvarout,uzm) |
---|
727 | if (ierr.ne.NF_NOERR) then |
---|
728 | write(*,*)'Error, Failed to write variable u' |
---|
729 | stop |
---|
730 | endif |
---|
731 | |
---|
732 | |
---|
733 | !3.2.4 Zonal mean meridional wind |
---|
734 | call def_var(nout,"v","Zonal mean meridional wind","m/s",4,& |
---|
735 | (/londimout,latdimout,altdimout,timedimout/),vvarout,ierr) |
---|
736 | if (ierr.ne.NF_NOERR) then |
---|
737 | write(*,*) 'Error, could not define variable v' |
---|
738 | stop "" |
---|
739 | endif |
---|
740 | |
---|
741 | !Write in the output file |
---|
742 | ierr=NF_PUT_VAR_REAL(nout,vvarout,vzm) |
---|
743 | if (ierr.ne.NF_NOERR) then |
---|
744 | write(*,*)'Error, Failed to write variable v' |
---|
745 | stop |
---|
746 | endif |
---|
747 | |
---|
748 | !3.2.5 Zonal mean density |
---|
749 | call def_var(nout,"rho","Zonal mean atmospheric density","",4,& |
---|
750 | (/londimout,latdimout,altdimout,timedimout/),rhovarout,ierr) |
---|
751 | if (ierr.ne.NF_NOERR) then |
---|
752 | write(*,*) 'Error, could not define variable rho' |
---|
753 | stop "" |
---|
754 | endif |
---|
755 | |
---|
756 | !Write in the output file |
---|
757 | ierr=NF_PUT_VAR_REAL(nout,rhovarout,rhozm) |
---|
758 | if (ierr.ne.NF_NOERR) then |
---|
759 | write(*,*)'Error, Failed to write variable rho' |
---|
760 | stop |
---|
761 | endif |
---|
762 | |
---|
763 | !3.2.6 Zonal mean temperature |
---|
764 | call def_var(nout,"temp","Zonal mean temperature","K",4,& |
---|
765 | (/londimout,latdimout,altdimout,timedimout/),tempvarout,ierr) |
---|
766 | if (ierr.ne.NF_NOERR) then |
---|
767 | write(*,*) 'Error, could not define variable temp' |
---|
768 | stop "" |
---|
769 | endif |
---|
770 | |
---|
771 | !Write in the output file |
---|
772 | ierr=NF_PUT_VAR_REAL(nout,tempvarout,tempzm) |
---|
773 | if (ierr.ne.NF_NOERR) then |
---|
774 | write(*,*)'Error, Failed to write variable temp' |
---|
775 | stop |
---|
776 | endif |
---|
777 | |
---|
778 | !3.2.7 Zonal mean surface pressure |
---|
779 | call def_var(nout,"ps","Zonal mean surface pressure","Pa",3,& |
---|
780 | (/londimout,latdimout,timedimout/),psvarout,ierr) |
---|
781 | if (ierr.ne.NF_NOERR) then |
---|
782 | write(*,*) 'Error, could not define variable ps' |
---|
783 | stop "" |
---|
784 | endif |
---|
785 | |
---|
786 | !Write in the output file |
---|
787 | ierr=NF_PUT_VAR_REAL(nout,psvarout,pszm) |
---|
788 | if (ierr.ne.NF_NOERR) then |
---|
789 | write(*,*)'Error, Failed to write variable ps' |
---|
790 | stop |
---|
791 | endif |
---|
792 | |
---|
793 | |
---|
794 | !3.2.8 Zonal mean geopotential |
---|
795 | call def_var(nout,"phisinit","Zonal mean initial geopotential","",2,& |
---|
796 | (/londimout,latdimout/),phisinitvarout,ierr) |
---|
797 | if (ierr.ne.NF_NOERR) then |
---|
798 | write(*,*) 'Error, could not define variable phisinit' |
---|
799 | stop "" |
---|
800 | endif |
---|
801 | |
---|
802 | !Write in the output file |
---|
803 | ierr=NF_PUT_VAR_REAL(nout,phisinitvarout,phisinitzm) |
---|
804 | if (ierr.ne.NF_NOERR) then |
---|
805 | write(*,*)'Error, Failed to write variable phisinit' |
---|
806 | stop |
---|
807 | endif |
---|
808 | |
---|
809 | ! Close input file |
---|
810 | ierr=nf_close(nid) |
---|
811 | |
---|
812 | ! Close output file |
---|
813 | ierr=NF_CLOSE(nout) |
---|
814 | |
---|
815 | contains |
---|
816 | |
---|
817 | !****************************************************************************** |
---|
818 | Subroutine initiate (filename,lat,alt,time,& |
---|
819 | nout,latdimout,londimout,altdimout,timedimout,& |
---|
820 | lonvarout,timevarout) |
---|
821 | !============================================================================== |
---|
822 | ! Purpose: |
---|
823 | ! Create and initialize a data file (NetCDF format) |
---|
824 | !============================================================================== |
---|
825 | ! Remarks: |
---|
826 | ! The NetCDF file (created in this subroutine) remains open |
---|
827 | !============================================================================== |
---|
828 | |
---|
829 | implicit none |
---|
830 | |
---|
831 | include "netcdf.inc" ! NetCDF definitions |
---|
832 | |
---|
833 | !============================================================================== |
---|
834 | ! Arguments: |
---|
835 | !============================================================================== |
---|
836 | character (len=*), intent(in):: filename |
---|
837 | ! filename(): the file's name |
---|
838 | real, dimension(:), intent(in):: lat |
---|
839 | ! lat(): latitude |
---|
840 | real, dimension(:), intent(in):: alt |
---|
841 | ! alt(): altitude |
---|
842 | real, dimension(:), intent(in):: time |
---|
843 | ! time(): Time |
---|
844 | integer, intent(out):: nout |
---|
845 | ! nout: [netcdf] file ID |
---|
846 | integer, intent(out):: latdimout |
---|
847 | ! latdimout: [netcdf] lat() (i.e.: latitude) ID |
---|
848 | integer, intent(out):: londimout |
---|
849 | ! londimout: [netcdf] lon() ID |
---|
850 | integer, intent(out):: altdimout |
---|
851 | ! altdimout: [netcdf] alt() ID |
---|
852 | integer, intent(out):: timedimout |
---|
853 | ! timedimout: [netcdf] "Time" ID |
---|
854 | integer, intent(out):: lonvarout |
---|
855 | ! timevarout: [netcdf] Longiture (considered as a variable) ID |
---|
856 | integer, intent(out):: timevarout |
---|
857 | ! timevarout: [netcdf] Time (considered as a variable) ID |
---|
858 | |
---|
859 | !============================================================================== |
---|
860 | ! Local variables: |
---|
861 | !============================================================================== |
---|
862 | !integer :: latdim,londim,altdim,timedim |
---|
863 | integer :: nvarid,ierr |
---|
864 | ! nvarid: [netcdf] ID of a variable |
---|
865 | ! ierr: [netcdf] return error code (from called subroutines) |
---|
866 | |
---|
867 | !============================================================================== |
---|
868 | ! 1. Create (and open) output file |
---|
869 | !============================================================================== |
---|
870 | write(*,*) "creating "//trim(adjustl(filename))//'...' |
---|
871 | ierr = NF_CREATE(filename,NF_CLOBBER,nout) |
---|
872 | ! NB: setting NF_CLOBBER mode means that it's OK to overwrite an existing file |
---|
873 | if (ierr.NE.NF_NOERR) then |
---|
874 | WRITE(*,*)'ERROR: Impossible to create the file.' |
---|
875 | stop "" |
---|
876 | endif |
---|
877 | |
---|
878 | !============================================================================== |
---|
879 | ! 2. Define/write "dimensions" and get their IDs |
---|
880 | !============================================================================== |
---|
881 | |
---|
882 | ierr = NF_DEF_DIM(nout, "latitude", size(lat), latdimout) |
---|
883 | !ierr = NF_DEF_DIM(nout, "longitude", NF_UNLIMITED, londimout) |
---|
884 | ierr = NF_DEF_DIM(nout, "longitude", 1, londimout) |
---|
885 | ierr = NF_DEF_DIM(nout, "altitude", size(alt), altdimout) |
---|
886 | ierr = NF_DEF_DIM(nout, "Time", 1, timedimout) |
---|
887 | |
---|
888 | ! End netcdf define mode |
---|
889 | ierr = NF_ENDDEF(nout) |
---|
890 | |
---|
891 | !============================================================================== |
---|
892 | ! 3. Write "Time" (attributes) |
---|
893 | !============================================================================== |
---|
894 | |
---|
895 | call def_var(nout,"Time","Time","years since 0000-00-0 00:00:00",1,& |
---|
896 | (/timedimout/),timevarout,ierr) |
---|
897 | |
---|
898 | !============================================================================== |
---|
899 | ! 4. Write "latitude" (data and attributes) |
---|
900 | !============================================================================== |
---|
901 | |
---|
902 | call def_var(nout,"latitude","latitude","degrees_north",1,& |
---|
903 | (/latdimout/),nvarid,ierr) |
---|
904 | |
---|
905 | #ifdef NC_DOUBLE |
---|
906 | ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lat) |
---|
907 | #else |
---|
908 | ierr = NF_PUT_VAR_REAL (nout,nvarid,lat) |
---|
909 | #endif |
---|
910 | |
---|
911 | !============================================================================== |
---|
912 | ! 4. Write "longitude" (attributes) |
---|
913 | !============================================================================== |
---|
914 | |
---|
915 | call def_var(nout,"longitude","East longitude","degrees_east",1,& |
---|
916 | (/londimout/),lonvarout,ierr) |
---|
917 | |
---|
918 | |
---|
919 | !============================================================================== |
---|
920 | ! 4. Write "altitude" (data and attributes) |
---|
921 | !============================================================================== |
---|
922 | |
---|
923 | ! Switch to netcdf define mode |
---|
924 | |
---|
925 | call def_var(nout,"altitude","Altitude","km",1,& |
---|
926 | (/altdimout/),nvarid,ierr) |
---|
927 | |
---|
928 | #ifdef NC_DOUBLE |
---|
929 | ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,alt) |
---|
930 | #else |
---|
931 | ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) |
---|
932 | #endif |
---|
933 | |
---|
934 | end Subroutine initiate |
---|
935 | !****************************************************************************** |
---|
936 | subroutine init2(infid,lonlen,latlen,altlen, & |
---|
937 | outfid,londimout,latdimout,altdimout) |
---|
938 | !============================================================================== |
---|
939 | ! Purpose: |
---|
940 | ! Copy aps() and bps() from input file to outpout file |
---|
941 | !============================================================================== |
---|
942 | ! Remarks: |
---|
943 | ! The NetCDF files must be open |
---|
944 | !============================================================================== |
---|
945 | |
---|
946 | implicit none |
---|
947 | |
---|
948 | include "netcdf.inc" ! NetCDF definitions |
---|
949 | |
---|
950 | !============================================================================== |
---|
951 | ! Arguments: |
---|
952 | !============================================================================== |
---|
953 | integer, intent(in) :: infid ! NetCDF output file ID |
---|
954 | integer, intent(in) :: lonlen ! # of grid points along longitude |
---|
955 | integer, intent(in) :: latlen ! # of grid points along latitude |
---|
956 | integer, intent(in) :: altlen ! # of grid points along latitude |
---|
957 | integer, intent(in) :: outfid ! NetCDF output file ID |
---|
958 | integer, intent(in) :: londimout ! longitude dimension ID |
---|
959 | integer, intent(in) :: latdimout ! latitude dimension ID |
---|
960 | integer, intent(in) :: altdimout ! altitude dimension ID |
---|
961 | !============================================================================== |
---|
962 | ! Local variables: |
---|
963 | !============================================================================== |
---|
964 | real,dimension(:),allocatable :: aps,bps ! hybrid vertical coordinates |
---|
965 | integer :: apsid,bpsid |
---|
966 | integer :: ierr |
---|
967 | integer :: tmpvarid ! temporary variable ID |
---|
968 | logical :: aps_ok, bps_ok ! are "phisinit" "aps" "bps" available ? |
---|
969 | |
---|
970 | |
---|
971 | !============================================================================== |
---|
972 | ! 1. Read data from input file |
---|
973 | !============================================================================== |
---|
974 | |
---|
975 | ! hybrid coordinate aps |
---|
976 | allocate(aps(altlen)) |
---|
977 | ierr=NF_INQ_VARID(infid,"aps",tmpvarid) |
---|
978 | if (ierr.ne.NF_NOERR) then |
---|
979 | write(*,*) "oops Failed to get aps ID. OK" |
---|
980 | aps_ok=.false. |
---|
981 | else |
---|
982 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) |
---|
983 | if (ierr.ne.NF_NOERR) then |
---|
984 | stop "error: Failed reading aps" |
---|
985 | endif |
---|
986 | aps_ok=.true. |
---|
987 | endif |
---|
988 | |
---|
989 | ! hybrid coordinate bps |
---|
990 | allocate(bps(altlen)) |
---|
991 | ierr=NF_INQ_VARID(infid,"bps",tmpvarid) |
---|
992 | if (ierr.ne.NF_NOERR) then |
---|
993 | write(*,*) "oops: Failed to get bps ID. OK" |
---|
994 | bps_ok=.false. |
---|
995 | else |
---|
996 | ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) |
---|
997 | if (ierr.ne.NF_NOERR) then |
---|
998 | stop "Error: Failed reading bps" |
---|
999 | endif |
---|
1000 | bps_ok=.true. |
---|
1001 | endif |
---|
1002 | |
---|
1003 | !============================================================================== |
---|
1004 | ! 2. Write |
---|
1005 | !============================================================================== |
---|
1006 | |
---|
1007 | !============================================================================== |
---|
1008 | ! 2.2. Hybrid coordinates aps() and bps() |
---|
1009 | !============================================================================== |
---|
1010 | |
---|
1011 | IF (aps_ok) then |
---|
1012 | ! define aps |
---|
1013 | call def_var(nout,"aps","hybrid pressure at midlayers"," ",1,& |
---|
1014 | (/altdimout/),apsid,ierr) |
---|
1015 | if (ierr.ne.NF_NOERR) then |
---|
1016 | stop "Error: Failed to def_var aps" |
---|
1017 | endif |
---|
1018 | |
---|
1019 | ! write aps |
---|
1020 | #ifdef NC_DOUBLE |
---|
1021 | ierr=NF_PUT_VAR_DOUBLE(outfid,apsid,aps) |
---|
1022 | #else |
---|
1023 | ierr=NF_PUT_VAR_REAL(outfid,apsid,aps) |
---|
1024 | #endif |
---|
1025 | if (ierr.ne.NF_NOERR) then |
---|
1026 | stop "Error: Failed to write aps" |
---|
1027 | endif |
---|
1028 | ENDIF |
---|
1029 | |
---|
1030 | IF (bps_ok) then |
---|
1031 | ! define bps |
---|
1032 | call def_var(nout,"bps","hybrid sigma at midlayers"," ",1,& |
---|
1033 | (/altdimout/),bpsid,ierr) |
---|
1034 | if (ierr.ne.NF_NOERR) then |
---|
1035 | stop "Error: Failed to def_var bps" |
---|
1036 | endif |
---|
1037 | |
---|
1038 | ! write bps |
---|
1039 | #ifdef NC_DOUBLE |
---|
1040 | ierr=NF_PUT_VAR_DOUBLE(outfid,bpsid,bps) |
---|
1041 | #else |
---|
1042 | ierr=NF_PUT_VAR_REAL(outfid,bpsid,bps) |
---|
1043 | #endif |
---|
1044 | if (ierr.ne.NF_NOERR) then |
---|
1045 | stop "Error: Failed to write bps" |
---|
1046 | endif |
---|
1047 | ENDIF |
---|
1048 | |
---|
1049 | |
---|
1050 | |
---|
1051 | ! Cleanup |
---|
1052 | deallocate(aps) |
---|
1053 | deallocate(bps) |
---|
1054 | |
---|
1055 | end subroutine init2 |
---|
1056 | !****************************************************************************** |
---|
1057 | subroutine def_var(nid,name,title,units,nbdim,dim,nvarid,ierr) |
---|
1058 | !============================================================================== |
---|
1059 | ! Purpose: Write a variable (i.e: add a variable to a dataset) |
---|
1060 | ! called "name"; along with its attributes "title", "units"... |
---|
1061 | ! to a file (following the NetCDF format) |
---|
1062 | !============================================================================== |
---|
1063 | ! Remarks: |
---|
1064 | ! The NetCDF file must be open |
---|
1065 | !============================================================================== |
---|
1066 | |
---|
1067 | implicit none |
---|
1068 | |
---|
1069 | include "netcdf.inc" ! NetCDF definitions |
---|
1070 | |
---|
1071 | !============================================================================== |
---|
1072 | ! Arguments: |
---|
1073 | !============================================================================== |
---|
1074 | integer, intent(in) :: nid |
---|
1075 | ! nid: [netcdf] file ID # |
---|
1076 | character (len=*), intent(in) :: name |
---|
1077 | ! name(): [netcdf] variable's name |
---|
1078 | character (len=*), intent(in) :: title |
---|
1079 | ! title(): [netcdf] variable's "title" attribute |
---|
1080 | character (len=*), intent(in) :: units |
---|
1081 | ! unit(): [netcdf] variable's "units" attribute |
---|
1082 | integer, intent(in) :: nbdim |
---|
1083 | ! nbdim: number of dimensions of the variable |
---|
1084 | integer, dimension(nbdim), intent(in) :: dim |
---|
1085 | ! dim(nbdim): [netcdf] dimension(s) ID(s) |
---|
1086 | integer, intent(out) :: nvarid |
---|
1087 | ! nvarid: [netcdf] ID # of the variable |
---|
1088 | integer, intent(out) :: ierr |
---|
1089 | ! ierr: [netcdf] subroutines returned error code |
---|
1090 | |
---|
1091 | ! Switch to netcdf define mode |
---|
1092 | ierr=NF_REDEF(nid) |
---|
1093 | |
---|
1094 | ! Insert the definition of the variable |
---|
1095 | #ifdef NC_DOUBLE |
---|
1096 | ierr=NF_DEF_VAR(nid,adjustl(name),NF_DOUBLE,nbdim,dim,nvarid) |
---|
1097 | #else |
---|
1098 | ierr=NF_DEF_VAR(nid,adjustl(name),NF_FLOAT,nbdim,dim,nvarid) |
---|
1099 | #endif |
---|
1100 | |
---|
1101 | ! Write the attributes |
---|
1102 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"title",len_trim(adjustl(title)),adjustl(title)) |
---|
1103 | ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",len_trim(adjustl(units)),adjustl(units)) |
---|
1104 | |
---|
1105 | ! End netcdf define mode |
---|
1106 | ierr=NF_ENDDEF(nid) |
---|
1107 | |
---|
1108 | end subroutine def_var |
---|
1109 | |
---|
1110 | end program streamfunction |
---|