1 | program streamfunction |
---|
2 | |
---|
3 | ! SL 12/2009: |
---|
4 | ! This program reads 4D (lon-lat-alt-time) fields directly from LMD outputs |
---|
5 | ! without regrid : histmth OR from files recast in log P coordinates (_P) |
---|
6 | ! |
---|
7 | ! it computes: |
---|
8 | ! dmass -- 4D -- mass of each cell |
---|
9 | ! psi -- 3D -- Stream function |
---|
10 | ! |
---|
11 | ! Minimal requirements and dependencies: |
---|
12 | ! The dataset must include the following data: |
---|
13 | ! - surface pressure and surface geopotential |
---|
14 | ! - meridional winds |
---|
15 | |
---|
16 | implicit none |
---|
17 | |
---|
18 | include "netcdf.inc" ! NetCDF definitions |
---|
19 | |
---|
20 | character (len=128) :: infile ! input file name (name_P.nc) |
---|
21 | character (len=128) :: outfile ! output file name |
---|
22 | |
---|
23 | character (len=64) :: text ! to store some text |
---|
24 | integer infid ! NetCDF input file ID |
---|
25 | integer outfid ! NetCDF output file ID |
---|
26 | integer lon_dimid,lat_dimid,alt_dimid,time_dimid ! NetCDF dimension IDs |
---|
27 | integer lon_varid,lat_varid,alt_varid,time_varid |
---|
28 | integer :: datashape1d ! shape of 1D datasets |
---|
29 | integer,dimension(2) :: datashape2d ! shape of 2D datasets |
---|
30 | integer,dimension(3) :: datashape3d ! shape of 3D datasets |
---|
31 | integer,dimension(4) :: datashape4d ! shape of 4D datasets |
---|
32 | |
---|
33 | real :: miss_val ! special "missing value" to specify missing data |
---|
34 | real,parameter :: miss_val_def=-9.99e+33 ! default value for "missing value" |
---|
35 | real :: pi |
---|
36 | real,dimension(:),allocatable :: lon ! longitude |
---|
37 | integer lonlength ! # of grid points along longitude |
---|
38 | real,dimension(:),allocatable :: lat ! latitude |
---|
39 | real,dimension(:),allocatable :: coslat ! cos of latitude |
---|
40 | integer latlength ! # of grid points along latitude |
---|
41 | real,dimension(:),allocatable :: plev ! Pressure levels (Pa) |
---|
42 | integer altlength ! # of grid point along altitude (of input datasets) |
---|
43 | real,dimension(:),allocatable :: time ! time |
---|
44 | integer timelength ! # of points along time |
---|
45 | real,dimension(:,:,:),allocatable :: ps ! surface pressure |
---|
46 | real,dimension(:,:,:,:),allocatable :: vitv ! meridional wind (in m/s) |
---|
47 | real,dimension(:,:,:,:),allocatable :: za ! areoid levels (m) |
---|
48 | |
---|
49 | real,dimension(:,:,:,:),allocatable :: rayon ! distance to center (m) |
---|
50 | real,dimension(:,:,:,:),allocatable :: grav ! gravity field (m s-2) |
---|
51 | real,dimension(:,:,:,:),allocatable :: dmass ! mass in cell (kg) |
---|
52 | real,dimension(:,:,:,:),allocatable :: vm ! meridional mass flux |
---|
53 | real,dimension(:,:,:),allocatable :: psi ! stream function |
---|
54 | |
---|
55 | integer ierr,ierr1,ierr2 ! NetCDF routines return codes |
---|
56 | integer i,j,ilon,ilat,ilev,itim ! for loops |
---|
57 | logical :: lmdflag |
---|
58 | |
---|
59 | real :: deltalat,deltalon ! lat and lon intervals in radians |
---|
60 | |
---|
61 | include "planet.h" |
---|
62 | |
---|
63 | !=============================================================================== |
---|
64 | ! 1. Input parameters |
---|
65 | !=============================================================================== |
---|
66 | |
---|
67 | pi = 2.*asin(1.) |
---|
68 | miss_val = miss_val_def |
---|
69 | |
---|
70 | write(*,*) "" |
---|
71 | write(*,*) "You are working on the atmosphere of ",planet |
---|
72 | |
---|
73 | !=============================================================================== |
---|
74 | ! 1.1 Input file |
---|
75 | !=============================================================================== |
---|
76 | |
---|
77 | write(*,*) "" |
---|
78 | write(*,*) " Program valid for Venus or Titan LMD output files" |
---|
79 | write(*,*) "Enter input file name:" |
---|
80 | |
---|
81 | read(*,'(a128)') infile |
---|
82 | write(*,*) "" |
---|
83 | |
---|
84 | ! open input file |
---|
85 | |
---|
86 | ierr = NF_OPEN(infile,NF_NOWRITE,infid) |
---|
87 | if (ierr.ne.NF_NOERR) then |
---|
88 | write(*,*) 'ERROR: Pb opening file ',trim(infile) |
---|
89 | stop "" |
---|
90 | endif |
---|
91 | |
---|
92 | !=============================================================================== |
---|
93 | ! 1.2 Get grids in lon,lat,alt(pressure),time |
---|
94 | !=============================================================================== |
---|
95 | |
---|
96 | call get_iddim(infid,lat_varid,latlength,lon_varid,lonlength,& |
---|
97 | alt_varid,altlength,time_varid,timelength,lmdflag ) |
---|
98 | |
---|
99 | allocate(lon(lonlength)) |
---|
100 | ierr=NF_GET_VAR_REAL(infid,lon_varid,lon) |
---|
101 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading longitude" |
---|
102 | |
---|
103 | allocate(lat(latlength)) |
---|
104 | ierr=NF_GET_VAR_REAL(infid,lat_varid,lat) |
---|
105 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading lat" |
---|
106 | |
---|
107 | allocate(coslat(latlength)) |
---|
108 | ! Beware of rounding problems at poles... |
---|
109 | coslat(:) = max(0.,cos(lat(:)*pi/180.)) |
---|
110 | |
---|
111 | ! Lat, lon pressure intervals |
---|
112 | deltalat = abs(lat(2)-lat(1))*pi/180. |
---|
113 | deltalon = abs(lon(2)-lon(1))*pi/180. |
---|
114 | |
---|
115 | allocate(plev(altlength)) |
---|
116 | ierr=NF_GET_VAR_REAL(infid,alt_varid,plev) |
---|
117 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading altitude (ie pressure levels)" |
---|
118 | |
---|
119 | allocate(time(timelength)) |
---|
120 | ierr=NF_GET_VAR_REAL(infid,time_varid,time) |
---|
121 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading time" |
---|
122 | |
---|
123 | ! Time axis IN PLANET DAYS |
---|
124 | |
---|
125 | time=time/localday |
---|
126 | |
---|
127 | !=============================================================================== |
---|
128 | ! 1.3 Get output file name |
---|
129 | !=============================================================================== |
---|
130 | write(*,*) "" |
---|
131 | !write(*,*) "Enter output file name" |
---|
132 | !read(*,*) outfile |
---|
133 | outfile=infile(1:len_trim(infile)-3)//"_PSI.nc" |
---|
134 | write(*,*) "Output file name is: "//trim(outfile) |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | !=============================================================================== |
---|
139 | ! 2.1 Store needed fields |
---|
140 | !=============================================================================== |
---|
141 | |
---|
142 | !=============================================================================== |
---|
143 | ! 2.1.1 Surface pressure |
---|
144 | !=============================================================================== |
---|
145 | allocate(ps(lonlength,latlength,timelength)) |
---|
146 | |
---|
147 | text="ps" |
---|
148 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
149 | if (ierr1.ne.NF_NOERR) then |
---|
150 | write(*,*) " looking for psol instead... " |
---|
151 | text="psol" |
---|
152 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
153 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get psol ID" |
---|
154 | endif |
---|
155 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading surface pressure" |
---|
156 | |
---|
157 | !=============================================================================== |
---|
158 | ! 2.1.3 Winds |
---|
159 | !=============================================================================== |
---|
160 | allocate(vitv(lonlength,latlength,altlength,timelength)) |
---|
161 | |
---|
162 | ! meridional wind vitv (in m/s) |
---|
163 | text="vitv" |
---|
164 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,vitv,miss_val,ierr1,ierr2) |
---|
165 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get vitv ID" |
---|
166 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading meridional wind" |
---|
167 | |
---|
168 | !=============================================================================== |
---|
169 | ! 2.1.4 Altitude above areoide |
---|
170 | !=============================================================================== |
---|
171 | |
---|
172 | allocate(za(lonlength,latlength,altlength,timelength)) |
---|
173 | ! present only in _P regrided files |
---|
174 | ! For others, using geop/g0 |
---|
175 | |
---|
176 | text="zareoid" |
---|
177 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,za,miss_val,ierr1,ierr2) |
---|
178 | if (ierr1.ne.NF_NOERR) then |
---|
179 | write(*,*) " looking for geop instead... " |
---|
180 | text="geop" |
---|
181 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,za,miss_val,ierr1,ierr2) |
---|
182 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get geop ID" |
---|
183 | do itim=1,timelength |
---|
184 | do ilon=1,lonlength |
---|
185 | do ilat=1,latlength |
---|
186 | do ilev=1,altlength |
---|
187 | if (za(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
188 | za(ilon,ilat,ilev,itim) = za(ilon,ilat,ilev,itim)/(g0*1000.) ! in km |
---|
189 | else |
---|
190 | za(ilon,ilat,ilev,itim) = miss_val |
---|
191 | endif |
---|
192 | enddo |
---|
193 | enddo |
---|
194 | enddo |
---|
195 | enddo |
---|
196 | endif |
---|
197 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading zareoid/geop" |
---|
198 | |
---|
199 | !=============================================================================== |
---|
200 | ! 2.2 Computations |
---|
201 | !=============================================================================== |
---|
202 | |
---|
203 | !=============================================================================== |
---|
204 | ! 2.2.2 Mass in cells |
---|
205 | !=============================================================================== |
---|
206 | allocate(rayon(lonlength,latlength,altlength,timelength)) |
---|
207 | allocate(grav(lonlength,latlength,altlength,timelength)) |
---|
208 | allocate(dmass(lonlength,latlength,altlength,timelength)) |
---|
209 | |
---|
210 | do itim=1,timelength |
---|
211 | do ilon=1,lonlength |
---|
212 | do ilat=1,latlength |
---|
213 | do ilev=1,altlength |
---|
214 | ! Need to be consistent with GCM computations |
---|
215 | if (za(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
216 | rayon(ilon,ilat,ilev,itim) = a0 |
---|
217 | ! rayon(ilon,ilat,ilev,itim) = za(ilon,ilat,ilev,itim) + a0 |
---|
218 | grav(ilon,ilat,ilev,itim) = g0*a0*a0 & |
---|
219 | /(rayon(ilon,ilat,ilev,itim)*rayon(ilon,ilat,ilev,itim)) |
---|
220 | else |
---|
221 | rayon(ilon,ilat,ilev,itim) = miss_val |
---|
222 | grav(ilon,ilat,ilev,itim) = miss_val |
---|
223 | endif |
---|
224 | enddo |
---|
225 | enddo |
---|
226 | enddo |
---|
227 | enddo ! timelength |
---|
228 | |
---|
229 | call cellmass(infid,latlength,lonlength,altlength,timelength,lmdflag, & |
---|
230 | miss_val,deltalon,deltalat,coslat,plev,ps,grav,rayon, & |
---|
231 | dmass ) |
---|
232 | |
---|
233 | !=============================================================================== |
---|
234 | ! 2.2.8 Stream function |
---|
235 | !=============================================================================== |
---|
236 | allocate(vm(lonlength,latlength,altlength,timelength)) |
---|
237 | allocate(psi(latlength,altlength,timelength)) |
---|
238 | |
---|
239 | do itim=1,timelength |
---|
240 | |
---|
241 | do ilat=1,latlength |
---|
242 | do ilev=1,altlength |
---|
243 | do ilon=1,lonlength |
---|
244 | if (dmass(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
245 | vm(ilon,ilat,ilev,itim) = vitv(ilon,ilat,ilev,itim) & |
---|
246 | * dmass(ilon,ilat,ilev,itim) & |
---|
247 | / (rayon(ilon,ilat,ilev,itim)*deltalat) |
---|
248 | else |
---|
249 | vm(ilon,ilat,ilev,itim) = miss_val |
---|
250 | endif |
---|
251 | enddo |
---|
252 | enddo |
---|
253 | enddo |
---|
254 | |
---|
255 | |
---|
256 | do ilat=1,latlength |
---|
257 | psi(ilat,altlength,itim) = 0. |
---|
258 | do ilon=1,lonlength |
---|
259 | if (vm(ilon,ilat,altlength,itim).ne.miss_val) then |
---|
260 | psi(ilat,altlength,itim) = psi(ilat,altlength,itim) & |
---|
261 | + vm(ilon,ilat,altlength,itim) |
---|
262 | endif |
---|
263 | enddo |
---|
264 | do ilev=altlength-1,1,-1 |
---|
265 | psi(ilat,ilev,itim) = psi(ilat,ilev+1,itim) |
---|
266 | do ilon=1,lonlength |
---|
267 | if (vm(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
268 | psi(ilat,ilev,itim) = psi(ilat,ilev,itim) & |
---|
269 | + vm(ilon,ilat,ilev,itim) |
---|
270 | endif |
---|
271 | enddo |
---|
272 | enddo |
---|
273 | enddo |
---|
274 | |
---|
275 | enddo ! timelength |
---|
276 | |
---|
277 | print*,"End of computations" |
---|
278 | |
---|
279 | !=============================================================================== |
---|
280 | ! 3. Create output file |
---|
281 | !=============================================================================== |
---|
282 | |
---|
283 | ! Create output file |
---|
284 | ierr=NF_CREATE(outfile,NF_CLOBBER,outfid) |
---|
285 | if (ierr.ne.NF_NOERR) then |
---|
286 | write(*,*)"Error: could not create file ",outfile |
---|
287 | stop |
---|
288 | endif |
---|
289 | |
---|
290 | !=============================================================================== |
---|
291 | ! 3.1. Define and write dimensions |
---|
292 | !=============================================================================== |
---|
293 | |
---|
294 | call write_dim(outfid,lonlength,latlength,altlength,timelength, & |
---|
295 | lon,lat,plev,time,lon_dimid,lat_dimid,alt_dimid,time_dimid) |
---|
296 | |
---|
297 | !=============================================================================== |
---|
298 | ! 3.2. Define and write variables |
---|
299 | !=============================================================================== |
---|
300 | |
---|
301 | ! 1D Variables |
---|
302 | |
---|
303 | datashape1d=time_dimid |
---|
304 | |
---|
305 | ! 3D variables |
---|
306 | |
---|
307 | datashape3d(1)=lon_dimid |
---|
308 | datashape3d(2)=lat_dimid |
---|
309 | datashape3d(3)=time_dimid |
---|
310 | |
---|
311 | call write_var3d(outfid,datashape3d,lonlength,latlength,timelength,& |
---|
312 | "ps ", "Surface pressure ","Pa ",miss_val,& |
---|
313 | ps ) |
---|
314 | |
---|
315 | datashape3d(1)=lat_dimid |
---|
316 | datashape3d(2)=alt_dimid |
---|
317 | datashape3d(3)=time_dimid |
---|
318 | |
---|
319 | call write_var3d(outfid,datashape3d,lonlength,latlength,timelength,& |
---|
320 | "psi ", "Stream function ","kg/s ",miss_val,& |
---|
321 | psi ) |
---|
322 | |
---|
323 | ! 4D variables |
---|
324 | |
---|
325 | datashape4d(1)=lon_dimid |
---|
326 | datashape4d(2)=lat_dimid |
---|
327 | datashape4d(3)=alt_dimid |
---|
328 | datashape4d(4)=time_dimid |
---|
329 | |
---|
330 | call write_var4d(outfid,datashape4d,lonlength,latlength,altlength,timelength,& |
---|
331 | "dmass ", "Mass ","kg ",miss_val,& |
---|
332 | dmass ) |
---|
333 | |
---|
334 | !!!! Close output file |
---|
335 | ierr=NF_CLOSE(outfid) |
---|
336 | if (ierr.ne.NF_NOERR) then |
---|
337 | write(*,*) 'Error, failed to close output file ',outfile |
---|
338 | endif |
---|
339 | |
---|
340 | |
---|
341 | end program |
---|