Changeset 2434 for trunk/LMDZ.MARS/util/solzenangle.F90
- Timestamp:
- Nov 17, 2020, 1:36:34 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.MARS/util/solzenangle.F90
r2416 r2434 15 15 character (len=50) file 16 16 ! file(): input file(s) names(s) 17 character (len=30), dimension(1 5) :: notconcat18 ! not concat(): names of the (15) variables that won't be concatenated17 character (len=30), dimension(16) :: notprocessed 18 ! notprocessed(): names of the (16) variables that won't be processed 19 19 character (len=50), dimension(:), allocatable :: var 20 ! var(): name(s) of variable(s) that will be concatenated21 character (len= 50) :: tmpvar,title,units20 ! var(): name(s) of variable(s) that will be processed 21 character (len=100) :: tmpvar,long_name,units 22 22 ! tmpvar(): used to temporarily store a variable name 23 ! title(): [netcdf] title attribute23 ! long_name(): [netcdf] long_name attribute 24 24 ! units(): [netcdf] units attribute 25 25 character (len=100) :: filename,vartmp … … 62 62 ! ctldim: [netcdf] "controle" dim ID 63 63 ! timedim: [netcdf] "timedim" dim ID 64 integer :: gcmlayerdim ! NetCDF dimension ID for # of layers in GCM 64 65 integer :: latvar,lonvar,altvar,ctlvar,timevar 65 66 ! latvar: [netcdf] ID of "latitude" variable … … 72 73 ! latlen: # of elements of lat() array 73 74 ! lonlen: # of elements of lon() array 74 ! alt var: # of elements of alt() array75 ! ctl var: # of elements of ctl() array75 ! altlen: # of elements of alt() array 76 ! ctllen: # of elements of ctl() array 76 77 ! timelen: # of elements of time() array 77 78 ! timelen_tot:# =timelen or timelen+1 (if 1 more time to interpolate needed) 78 79 integer :: nsol 79 80 ! nsol: # of sols in the input file AND # of elements of time() array in output 81 integer :: GCM_layers ! number of GCM atmospheric layers (may not be 82 ! same as altlen if file is an output of zrecast) 80 83 integer :: nout,latdimout,londimout,altdimout,timedimout,timevarout 81 84 ! nout: [netcdf] output file ID … … 85 88 ! timedimout: [netcdf] output time (dimension) ID 86 89 ! timevarout: [netcdf] ID of output "Time" variable 90 integer :: layerdimout ! NetCDF dimension ID for # of layers in GCM 87 91 integer :: varidout 88 92 ! varidout: [netcdf] variable ID # (of a variable to write to the output file) 89 integer :: Nnot concat,var_ok90 ! Nnot concat: # of (leading)variables that won't be concatenated93 integer :: Nnotprocessed,var_ok 94 ! Nnotprocessed: # of (leading)variables that won't be concatenated 91 95 ! var_ok: flag (0 or 1) 92 96 integer, dimension(4) :: corner,edges,dim … … 107 111 real :: sza ! solar zenith angle 108 112 character (len=30) :: szastr ! to store the sza value in a string 109 real :: start timeoffset ! offset(in sols) of sol 0 in input file, wrt Ls=0113 real :: startsol ! starting date (in sols) of sol 0 in input file, wrt Ls=0 110 114 real :: tmpsol ! to temporarily store a sol value 111 115 real :: tmpLs ! to temporarily store a Ls value … … 121 125 ! valid_range(2): [netcdf] interval in which a value is considered valid 122 126 logical :: stats ! stats=T when reading a "stats" kind of ile 127 real :: year_day_gcm = 669. ! number of sols in a year in GCM 128 129 123 130 124 131 … … 160 167 !============================================================================== 161 168 162 notconcat(1)='Time' 163 notconcat(2)='controle' 164 notconcat(3)='rlonu' 165 notconcat(4)='latitude' 166 notconcat(5)='longitude' 167 notconcat(6)='altitude' 168 notconcat(7)='rlatv' 169 notconcat(8)='aps' 170 notconcat(9)='bps' 171 notconcat(10)='ap' 172 notconcat(11)='bp' 173 notconcat(12)='cu' 174 notconcat(13)='cv' 175 notconcat(14)='aire' 176 notconcat(15)='phisinit' 169 notprocessed(1)='Time' 170 notprocessed(2)='controle' 171 notprocessed(3)='rlonu' 172 notprocessed(4)='latitude' 173 notprocessed(5)='longitude' 174 notprocessed(6)='altitude' 175 notprocessed(7)='rlatv' 176 notprocessed(8)='aps' 177 notprocessed(9)='bps' 178 notprocessed(10)='ap' 179 notprocessed(11)='bp' 180 notprocessed(12)='soildepth' 181 notprocessed(13)='cu' 182 notprocessed(14)='cv' 183 notprocessed(15)='aire' 184 notprocessed(16)='phisinit' 177 185 178 186 !============================================================================== … … 180 188 !============================================================================== 181 189 write(*,*) 182 Nnot concat=0190 Nnotprocessed=0 183 191 do i=1,nbvarfile 184 192 ierr=NF_INQ_VARNAME(nid,i,vartmp) 185 193 ! vartmp now contains the "name" of variable of ID # i 186 194 var_ok=0 187 do inter=1,1 5188 if (vartmp.eq.not concat(inter)) then195 do inter=1,16 196 if (vartmp.eq.notprocessed(inter)) then 189 197 var_ok=1 190 Nnot concat=Nnotconcat+1198 Nnotprocessed=Nnotprocessed+1 191 199 endif 192 200 enddo … … 194 202 enddo 195 203 196 ! Nnot concat: # of variables that won't be processed204 ! Nnotprocessed: # of variables that won't be processed 197 205 ! nbvarfile: total # of variables in file 198 allocate(var(nbvarfile-Nnot concat),stat=ierr)206 allocate(var(nbvarfile-Nnotprocessed),stat=ierr) 199 207 if (ierr.ne.0) then 200 write(*,*) "Error: failed allocation of var(nbvarfile-Nnot concat)"208 write(*,*) "Error: failed allocation of var(nbvarfile-Nnotprocessed)" 201 209 write(*,*) " nbvarfile=",nbvarfile 202 write(*,*) " Nnot concat=",Nnotconcat210 write(*,*) " Nnotprocessed=",Nnotprocessed 203 211 stop 204 212 endif … … 218 226 219 227 if (tmpvar=="all") then 220 nbvar=nbvarfile-Nnot concat221 do j=Nnot concat+1,nbvarfile222 ierr=nf_inq_varname(nid,j,var(j-Nnot concat))228 nbvar=nbvarfile-Nnotprocessed 229 do j=Nnotprocessed+1,nbvarfile 230 ierr=nf_inq_varname(nid,j,var(j-Nnotprocessed)) 223 231 enddo 224 232 ! Variables names from the file are stored in var() 225 nbvar=nbvarfile-Nnot concat233 nbvar=nbvarfile-Nnotprocessed 226 234 do i=1,nbvar 227 ierr=nf_inq_varname(nid,i+Nnot concat,var(i))235 ierr=nf_inq_varname(nid,i+Nnotprocessed,var(i)) 228 236 write(*,'(a9,1x,i2,1x,a1,1x,a50)') "variable ",i,":",var(i) 229 237 enddo … … 290 298 ! write(*,*) "altlen: ",altlen 291 299 300 ! load size of aps() or sigma() (in case it is not altlen) 301 ! default is that GCM_layers=altlen 302 ! but for outputs of zrecast, it may be a different value 303 ierr=NF_INQ_DIMID(nid,"GCM_layers",gcmlayerdim) 304 if (ierr.ne.NF_NOERR) then 305 ! didn't find a GCM_layers dimension; therefore we have: 306 GCM_layers=altlen 307 else 308 ! load value of GCM_layers 309 ierr=NF_INQ_DIMLEN(nid,gcmlayerdim,GCM_layers) 310 endif 311 ! write(*,*) "GCM_layers=",GCM_layers 312 292 313 ierr=NF_INQ_DIMID(nid,"index",ctldim) 293 314 if (ierr.NE.NF_NOERR) then … … 304 325 ierr=NF_INQ_DIMLEN(nid,ctldim,ctllen) 305 326 endif 306 ! write(*,*) "controle: ",controle 327 307 328 308 329 !============================================================================== … … 311 332 !============================================================================== 312 333 313 ! First call; initialize/allocate314 334 allocate(lat(latlen),stat=ierr) 315 335 if (ierr.ne.0) then … … 333 353 endif 334 354 335 #ifdef NC_DOUBLE336 ierr = NF_GET_VAR_DOUBLE(nid,latvar,lat)337 #else338 355 ierr = NF_GET_VAR_REAL(nid,latvar,lat) 339 #endif340 356 if (ierr.ne.0) then 341 357 write(*,*) "Error: failed to load latitude" … … 344 360 endif 345 361 346 #ifdef NC_DOUBLE347 ierr = NF_GET_VAR_DOUBLE(nid,lonvar,lon)348 #else349 362 ierr = NF_GET_VAR_REAL(nid,lonvar,lon) 350 #endif351 363 if (ierr.ne.0) then 352 364 write(*,*) "Error: failed to load longitude" … … 355 367 endif 356 368 357 #ifdef NC_DOUBLE358 ierr = NF_GET_VAR_DOUBLE(nid,altvar,alt)359 #else360 369 ierr = NF_GET_VAR_REAL(nid,altvar,alt) 361 #endif362 370 if (ierr.ne.0) then 363 371 write(*,*) "Error: failed to load altitude" … … 367 375 ! Get altitude attributes to handle files with any altitude type 368 376 ierr=nf_get_att_text(nid,altvar,'long_name',altlong_name) 377 if (ierr.ne.nf_noerr) then 378 ! if no attribute "long_name", try "title" 379 ierr=nf_get_att_text(nid,altvar,"title",altlong_name) 380 endif 369 381 ierr=nf_get_att_text(nid,altvar,'units',altunits) 370 382 ierr=nf_get_att_text(nid,altvar,'positive',altpositive) 371 383 372 384 if (ctllen .ne. 0) then 373 #ifdef NC_DOUBLE374 ierr = NF_GET_VAR_DOUBLE(nid,ctlvar,ctl)375 #else376 385 ierr = NF_GET_VAR_REAL(nid,ctlvar,ctl) 377 #endif378 386 if (ierr.ne.0) then 379 387 write(*,*) "Error: failed to load controle" … … 403 411 ! write(*,*) "timelen: ",timelen 404 412 405 ! Sanity Check: Does "Time" has a " title" attribute413 ! Sanity Check: Does "Time" has a "long_name" attribute 406 414 ! and is it "Solar longitude" ? 407 ierr=nf_get_att_text(nid,timevar,"title",title) 408 if ((ierr.EQ.NF_NOERR).and.(title.eq."Solar longitude")) then 415 ierr=nf_get_att_text(nid,timevar,"long_name",long_name) 416 if (ierr.ne.nf_noerr) then 417 ! if no attribute "long_name", try "title" 418 ierr=nf_get_att_text(nid,timevar,"title",long_name) 419 endif 420 if ((ierr.EQ.NF_NOERR).and.(long_name.eq."Solar longitude")) then 409 421 write(*,*) "ERROR: Time axis in input file is in Solar Longitude!" 410 422 write(*,*) " localtime requires sols as time axis!" … … 420 432 endif 421 433 422 #ifdef NC_DOUBLE423 ierr = NF_GET_VAR_DOUBLE(nid,timevar,time)424 #else425 434 ierr = NF_GET_VAR_REAL(nid,timevar,time) 426 #endif427 435 if (ierr.NE.NF_NOERR) then 428 436 write(*,*) "Error , failed to load Time" … … 434 442 ! 2.4.1 Select local times to be saved "Time" in output file 435 443 !============================================================================== 436 write(*,*) "Planet side of the Solar Zenith Angle ? "444 write(*,*) "Planet side of the Solar Zenith Angle ? (morning or evening)" 437 445 read(*,*) planetside 438 446 if ((trim(planetside).ne."morning").and.(trim(planetside).ne."evening")) then … … 448 456 stop 449 457 endif 450 write(*,*) "Beginning date of the simulation file " 451 write(*,*) "(or median date of the simulation for a stats)?" 452 write(*,*) "(i.e. number of sols since Ls=0 at the Time=0.0 in the input file)" 453 read(*,*) starttimeoffset 458 459 ! Sol and season: 460 461 if (ctllen .ne. 0) then 462 startsol = int(int(ctl(4))+int(time(1) + ctl(27))) 463 else 464 startsol = int(time(1)) 465 end if 454 466 455 467 if ((timelen.eq.12).and.(time(1).eq.2).and.(time(timelen).eq.24))then … … 466 478 nsol = int(time(timelen)) - int(time(1)) 467 479 end if 468 469 480 allocate(intsol(nsol),stat=ierr) 470 481 if (ierr.ne.0) then … … 477 488 stop 478 489 endif 490 491 if (stats) then 492 write(*,*) "What is the mean Ls of this stats file (degree) ?" 493 read(*,*,iostat=ierr) tmpLs 494 startsol=0. 495 else 496 write(*,*) "Beginning date (sol since Ls=0) of this file is :",startsol 497 write(*,*) "If you do not want to change it, just write 'n' (recommended)" 498 write(*,*) "If you really wish to change it, write it now (not recommended!)" 499 read(*,*,iostat=ierr) startsol 500 if((ierr.eq.0).and.(ctllen.ne.0)) ctl(4)=startsol 501 write(*,*) "Beginning date (sol since Ls=0) will be :",startsol 502 end if 503 479 504 do it=1,nsol 480 505 intsol(it)= int(time(1)) + it-1. 481 506 do ilon=1,lonlen 482 ! For the computation of Ls, we try to take into account the rotation time483 ! of Mars it's supposed that the morning/evening507 ! For the computation of Ls, we try to take into account the 508 ! rotation time of Mars. It is supposed that the morning/evening 484 509 ! correspond to LT=6h/18h at the given longitude, which is 485 510 ! then reported to a sol value at longitude 0 486 511 if (trim(planetside).eq."morning") then 487 tmpsol = intsol(it) + (6.-lon(ilon)/15.)/24. + start timeoffset512 tmpsol = intsol(it) + (6.-lon(ilon)/15.)/24. + startsol 488 513 else !if trim(planetside).eq."evening" 489 tmpsol = intsol(it) + (18.-lon(ilon)/15.)/24. + start timeoffset514 tmpsol = intsol(it) + (18.-lon(ilon)/15.)/24. + startsol 490 515 endif 491 call sol2ls(tmpsol,tmpLs)516 if (.not.stats) call sol2ls(tmpsol,tmpLs) 492 517 do ilat=1,latlen 493 518 ! Compute the Local Time of the solar zenith angle at a given longitude … … 541 566 542 567 ! Initialize output file's lat,lon,alt and time dimensions 543 call initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen, &568 call initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen,GCM_layers,& 544 569 altlong_name,altunits,altpositive,& 545 nout,latdimout,londimout,altdimout,timedimout,timevarout) 546 570 nout,latdimout,londimout,altdimout,timedimout,& 571 layerdimout,timevarout) 572 547 573 ! Initialize output file's aps,bps and phisinit variables 548 call init2(nid,lonlen,latlen,altlen, altdim,&549 nout,londimout,latdimout,altdimout )550 574 call init2(nid,lonlen,latlen,altlen,GCM_layers,& 575 nout,londimout,latdimout,altdimout,& 576 layerdimout) 551 577 552 578 !============================================================================== … … 557 583 558 584 do it=1,nsol 559 #ifdef NC_DOUBLE 560 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,it,1,intsol(it)*24.) 561 #else 562 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,intsol(it)*24.) 563 #endif 585 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,intsol(it)*24.) 564 586 enddo 565 587 else 566 588 do it=1,nsol 567 #ifdef NC_DOUBLE 568 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,it,1,intsol(it)) 569 #else 570 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,intsol(it)) 571 #endif 572 if (ierr.NE.NF_NOERR) then 573 write(*,*) "Error , failed to write Time" 574 stop 575 endif 589 ierr= NF_PUT_VARA_REAL(nout,timevarout,it,1,intsol(it)) 590 if (ierr.NE.NF_NOERR) then 591 write(*,*) "Error , failed to write Time" 592 stop 593 endif 576 594 enddo 577 595 end if … … 584 602 585 603 !============================================================================== 586 ! 2.5.1 Check that variable to be read exist ein input file604 ! 2.5.1 Check that variable to be read exists in input file 587 605 !============================================================================== 588 606 … … 595 613 ierr=nf_inq_varndims(nid,varid,ndim) 596 614 615 ! Check that it is a 3D or 4D variable 616 if (ndim.lt.3) then 617 write(*,*) "Error:",trim(var(j))," is nor a 3D neither a 4D variable" 618 write(*,*) "We'll skip that variable..." 619 CYCLE ! go directly to the next variable 620 endif 621 597 622 !============================================================================== 598 623 ! 2.5.2 Prepare things in order to read/write the variable … … 642 667 643 668 units=" " 644 title=" " 645 ierr=nf_get_att_text(nid,varid,"title",title) 669 long_name=" " 670 ierr=nf_get_att_text(nid,varid,"long_name",long_name) 671 if (ierr.ne.nf_noerr) then 672 ! if no attribute "long_name", try "title" 673 ierr=nf_get_att_text(nid,varid,"title",long_name) 674 endif 646 675 ierr=nf_get_att_text(nid,varid,"units",units) 647 call def_var(nout,var(j), title,units,ndim,dim,varidout,ierr)676 call def_var(nout,var(j),long_name,units,ndim,dim,varidout,ierr) 648 677 649 678 … … 653 682 !============================================================================== 654 683 655 #ifdef NC_DOUBLE656 ierr = NF_GET_VAR_DOUBLE(nid,varid,var3d)657 miss=NF_GET_ATT_DOUBLE(nid,varid,"missing_value",missing)658 validr=NF_GET_ATT_DOUBLE(nid,varid,"valid_range",valid_range)659 #else660 684 ierr = NF_GET_VAR_REAL(nid,varid,var3d) 661 685 miss=NF_GET_ATT_REAL(nid,varid,"missing_value",missing) 662 686 validr=NF_GET_ATT_REAL(nid,varid,"valid_range",valid_range) 663 #endif 664 665 ! If there is no missing value attribute in the input variable, create one 687 688 ! If there is no missing value attribute in the input variable, create one 666 689 if (miss.ne.NF_NOERR) then 667 690 missing = miss_lt … … 673 696 !============================================================================== 674 697 675 do ilon=1,lonlen698 do ilon=1,lonlen 676 699 ! Recast GCM local time (sol decimal value) at each longitude : 677 700 do it=1,timelen ! loop time input file … … 688 711 lt_outc(it)=intsol(it) + lt_out(ilon,ilat,it)/24. 689 712 endif 690 ! If the computed local time exceeds the GCM input file time bounds, 691 ! put a missing value in the local time variable 692 if ((lt_outc(it).gt.lt_gcm(timelen_tot)).OR.(lt_outc(it).lt.lt_gcm(1))) then 693 lt_out(ilon,ilat,it)=miss_lt 694 lt_outc(it)=miss_lt 695 endif 713 if (nsol.eq.1) then 714 ! If 1 sol file (stats), use all data from that sol. 715 if(lt_outc(it).gt.lt_gcm(timelen_tot)) lt_outc(it)=lt_outc(it)-1 716 if(lt_outc(it).lt.lt_gcm(1)) lt_outc(it)=lt_outc(it)+1 717 else 718 ! If the computed local time exceeds the GCM input file time bounds, 719 ! put a missing value in the local time variable 720 if ((lt_outc(it).gt.lt_gcm(timelen_tot)).OR.(lt_outc(it).lt.lt_gcm(1))) then 721 lt_out(ilon,ilat,it)=miss_lt 722 lt_outc(it)=miss_lt 723 endif 724 end if 696 725 enddo ! local time 697 726 … … 710 739 enddo ! local time 711 740 712 else if 741 else if (ndim==4) then 713 742 do ialt=1,altlen 714 743 do it=1,timelen ! loop time input file … … 725 754 enddo ! local time 726 755 enddo ! alt 727 endif 756 endif ! ndim 728 757 729 758 enddo ! lat 730 end do ! lon759 end do ! lon 731 760 732 761 … … 735 764 !============================================================================== 736 765 737 #ifdef NC_DOUBLE 738 ierr= NF_PUT_VARA_DOUBLE(nout,varidout,corner,edges,var3d_lt) 739 #else 740 ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,var3d_lt) 741 #endif 742 743 if (ierr.ne.NF_NOERR) then 766 ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,var3d_lt) 767 768 if (ierr.ne.NF_NOERR) then 744 769 write(*,*) 'PUT_VAR ERROR: ',NF_STRERROR(ierr) 745 770 stop "" 746 endif771 endif 747 772 748 773 ! Write "missing_value" (and "valid_range" if there is one) attributes in output file 749 call missing_value(nout,varidout,validr,miss,valid_range,missing)774 call missing_value(nout,varidout,validr,miss,valid_range,missing) 750 775 751 776 ! free var3d() array 752 deallocate(var3d)753 deallocate(var3d_lt)777 deallocate(var3d) 778 deallocate(var3d_lt) 754 779 755 780 enddo ! of do j=1,nbvar … … 780 805 write(szastr,*) sza 781 806 if (trim(planetside).eq."morning") then 782 title="Morning-side LTST at sza="//trim(adjustl(szastr))//"deg"807 long_name="Morning-side LTST at sza="//trim(adjustl(szastr))//"deg" 783 808 else !if trim(planetside).eq."evening" 784 title="Evening-side LTST at sza="//trim(adjustl(szastr))//"deg" 785 endif 786 call def_var(nout,tmpvar,title,units,3,dim,varidout,ierr) 787 788 #ifdef NC_DOUBLE 789 ierr= NF_PUT_VARA_DOUBLE(nout,varidout,corner,edges,lt_out) 790 #else 809 long_name="Evening-side LTST at sza="//trim(adjustl(szastr))//"deg" 810 endif 811 call def_var(nout,tmpvar,long_name,units,3,dim,varidout,ierr) 812 791 813 ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,lt_out) 792 #endif793 814 if (ierr.ne.NF_NOERR) then 794 815 write(*,*) 'PUT_VAR ERROR: ',NF_STRERROR(ierr) … … 798 819 validr=NF_NOERR+1 799 820 miss=NF_NOERR 800 call missing_value(nout,varidout,validr,miss,(0,0),miss_lt) 821 valid_range(:)=0. 822 call missing_value(nout,varidout,validr,miss,valid_range,miss_lt) 801 823 write(*,*)"with missing value = ",miss_lt 802 824 !============================================================================== … … 837 859 !****************************************************************************** 838 860 839 subroutine initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen, &861 subroutine initiate(filename,lat,lon,alt,ctl,latlen,lonlen,altlen,ctllen,GCM_layers,& 840 862 altlong_name,altunits,altpositive,& 841 nout,latdimout,londimout,altdimout,timedimout,timevarout) 863 nout,latdimout,londimout,altdimout,timedimout,& 864 layerdimout,timevarout) 842 865 !============================================================================== 843 866 ! Purpose: … … 866 889 real, intent(in):: ctl(ctllen) 867 890 ! ctl(): controle 891 integer,intent(in) :: GCM_layers ! number of GCM layers 868 892 character (len=*), intent(in) :: altlong_name 869 893 ! altlong_name(): [netcdf] altitude "long_name" attribute … … 882 906 integer, intent(out):: timedimout 883 907 ! timedimout: [netcdf] "Time" ID 908 integer,intent(out) :: layerdimout 909 ! layerdimout: [netcdf] "GCM_layers" ID 884 910 integer, intent(out):: timevarout 885 911 ! timevarout: [netcdf] "Time" (considered as a variable) ID … … 943 969 endif 944 970 971 ierr = NF_DEF_DIM(nout, "GCM_layers", GCM_layers, layerdimout) 972 if (ierr.NE.NF_NOERR) then 973 WRITE(*,*)'initiate: error failed to define dimension <GCM_layers>' 974 write(*,*) NF_STRERROR(ierr) 975 stop "" 976 endif 977 945 978 ! End netcdf define mode 946 979 ierr = NF_ENDDEF(nout) … … 950 983 stop "" 951 984 endif 952 953 985 !============================================================================== 954 986 ! 3. Write "Time" and its attributes … … 960 992 ierr = NF_REDEF (nout) 961 993 ierr = NF_PUT_ATT_TEXT(nout,timevarout,'comment',135,& 962 "The true unit of the variable Time is the integer value of sol at lon=0deg. A false unit is put to enable reading from Grads or Ferret") 994 "The true unit of the variable Time is the integer value & 995 of sol at lon=0deg. A false unit is put to enable reading from Grads or Ferret") 963 996 ! End netcdf define mode 964 997 ierr = NF_ENDDEF(nout) … … 971 1004 (/latdimout/),nvarid,ierr) 972 1005 973 #ifdef NC_DOUBLE974 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lat)975 #else976 1006 ierr = NF_PUT_VAR_REAL (nout,nvarid,lat) 977 #endif978 1007 if (ierr.NE.NF_NOERR) then 979 1008 WRITE(*,*)'initiate: error failed writing variable <latitude>' … … 989 1018 (/londimout/),nvarid,ierr) 990 1019 991 #ifdef NC_DOUBLE992 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lon)993 #else994 1020 ierr = NF_PUT_VAR_REAL (nout,nvarid,lon) 995 #endif996 1021 if (ierr.NE.NF_NOERR) then 997 1022 WRITE(*,*)'initiate: error failed writing variable <longitude>' … … 1007 1032 ierr = NF_REDEF (nout) 1008 1033 1009 #ifdef NC_DOUBLE1010 ierr = NF_DEF_VAR (nout,"altitude",NF_DOUBLE,1,altdimout,nvarid)1011 #else1012 1034 ierr = NF_DEF_VAR (nout,"altitude",NF_FLOAT,1,altdimout,nvarid) 1013 #endif1014 1035 1015 1036 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'long_name',len_trim(adjustl(altlong_name)),adjustl(altlong_name)) … … 1020 1041 ierr = NF_ENDDEF(nout) 1021 1042 1022 #ifdef NC_DOUBLE 1023 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,alt) 1024 #else 1025 ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) 1026 #endif 1043 ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) 1027 1044 if (ierr.NE.NF_NOERR) then 1028 1045 WRITE(*,*)'initiate: error failed writing variable <altitude>' … … 1039 1056 ierr = NF_REDEF (nout) 1040 1057 1041 #ifdef NC_DOUBLE1042 ierr = NF_DEF_VAR (nout,"controle",NF_DOUBLE,1,ctldimout,nvarid)1043 #else1044 1058 ierr = NF_DEF_VAR (nout,"controle",NF_FLOAT,1,ctldimout,nvarid) 1045 #endif 1046 1047 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"title",18,"Control parameters") 1059 1060 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"long_name",18,"Control parameters") 1048 1061 1049 1062 ! End netcdf define mode 1050 1063 ierr = NF_ENDDEF(nout) 1051 1064 1052 #ifdef NC_DOUBLE1053 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,ctl)1054 #else1055 1065 ierr = NF_PUT_VAR_REAL (nout,nvarid,ctl) 1056 #endif1057 1066 if (ierr.NE.NF_NOERR) then 1058 1067 WRITE(*,*)'initiate: error failed writing variable <controle>' … … 1065 1074 1066 1075 !****************************************************************************** 1067 subroutine init2(infid,lonlen,latlen,altlen,altdimid, & 1068 outfid,londimout,latdimout,altdimout) 1076 subroutine init2(infid,lonlen,latlen,altlen,GCM_layers, & 1077 outfid,londimout,latdimout,altdimout, & 1078 layerdimout) 1069 1079 !============================================================================== 1070 1080 ! Purpose: … … 1086 1096 integer, intent(in) :: latlen ! # of grid points along latitude 1087 1097 integer, intent(in) :: altlen ! # of grid points along altitude 1088 integer, intent(in) :: altdimid ! ID of altitude dimension1098 integer, intent(in) :: GCM_layers ! # of GCM atmospheric layers 1089 1099 integer, intent(in) :: outfid ! NetCDF output file ID 1090 1100 integer, intent(in) :: londimout ! longitude dimension ID 1091 1101 integer, intent(in) :: latdimout ! latitude dimension ID 1092 1102 integer, intent(in) :: altdimout ! altitude dimension ID 1103 integer, intent(in) :: layerdimout ! GCM_layers dimension ID 1093 1104 !============================================================================== 1094 1105 ! Local variables: … … 1100 1111 integer :: tmpdimid ! temporary dimension ID 1101 1112 integer :: tmpvarid ! temporary variable ID 1102 logical :: phis, aps_ok, bps_ok ! are "phisinit" "aps" "bps" available ? 1113 integer :: tmplen ! temporary variable length 1114 logical :: phis,hybrid ! are "phisinit" and "aps"/"bps" available ? 1103 1115 1104 1116 … … 1108 1120 1109 1121 ! hybrid coordinate aps 1110 allocate(aps(altlen),stat=ierr) 1122 !write(*,*) "aps: altlen=",altlen," GCM_layers=",GCM_layers 1123 allocate(aps(GCM_layers),stat=ierr) 1124 if (ierr.ne.0) then 1125 write(*,*) "init2: failed to allocate aps!" 1126 stop 1127 endif 1128 ierr=NF_INQ_VARID(infid,"aps",tmpvarid) 1129 if (ierr.ne.NF_NOERR) then 1130 write(*,*) "Ooops. Failed to get aps ID. OK." 1131 hybrid=.false. 1132 else 1133 ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) 1134 hybrid=.true. 1135 if (ierr.ne.NF_NOERR) then 1136 stop "init2 Error: Failed reading aps" 1137 endif 1138 1139 ! hybrid coordinate bps 1140 ! write(*,*) "bps: altlen=",altlen," GCM_layers=",GCM_layers 1141 allocate(bps(GCM_layers),stat=ierr) 1111 1142 if (ierr.ne.0) then 1112 write(*,*) "init2: failed to allocate aps(altlen)"1143 write(*,*) "init2: failed to allocate bps!" 1113 1144 stop 1114 1145 endif 1115 1116 ierr=NF_INQ_VARID(infid,"aps",tmpvarid) 1117 if (ierr.ne.NF_NOERR) then 1118 write(*,*) "oops Failed to get aps ID. OK" 1119 aps_ok=.false. 1120 else 1121 ! Check that aps() is the right size (it most likely won't be if 1122 ! zrecast has be used to generate the input file) 1123 ierr=NF_INQ_VARDIMID(infid,tmpvarid,tmpdimid) 1124 if (tmpdimid.ne.altdimid) then 1125 write(*,*) "init2: wrong dimension size for aps(), skipping it ..." 1126 aps_ok=.false. 1127 else 1128 ierr=NF_GET_VAR_REAL(infid,tmpvarid,aps) 1129 if (ierr.ne.NF_NOERR) then 1130 stop "init2 error: Failed reading aps" 1131 endif 1132 aps_ok=.true. 1133 endif 1134 endif 1135 1136 ! hybrid coordinate bps 1137 allocate(bps(altlen),stat=ierr) 1138 if (ierr.ne.0) then 1139 write(*,*) "init2: failed to allocate bps(altlen)" 1140 stop 1141 endif 1142 1143 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 1144 if (ierr.ne.NF_NOERR) then 1145 write(*,*) "oops: Failed to get bps ID. OK" 1146 bps_ok=.false. 1147 else 1148 ! Check that bps() is the right size 1149 ierr=NF_INQ_VARDIMID(infid,tmpvarid,tmpdimid) 1150 if (tmpdimid.ne.altdimid) then 1151 write(*,*) "init2: wrong dimension size for bps(), skipping it ..." 1152 bps_ok=.false. 1146 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 1147 if (ierr.ne.NF_NOERR) then 1148 write(*,*) "Ooops. Failed to get bps ID. OK." 1149 hybrid=.false. 1153 1150 else 1154 1151 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) … … 1156 1153 stop "init2 Error: Failed reading bps" 1157 1154 endif 1158 bps_ok=.true.1159 1155 endif 1160 1156 endif … … 1188 1184 !============================================================================== 1189 1185 1190 IF ( aps_ok) then1186 IF (hybrid) then 1191 1187 ! define aps 1192 1188 call def_var(outfid,"aps","hybrid pressure at midlayers"," ",1,& 1193 (/ altdimout/),apsid,ierr)1189 (/layerdimout/),apsid,ierr) 1194 1190 if (ierr.ne.NF_NOERR) then 1195 1191 stop "Error: Failed to def_var aps" … … 1197 1193 1198 1194 ! write aps 1199 #ifdef NC_DOUBLE1200 ierr=NF_PUT_VAR_DOUBLE(outfid,apsid,aps)1201 #else1202 1195 ierr=NF_PUT_VAR_REAL(outfid,apsid,aps) 1203 #endif1204 1196 if (ierr.ne.NF_NOERR) then 1205 1197 stop "Error: Failed to write aps" 1206 1198 endif 1207 ENDIF ! of IF (aps_ok) 1208 1209 IF (bps_ok) then 1199 1210 1200 ! define bps 1211 1201 call def_var(outfid,"bps","hybrid sigma at midlayers"," ",1,& 1212 (/ altdimout/),bpsid,ierr)1202 (/layerdimout/),bpsid,ierr) 1213 1203 if (ierr.ne.NF_NOERR) then 1214 1204 stop "Error: Failed to def_var bps" … … 1216 1206 1217 1207 ! write bps 1218 #ifdef NC_DOUBLE1219 ierr=NF_PUT_VAR_DOUBLE(outfid,bpsid,bps)1220 #else1221 1208 ierr=NF_PUT_VAR_REAL(outfid,bpsid,bps) 1222 #endif1223 1209 if (ierr.ne.NF_NOERR) then 1224 1210 stop "Error: Failed to write bps" 1225 1211 endif 1226 ENDIF ! of IF (bps_ok) 1212 1213 deallocate(aps) 1214 deallocate(bps) 1215 ENDIF ! of IF (hybrid) 1227 1216 1228 1217 !============================================================================== … … 1240 1229 1241 1230 ! write phisinit 1242 #ifdef NC_DOUBLE1243 ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit)1244 #else1245 1231 ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit) 1246 #endif1247 1232 if (ierr.ne.NF_NOERR) then 1248 1233 stop "Error: Failed to write phisinit" 1249 1234 endif 1250 1235 1236 deallocate(phisinit) 1251 1237 ENDIF ! of IF (phis) 1252 1238 1253 1239 1254 ! Cleanup1255 deallocate(aps)1256 deallocate(bps)1257 deallocate(phisinit)1258 1259 1240 end subroutine init2 1260 1241 1261 1242 !****************************************************************************** 1262 subroutine def_var(nid,name, title,units,nbdim,dim,nvarid,ierr)1243 subroutine def_var(nid,name,long_name,units,nbdim,dim,nvarid,ierr) 1263 1244 !============================================================================== 1264 1245 ! Purpose: Write a variable (i.e: add a variable to a dataset) 1265 ! called "name"; along with its attributes " title", "units"...1246 ! called "name"; along with its attributes "long_name", "units"... 1266 1247 ! to a file (following the NetCDF format) 1267 1248 !============================================================================== … … 1281 1262 character (len=*), intent(in) :: name 1282 1263 ! name(): [netcdf] variable's name 1283 character (len=*), intent(in) :: title1284 ! title(): [netcdf] variable's "title" attribute1264 character (len=*), intent(in) :: long_name 1265 ! long_name(): [netcdf] variable's "long_name" attribute 1285 1266 character (len=*), intent(in) :: units 1286 1267 ! unit(): [netcdf] variable's "units" attribute … … 1298 1279 1299 1280 ! Insert the definition of the variable 1300 #ifdef NC_DOUBLE1301 ierr=NF_DEF_VAR(nid,adjustl(name),NF_DOUBLE,nbdim,dim,nvarid)1302 #else1303 1281 ierr=NF_DEF_VAR(nid,adjustl(name),NF_FLOAT,nbdim,dim,nvarid) 1304 #endif1305 1282 1306 1283 ! Write the attributes 1307 ierr=NF_PUT_ATT_TEXT(nid,nvarid," title",len_trim(adjustl(title)),adjustl(title))1284 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"long_name",len_trim(adjustl(long_name)),adjustl(long_name)) 1308 1285 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",len_trim(adjustl(units)),adjustl(units)) 1309 1286 … … 1363 1340 ! Write valid_range() attribute 1364 1341 if (validr.eq.NF_NOERR) then 1365 #ifdef NC_DOUBLE1366 ierr=NF_PUT_ATT_DOUBLE(nout,nvarid,'valid_range',NF_DOUBLE,2,valid_range)1367 #else1368 1342 ierr=NF_PUT_ATT_REAL(nout,nvarid,'valid_range',NF_FLOAT,2,valid_range) 1369 #endif1370 1343 1371 1344 if (ierr.ne.NF_NOERR) then … … 1380 1353 ! Write "missing_value" attribute 1381 1354 if (miss.eq.NF_NOERR) then 1382 #ifdef NC_DOUBLE1383 ierr= NF_PUT_ATT_DOUBLE(nout,nvarid,'missing_value',NF_DOUBLE,1,missing)1384 #else1385 1355 ierr= NF_PUT_ATT_REAL(nout,nvarid,'missing_value',NF_FLOAT,1,missing) 1386 #endif1387 1356 1388 1357 if (ierr.NE.NF_NOERR) then
Note: See TracChangeset
for help on using the changeset viewer.