!c Description:
!c  NetCDF ファイルの値の取得
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Histry: 
!c   Version    Date          Comment
!c   -------    ----------    --------
!c   1.0        2003-02-06    作成
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module interface_netcdf_get
  interface netcdf_get

     subroutine netcdf_get_int(ncid, name, var)
       integer, intent(in)      :: ncid
       integer, intent(out)     :: var
       character(*), intent(in) :: name
     end subroutine netcdf_get_int

     subroutine netcdf_get_int_1d(ncid, name, var)
       integer, intent(in)                   :: ncid
       integer, intent(out), allocatable     :: var(:)
       character(*), intent(in)              :: name
     end subroutine netcdf_get_int_1d

     subroutine netcdf_get_real(ncid, name, var)
       real(8), intent(out)      :: var
       integer, intent(in)       :: ncid
       character(*), intent(in)  :: name
     end subroutine netcdf_get_real

     subroutine netcdf_get_real_1d(ncid, name, var)
       real(8), intent(out), allocatable :: var(:)
       integer, intent(in)               :: ncid
       character(*), intent(in)         :: name
     end subroutine netcdf_get_real_1d

     subroutine netcdf_get_real_2d(ncid, name, var)
       real(8), intent(out), allocatable :: var(:,:)
       integer, intent(in)               :: ncid
       character(*), intent(in)          :: name
     end subroutine netcdf_get_real_2d
     
     subroutine netcdf_get_char(ncid, name, var)
       integer, intent(in)                    :: ncid
       character(*), intent(out), allocatable :: var(:)
       character(*), intent(in)               :: name
     end subroutine netcdf_get_char

  end interface
end module interface_netcdf_get

subroutine netcdf_get_int(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none
  integer, intent(in)       :: ncid
  integer, intent(out)      :: var
  character(*), intent(in)  :: name

  integer                   :: varid
  integer                   :: status


  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_get_int


subroutine netcdf_get_int_1d(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none
  integer, intent(in)                  :: ncid
  integer, intent(out), allocatable    :: var(:)
  character(*), intent(in)             :: name

  integer                   :: varid
  integer                   :: numdim
  integer                   :: dimid(nf90_max_var_dims)
  integer                   :: len
  integer                   :: status


  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_inquire_variable(ncid, varid, ndims = numdim)
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_variable(ncid, varid, dimids = dimid(:numdim))
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_dimension(ncid, dimid(1), len = len)
  if(status /= nf90_noerr) call netcdf_err(status)
  
  allocate(var(len))
  
  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_get_int_1d


subroutine netcdf_get_real(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none
  real(8), intent(out)      :: var
  integer, intent(in)       :: ncid
  character(*), intent(in) :: name

  integer                   :: varid
  integer                   :: status


  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_get_real


subroutine netcdf_get_real_1d(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none  
  real(8), intent(out), allocatable :: var(:)
  integer, intent(in)               :: ncid
  character(*), intent(in)         :: name

  integer                   :: varid
  integer                   :: numdim
  integer                   :: dimid(nf90_max_var_dims)
  integer                   :: len
  integer                   :: status


  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)
  
  status = nf90_inquire_variable(ncid, varid, ndims = numdim)
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_variable(ncid, varid, dimids = dimid(:numdim))
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_dimension(ncid, dimid(1), len = len)
  if(status /= nf90_noerr) call netcdf_err(status)
  
  allocate(var(len))
  
  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_get_real_1d


subroutine netcdf_get_real_2d(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none  
  real(8), intent(out), allocatable :: var(:,:)
  integer, intent(in)               :: ncid
  character(*), intent(in)         :: name

  integer                   :: varid
  integer                   :: numdim
  integer                   :: dimid(nf90_max_var_dims)
  integer                   :: len1
  integer                   :: len2
  integer                   :: status

  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_inquire_variable(ncid, varid, ndims = numdim)
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_variable(ncid, varid, dimids = dimid(:numdim))
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_dimension(ncid, dimid(1), len = len1)
  if(status /= nf90_noerr) call netcdf_err(status)
  status = nf90_inquire_dimension(ncid, dimid(2), len = len2)
  if(status /= nf90_noerr) call netcdf_err(status)

  allocate(var(len1, len2))
  
  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_get_real_2d


subroutine netcdf_get_char(ncid, name, var)
  use netcdf
  use interface_netcdf_err

  implicit none
  integer, intent(in)                    :: ncid
  character(*), intent(out), allocatable :: var(:)
  character(*), intent(in)               :: name

  integer                   :: varid
  integer                   :: numdim
  integer                   :: dimid(nf90_max_var_dims)
  integer                   :: len
  integer                   :: status

  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)
!  write(*,*) "1"

  status = nf90_inquire_variable(ncid, varid, ndims = numdim)
  if(status /= nf90_noerr) call netcdf_err(status)
!  write(*,*) "2"

  status = nf90_inquire_variable(ncid, varid, dimids = dimid(:numdim))
  if(status /= nf90_noerr) call netcdf_err(status)
!  write(*,*) "3"

  status = nf90_inquire_dimension(ncid, dimid(2), len = len)
  if(status /= nf90_noerr) call netcdf_err(status)
!  write(*,*) "4"
  
  allocate(var(len))
  
  status = nf90_get_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)
!  write(*,*) "5"

end subroutine netcdf_get_char
