subroutine nbi_readcdfr8_0(icdf, zname, zdata, idm, zvartype)
  !
  !  read r8 scalar
  !  adapt if scalar in file is r4...
 
  use ezcdf
  use nbi_com 
  implicit none

  integer, intent(in) :: icdf          ! netcdf file id
  character*(*), intent(in) :: zname   ! name of data to be read
  real(fp), intent(out) :: zdata         ! storage for data to be read
  integer, intent(in) :: idm(3)        ! actual dimensions
  character*(*), intent(in) :: zvartype  ! "r4" or "r8" expected

  !----------------------------------------------------
  ! local...

  real :: r4buf  ! in case data is r4...
  integer, save :: iwarn = 0

  !----------------------------------------------------

  if((idm(1).eq.-1).and.(idm(2).eq.-1).and.(idm(3).eq.-1)) then
     write(nonlin,*) ' '
     write(nonlin,*) ' %nbi_readcdfr8_0:  not in state file:  -----> ', &
          trim(zname)
     write(nonlin,*) '  data set to zero (could be an old state file).'
     zdata = zero
     return
  endif

  if((idm(1).ne.0).or.(idm(2).ne.0).or.(idm(3).ne.0)) then
     write(nonlin,*) ' ?nbi_readcdfr8_0: variable: ', trim(zname), &
          ' dimension: ',idm
     write(nonlin,*) '  nbi_readcdfr8_0: scalar data expected.'
     zdata = zero
     return
  endif

  if(zvartype.eq.'R8') then
     call cdfgetvar(icdf, zname, zdata)

  else if(zvartype.eq.'R4') then
     iwarn=iwarn+1
     if(iwarn.le.1) then
        write(nonlin,*) ' %nbi_readcdfr8_0: detected r4 data in state file: ',&
             trim(zname)
     endif

     call cdfgetvar(icdf, zname, r4buf)
     zdata = r4buf

  else
     write(nonlin,*) ' ?nbi_readcdfr8_0: variable: ', trim(zname), &
          ' dimension: ',idm,' vartype: ',trim(zvartype)
     write(nonlin,*) '  nbi_readcdfr8_0: "r4" or "r8" data expected.'
     write(nonlin,*) '  data set to zero (could be old state file).'
     zdata = zero
  endif
end subroutine nbi_readcdfr8_0

subroutine nbi_readcdfr8_1(icdf, zname, zdata, idim1, idm, zvartype)
  !
  !  read r8 1d vector
  !  adapt if vector in file is r4...
 
  use ezcdf
  use nbi_com
  implicit none

  integer, intent(in) :: icdf          ! netcdf file id
  character*(*), intent(in) :: zname   ! name of data to be read
  integer, intent(in) :: idim1         ! dimensioning info
  real(fp), intent(out) :: zdata(idim1)         ! storage for data to be read
  integer, intent(in) :: idm(3)        ! actual dimensions
  character*(*), intent(in) :: zvartype  ! "r4" or "r8" expected

  !----------------------------------------------------
  ! local...

  real, dimension(:), allocatable :: r4buf  ! in case data is r4...
  integer, save :: iwarn = 0

  !----------------------------------------------------

  if((idm(1).eq.-1).and.(idm(2).eq.-1).and.(idm(3).eq.-1)) then
     write(nonlin,*) ' '
     write(nonlin,*) ' %nbi_readcdfr8_1:  not in state file:  -----> ', &
          trim(zname)
     write(nonlin,*) '  data set to zero (could be an old state file).'
     zdata = zero
     return
  endif

  if((idm(1).ne.idim1).or.(idm(2).ne.0).or.(idm(3).ne.0)) then
     write(nonlin,*) ' ?nbi_readcdfr8_1: variable: ', trim(zname), &
          ' dimension: ',idm
     write(nonlin,*) '  expected dimensioning: ',idim1
     write(nonlin,*) '  nbi_readcdfr8_1: 1d vector data was expected.'
     zdata = zero
     return
  endif

  if(zvartype.eq.'R8') then
     call cdfgetvar(icdf, zname, zdata)

  else if(zvartype.eq.'R4') then
     iwarn=iwarn+1
     if(iwarn.le.1) then
        write(nonlin,*) ' %nbi_readcdfr8_1: detected r4 data in state file: ',&
             trim(zname)
     endif

     allocate(r4buf(idim1))
     call cdfgetvar(icdf, zname, r4buf)
     zdata = r4buf
     deallocate(r4buf)

  else
     write(nonlin,*) ' ?nbi_readcdfr8_1: variable: ', trim(zname), &
          ' dimension: ',idm,' vartype: ',trim(zvartype)
     write(nonlin,*) '  nbi_readcdfr8_1: "r4" or "r8" data expected.'
     write(nonlin,*) '  data set to zero (could be old state file).'
     zdata = zero
  endif
end subroutine nbi_readcdfr8_1

