#include "dims.h"
      module mgcmlbc_module
      use input_module,only: mgcm_lbc_file,tempdir
      implicit none
#include "params.h"
#include "netcdf.inc"
!
      integer :: ncid,ntime,idv_z,idv_tn,idv_un,idv_vn
      real,allocatable :: time(:)
!
! LBC data to be read from netcdf file every timestep:
      real,dimension(zimx,zjmx) :: z_mgcm,tn_mgcm,un_mgcm,vn_mgcm
      contains
!-----------------------------------------------------------------------
      subroutine open_mgcmlbc
!
! Open netcdf file mgcm_lbc_file. Allocate and read time var. 
! This is called once per run from init.
!
      character(len=1024) :: diskfile,attname,atttext
      integer :: i,ndims,nvars,ngatts,len,itype,istat,nmgcmlon,nmgcmlat
      integer :: id_time,idv_time,id_lon,id_lat
      real :: dtime
!
!     call mkdiskflnm(mgcm_lbc_file,diskfile)
!     call getms(mgcm_lbc_file,diskfile,tempdir,diskfile) 
      diskfile = ' '
      call getfile(mgcm_lbc_file,diskfile)
      write(6,"(72('-'))")
      write(6,"('Enter sub open_mgcmlbc')")
!
! Open existing netcdf file for read-only:
      istat = nf_open(diskfile,NF_NOWRITE,ncid)
      if (istat /= NF_NOERR) then
        write(6,"(/,'>>> open_mgcmlbc: error opening ',a,' as a ',
     |    'netcdf file.')") trim(diskfile)
        stop 'open_mgcmlbc'
      else
        write(6,"('Opened netcdf disk file ',a)") trim(diskfile)
      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(diskfile)
      do i=1,ngatts
        attname = ' ' ; atttext = ' '
        istat = nf_inq_attname(ncid,NF_GLOBAL,i,attname)
        istat = nf_inq_att(ncid,NF_GLOBAL,attname,itype,len)
        if (itype==NF_CHAR) then
          istat = nf_get_att_text(ncid,NF_GLOBAL,attname,atttext)
          write(6,"('  ',a,': ',a)") trim(attname),trim(atttext)
        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_mgcmlbc: error allocating ',
     |  ' time var: ntime=',i5)") ntime
!
! Read time var (maybe not necessary):
      istat = nf_inq_varid(ncid,'TIME',idv_time)  ! get id of time var
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'open_mgcmlbc: error getting of time var')
      istat = nf_get_var_double(ncid,idv_time,time) ! read time var
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'open_mgcmlbc: error reading time var')
      dtime = time(2)-time(1)
      write(6,"(/,'open_mgcmlbc: ntime=',i5,' dtime=',f10.4,' time(1)=',
     |  f10.3,' time(ntime)=',f10.3)") ntime,dtime,time(1),time(ntime)
!
! Check lon,lat dimensions -- should be same as zimx and zjmx in params.h:
      istat = nf_inq_dimid(ncid,'lon',id_lon)
      istat = nf_inq_dimlen(ncid,id_lon,nmgcmlon)
      if (nmgcmlon /= zimx) then
        write(6,"(/,'>>> open_mgcmlbc: nmgcmlon /= zimx: nmgcmlon=',i4,
     |    ' zimx=',i3)") nmgcmlon,zimx
        stop 'open_mgcmlbc'
      endif
!
      istat = nf_inq_dimid(ncid,'lat',id_lat)
      istat = nf_inq_dimlen(ncid,id_lat,nmgcmlat)
      if (nmgcmlat /= zjmx) then
        write(6,"(/,'>>> open_mgcmlbc: nmgcmlat /= zimx: nmgcmlat=',i4,
     |    ' zimx=',i3)") nmgcmlat,zimx
        stop 'open_mgcmlbc'
      endif
!
! 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_mgcmlbc: error getting id of var Z')
      istat = nf_inq_varid(ncid,'TN',idv_tn)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'open_mgcmlbc: error getting id of var TN')
      istat = nf_inq_varid(ncid,'UN',idv_un)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'open_mgcmlbc: error getting id of var UN')
      istat = nf_inq_varid(ncid,'VN',idv_vn)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'open_mgcmlbc: error getting id of var VN')
!
      write(6,"('Sub open_mgcmlbc returning.')")
      write(6,"(72('-'))")
      end subroutine open_mgcmlbc
!-----------------------------------------------------------------------
      subroutine rdmgcmlbc(itime)
!
! Read lbc data at time itime (this is called from advnce at every timestep).
!
      integer,intent(in) :: itime
      integer :: istat,icount3(3),istart3(3)
      real :: fmin,fmax
!
      if (itime < 0 .or. itime > ntime) then
        write(6,"(/,'>>> rdmgcmlbc: bad itime=',i5,' (ntime=',i5,')')")
     |    itime,ntime
        stop 'rdmgcmlbc'
      endif
!
      istart3(1:2) = 1
      istart3(3) = itime
      icount3(1) = zimx
      icount3(2) = zjmx
      icount3(3) = 1
!     write(6,"(/,'rdmgcmlbc: 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')
!     call fminmax(z_mgcm,zimx*zjmx,fmin,fmax)
!     write(6,"('  min,max Z  = ',2e12.4)") fmin,fmax
!
! TN:
      istat = nf_get_vara_double(ncid,idv_tn,istart3,icount3,tn_mgcm)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'rdmgcmlbc: error reading TN var')
!     call fminmax(tn_mgcm,zimx*zjmx,fmin,fmax)
!     write(6,"('  min,max TN = ',2e12.4)") fmin,fmax
!
! UN:
      istat = nf_get_vara_double(ncid,idv_un,istart3,icount3,un_mgcm)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'rdmgcmlbc: error reading UN var')
!     call fminmax(un_mgcm,zimx*zjmx,fmin,fmax)
!     write(6,"('  min,max UN = ',2e12.4)") fmin,fmax
!
! VN:
      istat = nf_get_vara_double(ncid,idv_vn,istart3,icount3,vn_mgcm)
      if (istat /= NF_NOERR) call handle_ncerr(istat,
     |  'rdmgcmlbc: error reading VN var')
!     call fminmax(vn_mgcm,zimx*zjmx,fmin,fmax)
!     write(6,"('  min,max VN = ',2e12.4)") fmin,fmax
!
      end subroutine rdmgcmlbc
!-----------------------------------------------------------------------
      subroutine handle_ncerr(istat,msg)
!
! 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 mgcmlbc_module
