!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-05    作成
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module interface_netcdf_put
  interface netcdf_put
     subroutine netcdf_put_int(ncid, name, var)
       integer, intent(in)       :: ncid
       integer, intent(in)       :: var
       character(*), intent(in) :: name
     end subroutine netcdf_put_int

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

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

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

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

  end interface
end module interface_netcdf_put



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

  implicit none
  integer, intent(in)       :: ncid
  integer, intent(in)       :: 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_put_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_put_int


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

  implicit none
  integer, intent(in)       :: ncid
  integer, intent(in)       :: 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_put_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_put_int_1d


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

  implicit none
  real(8), intent(in)       :: 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_put_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_put_real


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

  implicit none
  real(8), intent(in)       :: 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_put_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_put_real_1d


subroutine netcdf_put_char(ncid, name, var)
  use netcdf
  use interface_netcdf_err
  
  implicit none
  integer, intent(in)       :: ncid
  character(*), intent(in)  :: name
  character(*), intent(in)  :: var(:)
  
  integer                   :: varid
  integer                   :: status
  integer                   :: i
  
  status = nf90_inq_varid(ncid, name, varid)
  if(status /= nf90_noerr) call netcdf_err(status)     
  status = nf90_put_var(ncid, varid, var)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_put_char
