[816] | 1 | program energy |
---|
| 2 | |
---|
| 3 | ! SL 12/2009: |
---|
| 4 | ! This program reads 4D (lon-lat-alt-time) fields directly from LMD outputs without regrid : histmth |
---|
| 5 | ! |
---|
| 6 | ! it computes: |
---|
| 7 | ! dmass -- 4D -- mass of each cell |
---|
| 8 | ! sek -- 4D -- specific kinetic energy |
---|
| 9 | ! ek -- 1D -- integrated kinetic energy |
---|
| 10 | ! sep -- 4D -- specific potential energy |
---|
| 11 | ! ep -- 1D -- integrated potential energy |
---|
| 12 | ! |
---|
| 13 | ! Minimal requirements and dependencies: |
---|
| 14 | ! The dataset must include the following data: |
---|
| 15 | ! - surface pressure |
---|
| 16 | ! - atmospheric temperature |
---|
| 17 | ! - zonal and meridional winds |
---|
| 18 | |
---|
| 19 | ! VERTICAL WIND SPEED IS NEGLECTED IN KINETIC ENERGY |
---|
| 20 | |
---|
| 21 | implicit none |
---|
| 22 | |
---|
| 23 | include "netcdf.inc" ! NetCDF definitions |
---|
| 24 | |
---|
| 25 | character (len=128) :: infile ! input file name (name_P.nc) |
---|
| 26 | character (len=128) :: outfile ! output file name |
---|
| 27 | |
---|
| 28 | character (len=64) :: text ! to store some text |
---|
| 29 | integer infid ! NetCDF input file ID |
---|
| 30 | integer outfid ! NetCDF output file ID |
---|
| 31 | integer lon_dimid,lat_dimid,alt_dimid,time_dimid ! NetCDF dimension IDs |
---|
| 32 | integer lon_varid,lat_varid,alt_varid,time_varid |
---|
| 33 | integer :: datashape1d ! shape of 1D datasets |
---|
| 34 | integer,dimension(2) :: datashape2d ! shape of 2D datasets |
---|
| 35 | integer,dimension(3) :: datashape3d ! shape of 3D datasets |
---|
| 36 | integer,dimension(4) :: datashape4d ! shape of 4D datasets |
---|
| 37 | |
---|
[1454] | 38 | real :: miss_val ! special "missing value" to specify missing data |
---|
| 39 | real,parameter :: miss_val_def=-9.99e+33 ! default value for "missing value" |
---|
[816] | 40 | real :: pi |
---|
| 41 | real,dimension(:),allocatable :: lon ! longitude |
---|
| 42 | integer lonlength ! # of grid points along longitude |
---|
| 43 | real,dimension(:),allocatable :: lat ! latitude |
---|
[1454] | 44 | real,dimension(:),allocatable :: coslat ! cos of latitude |
---|
[816] | 45 | integer latlength ! # of grid points along latitude |
---|
| 46 | real,dimension(:),allocatable :: plev ! Pressure levels (Pa) |
---|
| 47 | integer altlength ! # of grid point along presnivs (of input datasets) |
---|
| 48 | real,dimension(:),allocatable :: time ! time |
---|
| 49 | integer timelength ! # of points along time |
---|
| 50 | real,dimension(:,:,:),allocatable :: ps ! surface pressure |
---|
| 51 | real,dimension(:,:,:,:),allocatable :: temp ! atmospheric temperature |
---|
| 52 | real,dimension(:,:,:,:),allocatable :: vitu ! zonal wind (in m/s) |
---|
| 53 | real,dimension(:,:,:,:),allocatable :: vitv ! meridional wind (in m/s) |
---|
| 54 | |
---|
| 55 | real,dimension(:,:,:,:),allocatable :: rayon ! distance to center (m) |
---|
| 56 | real,dimension(:,:,:,:),allocatable :: grav ! gravity field (m s-2) |
---|
| 57 | real,dimension(:,:,:,:),allocatable :: dmass ! mass in cell (kg) |
---|
| 58 | real,dimension(:,:,:,:),allocatable :: sek ! specific kinetic energy |
---|
| 59 | real,dimension(:),allocatable :: ek ! total kinetic energy |
---|
| 60 | real,dimension(:,:,:,:),allocatable :: sep ! specific potential energy |
---|
| 61 | real,dimension(:),allocatable :: ep ! total potential energy |
---|
| 62 | |
---|
| 63 | integer ierr,ierr1,ierr2 ! NetCDF routines return codes |
---|
| 64 | integer i,j,ilon,ilat,ilev,itim ! for loops |
---|
| 65 | |
---|
| 66 | real :: deltalat,deltalon ! lat and lon intervals in radians |
---|
| 67 | real,dimension(:,:,:,:),allocatable :: deltap ! pressure thickness of each layer (Pa) |
---|
| 68 | real :: tmpp ! temporary pressure |
---|
| 69 | real :: dz ! altitude diff |
---|
| 70 | real :: signe ! orientation of lon axis for mountain torque computation |
---|
| 71 | logical :: lmdflag |
---|
| 72 | |
---|
| 73 | real :: cpdet |
---|
| 74 | |
---|
| 75 | include "planet.h" |
---|
| 76 | |
---|
| 77 | !=============================================================================== |
---|
| 78 | ! 1. Input parameters |
---|
| 79 | !=============================================================================== |
---|
| 80 | |
---|
| 81 | pi = 2.*asin(1.) |
---|
[1454] | 82 | miss_val = miss_val_def |
---|
[816] | 83 | |
---|
| 84 | write(*,*) "" |
---|
| 85 | write(*,*) "You are working on the atmosphere of ",planet |
---|
| 86 | |
---|
| 87 | !=============================================================================== |
---|
| 88 | ! 1.1 Input file |
---|
| 89 | !=============================================================================== |
---|
| 90 | |
---|
| 91 | write(*,*) "" |
---|
| 92 | write(*,*) " Program valid for Venus or Titan LMD, or Venus CAM output files" |
---|
| 93 | write(*,*) "Enter input file name:" |
---|
| 94 | |
---|
| 95 | read(*,'(a128)') infile |
---|
| 96 | write(*,*) "" |
---|
| 97 | |
---|
| 98 | ! open input file |
---|
| 99 | |
---|
| 100 | ierr = NF_OPEN(infile,NF_NOWRITE,infid) |
---|
| 101 | if (ierr.ne.NF_NOERR) then |
---|
| 102 | write(*,*) 'ERROR: Pb opening file ',trim(infile) |
---|
| 103 | stop "" |
---|
| 104 | endif |
---|
| 105 | |
---|
| 106 | !=============================================================================== |
---|
| 107 | ! 1.2 Get grids in lon,lat,alt(pressure),time |
---|
| 108 | !=============================================================================== |
---|
| 109 | |
---|
| 110 | call get_iddim(infid,lat_varid,latlength,lon_varid,lonlength,& |
---|
| 111 | alt_varid,altlength,time_varid,timelength,lmdflag ) |
---|
| 112 | |
---|
| 113 | allocate(lon(lonlength)) |
---|
| 114 | ierr=NF_GET_VAR_REAL(infid,lon_varid,lon) |
---|
| 115 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading longitude" |
---|
| 116 | if(lon(1).gt.lon(2)) then |
---|
| 117 | signe=-1. |
---|
| 118 | else |
---|
| 119 | signe=1. |
---|
| 120 | endif |
---|
| 121 | |
---|
| 122 | allocate(lat(latlength)) |
---|
| 123 | ierr=NF_GET_VAR_REAL(infid,lat_varid,lat) |
---|
| 124 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading lat" |
---|
| 125 | |
---|
[1454] | 126 | allocate(coslat(latlength)) |
---|
| 127 | ! Beware of rounding problems at poles... |
---|
| 128 | coslat(:) = max(0.,cos(lat(:)*pi/180.)) |
---|
[816] | 129 | |
---|
| 130 | ! Lat, lon pressure intervals |
---|
[1454] | 131 | deltalat = abs(lat(2)-lat(1))*pi/180. |
---|
[816] | 132 | deltalon = abs(lon(2)-lon(1))*pi/180. |
---|
| 133 | |
---|
| 134 | allocate(plev(altlength)) |
---|
| 135 | ierr=NF_GET_VAR_REAL(infid,alt_varid,plev) |
---|
| 136 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading presnivs (ie pressure levels)" |
---|
| 137 | if(.not.lmdflag) then |
---|
| 138 | ! in CAM files, pressure in mbar and reversed... |
---|
| 139 | call reverselev(altlength,plev) |
---|
| 140 | plev=plev*100. ! convertion to Pa |
---|
| 141 | endif |
---|
| 142 | |
---|
| 143 | allocate(time(timelength)) |
---|
| 144 | ierr=NF_GET_VAR_REAL(infid,time_varid,time) |
---|
| 145 | if (ierr.ne.NF_NOERR) stop "Error: Failed reading time" |
---|
| 146 | |
---|
| 147 | ! Time axis IN PLANET DAYS |
---|
| 148 | |
---|
| 149 | if(.not.lmdflag) then |
---|
| 150 | ! in CAM files, time in Earth days... |
---|
| 151 | ! => seconds |
---|
| 152 | time=time*86400. |
---|
| 153 | endif |
---|
| 154 | time=time/localday |
---|
| 155 | |
---|
| 156 | !=============================================================================== |
---|
| 157 | ! 1.3 Get output file name |
---|
| 158 | !=============================================================================== |
---|
| 159 | write(*,*) "" |
---|
| 160 | !write(*,*) "Enter output file name" |
---|
| 161 | !read(*,*) outfile |
---|
| 162 | outfile=infile(1:len_trim(infile)-3)//"_NRG.nc" |
---|
| 163 | write(*,*) "Output file name is: "//trim(outfile) |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | !=============================================================================== |
---|
| 168 | ! 2.1 Store needed fields |
---|
| 169 | !=============================================================================== |
---|
| 170 | |
---|
| 171 | !=============================================================================== |
---|
| 172 | ! 2.1.1 Surface pressure |
---|
| 173 | !=============================================================================== |
---|
| 174 | allocate(ps(lonlength,latlength,timelength)) |
---|
| 175 | |
---|
| 176 | text="ps" |
---|
| 177 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
| 178 | if (ierr1.ne.NF_NOERR) then |
---|
| 179 | write(*,*) " looking for psol instead... " |
---|
| 180 | text="psol" |
---|
| 181 | call get_var3d(infid,lonlength,latlength,timelength,text,ps,ierr1,ierr2) |
---|
| 182 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get psol ID" |
---|
| 183 | endif |
---|
| 184 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading surface pressure" |
---|
| 185 | if((.not.lmdflag).and.(planet.eq."Venus")) call reverse3d(lonlength,latlength,timelength,ps) |
---|
| 186 | |
---|
| 187 | !=============================================================================== |
---|
| 188 | ! 2.1.2 Atmospheric temperature |
---|
| 189 | !=============================================================================== |
---|
| 190 | allocate(temp(lonlength,latlength,altlength,timelength)) |
---|
| 191 | |
---|
| 192 | if(lmdflag) then |
---|
| 193 | text="temp" |
---|
| 194 | else |
---|
| 195 | text="T" |
---|
| 196 | endif |
---|
[1454] | 197 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,temp,miss_val,ierr1,ierr2) |
---|
[816] | 198 | if (ierr1.ne.NF_NOERR) then |
---|
| 199 | write(*,*) " looking for t instead... " |
---|
| 200 | text="t" |
---|
[1454] | 201 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,temp,miss_val,ierr1,ierr2) |
---|
[816] | 202 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get temperature ID" |
---|
| 203 | endif |
---|
| 204 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading temperature" |
---|
| 205 | |
---|
| 206 | if(.not.lmdflag) call reverse4d(lonlength,latlength,altlength,timelength,temp) |
---|
| 207 | |
---|
| 208 | !=============================================================================== |
---|
| 209 | ! 2.1.3 Winds |
---|
| 210 | !=============================================================================== |
---|
| 211 | allocate(vitu(lonlength,latlength,altlength,timelength)) |
---|
| 212 | allocate(vitv(lonlength,latlength,altlength,timelength)) |
---|
| 213 | |
---|
| 214 | ! zonal wind vitu (in m/s) |
---|
| 215 | if(lmdflag) then |
---|
| 216 | text="vitu" |
---|
| 217 | else |
---|
| 218 | text="U" |
---|
| 219 | endif |
---|
[1454] | 220 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,vitu,miss_val,ierr1,ierr2) |
---|
[816] | 221 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get vitu ID" |
---|
| 222 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading zonal wind" |
---|
| 223 | |
---|
| 224 | if(.not.lmdflag) call reverse4d(lonlength,latlength,altlength,timelength,vitu) |
---|
| 225 | |
---|
| 226 | ! meridional wind vitv (in m/s) |
---|
| 227 | if(lmdflag) then |
---|
| 228 | text="vitv" |
---|
| 229 | else |
---|
| 230 | text="V" |
---|
| 231 | endif |
---|
[1454] | 232 | call get_var4d(infid,lonlength,latlength,altlength,timelength,text,vitv,miss_val,ierr1,ierr2) |
---|
[816] | 233 | if (ierr1.ne.NF_NOERR) stop "Error: Failed to get vitv ID" |
---|
| 234 | if (ierr2.ne.NF_NOERR) stop "Error: Failed reading meridional wind" |
---|
| 235 | |
---|
| 236 | if(.not.lmdflag) call reverse4d(lonlength,latlength,altlength,timelength,vitv) |
---|
| 237 | |
---|
| 238 | !=============================================================================== |
---|
| 239 | ! 2.1.4 Altitude above areoide |
---|
| 240 | !=============================================================================== |
---|
| 241 | ! Only needed if g(z) on Titan... |
---|
| 242 | |
---|
| 243 | !allocate(za(lonlength,latlength,altlength,timelength)) |
---|
| 244 | |
---|
| 245 | !text="zareoid" |
---|
[1454] | 246 | !call get_var4d(infid,lonlength,latlength,altlength,timelength,text,za,miss_val,ierr1,ierr2) |
---|
[816] | 247 | !if (ierr1.ne.NF_NOERR) stop "Error: Failed to get za ID" |
---|
| 248 | !if (ierr2.ne.NF_NOERR) stop "Error: Failed reading zareoid" |
---|
| 249 | |
---|
| 250 | !=============================================================================== |
---|
| 251 | ! 2.2 Computations |
---|
| 252 | !=============================================================================== |
---|
| 253 | |
---|
| 254 | !=============================================================================== |
---|
| 255 | ! 2.2.2 Mass in cells |
---|
| 256 | !=============================================================================== |
---|
| 257 | allocate(rayon(lonlength,latlength,altlength,timelength)) |
---|
| 258 | allocate(grav(lonlength,latlength,altlength,timelength)) |
---|
| 259 | allocate(dmass(lonlength,latlength,altlength,timelength)) |
---|
| 260 | |
---|
| 261 | do itim=1,timelength |
---|
| 262 | do ilon=1,lonlength |
---|
| 263 | do ilat=1,latlength |
---|
| 264 | do ilev=1,altlength |
---|
| 265 | ! Need to be consistent with GCM computations |
---|
| 266 | ! if (za(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
| 267 | rayon(ilon,ilat,ilev,itim) = a0 |
---|
| 268 | ! rayon(ilon,ilat,ilev,itim) = za(ilon,ilat,ilev,itim) + a0 |
---|
| 269 | grav(ilon,ilat,ilev,itim) = g0*a0*a0 & |
---|
| 270 | /(rayon(ilon,ilat,ilev,itim)*rayon(ilon,ilat,ilev,itim)) |
---|
| 271 | ! else |
---|
| 272 | ! rayon(ilon,ilat,ilev,itim) = miss_val |
---|
| 273 | ! grav(ilon,ilat,ilev,itim) = miss_val |
---|
| 274 | ! endif |
---|
| 275 | enddo |
---|
| 276 | enddo |
---|
| 277 | enddo |
---|
| 278 | enddo ! timelength |
---|
| 279 | |
---|
[1454] | 280 | call cellmass(infid,latlength,lonlength,altlength,timelength,lmdflag, & |
---|
| 281 | miss_val,deltalon,deltalat,coslat,plev,ps,grav,rayon, & |
---|
[816] | 282 | dmass ) |
---|
| 283 | |
---|
| 284 | !=============================================================================== |
---|
| 285 | ! 2.2.6 Specific energies |
---|
| 286 | !=============================================================================== |
---|
| 287 | allocate(sek(lonlength,latlength,altlength,timelength)) |
---|
| 288 | allocate(sep(lonlength,latlength,altlength,timelength)) |
---|
| 289 | |
---|
| 290 | do itim=1,timelength |
---|
| 291 | |
---|
| 292 | do ilon=1,lonlength |
---|
| 293 | do ilat=1,latlength |
---|
| 294 | do ilev=1,altlength |
---|
| 295 | if (rayon(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
| 296 | if ((vitu(ilon,ilat,ilev,itim).lt.miss_val) & |
---|
| 297 | .and.(vitv(ilon,ilat,ilev,itim).lt.miss_val)) then |
---|
| 298 | sek(ilon,ilat,ilev,itim) = 0.5 * & |
---|
| 299 | ( vitu(ilon,ilat,ilev,itim)*vitu(ilon,ilat,ilev,itim) & |
---|
| 300 | + vitv(ilon,ilat,ilev,itim)*vitv(ilon,ilat,ilev,itim) ) |
---|
| 301 | else |
---|
| 302 | sek(ilon,ilat,ilev,itim) = miss_val |
---|
| 303 | endif |
---|
| 304 | if (temp(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
| 305 | sep(ilon,ilat,ilev,itim) = temp(ilon,ilat,ilev,itim) & |
---|
| 306 | * cpdet(temp(ilon,ilat,ilev,itim)) |
---|
| 307 | else |
---|
| 308 | sep(ilon,ilat,ilev,itim) = miss_val |
---|
| 309 | endif |
---|
| 310 | else |
---|
| 311 | sek(ilon,ilat,ilev,itim) = miss_val |
---|
| 312 | sep(ilon,ilat,ilev,itim) = miss_val |
---|
| 313 | endif |
---|
| 314 | enddo |
---|
| 315 | enddo |
---|
| 316 | enddo |
---|
| 317 | |
---|
| 318 | enddo ! timelength |
---|
| 319 | |
---|
| 320 | !=============================================================================== |
---|
| 321 | ! 2.2.7 Total energies |
---|
| 322 | !=============================================================================== |
---|
| 323 | allocate(ek(timelength)) |
---|
| 324 | allocate(ep(timelength)) |
---|
| 325 | |
---|
| 326 | do itim=1,timelength |
---|
| 327 | |
---|
| 328 | ek(itim) = 0. |
---|
| 329 | ep(itim) = 0. |
---|
| 330 | do ilon=1,lonlength |
---|
| 331 | do ilat=1,latlength |
---|
| 332 | do ilev=1,altlength |
---|
| 333 | if (sek(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
| 334 | ek(itim) = ek(itim) & |
---|
| 335 | + sek(ilon,ilat,ilev,itim) * dmass(ilon,ilat,ilev,itim) |
---|
| 336 | endif |
---|
| 337 | if (sep(ilon,ilat,ilev,itim).ne.miss_val) then |
---|
| 338 | ep(itim) = ep(itim) & |
---|
| 339 | + sep(ilon,ilat,ilev,itim) * dmass(ilon,ilat,ilev,itim) |
---|
| 340 | endif |
---|
| 341 | enddo |
---|
| 342 | enddo |
---|
| 343 | enddo |
---|
| 344 | if (ek(itim).eq.0.) then |
---|
| 345 | ek(itim) = miss_val |
---|
| 346 | ep(itim) = miss_val |
---|
| 347 | endif |
---|
| 348 | |
---|
| 349 | enddo ! timelength |
---|
| 350 | |
---|
| 351 | print*,"End of computations" |
---|
| 352 | |
---|
| 353 | !=============================================================================== |
---|
| 354 | ! 3. Create output file |
---|
| 355 | !=============================================================================== |
---|
| 356 | |
---|
| 357 | ! Create output file |
---|
| 358 | ierr=NF_CREATE(outfile,NF_CLOBBER,outfid) |
---|
| 359 | if (ierr.ne.NF_NOERR) then |
---|
| 360 | write(*,*)"Error: could not create file ",outfile |
---|
| 361 | stop |
---|
| 362 | endif |
---|
| 363 | |
---|
| 364 | !=============================================================================== |
---|
| 365 | ! 3.1. Define and write dimensions |
---|
| 366 | !=============================================================================== |
---|
| 367 | |
---|
| 368 | call write_dim(outfid,lonlength,latlength,altlength,timelength, & |
---|
| 369 | lon,lat,plev,time,lon_dimid,lat_dimid,alt_dimid,time_dimid) |
---|
| 370 | |
---|
| 371 | !=============================================================================== |
---|
| 372 | ! 3.2. Define and write variables |
---|
| 373 | !=============================================================================== |
---|
| 374 | |
---|
| 375 | ! 1D Variables |
---|
| 376 | |
---|
| 377 | datashape1d=time_dimid |
---|
| 378 | |
---|
| 379 | call write_var1d(outfid,datashape1d,timelength,& |
---|
| 380 | "ek ", "Total kinetic energy","J ",miss_val,& |
---|
| 381 | ek ) |
---|
| 382 | |
---|
| 383 | call write_var1d(outfid,datashape1d,timelength,& |
---|
| 384 | "ep ", "Total pot energy ","J ",miss_val,& |
---|
| 385 | ep ) |
---|
| 386 | |
---|
| 387 | ! 3D variables |
---|
| 388 | |
---|
| 389 | datashape3d(1)=lon_dimid |
---|
| 390 | datashape3d(2)=lat_dimid |
---|
| 391 | datashape3d(3)=time_dimid |
---|
| 392 | |
---|
| 393 | call write_var3d(outfid,datashape3d,lonlength,latlength,timelength,& |
---|
| 394 | "ps ", "Surface pressure ","Pa ",miss_val,& |
---|
| 395 | ps ) |
---|
| 396 | |
---|
| 397 | ! 4D variables |
---|
| 398 | |
---|
| 399 | datashape4d(1)=lon_dimid |
---|
| 400 | datashape4d(2)=lat_dimid |
---|
| 401 | datashape4d(3)=alt_dimid |
---|
| 402 | datashape4d(4)=time_dimid |
---|
| 403 | |
---|
| 404 | call write_var4d(outfid,datashape4d,lonlength,latlength,altlength,timelength,& |
---|
| 405 | "dmass ", "Mass ","kg ",miss_val,& |
---|
| 406 | dmass ) |
---|
| 407 | |
---|
| 408 | call write_var4d(outfid,datashape4d,lonlength,latlength,altlength,timelength,& |
---|
| 409 | "sek ", "Specific kin energy ","J/kg ",miss_val,& |
---|
| 410 | sek ) |
---|
| 411 | |
---|
| 412 | call write_var4d(outfid,datashape4d,lonlength,latlength,altlength,timelength,& |
---|
| 413 | "sep ", "Specific pot energy ","J/kg ",miss_val,& |
---|
| 414 | sep ) |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | !!!! Close output file |
---|
| 418 | ierr=NF_CLOSE(outfid) |
---|
| 419 | if (ierr.ne.NF_NOERR) then |
---|
| 420 | write(*,*) 'Error, failed to close output file ',outfile |
---|
| 421 | endif |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | end program |
---|