Changeset 2402
- Timestamp:
- Jul 8, 2020, 11:37:08 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UTIL/zrecast.F90
r1442 r2402 89 89 integer nbvarinfile ! # of variables in input file 90 90 integer nbattr ! # of attributes of a given variable in input file 91 integer nbvar3dinfile ! # of 3D (lon,lat,time) variables in input file 91 92 integer nbvar4dinfile ! # of 4D (lon,lat,alt,time) variables in input file 92 93 integer outfid ! NetCDF output file ID … … 108 109 character (len=64), dimension(:), allocatable :: var 109 110 ! var(): names of variables that will be processed 111 integer, allocatable :: varndims(:) ! number of dimensions of var(:) variables 110 112 integer nbvar ! # of variables to process 111 113 integer,dimension(:),allocatable :: var_id ! IDs of variables var() (in outfile) … … 133 135 real,dimension(:,:,:,:),allocatable :: ra_gcm ! radial distance (m) to 134 136 ! center of Mars, (of GCM grid points) 137 real,dimension(:,:,:),allocatable :: surfdata ! to store a GCM 3D dataset 135 138 real,dimension(:,:,:,:),allocatable :: indata ! to store a GCM 4D dataset 136 139 real,dimension(:,:,:,:),allocatable :: outdata ! to store a 4D dataset … … 202 205 write(*,*)" The following variables have been found:" 203 206 nbvar4dinfile=0 207 nbvar3dinfile=0 204 208 do i=1,nbvarinfile 205 209 ! get name of variable # i … … 211 215 write(*,*) trim(tmpvarname) 212 216 endif 217 if (tmpndims.eq.3) then 218 nbvar3dinfile=nbvar3dinfile+1 219 write(*,*) trim(tmpvarname) 220 endif 213 221 enddo 214 222 215 allocate(var(nbvar4dinfile ),stat=ierr)223 allocate(var(nbvar4dinfile+nbvar3dinfile),stat=ierr) 216 224 if (ierr.ne.0) then 217 write(*,*) "Error! failed memory allocation of var(nbvar4dinfile)" 225 write(*,*) "Error! failed memory allocation of var(nbvar4dinfile+nbvar3dinfile)" 226 stop 227 endif 228 allocate(varndims(nbvar4dinfile+nbvar3dinfile),stat=ierr) 229 if (ierr.ne.0) then 230 write(*,*) "Error! failed memory allocation of varndims(nbvar4dinfile+nbvar3dinfile)" 218 231 stop 219 232 endif … … 229 242 ierr=NF_INQ_VARID(infid,tmpvarname,tmpvarid) 230 243 if (ierr.eq.NF_NOERR) then ! valid name 231 ! also check that it is indeed a 4D variable244 ! also check that it is indeed a 3D or 4D variable 232 245 ierr=NF_INQ_VARNDIMS(infid,tmpvarid,tmpndims) 233 if ( tmpndims.eq.4) then246 if ((tmpndims.eq.4).or.(tmpndims.eq.3)) then 234 247 nbvar=nbvar+1 235 var(nbvar)=tmpvarname 248 var(nbvar)=tmpvarname 249 varndims(nbvar)=tmpndims 236 250 else 237 write(*,*) 'Error: ',trim(tmpvarname),' is not a 4D variable'251 write(*,*) 'Error: ',trim(tmpvarname),' is not a 3D or 4D variable' 238 252 write(*,*) ' (we''ll skip that one)' 239 253 endif … … 249 263 nbvar=0 250 264 do i=1,nbvarinfile 251 ! look for 4D variables265 ! look for 3D and 4D variables 252 266 ierr=NF_INQ_VARNDIMS(infid,i,tmpndims) 253 if ( tmpndims.eq.4) then267 if ((tmpndims.eq.4).or.(tmpndims.eq.3)) then 254 268 nbvar=nbvar+1 255 269 ! get the corresponding name 256 270 ierr=NF_INQ_VARNAME(infid,i,tmpvarname) 257 271 var(nbvar)=tmpvarname 272 varndims(nbvar)=tmpndims 258 273 endif 259 274 enddo … … 698 713 if (.not.auto_mcd_levels) then 699 714 write(*,*) "" 700 write(*,*) "Enter min and max of vertical coordinate (Pa or m)"701 write(*,*) " (in that order and on the same line)"702 715 if (ztype.eq.1) then ! pressure coordinate 703 read(*,*) pmin,pmax 716 write(*,*) "Enter min and max of vertical coordinate (Pa)" 717 write(*,*) " (in that order and on the same line)" 718 read(*,*,iostat=ierr) pmin,pmax 719 if ((ierr/=0).or.(pmin.gt.pmax)) then 720 write(*,*) "Error: wrong vertical coordinate boundaries" 721 write(*,*) " pmin=",pmin 722 write(*,*) " pmax=",pmax 723 stop 724 endif 704 725 else ! altitude coordinate, except in MCD case 705 read(*,*) zamin,zamax 726 write(*,*) "Enter min and max of vertical coordinate (m)" 727 write(*,*) " (in that order and on the same line)" 728 read(*,*,iostat=ierr) zamin,zamax 729 if ((ierr/=0).or.(zamin.gt.zamax)) then 730 write(*,*) "Error: wrong vertical coordinate boundaries" 731 write(*,*) " zamin=",zamin 732 write(*,*) " zamax=",zamax 733 stop 734 endif 706 735 endif 707 736 endif … … 710 739 if (ztype.eq.1) then ! pressure coordinate 711 740 write(*,*) "Number of levels along vertical coordinate?" 712 read(*,*) nblev 741 read(*,*,iostat=ierr) nblev 742 if (ierr/=0) then 743 write(*,*) "Error: bad input value for nblev ", nblev 744 stop 745 endif 713 746 allocate(plevel(nblev),stat=ierr) 714 747 if (ierr.ne.0) then … … 728 761 else if (ztype.eq.2) then ! above areoid heights 729 762 write(*,*) "Number of levels along vertical coordinate?" 730 read(*,*) nblev 763 read(*,*,iostat=ierr) nblev 764 if (ierr/=0) then 765 write(*,*) "Error: bad input value for nblev ", nblev 766 stop 767 endif 731 768 allocate(zareoid(nblev),stat=ierr) 732 769 if (ierr.ne.0) then … … 756 793 else 757 794 write(*,*) "Number of levels along vertical coordinate?" 758 read(*,*) nblev 795 read(*,*,iostat=ierr) nblev 796 if (ierr/=0) then 797 write(*,*) "Error: bad input value for nblev ", nblev 798 stop 799 endif 759 800 allocate(zsurface(nblev),stat=ierr) 760 801 if (ierr.ne.0) then … … 773 814 else ! distance to center of planet 774 815 write(*,*) "Number of levels along vertical coordinate?" 775 read(*,*) nblev 816 read(*,*,iostat=ierr) nblev 817 if (ierr/=0) then 818 write(*,*) "Error: bad input value for nblev ", nblev 819 stop 820 endif 776 821 allocate(zradius(nblev),stat=ierr) 777 822 if (ierr.ne.0) then … … 792 837 write(*,*) "" 793 838 write(*,*) "Number of levels along vertical coordinate?" 794 read(*,*) nblev 839 read(*,*,iostat=ierr) nblev 840 if (ierr/=0) then 841 write(*,*) "Error: bad input value for nblev ", nblev 842 stop 843 endif 795 844 if (ztype.eq.1) then ! pressure coordinate 796 845 allocate(plevel(nblev),stat=ierr) … … 804 853 write(*,*) " (one value per line)" 805 854 do i=1,nblev 806 read(*,*) plevel(i) 855 read(*,*,iostat=ierr) plevel(i) 856 if (ierr/=0) then 857 write(*,*) "Error: bad input value for plevel(i):",plevel(i) 858 stop 859 endif 807 860 enddo 808 861 else if (ztype.eq.2) then ! above areoid heights … … 816 869 write(*,*) " from min to max (one value per line)" 817 870 do i=1,nblev 818 read(*,*) zareoid(i) 871 read(*,*,iostat=ierr) zareoid(i) 872 if (ierr/=0) then 873 write(*,*) "Error: bad input value for zareoid(i):",zareoid(i) 874 stop 875 endif 819 876 enddo 820 877 else if (ztype.eq.3) then ! above local surface … … 828 885 write(*,*) " from min to max (one value per line)" 829 886 do i=1,nblev 830 read(*,*) zsurface(i) 887 read(*,*,iostat=ierr) zsurface(i) 888 if (ierr/=0) then 889 write(*,*) "Error: bad input value for zsurface(i):",zsurface(i) 890 stop 891 endif 831 892 enddo 832 893 else ! distance to center of planet … … 840 901 write(*,*) " from min to max (one value per line)" 841 902 do i=1,nblev 842 read(*,*) zradius(i) 903 read(*,*,iostat=ierr) zradius(i) 904 if (ierr/=0) then 905 write(*,*) "Error: bad input value for zradius(i):",zradius(i) 906 stop 907 endif 843 908 enddo 844 909 endif … … 1474 1539 endif 1475 1540 1476 ! 3.3.3 Define 4D variables1541 ! 3.3.3 Define 3D and 4D variables 1477 1542 1478 1543 ! add pressure or zareoid … … 1538 1603 stop 1539 1604 endif 1540 do i=1,nbvar 1605 do i=1,nbvar ! loop on 3D+4D variables 1606 if (var(i)=="ps") cycle ! skip surface pressure (already processed) 1607 ! other variables 1541 1608 write(*,*) "" 1542 1609 write(*,*) "Creating ",trim(var(i)) 1543 ! define the variable 1544 ierr=NF_DEF_VAR(outfid,var(i),NF_REAL,4,datashape,var_id(i)) 1545 if (ierr.ne.NF_NOERR) then 1546 write(*,*) 'Error, could not define variable ',trim(var(i)) 1547 write(*,*) NF_STRERROR(ierr) 1548 stop 1610 if (varndims(i).eq.3) then 1611 ! define the variable 1612 ierr=NF_DEF_VAR(outfid,var(i),NF_REAL,3,surfdatashape,var_id(i)) 1613 if (ierr.ne.NF_NOERR) then 1614 write(*,*) 'Error, could not define variable ',trim(var(i)) 1615 write(*,*) NF_STRERROR(ierr) 1616 stop 1617 endif 1618 elseif (varndims(i).eq.4) then 1619 ! define the variable 1620 ierr=NF_DEF_VAR(outfid,var(i),NF_REAL,4,datashape,var_id(i)) 1621 if (ierr.ne.NF_NOERR) then 1622 write(*,*) 'Error, could not define variable ',trim(var(i)) 1623 write(*,*) NF_STRERROR(ierr) 1624 stop 1625 endif 1626 else 1627 write(*,*) "Unexpected value for varndims(i)!" 1628 write(*,*) " i=",i," varndims(i)=",varndims(i) 1629 stop 1549 1630 endif 1550 1631 … … 1780 1861 endif 1781 1862 1863 ! allocate surfdata() to store input 3D GCM data 1864 allocate(surfdata(lonlength,latlength,timelength),stat=ierr) 1865 if (ierr.ne.0) then 1866 write(*,*) "Error: Failed to allocate surfdata(lonlength,latlength,timelength)" 1867 write(*,*) " lonlength=",lonlength," latlength=",latlength 1868 write(*,*) " timelength=",timelength 1869 stop 1870 endif 1871 1872 do i=1,nbvar ! loop on 3D+4D variables 1873 if (varndims(i).eq.4) cycle ! skip 4D variables 1874 if (var(i)=="ps") cycle ! skip surface pressure (already processed) 1875 ! load the variable from input file 1876 ierr=NF_INQ_VARID(infid,var(i),tmpvarid) 1877 if (ierr.ne.NF_NOERR) then 1878 write(*,*) "Error: Failed to get ",trim(var(i)),"ID" 1879 write(*,*) NF_STRERROR(ierr) 1880 stop 1881 endif 1882 ierr=NF_GET_VAR_REAL(infid,tmpvarid,surfdata) 1883 if (ierr.ne.NF_NOERR) then 1884 write(*,*) "Error: Failed to load ",trim(var(i)) 1885 write(*,*) NF_STRERROR(ierr) 1886 stop 1887 endif 1888 ! write the variable to output file 1889 ierr=NF_PUT_VARA_REAL(outfid,var_id(i),corner(1:3),edges(1:3),surfdata) 1890 if (ierr.ne.NF_NOERR) then 1891 write(*,*) "Error: Could not write ",trim(var(i))," data to output file" 1892 write(*,*) NF_STRERROR(ierr) 1893 stop 1894 endif 1895 enddo ! of do i=1,nbvar 1896 1782 1897 !=============================================================================== 1783 1898 ! 4. Interpolate and write 4D variables … … 1804 1919 ! 4.1 If output is in pressure coordinate 1805 1920 if (ztype.eq.1) then 1806 do i=1,nbvar ! loop on 4D variable to process 1921 do i=1,nbvar ! loop on 3D+4D variable to process 1922 ! Skip 3D variables 1923 if (varndims(i).eq.3) cycle 1807 1924 ! identify and read a dataset 1808 1925 ierr=NF_INQ_VARID(infid,var(i),tmpvarid) … … 1850 1967 ! 4.2 If output is in above areoid altitude 1851 1968 if (ztype.eq.2) then 1852 do i=1,nbvar ! loop on 4D variable to process 1969 do i=1,nbvar ! loop on 3D+4D variable to process 1970 ! Skip 3D variables 1971 if (varndims(i).eq.3) cycle 1853 1972 ! identify and read a dataset 1854 1973 ierr=NF_INQ_VARID(infid,var(i),tmpvarid) … … 1901 2020 ! 4.3 If output is in above local surface altitude 1902 2021 if (ztype.eq.3) then 1903 do i=1,nbvar ! loop on 4D variable to process 2022 do i=1,nbvar ! loop on 3D+4D variable to process 2023 ! Skip 3D variables 2024 if (varndims(i).eq.3) cycle 1904 2025 write(*,*) " " 1905 2026 write(*,*) "Processing "//trim(var(i)) … … 1956 2077 ! 4.4 If output is in radial distance to center of planet 1957 2078 if (ztype.eq.4) then 1958 do i=1,nbvar ! loop on 4D variable to process 2079 do i=1,nbvar ! loop on 3D+4D variable to process 2080 ! Skip 3D variables 2081 if (varndims(i).eq.3) cycle 1959 2082 ! identify and read a dataset 1960 2083 ierr=NF_INQ_VARID(infid,var(i),tmpvarid) … … 2011 2134 write(*,*) NF_STRERROR(ierr) 2012 2135 endif 2136 2137 write(*,*) 2138 write(*,*) "zrecast: All is well that ends well." 2013 2139 2014 2140 end program
Note: See TracChangeset
for help on using the changeset viewer.