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