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=9.99e+33 ! 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 :: latrad ! latitude in radian |
---|
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 | |
---|
48 | real,dimension(:,:,:,:),allocatable :: rayon ! distance to center (m) |
---|
49 | real,dimension(:,:,:,:),allocatable :: grav ! gravity field (m s-2) |
---|
50 | real,dimension(:,:,:,:),allocatable :: dmass ! mass in cell (kg) |
---|
51 | real,dimension(:,:,:,:),allocatable :: vm ! meridional mass flux |
---|
52 | real,dimension(:,:,:),allocatable :: psi ! stream function |
---|
53 | |
---|
54 | integer ierr,ierr1,ierr2 ! NetCDF routines return codes |
---|
55 | integer i,j,ilon,ilat,ilev,itim ! for loops |
---|
56 | logical :: lmdflag |
---|
57 | |
---|
58 | real :: deltalat,deltalon ! lat and lon intervals in radians |
---|
59 | |
---|
60 | include "planet.h" |
---|
61 | |
---|
62 | !=============================================================================== |
---|
63 | ! 1. Input parameters |
---|
64 | !=============================================================================== |
---|
65 | |
---|
66 | pi = 2.*asin(1.) |
---|
67 | |
---|
68 | write(*,*) "" |
---|
69 | write(*,*) "You are working on the atmosphere of ",planet |
---|
70 | |
---|
71 | !=============================================================================== |
---|
72 | ! 1.1 Input file |
---|
73 | !=============================================================================== |
---|
74 | |
---|
75 | write(*,*) "" |
---|
76 | write(*,*) " Program valid for Venus or Titan LMD output files" |
---|
77 | write(*,*) "Enter input file name:" |
---|
78 | |
---|
79 | read(*,'(a128)') infile |
---|
80 | write(*,*) "" |
---|
81 | |
---|
82 | ! open input file |
---|
83 | |
---|
84 | ierr = NF_OPEN(infile,NF_NOWRITE,infid) |
---|
85 | if (ierr.ne.NF_NOERR) then |
---|
86 | write(*,*) 'ERROR: Pb opening file ',trim(infile) |
---|
87 | stop "" |
---|
88 | endif |
---|
89 | |
---|
90 | !=============================================================================== |
---|
91 | ! 1.2 Get grids in lon,lat,alt(pressure),time |
---|
92 | !=============================================================================== |
---|
93 | |
---|
94 | call get_iddim(infid,lat_varid,latlength,lon_varid,lonlength,& |
---|
95 | alt_varid,altlength,time_varid,timelength,lmdflag ) |
---|
96 | |
---|
97 | allocate(lon(lonlength)) |
---|
98 | ierr=NF_GET_VAR_REAL(infid,lon_varid,lon) |
---|
99 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading longitude" |
---|
100 | |
---|
101 | allocate(lat(latlength)) |
---|
102 | ierr=NF_GET_VAR_REAL(infid,lat_varid,lat) |
---|
103 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading lat" |
---|
104 | |
---|
105 | allocate(latrad(latlength)) |
---|
106 | latrad = lat*pi/180. |
---|
107 | |
---|
108 | ! Lat, lon pressure intervals |
---|
109 | deltalat = abs(latrad(2)-latrad(1)) |
---|
110 | deltalon = abs(lon(2)-lon(1))*pi/180. |
---|
111 | |
---|
112 | allocate(plev(altlength)) |
---|
113 | ierr=NF_GET_VAR_REAL(infid,alt_varid,plev) |
---|
114 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading altitude (ie pressure levels)" |
---|
115 | |
---|
116 | allocate(time(timelength)) |
---|
117 | ierr=NF_GET_VAR_REAL(infid,time_varid,time) |
---|
118 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading time" |
---|
119 | |
---|
120 | ! Time axis IN PLANET DAYS |
---|
121 | |
---|
122 | time=time/localday |
---|
123 | |
---|
124 | !=============================================================================== |
---|
125 | ! 1.3 Get output file name |
---|
126 | !=============================================================================== |
---|
127 | write(*,*) "" |
---|
128 | !write(*,*) "Enter output file name" |
---|
129 | !read(*,*) outfile |
---|
130 | outfile=infile(1:len_trim(infile)-3)//"_PSI.nc" |
---|
131 | write(*,*) "Output file name is: "//trim(outfile) |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | !=============================================================================== |
---|
136 | ! 2.1 Store needed fields |
---|
137 | !=============================================================================== |
---|
138 | |
---|
139 | !=============================================================================== |
---|
140 | ! 2.1.1 Surface pressure |
---|
141 | !=============================================================================== |
---|
142 | allocate(ps(lonlength,latlength,timelength)) |
---|
143 | |
---|
144 | text="ps" |
---|
145 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
146 | if (ierr1.ne.NF_NOERR) then |
---|
147 | write(*,*) " looking for psol instead... " |
---|
148 | text="psol" |
---|
149 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
150 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get psol ID" |
---|
151 | endif |
---|
152 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading surface pressure" |
---|
153 | |
---|
154 | !=============================================================================== |
---|
155 | ! 2.1.3 Winds |
---|
156 | !=============================================================================== |
---|
157 | allocate(vitv(lonlength,latlength,altlength,timelength)) |
---|
158 | |
---|
159 | ! meridional wind vitv (in m/s) |
---|
160 | text="vitv" |
---|
161 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,vitv,ierr1,ierr2) |
---|
162 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get vitv ID" |
---|
163 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading meridional wind" |
---|
164 | |
---|
165 | !=============================================================================== |
---|
166 | ! 2.1.4 Altitude above areoide |
---|
167 | !=============================================================================== |
---|
168 | ! Only needed if g(z) on Titan... |
---|
169 | |
---|
170 | !allocate(za(lonlength,latlength,altlength,timelength)) |
---|
171 | |
---|
172 | !text="zareoid" |
---|
173 | !call get_var4d(infid,lonlength,latlength,altlength,timelength,text,za,ierr1,ierr2) |
---|
174 | !if (ierr1.ne.NF_NOERR) stop "Error: Failed to get za ID" |
---|
175 | !if (ierr2.ne.NF_NOERR) stop "Error: Failed reading zareoid" |
---|
176 | |
---|
177 | !=============================================================================== |
---|
178 | ! 2.2 Computations |
---|
179 | !=============================================================================== |
---|
180 | |
---|
181 | !=============================================================================== |
---|
182 | ! 2.2.2 Mass in cells |
---|
183 | !=============================================================================== |
---|
184 | allocate(rayon(lonlength,latlength,altlength,timelength)) |
---|
185 | allocate(grav(lonlength,latlength,altlength,timelength)) |
---|
186 | allocate(dmass(lonlength,latlength,altlength,timelength)) |
---|
187 | |
---|
188 | do itim=1,timelength |
---|
189 | do ilon=1,lonlength |
---|
190 | do ilat=1,latlength |
---|
191 | do ilev=1,altlength |
---|
192 | ! Need to be consistent with GCM computations |
---|
193 | ! if (za(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
194 | rayon(ilon,ilat,ilev,itim) = a0 |
---|
195 | ! rayon(ilon,ilat,ilev,itim) = za(ilon,ilat,ilev,itim) + a0 |
---|
196 | grav(ilon,ilat,ilev,itim) = g0*a0*a0 & |
---|
197 | /(rayon(ilon,ilat,ilev,itim)*rayon(ilon,ilat,ilev,itim)) |
---|
198 | ! else |
---|
199 | ! rayon(ilon,ilat,ilev,itim) = miss_val |
---|
200 | ! grav(ilon,ilat,ilev,itim) = miss_val |
---|
201 | ! endif |
---|
202 | enddo |
---|
203 | enddo |
---|
204 | enddo |
---|
205 | enddo ! timelength |
---|
206 | |
---|
207 | call cellmass(infid,latlength,lonlength,altlength,timelength, & |
---|
208 | lmdflag,deltalon,deltalat,latrad,plev,ps,grav,rayon, & |
---|
209 | dmass ) |
---|
210 | |
---|
211 | !=============================================================================== |
---|
212 | ! 2.2.8 Stream function |
---|
213 | !=============================================================================== |
---|
214 | allocate(vm(lonlength,latlength,altlength,timelength)) |
---|
215 | allocate(psi(latlength,altlength,timelength)) |
---|
216 | |
---|
217 | do itim=1,timelength |
---|
218 | |
---|
219 | do ilat=1,latlength |
---|
220 | do ilev=1,altlength |
---|
221 | do ilon=1,lonlength |
---|
222 | if (rayon(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
223 | vm(ilon,ilat,ilev,itim) = vitv(ilon,ilat,ilev,itim) & |
---|
224 | * dmass(ilon,ilat,ilev,itim) & |
---|
225 | / (rayon(ilon,ilat,ilev,itim)*deltalat) |
---|
226 | else |
---|
227 | vm(ilon,ilat,ilev,itim) = miss_val |
---|
228 | endif |
---|
229 | enddo |
---|
230 | enddo |
---|
231 | enddo |
---|
232 | |
---|
233 | |
---|
234 | do ilat=1,latlength |
---|
235 | psi(ilat,altlength,itim) = 0. |
---|
236 | do ilon=1,lonlength |
---|
237 | if (rayon(ilon,ilat,altlength,itim).ne.miss_val) then |
---|
238 | psi(ilat,altlength,itim) = psi(ilat,altlength,itim) & |
---|
239 | + vm(ilon,ilat,altlength,itim) |
---|
240 | endif |
---|
241 | enddo |
---|
242 | do ilev=altlength-1,1,-1 |
---|
243 | psi(ilat,ilev,itim) = psi(ilat,ilev+1,itim) |
---|
244 | do ilon=1,lonlength |
---|
245 | if (rayon(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
246 | psi(ilat,ilev,itim) = psi(ilat,ilev,itim) & |
---|
247 | + vm(ilon,ilat,ilev,itim) |
---|
248 | endif |
---|
249 | enddo |
---|
250 | enddo |
---|
251 | enddo |
---|
252 | |
---|
253 | enddo ! timelength |
---|
254 | |
---|
255 | print*,"End of computations" |
---|
256 | |
---|
257 | !=============================================================================== |
---|
258 | ! 3. Create output file |
---|
259 | !=============================================================================== |
---|
260 | |
---|
261 | ! Create output file |
---|
262 | ierr=NF_CREATE(outfile,NF_CLOBBER,outfid) |
---|
263 | if (ierr.ne.NF_NOERR) then |
---|
264 | write(*,*)"Error: could not create file ",outfile |
---|
265 | stop |
---|
266 | endif |
---|
267 | |
---|
268 | !=============================================================================== |
---|
269 | ! 3.1. Define and write dimensions |
---|
270 | !=============================================================================== |
---|
271 | |
---|
272 | call write_dim(outfid,lonlength,latlength,altlength,timelength, & |
---|
273 | lon,lat,plev,time,lon_dimid,lat_dimid,alt_dimid,time_dimid) |
---|
274 | |
---|
275 | !=============================================================================== |
---|
276 | ! 3.2. Define and write variables |
---|
277 | !=============================================================================== |
---|
278 | |
---|
279 | ! 1D Variables |
---|
280 | |
---|
281 | datashape1d=time_dimid |
---|
282 | |
---|
283 | ! 3D variables |
---|
284 | |
---|
285 | datashape3d(1)=lon_dimid |
---|
286 | datashape3d(2)=lat_dimid |
---|
287 | datashape3d(3)=time_dimid |
---|
288 | |
---|
289 | call write_var3d(outfid,datashape3d,lonlength,latlength,timelength,& |
---|
290 | "ps ", "Surface pressure ","Pa ",miss_val,& |
---|
291 | ps ) |
---|
292 | |
---|
293 | datashape3d(1)=lat_dimid |
---|
294 | datashape3d(2)=alt_dimid |
---|
295 | datashape3d(3)=time_dimid |
---|
296 | |
---|
297 | call write_var3d(outfid,datashape3d,lonlength,latlength,timelength,& |
---|
298 | "psi ", "Stream function ","kg/s ",miss_val,& |
---|
299 | psi ) |
---|
300 | |
---|
301 | ! 4D variables |
---|
302 | |
---|
303 | datashape4d(1)=lon_dimid |
---|
304 | datashape4d(2)=lat_dimid |
---|
305 | datashape4d(3)=alt_dimid |
---|
306 | datashape4d(4)=time_dimid |
---|
307 | |
---|
308 | call write_var4d(outfid,datashape4d,lonlength,latlength,altlength,timelength,& |
---|
309 | "dmass ", "Mass ","kg ",miss_val,& |
---|
310 | dmass ) |
---|
311 | |
---|
312 | !!!! Close output file |
---|
313 | ierr=NF_CLOSE(outfid) |
---|
314 | if (ierr.ne.NF_NOERR) then |
---|
315 | write(*,*) 'Error, failed to close output file ',outfile |
---|
316 | endif |
---|
317 | |
---|
318 | |
---|
319 | end program |
---|