Changeset 2434 for trunk/LMDZ.MARS/util/concatnc.F90
- Timestamp:
- Nov 17, 2020, 1:36:34 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.MARS/util/concatnc.F90
r2395 r2434 34 34 character (len=50), dimension(:), allocatable :: var 35 35 ! var(): name(s) of variable(s) that will be concatenated 36 character (len= 50) :: tmpvar,tmpfile,title,units36 character (len=100) :: tmpvar,tmpfile,long_name,units 37 37 ! tmpvar(): used to temporarily store a variable name 38 38 ! tmpfile(): used to temporarily store a file name 39 ! title(): [netcdf] title attribute39 ! long_name(): [netcdf] long_name attribute 40 40 ! units(): [netcdf] units attribute 41 41 character (len=100) :: filename,vartmp 42 42 ! filename(): output file name 43 43 ! vartmp(): temporary variable name (read from netcdf input file) 44 !character (len=1) :: ccopy 45 ! ccpy: 'y' or 'n' answer 44 character (len=50) :: altlong_name,altunits,altpositive 45 ! altlong_name(): [netcdf] altitude "long_name" attribute 46 ! altunits(): [netcdf] altitude "units" attribute 47 ! altpositive(): [netcdf] altitude "positive" attribute 46 48 character (len=4) :: axis 47 ! axis: "ls" or "sol"49 ! axis: "sol" or "ls" or "adls" 48 50 integer :: nid,ierr,miss 49 51 ! nid: [netcdf] file ID # … … 115 117 real :: ctlsol 116 118 ! ctlsol: starting sol value of the file, stored in the variable ctl() 117 real :: memotime 118 ! memotime: (cumulative) time value, in martian days (sols) 119 real :: previous_last_output_time, output_time 120 ! previous_last_output_time: last time stored in concat.nc after treating a file 121 logical :: firstsol 122 ! firstsol: true if the first file's starting day must be used in the output 123 real :: startsol 124 ! startsol: sol of the firstday as requested by the users (used if firstsol="y") 119 125 real :: starttimeoffset 120 ! starttimeoffset: offset given by the user for the output time axis 121 character (len=1) :: firstsol 122 ! firstsol: =[y/n], to know whether the user offset or the first file's starting day must be used in the output 126 ! starttimeoffset: offset given by the user for the output time axis (0 if ! firstsol="n") 123 127 real :: missing 124 128 !PARAMETER(missing=1E+20) … … 126 130 real, dimension(2) :: valid_range 127 131 ! valid_range(2): [netcdf] interval in which a value is considered valid 132 real :: year_day = 669. 128 133 129 134 !============================================================================== … … 154 159 !============================================================================== 155 160 156 write(*,*) 157 write(*,*) "Starting day to be stored in the output file time axis?"158 write(*,*) "(e.g.: 100 if you want the output file to start at time=100 sols)"159 write(*,*) "Your answer will bypass any starting day value stored in the 1st" 160 write(*,*) "input file. Conversely, just type return if you want this 1st file's" 161 write(*,*) "value to be kept for the output file starting day." 162 read(*,*,iostat=ierr) starttimeoffset 163 if ( ierr.ne.0) then161 write(*,*) "Starting time in the concat file ?" 162 write(*,*) " If you really wish to change it, write it now (not recommanded!)" 163 write(*,*) " If you do not want to change it, just write 'n' (recommanded)" 164 165 read(*,*,iostat=ierr) startsol 166 167 firstsol= (ierr.eq.0) 168 if (firstsol) then 164 169 ! if nothing or a character that is not a float is read 165 170 write(*,*) "1st input file's starting day will serve as starting day for the outfile." 166 firstsol='n'167 171 else ! if the user gave a number 168 172 write(*,*) "Your input day will serve as starting day for the outfile." 169 firstsol='y'170 173 endif 171 174 … … 190 193 191 194 write(*,*) 192 write(*,*) "W arning: to read the result with grads, choose sol"193 write(*,*) " Warning: use ferret to read the non linear scale ls"194 write(*,*) " Which time axis should be given in the output? (sol/ls)"195 write(*,*) "Which time axis should be given in the output? (sol/ls/adls)" 196 write(*,*) " (Warning: to read the result with grads, choose sol)" 197 write(*,*) " To keep sol as the time axis, and just add Ls as a variable, type adls " 195 198 read(*,*) axis 196 199 ! loop as long as axis is neither "sol" nor "ls" 197 do while ((axis/="sol").AND.(axis/="ls") )200 do while ((axis/="sol").AND.(axis/="ls").AND.(axis/="adls")) 198 201 read(*,*) axis 199 202 enddo … … 255 258 if (tmpvar=="all") then 256 259 if (axis=="ls") then 257 ! write(*,*) "Do you want to keep the original file? (y/n)"258 ! read(*,*) ccopy259 ! if ((ccopy=="n").or.(ccopy=="N")) then260 ! do i=1,nbfile261 ! ierr=NF_CLOSE(nid)262 ! ierr = NF_OPEN(file(1),NF_WRITE,nid)263 ! call change_time_axis(nid,ierr)264 ! ierr=NF_CLOSE(nid)265 ! STOP ""266 ! enddo267 ! else268 260 nbvar=nbvarfile-Nnotconcat 269 261 do j=Nnotconcat+1,nbvarfile 270 262 ierr=nf_inq_varname(nid,j,var(j-Nnotconcat)) 271 263 enddo 272 ! endif ! of if ((ccopy=="n").or.(ccopy=="N"))273 264 endif ! of if (axis=="ls") 274 265 ! Variables names from the file are catched … … 394 385 endif 395 386 endif 396 ! write(*,*) "controle: ",controle397 387 398 388 if (ctllen .ne. 0) then ! variable controle 399 389 allocate(ctl(ctllen)) 400 #ifdef NC_DOUBLE401 ierr = NF_GET_VAR_DOUBLE(nid,ctlvar,ctl)402 #else403 390 ierr = NF_GET_VAR_REAL(nid,ctlvar,ctl) 404 #endif405 391 ctlsol = ctl(4) 406 392 endif … … 425 411 allocate(time(timelen)) 426 412 427 #ifdef NC_DOUBLE428 ierr = NF_GET_VAR_DOUBLE(nid,timevar,time)429 #else430 413 ierr = NF_GET_VAR_REAL(nid,timevar,time) 431 #endif432 433 414 434 415 !============================================================================== … … 437 418 !============================================================================== 438 419 439 if (i==1) then ! First call; initialize/allocate420 if (i==1) then ! First File ; initialize/allocate 440 421 memolatlen=latlen 441 422 memolonlen=lonlen … … 445 426 allocate(lon(lonlen)) 446 427 allocate(alt(altlen)) 447 #ifdef NC_DOUBLE448 ierr = NF_GET_VAR_DOUBLE(nid,latvar,lat)449 ierr = NF_GET_VAR_DOUBLE(nid,lonvar,lon)450 ierr = NF_GET_VAR_DOUBLE(nid,altvar,alt)451 #else452 428 ierr = NF_GET_VAR_REAL(nid,latvar,lat) 453 429 ierr = NF_GET_VAR_REAL(nid,lonvar,lon) 454 430 ierr = NF_GET_VAR_REAL(nid,altvar,alt) 455 #endif 431 ! Get altitude attributes to handle files with any altitude type 432 ierr=nf_get_att_text(nid,altvar,'long_name',altlong_name) 433 if (ierr.ne.nf_noerr) then 434 ! if no attribute "long_name", try "title" 435 ierr=nf_get_att_text(nid,altvar,"title",altlong_name) 436 endif 437 ierr=nf_get_att_text(nid,altvar,'units',altunits) 438 ierr=nf_get_att_text(nid,altvar,'positive',altpositive) 456 439 write(*,*) 440 457 441 if (ctllen .ne. 0) then 458 if (int(starttimeoffset) .ne. (int(ctlsol)+int(time(1))) ) then 459 write(*,*) "WARNING: Starting day of the first file is not ",& 460 int(starttimeoffset)," but ",int(ctlsol)+int(time(1)),"!" 442 443 if (firstsol) then 444 starttimeoffset = float(int(ctlsol)+int(time(1) + ctl(27))) - startsol 445 else ! if firstsol.eq..false. 446 ! if there is no user chosen first sol, nevertheless use 447 ! starttimeoffset to lower the sol number (same exact season) 448 startsol = modulo(int(int(ctlsol)+int(time(1) + ctl(27))),int(year_day)) 449 starttimeoffset = float(int(ctlsol)+int(time(1) + ctl(27))) - startsol 461 450 endif 462 463 if (firstsol.eq.'y') then 464 starttimeoffset = float(int(ctlsol)+int(time(1))) + ctl(27) - starttimeoffset 465 else ! if firstsol.eq.'n' 466 starttimeoffset = 0 467 endif 468 469 memotime=float(int(ctlsol)+int(time(1))) + ctl(27) 451 452 ! artificially estimating "previous_last_output_time" for first file read: 453 previous_last_output_time= startsol 454 470 455 ctl(4) = 0. ! values written in the output 471 456 ctl(27) = 0. ! file controle variable (resp. day_ini and hour_ini) 472 457 473 458 else ! if no variable "controle" 474 if (int(starttimeoffset) .ne. (int(time(1))) ) then 475 write(*,*) "WARNING: Starting day of the first file is not ",& 476 int(starttimeoffset)," but ",int(time(1)),"!" 477 endif 478 479 if (firstsol.eq.'y') then 480 starttimeoffset = int(time(1)) - starttimeoffset 481 else ! if firstsol.eq.'n' 459 if (firstsol) then 460 starttimeoffset = int(time(1)) - startsol 461 else ! if firstsol.eq.false 482 462 starttimeoffset = 0 483 463 endif 484 464 485 memotime=float(int(time(1))) 465 ! artificially estimating "previous_last_output_time" for first file read: 466 previous_last_output_time=time(1) -(time(2) - time(1)) - starttimeoffset 486 467 endif 468 write(*,*) "Original starting day of 1st input file", previous_last_output_time + starttimeoffset 469 write(*,*) "Starting day of the concat file : ", previous_last_output_time 470 487 471 488 472 ! Initialize output file's lat,lon,alt and time dimensions 489 473 write(*,*) 490 call initiate(filename,lat,lon,alt,ctl,GCM_layers,nout,& 491 latdimout,londimout,altdimout,timedimout,& 474 call initiate(filename,lat,lon,alt,ctl,GCM_layers,& 475 altlong_name,altunits,altpositive,& 476 nout,latdimout,londimout,altdimout,timedimout,& 492 477 layerdimout,interlayerdimout,timevarout,axis) 493 478 ! Initialize output file's aps,bps,ap,bp and phisinit variables 494 call init2(nid,lonlen,latlen,altlen,GCM_layers,&479 call init2(nid,lonlen,latlen,altlen,GCM_layers,& 495 480 nout,londimout,latdimout,altdimout,& 496 481 layerdimout,interlayerdimout) 497 482 498 else ! Not a first call,483 else ! Not first file to concatenate 499 484 ! Check that latitude,longitude and altitude of current input file 500 485 ! are identical to those of the output file … … 521 506 522 507 rep=0 523 write(*,*) 524 write(*,'(a3,1x,f6.1)') "Sol",memotime-starttimeoffset 525 ! if (ctllen.ne.0) write(*,'(a30,1x,f6.1)') "In the current file's ctl: Sol",ctlsol 526 527 ! Add (memotime)/(ctlsol) offset and write "concatenated" time values to output file 528 if (ctllen.ne.0) then 529 do k=reptime+1,reptime+timelen 508 509 do k=reptime+1,reptime+timelen 530 510 rep=rep+1 531 if ((time(rep)+ctlsol).le.memotime) then 532 write(*,*) "ERROR : the time intervals of the files are not dissociated" 533 stop "" 534 endif 535 #ifdef NC_DOUBLE 536 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,k,1,ctlsol+time(rep)-starttimeoffset) 537 #else 538 ierr= NF_PUT_VARA_REAL(nout,timevarout,k,1,ctlsol+time(rep)-starttimeoffset) 539 #endif 540 ! 541 enddo 542 543 ! Compute new time offset (for further concatenations) 544 memotime=ctlsol+time(timelen) ! the actual last day of the current file 545 ! becomes the reference time for next file 546 ! leaving potential "time holes" in the output time axis 547 ! at the junction of two files' time axes 548 549 else ! if no variable "controle" 550 551 do k=reptime+1,reptime+timelen 552 rep=rep+1 553 #ifdef NC_DOUBLE 554 ierr= NF_PUT_VARA_DOUBLE(nout,timevarout,k,1,memotime+time(rep)-int(time(1))-starttimeoffset) 555 #else 556 ierr= NF_PUT_VARA_REAL(nout,timevarout,k,1,memotime+time(rep)-int(time(1))-starttimeoffset) 557 #endif 558 enddo 559 560 ! Compute new time offset (for further concatenations) 561 memotime=memotime+time(timelen)-time(1) ! files are concatenated one after another 562 ! even if their time axes normally don't 563 ! directly follow each other 564 endif ! if ctllen.ne.0 511 if (ctllen.ne.0) then 512 output_time=time(rep)+ctlsol-starttimeoffset 513 ! correction if the file to concatenate seems "before" previous file 514 ! (for instance to concatenate diagfi from the previous year at the enf of a year) 515 do while (output_time.lt.previous_last_output_time) 516 output_time = output_time + year_day 517 end do 518 else ! if no control, just add timestep after timestep 519 output_time= previous_last_output_time + (time(2)-time(1)) & 520 + (time(rep)-time(1)) 521 end if 522 if (rep.eq.1) write(*,*) "Sol", int(output_time) 523 524 ierr= NF_PUT_VARA_REAL(nout,timevarout,k,1,output_time) 525 end do 526 ! use the last output_time value to update memotime 527 previous_last_output_time = output_time 528 565 529 566 530 !============================================================================== … … 577 541 ierr=nf_inq_varid(nid,var(j),varid) 578 542 if (ierr.NE.NF_NOERR) then 579 write(*,*) 'ERROR: Field <',var(j),'> not found in file '//file(i)543 write(*,*) 'ERROR: Field <',var(j),'> not found in file '//file(i) 580 544 stop "" 581 545 endif 582 546 ierr=nf_inq_varndims(nid,varid,ndim) 583 547 548 ! Check that it is a 1D, 3D or 4D variable 549 if ((ndim.ne.1).and.(ndim.ne.3).and.(ndim.ne.4)) then 550 write(*,*) "Error:",trim(var(j))," is not a time-dependent variable," 551 write(*,*) "so it can't be concatenated." 552 write(*,*) "We'll skip that variable..." 553 CYCLE ! go directly to the next variable 554 endif 555 584 556 !============================================================================== 585 557 ! 2.5.2 Prepare things in order to read/write the variable … … 644 616 if (i==1) then ! First call: write some definitions to output file 645 617 units=" " 646 title=" " 647 ierr=nf_get_att_text(nid,varid,"title",title) 618 long_name=" " 619 ierr=nf_get_att_text(nid,varid,"long_name",long_name) 620 if (ierr.ne.nf_noerr) then 621 ! if no attribute "long_name", try "title" 622 ierr=nf_get_att_text(nid,varid,"title",long_name) 623 endif 648 624 ierr=nf_get_att_text(nid,varid,"units",units) 649 call def_var(nout,var(j), title,units,ndim,dim,varidout,ierr)625 call def_var(nout,var(j),long_name,units,ndim,dim,varidout,ierr) 650 626 else 651 627 ierr=NF_INQ_VARID(nout,var(j),varidout) … … 656 632 !============================================================================== 657 633 658 #ifdef NC_DOUBLE659 ierr = NF_GET_VAR_DOUBLE(nid,varid,var3d)660 ierr= NF_PUT_VARA_DOUBLE(nout,varidout,corner,edges,var3d)661 miss=NF_GET_ATT_DOUBLE(nid,varid,"missing_value",missing)662 miss=NF_GET_ATT_DOUBLE(nid,varid,"valid_range",valid_range)663 #else664 634 ierr = NF_GET_VAR_REAL(nid,varid,var3d) 665 635 ierr= NF_PUT_VARA_REAL(nout,varidout,corner,edges,var3d) 666 636 miss=NF_GET_ATT_REAL(nid,varid,"missing_value",missing) 667 637 miss=NF_GET_ATT_REAL(nid,varid,"valid_range",valid_range) 668 #endif669 638 670 639 if (ierr.ne.NF_NOERR) then … … 700 669 !============================================================================== 701 670 702 if (axis=="ls") then 703 write(*,*);write(*,*) "Converting the time axis from sol to Ls..." 704 call change_time_axis(nout,ierr) 671 if ((axis=="ls").or.(axis=="adls")) then 672 call change_time_axis(nout,ierr,axis) 705 673 endif 706 674 … … 712 680 713 681 !****************************************************************************** 714 subroutine initiate (filename,lat,lon,alt,ctl,GCM_layers,nout,& 715 latdimout,londimout,altdimout,timedimout,& 716 layerdimout,interlayerdimout,timevarout,axis) 682 subroutine initiate (filename,lat,lon,alt,ctl,GCM_layers,& 683 altlong_name,altunits,altpositive,& 684 nout,latdimout,londimout,altdimout,timedimout,& 685 layerdimout,interlayerdimout,timevarout,axis) 717 686 !============================================================================== 718 687 ! Purpose: … … 741 710 ! ctl(): controle 742 711 integer,intent(in) :: GCM_layers ! number of GCM layers 712 character (len=*), intent(in) :: altlong_name 713 ! altlong_name(): [netcdf] altitude "long_name" attribute 714 character (len=*), intent(in) :: altunits 715 ! altunits(): [netcdf] altitude "units" attribute 716 character (len=*), intent(in) :: altpositive 717 ! altpositive(): [netcdf] altitude "positive" attribute 743 718 integer, intent(out):: nout 744 719 ! nout: [netcdf] file ID … … 787 762 ierr = NF_DEF_DIM(nout, "longitude", size(lon), londimout) 788 763 ierr = NF_DEF_DIM(nout, "altitude", size(alt), altdimout) 789 if ( size(ctl).ne.0) ierr = NF_DEF_DIM(nout, "index", size(ctl), ctldimout)764 if (ctllen.ne.0) ierr = NF_DEF_DIM(nout, "index", size(ctl), ctldimout) 790 765 ierr = NF_DEF_DIM(nout, "Time", NF_UNLIMITED, timedimout) 791 766 ierr = NF_DEF_DIM(nout, "GCM_layers", GCM_layers, layerdimout) … … 798 773 ! 3. Write "Time" and its attributes 799 774 !============================================================================== 800 if (axis=="sol") then 775 if (axis=="ls") then 776 call def_var(nout,"Time","Solar longitude","degree",1,& 777 (/timedimout/),timevarout,ierr) 778 else 801 779 call def_var(nout,"Time","Time","days since 0000-00-0 00:00:00",1,& 802 780 (/timedimout/),timevarout,ierr) 803 else ! Ls804 call def_var(nout,"Time","Solar longitude","days since 0000-00-0 00:00:00",1,&805 (/timedimout/),timevarout,ierr)806 781 endif 807 782 !============================================================================== … … 812 787 (/latdimout/),nvarid,ierr) 813 788 814 #ifdef NC_DOUBLE815 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lat)816 #else817 789 ierr = NF_PUT_VAR_REAL (nout,nvarid,lat) 818 #endif819 790 820 791 !============================================================================== … … 825 796 (/londimout/),nvarid,ierr) 826 797 827 #ifdef NC_DOUBLE828 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,lon)829 #else830 798 ierr = NF_PUT_VAR_REAL (nout,nvarid,lon) 831 #endif832 799 833 800 !============================================================================== … … 838 805 ierr = NF_REDEF (nout) 839 806 840 #ifdef NC_DOUBLE841 ierr = NF_DEF_VAR (nout,"altitude",NF_DOUBLE,1,altdimout,nvarid)842 #else843 807 ierr = NF_DEF_VAR (nout,"altitude",NF_FLOAT,1,altdimout,nvarid) 844 #endif 845 846 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"long_name",8,"altitude") 847 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'units',2,"km") 848 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'positive',2,"up") 808 809 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'long_name',len_trim(adjustl(altlong_name)),adjustl(altlong_name)) 810 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'units',len_trim(adjustl(altunits)),adjustl(altunits)) 811 ierr = NF_PUT_ATT_TEXT (nout,nvarid,'positive',len_trim(adjustl(altpositive)),adjustl(altpositive)) 849 812 850 813 ! End netcdf define mode 851 814 ierr = NF_ENDDEF(nout) 852 815 853 #ifdef NC_DOUBLE854 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,alt)855 #else856 816 ierr = NF_PUT_VAR_REAL (nout,nvarid,alt) 857 #endif858 817 859 818 !============================================================================== … … 861 820 !============================================================================== 862 821 863 if ( size(ctl).ne.0) then822 if (ctllen.ne.0) then 864 823 ! Switch to netcdf define mode 865 824 ierr = NF_REDEF (nout) 866 825 867 #ifdef NC_DOUBLE868 ierr = NF_DEF_VAR (nout,"controle",NF_DOUBLE,1,ctldimout,nvarid)869 #else870 826 ierr = NF_DEF_VAR (nout,"controle",NF_FLOAT,1,ctldimout,nvarid) 871 #endif 872 873 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"title",18,"Control parameters") 827 828 ierr = NF_PUT_ATT_TEXT (nout,nvarid,"long_name",18,"Control parameters") 874 829 875 830 ! End netcdf define mode 876 831 ierr = NF_ENDDEF(nout) 877 832 878 #ifdef NC_DOUBLE879 ierr = NF_PUT_VAR_DOUBLE (nout,nvarid,ctl)880 #else881 833 ierr = NF_PUT_VAR_REAL (nout,nvarid,ctl) 882 #endif883 834 endif 884 835 … … 890 841 !============================================================================== 891 842 ! Purpose: 892 ! Copy ap() 893 ! from input file to outp out file843 ! Copy ap(), bp(), aps(), bps(), aire() and phisinit() 844 ! from input file to output file 894 845 !============================================================================== 895 846 ! Remarks: … … 907 858 integer, intent(in) :: lonlen ! # of grid points along longitude 908 859 integer, intent(in) :: latlen ! # of grid points along latitude 909 integer, intent(in) :: altlen ! # of grid points along latitude860 integer, intent(in) :: altlen ! # of grid points along altitude 910 861 integer, intent(in) :: GCM_layers ! # of GCM atmospheric layers 911 862 integer, intent(in) :: outfid ! NetCDF output file ID … … 931 882 logical :: hybrid ! are "aps" and "bps" available ? 932 883 logical :: apbp ! are "ap" and "bp" available ? 884 logical :: sig ! is "sigma" available ? 933 885 934 886 !============================================================================== … … 976 928 write(*,*) "init2: failed to allocate ap!" 977 929 stop 930 endif 931 ierr=NF_INQ_VARID(infid,"ap",tmpvarid) 932 if (ierr.ne.NF_NOERR) then 933 write(*,*) "Ooops. Failed to get ap ID. OK." 934 apbp=.false. 978 935 else 979 ierr=NF_INQ_VARID(infid,"ap",tmpvarid) 980 if (ierr.ne.NF_NOERR) then 981 write(*,*) "Ooops. Failed to get ap ID. OK." 982 apbp=.false. 983 else 984 ierr=NF_GET_VAR_REAL(infid,tmpvarid,ap) 985 apbp=.true. 986 if (ierr.ne.NF_NOERR) then 987 stop "Error: Failed reading ap" 988 endif 936 ierr=NF_GET_VAR_REAL(infid,tmpvarid,ap) 937 apbp=.true. 938 if (ierr.ne.NF_NOERR) then 939 stop "Error: Failed reading ap" 989 940 endif 990 941 endif … … 995 946 write(*,*) "init2: failed to allocate bp!" 996 947 stop 948 endif 949 ierr=NF_INQ_VARID(infid,"bp",tmpvarid) 950 if (ierr.ne.NF_NOERR) then 951 write(*,*) "Ooops. Failed to get bp ID. OK." 952 apbp=.false. 997 953 else 998 ierr=NF_INQ_VARID(infid,"bp",tmpvarid) 999 if (ierr.ne.NF_NOERR) then 1000 write(*,*) "Ooops. Failed to get bp ID. OK." 1001 apbp=.false. 1002 else 1003 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bp) 1004 apbp=.true. 1005 if (ierr.ne.NF_NOERR) then 1006 stop "Error: Failed reading bp" 1007 endif 954 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bp) 955 apbp=.true. 956 if (ierr.ne.NF_NOERR) then 957 stop "Error: Failed reading bp" 1008 958 endif 1009 959 endif … … 1017 967 endif 1018 968 ierr=NF_INQ_VARID(infid,"sigma",tmpvarid) 1019 ierr=NF_GET_VAR_REAL(infid,tmpvarid,sigma) 1020 if (ierr.ne.NF_NOERR) then 1021 stop "init2 Error: Failed reading sigma" 969 if (ierr.ne.NF_NOERR) then 970 write(*,*) "Ooops. Failed to get sigma ID. OK." 971 sig=.false. 972 else 973 ierr=NF_GET_VAR_REAL(infid,tmpvarid,sigma) 974 sig=.true. 975 if (ierr.ne.NF_NOERR) then 976 stop "init2 Error: Failed reading sigma" 977 endif 1022 978 endif 1023 979 endif ! of if (.not.hybrid) … … 1075 1031 1076 1032 ! write aps 1077 #ifdef NC_DOUBLE1078 ierr=NF_PUT_VAR_DOUBLE(outfid,apsid,aps)1079 #else1080 1033 ierr=NF_PUT_VAR_REAL(outfid,apsid,aps) 1081 #endif1082 1034 if (ierr.ne.NF_NOERR) then 1083 1035 stop "init2 Error: Failed to write aps" … … 1092 1044 1093 1045 ! write bps 1094 #ifdef NC_DOUBLE1095 ierr=NF_PUT_VAR_DOUBLE(outfid,bpsid,bps)1096 #else1097 1046 ierr=NF_PUT_VAR_REAL(outfid,bpsid,bps) 1098 #endif1099 1047 if (ierr.ne.NF_NOERR) then 1100 1048 stop "init2 Error: Failed to write bps" … … 1110 1058 1111 1059 ! write ap 1112 #ifdef NC_DOUBLE1113 ierr=NF_PUT_VAR_DOUBLE(outfid,apid,ap)1114 #else1115 1060 ierr=NF_PUT_VAR_REAL(outfid,apid,ap) 1116 #endif1117 1061 if (ierr.ne.NF_NOERR) then 1118 1062 stop "Error: Failed to write ap" … … 1127 1071 1128 1072 ! write bp 1129 #ifdef NC_DOUBLE1130 ierr=NF_PUT_VAR_DOUBLE(outfid,bpid,bp)1131 #else1132 1073 ierr=NF_PUT_VAR_REAL(outfid,bpid,bp) 1133 #endif1134 1074 if (ierr.ne.NF_NOERR) then 1135 1075 stop "Error: Failed to write bp" … … 1137 1077 endif ! of if (apbp) 1138 1078 1139 else 1079 else if (sig) then 1140 1080 ! define sigma 1141 1081 call def_var(nout,"sigma","sigma at midlayers"," ",1,& … … 1145 1085 endif 1146 1086 ! write sigma 1147 #ifdef NC_DOUBLE1148 ierr=NF_PUT_VAR_DOUBLE(outfid,sigmaid,sigma)1149 #else1150 1087 ierr=NF_PUT_VAR_REAL(outfid,sigmaid,sigma) 1151 #endif1152 1088 if (ierr.ne.NF_NOERR) then 1153 1089 stop "init2 Error: Failed to write sigma" … … 1168 1104 1169 1105 ! write aire 1170 #ifdef NC_DOUBLE1171 ierr=NF_PUT_VAR_DOUBLE(outfid,tmpvarid,aire)1172 #else1173 1106 ierr=NF_PUT_VAR_REAL(outfid,tmpvarid,aire) 1174 #endif1175 1107 if (ierr.ne.NF_NOERR) then 1176 1108 stop "init2 Error: Failed to write aire" … … 1188 1120 1189 1121 ! write phisinit 1190 #ifdef NC_DOUBLE1191 ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit)1192 #else1193 1122 ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit) 1194 #endif1195 1123 if (ierr.ne.NF_NOERR) then 1196 1124 stop "init2 Error: Failed to write phisinit" … … 1211 1139 end subroutine init2 1212 1140 !****************************************************************************** 1213 subroutine def_var(nid,name, title,units,nbdim,dim,nvarid,ierr)1141 subroutine def_var(nid,name,long_name,units,nbdim,dim,nvarid,ierr) 1214 1142 !============================================================================== 1215 1143 ! Purpose: Write a variable (i.e: add a variable to a dataset) 1216 ! called "name"; along with its attributes " title", "units"...1144 ! called "name"; along with its attributes "long_name", "units"... 1217 1145 ! to a file (following the NetCDF format) 1218 1146 !============================================================================== … … 1232 1160 character (len=*), intent(in) :: name 1233 1161 ! name(): [netcdf] variable's name 1234 character (len=*), intent(in) :: title1235 ! title(): [netcdf] variable's "title" attribute1162 character (len=*), intent(in) :: long_name 1163 ! long_name(): [netcdf] variable's "long_name" attribute 1236 1164 character (len=*), intent(in) :: units 1237 1165 ! unit(): [netcdf] variable's "units" attribute … … 1249 1177 1250 1178 ! Insert the definition of the variable 1251 #ifdef NC_DOUBLE1252 ierr=NF_DEF_VAR(nid,adjustl(name),NF_DOUBLE,nbdim,dim,nvarid)1253 #else1254 1179 ierr=NF_DEF_VAR(nid,adjustl(name),NF_FLOAT,nbdim,dim,nvarid) 1255 #endif1256 1180 1257 1181 ! Write the attributes 1258 ierr=NF_PUT_ATT_TEXT(nid,nvarid," title",len_trim(adjustl(title)),adjustl(title))1182 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"long_name",len_trim(adjustl(long_name)),adjustl(long_name)) 1259 1183 ierr=NF_PUT_ATT_TEXT(nid,nvarid,"units",len_trim(adjustl(units)),adjustl(units)) 1260 1184 … … 1264 1188 end subroutine def_var 1265 1189 !****************************************************************************** 1266 subroutine change_time_axis(nid,ierr )1190 subroutine change_time_axis(nid,ierr,axis) 1267 1191 !============================================================================== 1268 1192 ! Purpose: … … 1285 1209 integer, intent(out) :: ierr 1286 1210 ! ierr: [netcdf] return error code 1211 character (len=4),intent(in) :: axis 1212 ! axis: "ls" or "sol" 1287 1213 1288 1214 !============================================================================== … … 1315 1241 ierr=NF_INQ_DIMLEN(nid,timedim,timelen) 1316 1242 allocate(time(timelen),ls(timelen)) 1317 #ifdef NC_DOUBLE1318 ierr = NF_GET_VAR_DOUBLE(nid,nvarid,time)1319 #else1320 1243 ierr = NF_GET_VAR_REAL(nid,nvarid,time) 1321 #endif1322 1244 if (ierr.ne.NF_NOERR) then 1323 1245 write(*,*) "ERROR in change_time_axis: Failed to load Time" … … 1353 1275 !============================================================================== 1354 1276 1355 #ifdef NC_DOUBLE 1356 ierr = NF_PUT_VAR_DOUBLE(nid,nvarid,ls) 1357 #else 1277 if (axis.eq."ls") then 1278 write(*,*) "Converting the time axis from sol to Ls..." 1279 else ! if (axis.eq."adls") 1280 write(*,*) "Adding Ls as a 1D time variable" 1281 call def_var(nout,"Ls","Solar Longitude","degree",1, (/timedimout/),nvarid,ierr) 1282 end if 1283 1358 1284 ierr = NF_PUT_VAR_REAL(nid,nvarid,ls) 1359 #endif1360 1285 if (ierr.ne.NF_NOERR) then 1361 write(*,*) "ERROR in change_time_axis: Failed to write Ls"1362 stop1286 write(*,*) "ERROR in change_time_axis: Failed to write Ls" 1287 stop 1363 1288 endif 1364 1289 … … 1502 1427 1503 1428 ! Write valid_range() attribute 1504 #ifdef NC_DOUBLE1505 ierr=NF_PUT_ATT_DOUBLE(nout,nvarid,'valid_range',NF_DOUBLE,2,valid_range)1506 #else1507 1429 ierr=NF_PUT_ATT_REAL(nout,nvarid,'valid_range',NF_FLOAT,2,valid_range) 1508 #endif1509 1430 1510 1431 if (ierr.ne.NF_NOERR) then … … 1517 1438 1518 1439 ! Write "missing_value" attribute 1519 #ifdef NC_DOUBLE1520 ierr= NF_PUT_ATT_DOUBLE(nout,nvarid,'missing_value',NF_DOUBLE,1,missing)1521 #else1522 1440 ierr= NF_PUT_ATT_REAL(nout,nvarid,'missing_value',NF_FLOAT,1,missing) 1523 #endif1524 1441 1525 1442 if (ierr.NE.NF_NOERR) then
Note: See TracChangeset
for help on using the changeset viewer.