program localtime ! ---------------------------------------------------------------------------- ! Program to redistribute and interpolate the variable a the same ! local times everywhere ! input : diagfi.nc / concat.nc / stats.nc kind of files ! author: F. Forget ! ---------------------------------------------------------------------------- implicit none include "netcdf.inc" ! NetCDF definitions character (len=50) file ! file(): input file(s) names(s) character (len=30), dimension(15) :: notconcat ! notconcat(): names of the (15) variables that won't be concatenated character (len=50), dimension(:), allocatable :: var ! var(): name(s) of variable(s) that will be concatenated character (len=50) :: tmpvar,title,units ! tmpvar(): used to temporarily store a variable name ! title(): [netcdf] title attribute ! units(): [netcdf] units attribute character (len=100) :: filename,vartmp ! filename(): output file name ! vartmp(): temporary variable name (read from netcdf input file) !character (len=1) :: ccopy ! ccpy: 'y' or 'n' answer integer :: nid,ierr,miss ! nid: [netcdf] file ID # ! ierr: [netcdf] subroutine returned error code ! miss: [netcdf] subroutine returned error code integer :: i,j,k,inter ! for various loops integer :: varid ! varid: [netcdf] variable ID # real, dimension(:), allocatable:: lat,lon,alt,ctl,time ! lat(): array, stores latitude coordinates ! lon(): array, stores longitude coordinates ! alt(): array, stores altitude coordinates ! ctl(): array, stores controle array ! time(): array, stores time coordinates integer :: nbvar,nbvarfile,ndim ! nbvar: # of variables to concatenate ! nbfile: # number of input file(s) ! nbvarfile: total # of variables in an input file ! ndim: [netcdf] # (3 or 4) of dimensions (for variables) integer :: latdim,londim,altdim,ctldim,timedim ! latdim: [netcdf] "latitude" dim ID ! londim: [netcdf] "longitude" dim ID ! altdim: [netcdf] "altdim" dim ID ! ctldim: [netcdf] "controle" dim ID ! timedim: [netcdf] "timedim" dim ID integer :: latvar,lonvar,altvar,ctlvar,timevar ! latvar: [netcdf] ID of "latitude" variable ! lonvar: [netcdf] ID of "longitude" variable ! altvar: [netcdf] ID of "altitude" variable ! ctlvar: [netcdf] ID of "controle" variable ! timevar: [netcdf] ID of "Time" variable integer :: latlen,lonlen,altlen,ctllen,timelen,timelen_lt,timelen_tot integer :: ilat,ilon,ialt,it ! latlen: # of elements of lat() array ! lonlen: # of elements of lon() array ! altvar: # of elements of alt() array ! ctlvar: # of elements of ctl() array ! timelen: # of elemnets of time() array ! timelen_tot:# =timelen or timelen+1 (if 1 more time to interpolate needed) ! timelen_lt: # of elemnets of time() array in output integer :: nout,latdimout,londimout,altdimout,timedimout,timevarout integer :: nhour,nsol ! nout: [netcdf] output file ID ! latdimout: [netcdf] output latitude (dimension) ID ! londimout: [netcdf] output longitude (dimension) ID ! altdimout: [netcdf] output altitude (dimension) ID ! timedimout: [netcdf] output time (dimension) ID ! timevarout: [netcdf] ID of output "Time" variable integer :: varidout ! varidout: [netcdf] variable ID # (of a variable to write to the output file) integer :: Nnotconcat,var_ok ! Nnotconcat: # of (leading)variables that won't be concatenated ! var_ok: flag (0 or 1) integer, dimension(4) :: corner,edges,dim ! corner: [netcdf] ! edges: [netcdf] ! dim: [netcdf] real, dimension(:,:,:,:), allocatable :: var3d ! var3D(,,,): 4D array to store a field real, dimension(:,:,:,:), allocatable :: var3d_lt ! var3D_lt(,,,): 4D array to store a field in local time coordinate real, dimension(:), allocatable :: lt_gcm,lt_hour real, dimension(:), allocatable :: lt_out,lt_outc real, dimension(:), allocatable :: var_gcm real :: missing !PARAMETER(missing=1E+20) ! missing: [netcdf] to handle "missing" values when reading/writing files real, dimension(2) :: valid_range ! valid_range(2): [netcdf] interval in which a value is considered valid logical :: stats ! stats=T when reading a "stats" kind of ile !============================================================================== ! 1.1. Get input file name(s) !============================================================================== write(*,*) write(*,*) "which file do you want to use? (diagfi... stats... concat...)" read(*,'(a50)') file if (len_trim(file).eq.0) then write(*,*) "no file... game over" stop "" endif !============================================================================== ! 1.3. Open the first input file !============================================================================== ierr = NF_OPEN(file,NF_NOWRITE,nid) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Pb opening file '//file stop "" endif ierr=NF_INQ_NVARS(nid,nbvarfile) ! nbvarfile now set to be the (total) number of variables in file !============================================================================== ! 1.4. Ask for (output) "Time" axis type !============================================================================== notconcat(1)='Time' notconcat(2)='controle' notconcat(3)='rlonu' notconcat(4)='latitude' notconcat(5)='longitude' notconcat(6)='altitude' notconcat(7)='rlatv' notconcat(8)='aps' notconcat(9)='bps' notconcat(10)='ap' notconcat(11)='bp' notconcat(12)='cu' notconcat(13)='cv' notconcat(14)='aire' notconcat(15)='phisinit' !============================================================================== ! 1.5. Get (and check) list of variables to concatenate !============================================================================== write(*,*) Nnotconcat=0 do i=1,nbvarfile ierr=NF_INQ_VARNAME(nid,i,vartmp) ! vartmp now contains the "name" of variable of ID # i var_ok=0 do inter=1,15 if (vartmp.eq.notconcat(inter)) then var_ok=1 Nnotconcat=Nnotconcat+1 endif enddo if (var_ok.eq.0) write(*,*) trim(vartmp) enddo ! Nnotconcat: # of variables that won't be concatenated ! nbvarfile: total # of variables in file allocate(var(nbvarfile-Nnotconcat)) write(*,*) write(*,*) "which variables do you want to redistribute ?" write(*,*) "all / list of (separated by s)" write(*,*) "(an empty line , i.e: just , implies end of list)" nbvar=0 read(*,'(a50)') tmpvar do while ((tmpvar/=' ').AND.(trim(tmpvar)/='all')) nbvar=nbvar+1 var(nbvar)=tmpvar read(*,'(a50)') tmpvar enddo if (tmpvar=="all") then nbvar=nbvarfile-Nnotconcat do j=Nnotconcat+1,nbvarfile ierr=nf_inq_varname(nid,j,var(j-Nnotconcat)) enddo ! Variables names from the file are catched nbvar=nbvarfile-Nnotconcat do i=1,nbvar ierr=nf_inq_varname(nid,i+Nnotconcat,var(i)) write(*,'(a9,1x,i2,1x,a1,1x,a50)') "variable ",i,":",var(i) enddo else if(nbvar==0) then write(*,*) "no variable... game over" stop "" endif ! of if (tmpvar=="all") !============================================================================== ! 1.6. Get output file name !============================================================================== filename=file(1:len_trim(file)-3)//"_LT.nc" write(*,*) filename !============================================================================== ! 2.1. Open input file !============================================================================== if (i/=1) then write(*,*) write(*,*) "opening "//trim(file)//"..." ierr = NF_OPEN(file,NF_NOWRITE,nid) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Pb opening file '//file write(*,*) NF_STRERROR(ierr) stop "" endif endif !============================================================================== ! 2.2. Read (and check) dimensions of variables from input file !============================================================================== ierr=NF_INQ_DIMID(nid,"latitude",latdim) ierr=NF_INQ_VARID(nid,"latitude",latvar) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Field is missing in file'//file stop "" endif ierr=NF_INQ_DIMLEN(nid,latdim,latlen) ! write(*,*) "latlen: ",latlen ierr=NF_INQ_DIMID(nid,"longitude",londim) ierr=NF_INQ_VARID(nid,"longitude",lonvar) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Field is missing in file'//file stop "" endif ierr=NF_INQ_DIMLEN(nid,londim,lonlen) ! write(*,*) "lonlen: ",lonlen ierr=NF_INQ_DIMID(nid,"altitude",altdim) ierr=NF_INQ_VARID(nid,"altitude",altvar) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Field is missing in file'//file stop "" endif ierr=NF_INQ_DIMLEN(nid,altdim,altlen) ! write(*,*) "altlen: ",altlen ierr=NF_INQ_DIMID(nid,"index",ctldim) ierr=NF_INQ_VARID(nid,"controle",ctlvar) if (ierr.NE.NF_NOERR) then write(*,*) 'Field is missing in file'//file ctllen=0 !stop "" else ierr=NF_INQ_DIMLEN(nid,ctldim,ctllen) endif ! write(*,*) "controle: ",controle !============================================================================== ! 2.3. Read (and check compatibility of) dimensions of ! variables from input file !============================================================================== ! First call; initialize/allocate allocate(lat(latlen)) allocate(lon(lonlen)) allocate(alt(altlen)) allocate(ctl(ctllen)) #ifdef NC_DOUBLE ierr = NF_GET_VAR_DOUBLE(nid,latvar,lat) ierr = NF_GET_VAR_DOUBLE(nid,lonvar,lon) ierr = NF_GET_VAR_DOUBLE(nid,altvar,alt) if (ctllen .ne. -1) ierr = NF_GET_VAR_DOUBLE(nid,ctlvar,ctl) #else ierr = NF_GET_VAR_REAL(nid,latvar,lat) ierr = NF_GET_VAR_REAL(nid,lonvar,lon) ierr = NF_GET_VAR_REAL(nid,altvar,alt) if (ctllen .ne. -1) ierr = NF_GET_VAR_REAL(nid,ctlvar,ctl) #endif !============================================================================== ! 2.4. Handle "Time" dimension from input file !============================================================================== !============================================================================== ! 2.4.0 Read "Time" dimension from input file !============================================================================== ierr=NF_INQ_DIMID(nid,"Time",timedim) if (ierr.NE.NF_NOERR) then write(*,*) 'ERROR: Dimension