Changeset 2434 for trunk/LMDZ.MARS/util/localtime.F90
- Timestamp:
- Nov 17, 2020, 1:36:34 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.MARS/util/localtime.F90
r2280 r2434 14 14 character (len=50) file 15 15 ! file(): input file(s) names(s) 16 character (len=30), dimension(1 5) :: notconcat17 ! not concat(): names of the (15) variables that won't be concatenated16 character (len=30), dimension(16) :: notprocessed 17 ! notprocessed(): names of the (16) variables that won't be processed 18 18 character (len=50), dimension(:), allocatable :: var 19 ! var(): name(s) of variable(s) that will be concatenated20 character (len= 50) :: tmpvar,title,units19 ! var(): name(s) of variable(s) that will be processed 20 character (len=100) :: tmpvar,long_name,units 21 21 ! tmpvar(): used to temporarily store a variable name 22 ! title(): [netcdf] title attribute22 ! long_name(): [netcdf] long_name attribute 23 23 ! units(): [netcdf] units attribute 24 24 character (len=100) :: filename,vartmp … … 58 58 ! ctldim: [netcdf] "controle" dim ID 59 59 ! timedim: [netcdf] "timedim" dim ID 60 integer :: gcmlayerdim ! NetCDF dimension ID for # of layers in GCM 60 61 integer :: latvar,lonvar,altvar,ctlvar,timevar 61 62 ! latvar: [netcdf] ID of "latitude" variable … … 68 69 ! latlen: # of elements of lat() array 69 70 ! lonlen: # of elements of lon() array 70 ! alt var: # of elements of alt() array71 ! ctl var: # of elements of ctl() array71 ! altlen: # of elements of alt() array 72 ! ctllen: # of elements of ctl() array 72 73 ! timelen: # of elemnets of time() array 73 74 ! timelen_tot:# =timelen or timelen+1 (if 1 more time to interpolate needed) 74 75 ! timelen_lt: # of elemnets of time() array in output 76 integer :: nhour,nsol 77 integer :: GCM_layers ! number of GCM atmospheric layers (may not be 78 ! same as altlen if file is an output of zrecast) 75 79 integer :: nout,latdimout,londimout,altdimout,timedimout,timevarout 76 integer :: nhour,nsol77 80 ! nout: [netcdf] output file ID 78 81 ! latdimout: [netcdf] output latitude (dimension) ID … … 81 84 ! timedimout: [netcdf] output time (dimension) ID 82 85 ! timevarout: [netcdf] ID of output "Time" variable 86 integer :: layerdimout ! NetCDF dimension ID for # of layers in GCM 83 87 integer :: varidout 84 88 ! varidout: [netcdf] variable ID # (of a variable to write to the output file) 85 integer :: Nnot concat,var_ok86 ! Nnot concat: # of (leading)variables that won't be concatenated89 integer :: Nnotprocessed,var_ok 90 ! Nnotprocessed: # of (leading)variables that won't be concatenated 87 91 ! var_ok: flag (0 or 1) 88 92 integer, dimension(4) :: corner,edges,dim … … 143 147 !============================================================================== 144 148 145 notconcat(1)='Time' 146 notconcat(2)='controle' 147 notconcat(3)='rlonu' 148 notconcat(4)='latitude' 149 notconcat(5)='longitude' 150 notconcat(6)='altitude' 151 notconcat(7)='rlatv' 152 notconcat(8)='aps' 153 notconcat(9)='bps' 154 notconcat(10)='ap' 155 notconcat(11)='bp' 156 notconcat(12)='cu' 157 notconcat(13)='cv' 158 notconcat(14)='aire' 159 notconcat(15)='phisinit' 149 notprocessed(1)='Time' 150 notprocessed(2)='controle' 151 notprocessed(3)='rlonu' 152 notprocessed(4)='latitude' 153 notprocessed(5)='longitude' 154 notprocessed(6)='altitude' 155 notprocessed(7)='rlatv' 156 notprocessed(8)='aps' 157 notprocessed(9)='bps' 158 notprocessed(10)='ap' 159 notprocessed(11)='bp' 160 notprocessed(12)='soildepth' 161 notprocessed(13)='cu' 162 notprocessed(14)='cv' 163 notprocessed(15)='aire' 164 notprocessed(16)='phisinit' 165 160 166 161 167 !============================================================================== … … 163 169 !============================================================================== 164 170 write(*,*) 165 Nnot concat=0171 Nnotprocessed=0 166 172 do i=1,nbvarfile 167 173 ierr=NF_INQ_VARNAME(nid,i,vartmp) 168 174 ! vartmp now contains the "name" of variable of ID # i 169 175 var_ok=0 170 do inter=1,1 5171 if (vartmp.eq.not concat(inter)) then176 do inter=1,16 177 if (vartmp.eq.notprocessed(inter)) then 172 178 var_ok=1 173 Nnot concat=Nnotconcat+1179 Nnotprocessed=Nnotprocessed+1 174 180 endif 175 181 enddo … … 177 183 enddo 178 184 179 ! Nnot concat: # of variables that won't be processed185 ! Nnotprocessed: # of variables that won't be processed 180 186 ! nbvarfile: total # of variables in file 181 allocate(var(nbvarfile-Nnot concat),stat=ierr)187 allocate(var(nbvarfile-Nnotprocessed),stat=ierr) 182 188 if (ierr.ne.0) then 183 write(*,*) "Error: failed allocation of var(nbvarfile-Nnot concat)"189 write(*,*) "Error: failed allocation of var(nbvarfile-Nnotprocessed)" 184 190 write(*,*) " nbvarfile=",nbvarfile 185 write(*,*) " Nnot concat=",Nnotconcat191 write(*,*) " Nnotprocessed=",Nnotprocessed 186 192 stop 187 193 endif … … 201 207 202 208 if (tmpvar=="all") then 203 nbvar=nbvarfile-Nnot concat204 do j=Nnot concat+1,nbvarfile205 ierr=nf_inq_varname(nid,j,var(j-Nnot concat))209 nbvar=nbvarfile-Nnotprocessed 210 do j=Nnotprocessed+1,nbvarfile 211 ierr=nf_inq_varname(nid,j,var(j-Nnotprocessed)) 206 212 enddo 207 213 ! Variables names from the file are stored in var() 208 nbvar=nbvarfile-Nnot concat214 nbvar=nbvarfile-Nnotprocessed 209 215 do i=1,nbvar 210 ierr=nf_inq_varname(nid,i+Nnot concat,var(i))216 ierr=nf_inq_varname(nid,i+Nnotprocessed,var(i)) 211 217 write(*,'(a9,1x,i2,1x,a1,1x,a50)') "variable ",i,":",var(i) 212 218 enddo … … 220 226 !============================================================================== 221 227 filename=file(1:len_trim(file)-3)//"_LT.nc" 222 write(*,*) trim(filename)228 write(*,*) "Output file :"//trim(filename) 223 229 224 230 !============================================================================== … … 226 232 !============================================================================== 227 233 228 229 230 231 232 233 234 235 234 write(*,*) 235 write(*,*) "opening "//trim(file)//"..." 236 ierr = NF_OPEN(file,NF_NOWRITE,nid) 237 if (ierr.NE.NF_NOERR) then 238 write(*,*) 'ERROR: Pb opening file '//trim(file) 239 write(*,*) NF_STRERROR(ierr) 240 stop "" 241 endif 236 242 237 243 !============================================================================== … … 277 283 ierr=NF_INQ_DIMLEN(nid,altdim,altlen) 278 284 ! write(*,*) "altlen: ",altlen 285 286 ! load size of aps() or sigma() (in case it is not altlen) 287 ! default is that GCM_layers=altlen 288 ! but for outputs of zrecast, it may be a different value 289 ierr=NF_INQ_DIMID(nid,"GCM_layers",gcmlayerdim) 290 if (ierr.ne.NF_NOERR) then 291 ! didn't find a GCM_layers dimension; therefore we have: 292 GCM_layers=altlen 293 else 294 ! load value of GCM_layers 295 ierr=NF_INQ_DIMLEN(nid,gcmlayerdim,GCM_layers) 296 endif 297 ! write(*,*) "GCM_layers=",GCM_layers 279 298 280 299 ierr=NF_INQ_DIMID(nid,"index",ctldim) … … 299 318 !============================================================================== 300 319 301 ! First call; initialize/allocate 302 allocate(lat(latlen),stat=ierr) 303 if (ierr.ne.0) then 304 write(*,*) "Error: failed to allocate lat(latlen)" 305 stop 306 endif 307 allocate(lon(lonlen),stat=ierr) 308 if (ierr.ne.0) then 309 write(*,*) "Error: failed to allocate lon(lonlen)" 310 stop 311 endif 312 allocate(alt(altlen),stat=ierr) 313 if (ierr.ne.0) then 314 write(*,*) "Error: failed to allocate alt(altlen)" 315 stop 316 endif 317 allocate(ctl(ctllen),stat=ierr) 318 if (ierr.ne.0) then 319 write(*,*) "Error: failed to allocate ctl(ctllen)" 320 stop 321 endif 322 323 #ifdef NC_DOUBLE 324 ierr = NF_GET_VAR_DOUBLE(nid,latvar,lat) 325 #else 326 ierr = NF_GET_VAR_REAL(nid,latvar,lat) 327 #endif 328 if (ierr.ne.0) then 329 write(*,*) "Error: failed to load latitude" 330 write(*,*) NF_STRERROR(ierr) 331 stop 332 endif 333 334 #ifdef NC_DOUBLE 335 ierr = NF_GET_VAR_DOUBLE(nid,lonvar,lon) 336 #else 337 ierr = NF_GET_VAR_REAL(nid,lonvar,lon) 338 #endif 339 if (ierr.ne.0) then 340 write(*,*) "Error: failed to load longitude" 341 write(*,*) NF_STRERROR(ierr) 342 stop 343 endif 344 345 #ifdef NC_DOUBLE 346 ierr = NF_GET_VAR_DOUBLE(nid,altvar,alt) 347 #else 348 ierr = NF_GET_VAR_REAL(nid,altvar,alt) 349 #endif 350 if (ierr.ne.0) then 351 write(*,*) "Error: failed to load altitude" 352 write(*,*) NF_STRERROR(ierr) 353 stop 354 endif 320 allocate(lat(latlen),stat=ierr) 321 if (ierr.ne.0) then 322 write(*,*) "Error: failed to allocate lat(latlen)" 323 stop 324 endif 325 allocate(lon(lonlen),stat=ierr) 326 if (ierr.ne.0) then 327 write(*,*) "Error: failed to allocate lon(lonlen)" 328 stop 329 endif 330 allocate(alt(altlen),stat=ierr) 331 if (ierr.ne.0) then 332 write(*,*) "Error: failed to allocate alt(altlen)" 333 stop 334 endif 335 allocate(ctl(ctllen),stat=ierr) 336 if (ierr.ne.0) then 337 write(*,*) "Error: failed to allocate ctl(ctllen)" 338 stop 339 endif 340 341 ierr = NF_GET_VAR_REAL(nid,latvar,lat) 342 if (ierr.ne.0) then 343 write(*,*) "Error: failed to load latitude" 344 write(*,*) NF_STRERROR(ierr) 345 stop 346 endif 347 348 ierr = NF_GET_VAR_REAL(nid,lonvar,lon) 349 if (ierr.ne.0) then 350 write(*,*) "Error: failed to load longitude" 351 write(*,*) NF_STRERROR(ierr) 352 stop 353 endif 354 355 ierr = NF_GET_VAR_REAL(nid,altvar,alt) 356 if (ierr.ne.0) then 357 write(*,*) "Error: failed to load altitude" 358 write(*,*) NF_STRERROR(ierr) 359 stop 360 endif 355 361 ! Get altitude attributes to handle files with any altitude type 356 ierr=nf_get_att_text(nid,altvar,'long_name',altlong_name)357 ierr=nf_get_att_text(nid,altvar,'units',altunits) 358 ierr=nf_get_att_text(nid,altvar,'positive',altpositive) 359 360 if (ctllen .ne. 0) then361 #ifdef NC_DOUBLE 362 ierr = NF_GET_VAR_DOUBLE(nid,ctlvar,ctl)363 #else 364 ierr = NF_GET_VAR_REAL(nid,ctlvar,ctl)365 #endif 366 367 368 369 370 371 362 ierr=nf_get_att_text(nid,altvar,'long_name',altlong_name) 363 if (ierr.ne.nf_noerr) then 364 ! if no attribute "long_name", try "title" 365 ierr=nf_get_att_text(nid,timevar,"title",long_name) 366 endif 367 ierr=nf_get_att_text(nid,altvar,'units',altunits) 368 ierr=nf_get_att_text(nid,altvar,'positive',altpositive) 369 370 if (ctllen .ne. 0) then 371 ierr = NF_GET_VAR_REAL(nid,ctlvar,ctl) 372 if (ierr.ne.0) then 373 write(*,*) "Error: failed to load controle" 374 write(*,*) NF_STRERROR(ierr) 375 stop 376 endif 377 endif ! of if (ctllen .ne. 0) 372 378 373 379 !============================================================================== … … 391 397 ! write(*,*) "timelen: ",timelen 392 398 393 ! Sanity Check: Does "Time" has a " title" attribute399 ! Sanity Check: Does "Time" has a "long_name" attribute 394 400 ! and is it "Solar longitude" ? 395 ierr=nf_get_att_text(nid,timevar,"title",title) 396 if ((ierr.EQ.NF_NOERR).and.(title.eq."Solar longitude")) then 401 ierr=nf_get_att_text(nid,timevar,"long_name",long_name) 402 if (ierr.ne.nf_noerr) then 403 ! if no attribute "long_name", try "title" 404 ierr=nf_get_att_text(nid,timevar,"title",long_name) 405 endif 406 if ((ierr.EQ.NF_NOERR).and.(long_name.eq."Solar longitude")) then 397 407 write(*,*) "ERROR: Time axis in input file is in Solar Longitude!" 398 408 write(*,*) " localtime requires sols as time axis!" … … 408 418 endif 409 419 410 #ifdef NC_DOUBLE411 ierr = NF_GET_VAR_DOUBLE(nid,timevar,time)412 #else413 420 ierr = NF_GET_VAR_REAL(nid,timevar,time) 414 #endif415 421 if (ierr.NE.NF_NOERR) then 416 422 write(*,*) "Error , failed to load Time" … … 432 438 write(*,*) 'list of selected local time (0<t<24)' 433 439 do it=1,nhour 434 440 read(*,*) lt_hour(it) 435 441 end do 436 442 437 443 if ((timelen.eq.12).and.(time(1).eq.2).and.(time(timelen).eq.24))then 438 439 440 441 442 443 444 445 446 447 448 444 write(*,*) 'We detect a ""stats"" file' 445 stats=.true. 446 timelen_lt=nhour 447 allocate(lt_out(timelen_lt),stat=ierr) 448 if (ierr.ne.0) then 449 write(*,*) "Error: failed to allocate lt_hour(nhour)" 450 stop 451 endif 452 do it=1,nhour 453 lt_out(it)=lt_hour(it)/24. 454 end do 449 455 ! We rewrite the time from "stats" from 0 to 1 sol... 450 451 452 453 456 do it=1,timelen ! loop temps in 457 time(it) = time(it)/24. 458 end do 459 nsol =1 454 460 else ! case of a diagfi or concatnc file 455 461 stats=.false. … … 469 475 end do 470 476 end do 471 477 end if 472 478 473 479 if (nsol.gt.1) then … … 499 505 500 506 ! Initialize output file's lat,lon,alt and time dimensions 501 call initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen, &507 call initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen,GCM_layers,& 502 508 altlong_name,altunits,altpositive,& 503 nout,latdimout,londimout,altdimout,timedimout,timevarout) 509 nout,latdimout,londimout,altdimout,timedimout,& 510 layerdimout,timevarout) 504 511 505 512 ! Initialize output file's aps,bps and phisinit variables 506 call init2(nid,lonlen,latlen,altlen,altdim,& 507 nout,londimout,latdimout,altdimout) 513 call init2(nid,lonlen,latlen,altlen,GCM_layers,& 514 nout,londimout,latdimout,altdimout,& 515 layerdimout) 508 516 509 517 … … 515 523 516 524 do it=1,timelen_lt 517 #ifdef NC_DOUBLE518 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,it,1,lt_hour(it))519 #else520 525 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,lt_hour(it)) 521 #endif522 526 enddo 523 527 else 524 528 do it=1,timelen_lt 525 #ifdef NC_DOUBLE526 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,it,1,lt_out(it))527 #else528 529 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,lt_out(it)) 529 #endif530 530 if (ierr.NE.NF_NOERR) then 531 531 write(*,*) "Error , failed to write Time" … … 553 553 ierr=nf_inq_varndims(nid,varid,ndim) 554 554 555 ! Check that it is a 3D or 4D variable 556 if (ndim.lt.3) then 557 write(*,*) "Error:",trim(var(j))," is nor a 3D neither a 4D variable" 558 write(*,*) "We'll skip that variable..." 559 CYCLE ! go directly to the next variable 560 endif 555 561 !============================================================================== 556 562 ! 2.5.2 Prepare things in order to read/write the variable … … 599 605 endif 600 606 601 units=" " 602 title=" " 603 ierr=nf_get_att_text(nid,varid,"title",title) 604 ierr=nf_get_att_text(nid,varid,"units",units) 605 call def_var(nout,var(j),title,units,ndim,dim,varidout,ierr) 607 units=" " 608 long_name=" " 609 ierr=nf_get_att_text(nid,timevar,"long_name",long_name) 610 if (ierr.ne.nf_noerr) then 611 ! if no attribute "long_name", try "title" 612 ierr=nf_get_att_text(nid,timevar,"title",long_name) 613 endif 614 ierr=nf_get_att_text(nid,varid,"units",units) 615 call def_var(nout,var(j),long_name,units,ndim,dim,varidout,ierr) 606 616 607 617 … … 611 621 !============================================================================== 612 622 613 #ifdef NC_DOUBLE614 ierr = NF_GET_VAR_DOUBLE(nid,varid,var3d)615 miss=NF_GET_ATT_DOUBLE(nid,varid,"missing_value",missing)616 validr=NF_GET_ATT_DOUBLE(nid,varid,"valid_range",valid_range)617 #else618 623 ierr = NF_GET_VAR_REAL(nid,varid,var3d) 619 624 miss=NF_GET_ATT_REAL(nid,varid,"missing_value",missing) 620 625 validr=NF_GET_ATT_REAL(nid,varid,"valid_range",valid_range) 621 #endif622 626 623 627 !============================================================================== … … 677 681 !============================================================================== 678 682 679 #ifdef NC_DOUBLE680 ierr= NF_PUT_VARA_DOUBLE(nout,varidout,corner,edges,var3d_lt)681 #else682 683 ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,var3d_lt) 683 #endif684 684 685 685 if (ierr.ne.NF_NOERR) then … … 712 712 ierr=NF_CLOSE(nout) 713 713 714 write(*,*) "Program completed !" 715 714 716 contains 715 717 716 718 !****************************************************************************** 717 Subroutine initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen, &719 Subroutine initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen,GCM_layers,& 718 720 altlong_name,altunits,altpositive,& 719 nout,latdimout,londimout,altdimout,timedimout,timevarout) 721 nout,latdimout,londimout,altdimout,timedimout,& 722 layerdimout,timevarout) 720 723 !============================================================================== 721 724 ! Purpose: … … 744 747 real, intent(in):: ctl(ctllen) 745 748 ! ctl(): controle 749 integer,intent(in) :: GCM_layers ! number of GCM layers 746 750 character (len=*), intent(in) :: altlong_name 747 751 ! altlong_name(): [netcdf] altitude "long_name" attribute … … 760 764 integer, intent(out):: timedimout 761 765 ! timedimout: [netcdf] "Time" ID 766 integer,intent(out) :: layerdimout 767 ! layerdimout: [netcdf] "GCM_layers" ID 762 768 integer, intent(out):: timevarout 763 769 ! timevarout: [netcdf] "Time" (considered as a variable) ID … … 821 827 endif 822 828 829 ierr = NF_DEF_DIM(nout, "GCM_layers", GCM_layers, layerdimout) 830 823 831 ! End netcdf define mode 824 832 ierr = NF_ENDDEF(nout) … … 843 851 (/latdimout/),nvarid,ierr) 844 852 845 #ifdef NC_DOUBLE846 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lat)847 #else848 853 ierr = NF_PUT_VAR_REAL (nout,nvarid,lat) 849 #endif850 854 if (ierr.NE.NF_NOERR) then 851 855 WRITE(*,*)'initiate: error failed writing variable <latitude>' … … 861 865 (/londimout/),nvarid,ierr) 862 866 863 #ifdef NC_DOUBLE 864 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lon) 865 #else 867 866 868 ierr = NF_PUT_VAR_REAL (nout,nvarid,lon) 867 #endif868 869 if (ierr.NE.NF_NOERR) then 869 870 WRITE(*,*)'initiate: error failed writing variable <longitude>' … … 879 880 ierr = NF_REDEF (nout) 880 881 881 #ifdef NC_DOUBLE882 ierr = NF_DEF_VAR (nout,"altitude",NF_DOUBLE,1,altdimout,nvarid)883 #else884 882 ierr = NF_DEF_VAR (nout,"altitude",NF_FLOAT,1,altdimout,nvarid) 885 #endif886 883 887 884 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'long_name',len_trim(adjustl(altlong_name)),adjustl(altlong_name)) … … 892 889 ierr = NF_ENDDEF(nout) 893 890 894 #ifdef NC_DOUBLE 895 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,alt) 896 #else 897 ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) 898 #endif 891 ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) 899 892 if (ierr.NE.NF_NOERR) then 900 893 WRITE(*,*)'initiate: error failed writing variable <altitude>' … … 911 904 ierr = NF_REDEF (nout) 912 905 913 #ifdef NC_DOUBLE914 ierr = NF_DEF_VAR (nout,"controle",NF_DOUBLE,1,ctldimout,nvarid)915 #else916 906 ierr = NF_DEF_VAR (nout,"controle",NF_FLOAT,1,ctldimout,nvarid) 917 #endif 918 919 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"title",18,"Control parameters") 907 908 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"long_name",18,"Control parameters") 920 909 921 910 ! End netcdf define mode 922 911 ierr = NF_ENDDEF(nout) 923 912 924 #ifdef NC_DOUBLE925 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,ctl)926 #else927 913 ierr = NF_PUT_VAR_REAL (nout,nvarid,ctl) 928 #endif929 914 if (ierr.NE.NF_NOERR) then 930 915 WRITE(*,*)'initiate: error failed writing variable <controle>' … … 936 921 end Subroutine initiate 937 922 !****************************************************************************** 938 subroutine init2(infid,lonlen,latlen,altlen,altdimid, & 939 outfid,londimout,latdimout,altdimout) 923 subroutine init2(infid,lonlen,latlen,altlen,GCM_layers,& 924 outfid,londimout,latdimout,altdimout,& 925 layerdimout) 940 926 !============================================================================== 941 927 ! Purpose: … … 957 943 integer, intent(in) :: latlen ! # of grid points along latitude 958 944 integer, intent(in) :: altlen ! # of grid points along altitude 959 integer, intent(in) :: altdimid ! ID of altitude dimension945 integer, intent(in) :: GCM_layers ! # of GCM atmospheric layers 960 946 integer, intent(in) :: outfid ! NetCDF output file ID 961 947 integer, intent(in) :: londimout ! longitude dimension ID 962 948 integer, intent(in) :: latdimout ! latitude dimension ID 963 949 integer, intent(in) :: altdimout ! altitude dimension ID 950 integer, intent(in) :: layerdimout ! GCM_layers dimension ID 964 951 !============================================================================== 965 952 ! Local variables: … … 971 958 integer :: tmpdimid ! temporary dimension ID 972 959 integer :: tmpvarid ! temporary variable ID 973 logical :: phis, aps_ok, bps_ok ! are "phisinit" "aps""bps" available ?960 logical :: phis, hybrid ! are "phisinit" and "aps"/"bps" available ? 974 961 975 962 … … 979 966 980 967 ! hybrid coordinate aps 981 allocate(aps(altlen),stat=ierr) 968 !write(*,*) "aps: altlen=",altlen," GCM_layers=",GCM_layers 969 allocate(aps(GCM_layers),stat=ierr) 970 if (ierr.ne.0) then 971 write(*,*) "init2: failed to allocate aps!" 972 stop 973 endif 974 ierr=NF_INQ_VARID(infid,"aps",tmpvarid) 975 if (ierr.ne.NF_NOERR) then 976 write(*,*) "Ooops. Failed to get aps ID. OK." 977 hybrid=.false. 978 else 979 hybrid=.true. 980 ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) 981 hybrid=.true. 982 if (ierr.ne.NF_NOERR) then 983 stop "init2 Error: Failed reading aps" 984 endif 985 986 ! hybrid coordinate bps 987 ! write(*,*) "bps: altlen=",altlen," GCM_layers=",GCM_layers 988 allocate(bps(GCM_layers),stat=ierr) 982 989 if (ierr.ne.0) then 983 write(*,*) "init2: failed to allocate aps(altlen)"990 write(*,*) "init2: failed to allocate bps!" 984 991 stop 985 992 endif 986 987 ierr=NF_INQ_VARID(infid,"aps",tmpvarid) 988 if (ierr.ne.NF_NOERR) then 989 write(*,*) "oops Failed to get aps ID. OK" 990 aps_ok=.false. 991 else 992 ! Check that aps() is the right size (it most likely won't be if 993 ! zrecast has be used to generate the input file) 994 ierr=NF_INQ_VARDIMID(infid,tmpvarid,tmpdimid) 995 if (tmpdimid.ne.altdimid) then 996 write(*,*) "init2: wrong dimension size for aps(), skipping it ..." 997 aps_ok=.false. 998 else 999 ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) 1000 if (ierr.ne.NF_NOERR) then 1001 stop "init2 error: Failed reading aps" 1002 endif 1003 aps_ok=.true. 1004 endif 1005 endif 1006 1007 ! hybrid coordinate bps 1008 allocate(bps(altlen),stat=ierr) 1009 if (ierr.ne.0) then 1010 write(*,*) "init2: failed to allocate bps(altlen)" 1011 stop 1012 endif 1013 1014 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 1015 if (ierr.ne.NF_NOERR) then 1016 write(*,*) "oops: Failed to get bps ID. OK" 1017 bps_ok=.false. 1018 else 1019 ! Check that bps() is the right size 1020 ierr=NF_INQ_VARDIMID(infid,tmpvarid,tmpdimid) 1021 if (tmpdimid.ne.altdimid) then 1022 write(*,*) "init2: wrong dimension size for bps(), skipping it ..." 1023 bps_ok=.false. 993 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 994 if (ierr.ne.NF_NOERR) then 995 write(*,*) "Ooops. Failed to get bps ID. OK." 996 hybrid=.false. 1024 997 else 1025 998 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) … … 1027 1000 stop "init2 Error: Failed reading bps" 1028 1001 endif 1029 bps_ok=.true.1030 1002 endif 1031 1003 endif … … 1059 1031 !============================================================================== 1060 1032 1061 IF ( aps_ok) then1033 IF (hybrid) then 1062 1034 ! define aps 1063 1035 call def_var(outfid,"aps","hybrid pressure at midlayers"," ",1,& 1064 (/ altdimout/),apsid,ierr)1036 (/layerdimout/),apsid,ierr) 1065 1037 if (ierr.ne.NF_NOERR) then 1066 1038 stop "Error: Failed to def_var aps" … … 1068 1040 1069 1041 ! write aps 1070 #ifdef NC_DOUBLE1071 ierr=NF_PUT_VAR_DOUBLE(outfid,apsid,aps)1072 #else1073 1042 ierr=NF_PUT_VAR_REAL(outfid,apsid,aps) 1074 #endif1075 1043 if (ierr.ne.NF_NOERR) then 1076 1044 stop "Error: Failed to write aps" 1077 1045 endif 1078 ENDIF ! of IF (aps_ok) 1079 1080 IF (bps_ok) then 1046 1081 1047 ! define bps 1082 1048 call def_var(outfid,"bps","hybrid sigma at midlayers"," ",1,& 1083 (/ altdimout/),bpsid,ierr)1049 (/layerdimout/),bpsid,ierr) 1084 1050 if (ierr.ne.NF_NOERR) then 1085 1051 stop "Error: Failed to def_var bps" … … 1087 1053 1088 1054 ! write bps 1089 #ifdef NC_DOUBLE1090 ierr=NF_PUT_VAR_DOUBLE(outfid,bpsid,bps)1091 #else1092 1055 ierr=NF_PUT_VAR_REAL(outfid,bpsid,bps) 1093 #endif1094 1056 if (ierr.ne.NF_NOERR) then 1095 1057 stop "Error: Failed to write bps" 1096 1058 endif 1097 ENDIF ! of IF (bps_ok) 1059 1060 deallocate(aps) 1061 deallocate(bps) 1062 ENDIF ! of IF (hybrid) 1098 1063 1099 1064 !============================================================================== … … 1111 1076 1112 1077 ! write phisinit 1113 #ifdef NC_DOUBLE1114 ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit)1115 #else1116 1078 ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit) 1117 #endif1118 1079 if (ierr.ne.NF_NOERR) then 1119 1080 stop "Error: Failed to write phisinit" 1120 1081 endif 1121 1082 1083 deallocate(phisinit) 1122 1084 ENDIF ! of IF (phis) 1123 1085 1124 1086 1125 ! Cleanup1126 deallocate(aps)1127 deallocate(bps)1128 deallocate(phisinit)1129 1130 1087 end subroutine init2 1088 1131 1089 !****************************************************************************** 1132 subroutine def_var(nid,name, title,units,nbdim,dim,nvarid,ierr)1090 subroutine def_var(nid,name,long_name,units,nbdim,dim,nvarid,ierr) 1133 1091 !============================================================================== 1134 1092 ! Purpose: Write a variable (i.e: add a variable to a dataset) 1135 ! called "name"; along with its attributes " title", "units"...1093 ! called "name"; along with its attributes "long_name", "units"... 1136 1094 ! to a file (following the NetCDF format) 1137 1095 !============================================================================== … … 1151 1109 character (len=*), intent(in) :: name 1152 1110 ! name(): [netcdf] variable's name 1153 character (len=*), intent(in) :: title1154 ! title(): [netcdf] variable's "title" attribute1111 character (len=*), intent(in) :: long_name 1112 ! long_name(): [netcdf] variable's "long_name" attribute 1155 1113 character (len=*), intent(in) :: units 1156 1114 ! unit(): [netcdf] variable's "units" attribute … … 1168 1126 1169 1127 ! Insert the definition of the variable 1170 #ifdef NC_DOUBLE1171 ierr=NF_DEF_VAR(nid,adjustl(name),NF_DOUBLE,nbdim,dim,nvarid)1172 #else1173 1128 ierr=NF_DEF_VAR(nid,adjustl(name),NF_FLOAT,nbdim,dim,nvarid) 1174 #endif1175 1129 1176 1130 ! Write the attributes 1177 ierr=NF_PUT_ATT_TEXT(nid,nvarid," title",len_trim(adjustl(title)),adjustl(title))1131 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"long_name",len_trim(adjustl(long_name)),adjustl(long_name)) 1178 1132 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",len_trim(adjustl(units)),adjustl(units)) 1179 1133 … … 1232 1186 ! Write valid_range() attribute 1233 1187 if (validr.eq.NF_NOERR) then 1234 #ifdef NC_DOUBLE1235 ierr=NF_PUT_ATT_DOUBLE(nout,nvarid,'valid_range',NF_DOUBLE,2,valid_range)1236 #else1237 1188 ierr=NF_PUT_ATT_REAL(nout,nvarid,'valid_range',NF_FLOAT,2,valid_range) 1238 #endif1239 1189 1240 1190 if (ierr.ne.NF_NOERR) then … … 1246 1196 endif 1247 1197 endif 1248 1198 if (miss.eq.NF_NOERR) then 1249 1199 ! Write "missing_value" attribute 1250 if (miss.eq.NF_NOERR) then1251 #ifdef NC_DOUBLE1252 ierr= NF_PUT_ATT_DOUBLE(nout,nvarid,'missing_value',NF_DOUBLE,1,missing)1253 #else1254 1200 ierr= NF_PUT_ATT_REAL(nout,nvarid,'missing_value',NF_FLOAT,1,missing) 1255 #endif1256 1201 1257 1202 if (ierr.NE.NF_NOERR) then
Note: See TracChangeset
for help on using the changeset viewer.