module read_file_module implicit none #include "netcdf.inc" integer :: ncid integer :: idv_time,idv_xls,idv_t,idv_u,idv_v,idv_z, | idv_lon,idv_lat character(len=1024) :: attnames(6), atttext(6) ! ! LBC data to be read from netcdf file every timestep: integer,parameter :: nlon=72, nlat=36 real :: glon(nlon),glat(nlat) real,dimension(nlon,nlat) :: z_mgcm,t_mgcm,u_mgcm,v_mgcm real,allocatable :: time(:),xls(:) contains !----------------------------------------------------------------------- subroutine open_infile(filein,ntime) implicit none ! ! Args character(len=*),intent(in) :: filein integer,intent(out) :: ntime ! ! Local: integer :: i,istat,ndims,nvars,ngatts,len,itype, | id_time,id_lon,id_lat,nmgcmlon,nmgcmlat,itime ! character(len=1024) :: attname,atttext real :: dtime ! istat = nf_open(filein,NF_NOWRITE,ncid) if (istat /= NF_NOERR) then write(6,"(/,'>>> open_infile: error opening ',a)") trim(filein) stop 'open_infile' else write(6,"('Opened netcdf file ',a,' for reading: ncid=',i3)") | trim(filein),ncid endif ! ! Get number of dims, vars, atts. Print global attributes to stdout. istat = nf_inq(ncid,ndims,nvars,ngatts,id_time) write(6,"(/,'Global attributes from file ',a,':')") | trim(filein) attnames(:) = ' ' ; atttext(:) = ' ' do i=1,ngatts istat = nf_inq_attname(ncid,NF_GLOBAL,i,attnames(i)) istat = nf_inq_att(ncid,NF_GLOBAL,attnames(i),itype,len) if (itype==NF_CHAR) then istat = nf_get_att_text(ncid,NF_GLOBAL,attnames(i),atttext(i)) write(6,"(' ',a,': ',a)") trim(attnames(i)),trim(atttext(i)) endif enddo ! ! Get number of times (length of unlimited dimension) and allocate time var: istat = nf_inq_dimlen(ncid,id_time,ntime) if (allocated(time)) deallocate(time) allocate(time(ntime),stat=istat) if (istat /= 0) write(6,"('>>> open_infile: error allocating ', | ' time var: ntime=',i5)") ntime ! ! Read time var: istat = nf_inq_varid(ncid,'TIME',idv_time) ! get id of time var if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of time var') istat = nf_get_var_double(ncid,idv_time,time) ! read time var if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error reading time var') dtime = time(2)-time(1) write(6,"('open_infile: ntime=',i5,' dtime=',f10.4,' time(1)=', | f10.3,' time(ntime)=',f10.3)") ntime,dtime,time(1),time(ntime) ! ! Read XLS: ! if (allocated(xls)) deallocate(xls) ! allocate(xls(ntime),stat=istat) ! if (istat /= 0) write(6,"('>>> open_infile: error allocating ', ! | ' xls var: ntime=',i5)") ntime ! istat = nf_inq_varid(ncid,'XLS',idv_xls) ! get id of time var ! if (istat /= NF_NOERR) call handle_ncerr(istat, ! | 'open_infile: error getting id of xls var') ! istat = nf_get_var_double(ncid,idv_xls,xls) ! read xls var ! if (istat /= NF_NOERR) call handle_ncerr(istat, ! | 'open_infile: error reading xls var') ! write(6,"(/,'open_infile: ntime=',i5,' xls(1)=', ! | f10.3,' xls(ntime)=',f10.3)") ntime,xls(1),xls(ntime) ! ! Check lon,lat dimensions -- should be same as nlon and nlat: istat = nf_inq_dimid(ncid,'lon',id_lon) istat = nf_inq_dimlen(ncid,id_lon,nmgcmlon) if (nmgcmlon /= nlon) then write(6,"(/,'>>> open_infile: nmgcmlon /= nlon: nmgcmlon=',i4, | ' nlon=',i3)") nmgcmlon,nlon stop 'open_infile' endif ! istat = nf_inq_dimid(ncid,'lat',id_lat) istat = nf_inq_dimlen(ncid,id_lat,nmgcmlat) if (nmgcmlat /= nlat) then write(6,"(/,'>>> open_infile: nmgcmlat /= nlat: nmgcmlat=',i4, | ' zimx=',i3)") nmgcmlat,nlat stop 'open_infile' endif ! ! Read coord vars lat,lon: istat = nf_inq_varid(ncid,'LON',idv_lon) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of lon var') istat = nf_get_var_double(ncid,idv_lon,glon) ! read xls var if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error reading lon var') ! write(6,"(/,'open_infile: glon=',/,(8f8.2))") glon ! istat = nf_inq_varid(ncid,'LAT',idv_lat) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of lat var') istat = nf_get_var_double(ncid,idv_lat,glat) ! read xls var if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error reading lat var') ! write(6,"(/,'open_infile: glat=',/,(8f8.2))") glat ! ! Get id's for data vars z,t,u,v. These will be used by sub rdmgcmlbc. istat = nf_inq_varid(ncid,'Z',idv_z) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of var Z') istat = nf_inq_varid(ncid,'TN',idv_t) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of var TN') istat = nf_inq_varid(ncid,'UN',idv_u) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of var UN') istat = nf_inq_varid(ncid,'VN',idv_v) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'open_infile: error getting id of var VN') end subroutine open_infile !----------------------------------------------------------------------- subroutine read_file(itime) implicit none ! ! Args integer,intent(in) :: itime ! ! Local: integer :: istat,icount3(3),istart3(3) real :: fmin,fmax ! istart3(1:2) = 1 istart3(3) = itime icount3(1) = nlon icount3(2) = nlat icount3(3) = 1 ! write(6,"(/,'read_file: itime=',i5)") itime ! ! Z: istat = nf_get_vara_double(ncid,idv_z,istart3,icount3,z_mgcm) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'rdmgcmlbc: error reading Z var') ! write(6,"(' min,max Z = ',2e12.4)") minval(z_mgcm),maxval(z_mgcm) ! ! TN: istat = nf_get_vara_double(ncid,idv_t,istart3,icount3,t_mgcm) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'rdmgcmlbc: error reading TN var') ! write(6,"(' min,max T = ',2e12.4)") minval(t_mgcm),maxval(t_mgcm) ! ! UN: istat = nf_get_vara_double(ncid,idv_u,istart3,icount3,u_mgcm) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'rdmgcmlbc: error reading UN var') ! write(6,"(' min,max U = ',2e12.4)") minval(u_mgcm),maxval(u_mgcm) ! ! VN: istat = nf_get_vara_double(ncid,idv_v,istart3,icount3,v_mgcm) if (istat /= NF_NOERR) call handle_ncerr(istat, | 'rdmgcmlbc: error reading VN var') ! write(6,"(' min,max V = ',2e12.4)") minval(v_mgcm),maxval(v_mgcm) end subroutine read_file !----------------------------------------------------------------------- subroutine handle_ncerr(istat,msg) implicit none ! ! Handle a netcdf lib error: ! integer,intent(in) :: istat character(len=*),intent(in) :: msg ! write(6,"(/72('-'))") write(6,"('>>> Error from netcdf library:')") write(6,"(a)") trim(msg) write(6,"('istat=',i5)") istat write(6,"(a)") nf_strerror(istat) write(6,"(72('-')/)") return end subroutine handle_ncerr end module read_file_module