subroutine nbi_readcdfr8_2(icdf, zname, zdata, idim1, idim2, idm, zvartype)
  !
  !  read r8 1d vector
  !  adapt if vector in file is r4...
 
  use ezcdf
  use nbi_com
  implicit none

  integer, intent(in) :: icdf          ! netcdf file id
  character*(*), intent(in) :: zname   ! name of data to be read
  integer, intent(in) :: idim1,idim2         ! dimensioning info
  real(fp), intent(out) :: zdata(idim1,idim2)  ! storage for data to be read
  integer, intent(in) :: idm(3)        ! actual dimensions
  character*(*), intent(in) :: zvartype  ! "r4" or "r8" expected

  !----------------------------------------------------
  ! local...

  real, dimension(:,:), allocatable :: r4buf  ! in case data is r4...
  integer, save :: iwarn = 0

  !----------------------------------------------------

  if((idm(1).eq.-1).and.(idm(2).eq.-1).and.(idm(3).eq.-1)) then
     write(nonlin,*) ' '
     write(nonlin,*) ' %nbi_readcdfr8_2:  not in state file:  -----> ', &
          trim(zname)
     write(nonlin,*) '  data set to zero (could be an old state file).'
     zdata = zero
     return
  endif

  if((idm(1).ne.idim1).or.(idm(2).ne.idim2).or.(idm(3).ne.0)) then
     write(nonlin,*) ' ?nbi_readcdfr8_2: variable: ', trim(zname), &
          ' dimension: ',idm
     write(nonlin,*) '  expected dimensioning: ',idim1,idim2
     write(nonlin,*) '  nbi_readcdfr8_2: 2d array data was expected.'
     zdata = zero
     return
  endif

  if(zvartype.eq.'R8') then
     call cdfgetvar(icdf, zname, zdata)

  else if(zvartype.eq.'R4') then
     iwarn=iwarn+1
     if(iwarn.le.1) then
        write(nonlin,*) ' %nbi_readcdfr8_2: detected r4 data in state file: ',&
             trim(zname)
     endif

     allocate(r4buf(idim1,idim2))
     call cdfgetvar(icdf, zname, r4buf)
     zdata = r4buf
     deallocate(r4buf)

  else
     write(nonlin,*) ' ?nbi_readcdfr8_2: variable: ', trim(zname), &
          ' dimension: ',idm,' vartype: ',trim(zvartype)
     write(nonlin,*) '  nbi_readcdfr8_2: "r4" or "r8" data expected.'
     write(nonlin,*) '  data set to zero (could be old state file).'
     zdata = zero
  endif
end subroutine nbi_readcdfr8_2

subroutine nbi_readcdfr8_3(icdf, zname, zdata, idim1, idim2, idim3, &
     idm, zvartype)
  !
  !  read r8 1d vector
  !  adapt if vector in file is r4...
 
  use ezcdf
  use nbi_com
  implicit none

  integer, intent(in) :: icdf          ! netcdf file id
  character*(*), intent(in) :: zname   ! name of data to be read
  integer, intent(in) :: idim1,idim2,idim3        ! dimensioning info
  real(fp), intent(out) :: zdata(idim1,idim2,idim3) ! storage for data to be read
  integer, intent(in) :: idm(3)        ! actual dimensions
  character*(*), intent(in) :: zvartype  ! "r4" or "r8" expected

  !----------------------------------------------------
  ! local...

  real, dimension(:,:,:), allocatable :: r4buf  ! in case data is r4...
  integer, save :: iwarn = 0

  !----------------------------------------------------

  if((idm(1).eq.-1).and.(idm(2).eq.-1).and.(idm(3).eq.-1)) then
     write(nonlin,*) ' '
     write(nonlin,*) ' %nbi_readcdfr8_3:  not in state file:  -----> ', &
          trim(zname)
     write(nonlin,*) '  data set to zero (could be an old state file).'
     zdata = zero
     return
  endif

  if((idm(1).ne.idim1).or.(idm(2).ne.idim2).or.(idm(3).ne.idim3)) then
     write(nonlin,*) ' ?nbi_readcdfr8_3: variable: ', trim(zname), &
          ' dimension: ',idm
     write(nonlin,*) '  expected dimensioning: ',idim1,idim2,idim3
     write(nonlin,*) '  nbi_readcdfr8_3: 3d array data was expected.'
     zdata = zero
     return
  endif

  if(zvartype.eq.'R8') then
     call cdfgetvar(icdf, zname, zdata)

  else if(zvartype.eq.'R4') then
     iwarn=iwarn+1
     if(iwarn.le.1) then
        write(nonlin,*) ' %nbi_readcdfr8_3: detected r4 data in state file: ',&
             trim(zname)
     endif

     allocate(r4buf(idim1,idim2,idim3))
     call cdfgetvar(icdf, zname, r4buf)
     zdata = r4buf
     deallocate(r4buf)

  else
     write(nonlin,*) ' ?nbi_readcdfr8_3: variable: ', trim(zname), &
          ' dimension: ',idm,' vartype: ',trim(zvartype)
     write(nonlin,*) '  nbi_readcdfr8_3: "r4" or "r8" data expected.'
     write(nonlin,*) '  data set to zero (could be old state file).'
     zdata = zero
  endif
end subroutine nbi_readcdfr8_3

