Changeset 280 for trunk/LMDZ.MARS/util
- Timestamp:
- Sep 6, 2011, 11:02:38 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LMDZ.MARS/util/concatnc.F90
r137 r280 10 10 ! to output file (E. Millour, september 2006) 11 11 ! if available (F. Forget, october 2006) 12 ! + handle 1D data (EM, January 2007) 12 13 ! + ap(), bp() (F. Forget, February 2008) 14 ! + handle the possibility that number of GCM layers (aps, bps 15 ! or sigma) may be different from number of vertical levels 16 ! of data (which is the case for outputs from 'zrecast') 17 ! (EM, April 2010) 13 18 ! ******************************************************** 14 19 … … 17 22 include "netcdf.inc" ! NetCDF definitions 18 23 19 character (len=50), dimension( 200) :: file24 character (len=50), dimension(1000) :: file 20 25 ! file(): input file(s) names(s) 21 26 character (len=30), dimension(15) :: notconcat … … 43 48 integer :: varid 44 49 ! varid: [netcdf] variable ID # 45 integer :: memolatlen ,memolonlen,memoaltlen50 integer :: memolatlen=0,memolonlen=0,memoaltlen=0 46 51 ! memolatlen: # of elements of lat(), read from the first input file 47 52 ! memolonlen: # of elements of lon(), read from the first input file … … 62 67 ! altdim: [netcdf] "altdim" dim ID 63 68 ! timedim: [netcdf] "timedim" dim ID 69 integer :: gcmlayerdim ! NetCDF dimension ID for # of layers in GCM 64 70 integer :: latvar,lonvar,altvar,timevar 65 71 ! latvar: [netcdf] ID of "latitude" variable … … 72 78 ! altvar: # of elements of alt() array 73 79 ! timelen: # of elemnets of time() array 74 integer :: nout,latdimout,londimout,altdimout,apdimout,timedimout,timevarout 80 integer :: GCM_layers ! number of GCM atmospheric layers (may not be 81 ! same as altlen if file is an output of zrecast) 82 integer :: nout,latdimout,londimout,altdimout,timedimout,timevarout 75 83 ! nout: [netcdf] output file ID 76 84 ! latdimout: [netcdf] output latitude (dimension) ID … … 79 87 ! timedimout: [netcdf] output time (dimension) ID 80 88 ! timevarout: [netcdf] ID of output "Time" variable 89 integer :: layerdimout ! NetCDF dimension ID for # of layers in GCM 90 integer :: interlayerdimout ! dimension ID for # of interlayers in GCM 81 91 integer :: reptime,rep,varidout 82 92 ! reptime: total length of concatenated time() arrays … … 308 318 ! write(*,*) "altlen: ",altlen 309 319 320 ! load size of aps() or sigma() (in case it is not altlen) 321 ! default is that GCM_layers=altlen 322 ! but for outputs of zrecast, it may be a different value 323 ierr=NF_INQ_DIMID(nid,"GCM_layers",gcmlayerdim) 324 if (ierr.ne.NF_NOERR) then 325 ! didn't find a GCM_layers dimension; therefore we have: 326 GCM_layers=altlen 327 else 328 ! load value of GCM_layers 329 ierr=NF_INQ_DIMLEN(nid,gcmlayerdim,GCM_layers) 330 endif 331 ! write(*,*) "GCM_layers=",GCM_layers 332 310 333 !============================================================================== 311 334 ! 2.3. Read (and check compatibility of) dimensions of … … 330 353 #endif 331 354 ! Initialize output file's lat,lon,alt and time dimensions 332 call initiate (filename,lat,lon,alt,nout,& 333 latdimout,londimout,altdimout,apdimout,timedimout,timevarout) 355 call initiate (filename,lat,lon,alt,GCM_layers,nout,& 356 latdimout,londimout,altdimout,timedimout,& 357 layerdimout,interlayerdimout,timevarout) 334 358 ! Initialize output file's aps,bps,ap,bp and phisinit variables 335 call init2(nid,lonlen,latlen,altlen,& 336 nout,londimout,latdimout,altdimout,apdimout) 359 call init2(nid,lonlen,latlen,altlen,GCM_layers,& 360 nout,londimout,latdimout,altdimout,& 361 layerdimout,interlayerdimout) 337 362 338 363 else ! Not a first call, … … 424 449 ! build dim(),corner() and edges() arrays 425 450 ! and allocate var3d() array 426 if (ndim==3) then 451 if (ndim==1) then 452 allocate(var3d(timelen,1,1,1)) 453 dim(1)=timedimout 454 455 ! start indexes (where data values will be written) 456 corner(1)=reptime+1 457 corner(2)=1 458 corner(3)=1 459 corner(4)=1 460 461 ! length (along dimensions) of block of data to be written 462 edges(1)=timelen 463 edges(2)=1 464 edges(3)=1 465 edges(4)=1 466 467 else if (ndim==3) then 427 468 allocate(var3d(lonlen,latlen,timelen,1)) 428 469 dim(1)=londimout … … 527 568 528 569 !****************************************************************************** 529 Subroutine initiate (filename,lat,lon,alt,& 530 nout,latdimout,londimout,altdimout,apdimout,timedimout,timevarout) 570 subroutine initiate (filename,lat,lon,alt,GCM_layers,nout,& 571 latdimout,londimout,altdimout,timedimout,& 572 layerdimout,interlayerdimout,timevarout) 531 573 !============================================================================== 532 574 ! Purpose: … … 552 594 real, dimension(:), intent(in):: alt 553 595 ! alt(): altitude 596 integer,intent(in) :: GCM_layers ! number of GCM layers 554 597 integer, intent(out):: nout 555 598 ! nout: [netcdf] file ID … … 560 603 integer, intent(out):: altdimout 561 604 ! altdimout: [netcdf] alt() ID 562 integer, intent(out):: apdimout563 ! apdimout: [netcdf] ap() ID564 605 integer, intent(out):: timedimout 565 606 ! timedimout: [netcdf] "Time" ID 607 integer,intent(out) :: layerdimout 608 ! layerdimout: [netcdf] "GCM_layers" ID 609 integer,intent(out) :: interlayerdimout 610 ! layerdimout: [netcdf] "GCM_layers+1" ID 566 611 integer, intent(out):: timevarout 567 612 ! timevarout: [netcdf] "Time" (considered as a variable) ID … … 593 638 ierr = NF_DEF_DIM(nout, "longitude", size(lon), londimout) 594 639 ierr = NF_DEF_DIM(nout, "altitude", size(alt), altdimout) 595 ierr = NF_DEF_DIM(nout, "interlayer",(size(alt)+1), apdimout)596 640 ierr = NF_DEF_DIM(nout, "Time", NF_UNLIMITED, timedimout) 641 ierr = NF_DEF_DIM(nout, "GCM_layers", GCM_layers, layerdimout) 642 ierr = NF_DEF_DIM(nout, "GCM_interlayers",GCM_layers+1,interlayerdimout) 597 643 598 644 ! End netcdf define mode … … 660 706 end Subroutine initiate 661 707 !****************************************************************************** 662 subroutine init2(infid,lonlen,latlen,altlen, & 663 outfid,londimout,latdimout,altdimout,apdimout) 708 subroutine init2(infid,lonlen,latlen,altlen,GCM_layers, & 709 outfid,londimout,latdimout,altdimout, & 710 layerdimout,interlayerdimout) 664 711 !============================================================================== 665 712 ! Purpose: 666 ! Copy aps(), bps() and phisinit() from input file to outpout file 667 ! Copy ap(), bp() from input file to outpout file 713 ! Copy ap() , bp(), aps(), bps() and phisinit() from input file to outpout file 668 714 !============================================================================== 669 715 ! Remarks: … … 682 728 integer, intent(in) :: latlen ! # of grid points along latitude 683 729 integer, intent(in) :: altlen ! # of grid points along latitude 730 integer, intent(in) :: GCM_layers ! # of GCM atmospheric layers 684 731 integer, intent(in) :: outfid ! NetCDF output file ID 685 732 integer, intent(in) :: londimout ! longitude dimension ID 686 733 integer, intent(in) :: latdimout ! latitude dimension ID 687 734 integer, intent(in) :: altdimout ! altitude dimension ID 688 integer, intent(in) :: apdimout ! interlayer dimension ID 735 integer, intent(in) :: layerdimout ! GCM_layers dimension ID 736 integer, intent(in) :: interlayerdimout ! GCM_layers+1 dimension ID 689 737 !============================================================================== 690 738 ! Local variables: … … 692 740 real,dimension(:),allocatable :: aps,bps ! hybrid vertical coordinates 693 741 real,dimension(:),allocatable :: ap,bp ! hybrid vertical coordinates 742 real,dimension(:),allocatable :: sigma ! sigma levels 694 743 real,dimension(:,:),allocatable :: phisinit ! Ground geopotential 695 integer :: apsid,bpsid, phisinitid744 integer :: apsid,bpsid,sigmaid,phisinitid 696 745 integer :: apid,bpid 697 746 integer :: ierr 698 747 integer :: tmpvarid ! temporary variable ID 699 748 logical :: phis ! is "phisinit" available ? 700 logical :: hybrid ! are "aps" "bps" "ap" "bp" available ?749 logical :: hybrid ! are "aps" and "bps" available ? 701 750 702 751 !============================================================================== … … 705 754 706 755 ! hybrid coordinate aps 707 allocate(aps(altlen)) 756 !write(*,*) "aps: altlen=",altlen," GCM_layers=",GCM_layers 757 allocate(aps(GCM_layers),stat=ierr) 758 if (ierr.ne.0) then 759 write(*,*) "init2: failed to allocate aps!" 760 stop 761 endif 708 762 ierr=NF_INQ_VARID(infid,"aps",tmpvarid) 709 763 if (ierr.ne.NF_NOERR) then 710 write(*,*) "Ooops. Failed to get aps ID. OK ."764 write(*,*) "Ooops. Failed to get aps ID. OK, will look for sigma coord." 711 765 hybrid=.false. 712 766 else … … 714 768 hybrid=.true. 715 769 if (ierr.ne.NF_NOERR) then 716 stop "Error: Failed reading aps" 717 endif 718 endif 719 720 ! hybrid coordinate bps 721 allocate(bps(altlen)) 722 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 770 stop "init2 Error: Failed reading aps" 771 endif 772 773 ! hybrid coordinate bps 774 ! write(*,*) "bps: altlen=",altlen," GCM_layers=",GCM_layers 775 allocate(bps(GCM_layers),stat=ierr) 776 if (ierr.ne.0) then 777 write(*,*) "init2: failed to allocate bps!" 778 stop 779 endif 780 ierr=NF_INQ_VARID(infid,"bps",tmpvarid) 781 if (ierr.ne.NF_NOERR) then 782 stop "init2 Error: Failed to get bps ID." 783 endif 784 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) 785 if (ierr.ne.NF_NOERR) then 786 stop "init2 Error: Failed reading bps" 787 endif 788 endif 789 790 ! hybrid coordinate ap 791 allocate(ap(GCM_layers+1),stat=ierr) 792 if (ierr.ne.0) then 793 write(*,*) "init2: failed to allocate ap!" 794 stop 795 else 796 ierr=NF_INQ_VARID(infid,"ap",tmpvarid) 797 if (ierr.ne.NF_NOERR) then 798 write(*,*) "Ooops. Failed to get ap ID. OK." 799 hybrid=.false. 800 else 801 ierr=NF_GET_VAR_REAL(infid,tmpvarid,ap) 802 hybrid=.true. 803 if (ierr.ne.NF_NOERR) then 804 stop "Error: Failed reading ap" 805 endif 806 endif 807 endif 808 809 ! hybrid coordinate bp 810 allocate(bp(GCM_layers+1),stat=ierr) 811 if (ierr.ne.0) then 812 write(*,*) "init2: failed to allocate bp!" 813 stop 814 else 815 ierr=NF_INQ_VARID(infid,"bp",tmpvarid) 816 if (ierr.ne.NF_NOERR) then 817 write(*,*) "Ooops. Failed to get bp ID. OK." 818 hybrid=.false. 819 else 820 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bp) 821 hybrid=.true. 822 if (ierr.ne.NF_NOERR) then 823 stop "Error: Failed reading bp" 824 endif 825 endif 826 endif 827 828 ! sigma levels (if any) 829 if (.not.hybrid) then 830 allocate(sigma(GCM_layers),stat=ierr) 831 if (ierr.ne.0) then 832 write(*,*) "init2: failed to allocate sigma" 833 stop 834 endif 835 ierr=NF_INQ_VARID(infid,"sigma",tmpvarid) 836 ierr=NF_GET_VAR_REAL(infid,tmpvarid,sigma) 837 if (ierr.ne.NF_NOERR) then 838 stop "init2 Error: Failed reading sigma" 839 endif 840 endif ! of if (.not.hybrid) 841 842 ! ground geopotential phisinit 843 allocate(phisinit(lonlen,latlen),stat=ierr) 844 if (ierr.ne.0) then 845 write(*,*) "init2: failed to allocate phisinit!" 846 stop 847 endif 848 ierr=NF_INQ_VARID(infid,"phisinit",tmpvarid) 723 849 if (ierr.ne.NF_NOERR) then 724 write(*,*) "Ooops. Failed to get bps ID. OK." 725 hybrid=.false. 726 else 727 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bps) 728 hybrid=.true. 729 if (ierr.ne.NF_NOERR) then 730 stop "Error: Failed reading bps" 731 endif 732 endif 733 734 ! hybrid coordinate ap 735 allocate(ap(altlen+1)) 736 ierr=NF_INQ_VARID(infid,"ap",tmpvarid) 737 if (ierr.ne.NF_NOERR) then 738 write(*,*) "Ooops. Failed to get ap ID. OK." 739 hybrid=.false. 740 else 741 ierr=NF_GET_VAR_REAL(infid,tmpvarid,ap) 742 hybrid=.true. 743 if (ierr.ne.NF_NOERR) then 744 stop "Error: Failed reading ap" 745 endif 746 endif 747 748 ! hybrid coordinate bp 749 allocate(bp(altlen+1)) 750 ierr=NF_INQ_VARID(infid,"bp",tmpvarid) 751 if (ierr.ne.NF_NOERR) then 752 write(*,*) "Ooops. Failed to get bp ID. OK." 753 hybrid=.false. 754 else 755 ierr=NF_GET_VAR_REAL(infid,tmpvarid,bp) 756 hybrid=.true. 757 if (ierr.ne.NF_NOERR) then 758 stop "Error: Failed reading bp" 759 endif 760 endif 761 762 ! ground geopotential phisinit 763 ! ground geopotential phisinit 764 ierr=NF_INQ_VARID(infid,"phisinit",tmpvarid) 765 allocate(phisinit(lonlen,latlen)) 766 if (ierr.ne.NF_NOERR) then 767 write(*,*) "Failed to get phisinit ID. We must be reading a ""stats"" file" 768 phisinit = 0. 850 write(*,*)"init2 warning: Failed to get phisinit ID." 769 851 phis = .false. 770 852 else 771 853 ierr=NF_GET_VAR_REAL(infid,tmpvarid,phisinit) 772 854 if (ierr.ne.NF_NOERR) then 773 stop " Error: Failed reading phisinit"855 stop "init2 Error: Failed reading phisinit" 774 856 endif 775 857 phis = .true. … … 781 863 782 864 !============================================================================== 783 ! 2.2. Hybrid coordinates ap s() and bps()865 ! 2.2. Hybrid coordinates ap() , bp(), aps() and bps() 784 866 !============================================================================== 785 867 if(hybrid) then 786 868 ! define aps 787 869 call def_var(nout,"aps","hybrid pressure at midlayers"," ",1,& 788 (/ altdimout/),apsid,ierr)789 if (ierr.ne.NF_NOERR) then 790 stop " Error: Failed to def_var aps"870 (/layerdimout/),apsid,ierr) 871 if (ierr.ne.NF_NOERR) then 872 stop "init2 Error: Failed to def_var aps" 791 873 endif 792 874 … … 798 880 #endif 799 881 if (ierr.ne.NF_NOERR) then 800 stop " Error: Failed to write aps"882 stop "init2 Error: Failed to write aps" 801 883 endif 802 884 803 885 ! define bps 804 886 call def_var(nout,"bps","hybrid sigma at midlayers"," ",1,& 805 (/ altdimout/),bpsid,ierr)806 if (ierr.ne.NF_NOERR) then 807 stop " Error: Failed to def_var bps"887 (/layerdimout/),bpsid,ierr) 888 if (ierr.ne.NF_NOERR) then 889 stop "init2 Error: Failed to def_var bps" 808 890 endif 809 891 … … 815 897 #endif 816 898 if (ierr.ne.NF_NOERR) then 817 stop " Error: Failed to write bps"899 stop "init2 Error: Failed to write bps" 818 900 endif 819 901 820 902 ! define ap 821 903 call def_var(nout,"ap","hybrid sigma at interlayers"," ",1,& 822 (/ apdimout/),apid,ierr)904 (/interlayerdimout/),apid,ierr) 823 905 if (ierr.ne.NF_NOERR) then 824 906 stop "Error: Failed to def_var ap" … … 837 919 ! define bp 838 920 call def_var(nout,"bp","hybrid sigma at interlayers"," ",1,& 839 (/ apdimout/),bpid,ierr)921 (/interlayerdimout/),bpid,ierr) 840 922 if (ierr.ne.NF_NOERR) then 841 923 stop "Error: Failed to def_var bp" … … 851 933 stop "Error: Failed to write bp" 852 934 endif 853 endif 935 936 else 937 ! define sigma 938 call def_var(nout,"sigma","sigma at midlayers"," ",1,& 939 (/layerdimout/),sigmaid,ierr) 940 if (ierr.ne.NF_NOERR) then 941 stop "init2 Error: Failed to def_var sigma" 942 endif 943 ! write sigma 944 #ifdef NC_DOUBLE 945 ierr=NF_PUT_VAR_DOUBLE(outfid,sigmaid,sigma) 946 #else 947 ierr=NF_PUT_VAR_REAL(outfid,sigmaid,sigma) 948 #endif 949 if (ierr.ne.NF_NOERR) then 950 stop "init2 Error: Failed to write sigma" 951 endif 952 endif ! of if (hybrid) 854 953 855 954 !============================================================================== … … 859 958 IF (phis) THEN 860 959 861 !define phisinit862 call def_var(nout,"phisinit","Ground level geopotential"," ",2,&960 !define phisinit 961 call def_var(nout,"phisinit","Ground level geopotential"," ",2,& 863 962 (/londimout,latdimout/),phisinitid,ierr) 864 if (ierr.ne.NF_NOERR) then865 stop " Error: Failed to def_var phisinit"866 endif867 868 ! write phisinit869 #ifdef NC_DOUBLE 870 ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit)871 #else 872 ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit)873 #endif 874 if (ierr.ne.NF_NOERR) then875 stop "Error: Failed to write phisinit"876 endif877 878 ENDIF 963 if (ierr.ne.NF_NOERR) then 964 stop "init2 Error: Failed to def_var phisinit" 965 endif 966 967 ! write phisinit 968 #ifdef NC_DOUBLE 969 ierr=NF_PUT_VAR_DOUBLE(outfid,phisinitid,phisinit) 970 #else 971 ierr=NF_PUT_VAR_REAL(outfid,phisinitid,phisinit) 972 #endif 973 if (ierr.ne.NF_NOERR) then 974 stop "init2 Error: Failed to write phisinit" 975 endif 976 977 ENDIF ! of IF (phis) 879 978 880 979 881 980 ! Cleanup 882 deallocate(aps) 883 deallocate(bps) 884 deallocate(ap) 885 deallocate(bp) 886 deallocate(phisinit) 981 if (allocated(aps)) deallocate(aps) 982 if (allocated(bps)) deallocate(bps) 983 if (allocated(ap)) deallocate(ap) 984 if (allocated(bp)) deallocate(bp) 985 if (allocated(sigma)) deallocate(sigma) 986 if (allocated(phisinit)) deallocate(phisinit) 887 987 888 988 end subroutine init2
Note: See TracChangeset
for help on using the changeset viewer.