!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_redef
  interface netcdf_redef

     subroutine netcdf_redef_dim(ncid, name, len, dimid)
       integer, intent(in)       :: ncid
       integer, intent(in)       :: len
       integer, intent(in)       :: dimid
       character(*), intent(in)  :: name
     end subroutine netcdf_redef_dim

     subroutine netcdf_redef_var(ncid, name, dim_name1, dim_name2, long_name, units, type)
       integer, intent(in)       :: ncid
       integer, intent(in)       :: type 
       character(*), intent(in) :: name
       character(*), intent(in) :: dim_name1
       character(*), intent(in) :: dim_name2
       character(*), intent(in) :: units
       character(*), intent(in) :: long_name
     end subroutine netcdf_redef_var

  end interface
end module interface_netcdf_redef


subroutine netcdf_redef_dim(ncid, name, len, dimid)
  use netcdf
  use interface_netcdf_err

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

  integer                   :: status
  
  status = nf90_redef(ncid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_def_dim(ncid, name, len, dimid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_enddef(ncid)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_redef_dim


subroutine netcdf_redef_var(ncid, name, dim_name1, dim_name2, long_name, units, type)
  use netcdf
  use interface_netcdf_err

  implicit none  
  integer, intent(in)       :: ncid
  integer, intent(in)       :: type 
  character(*), intent(in) :: name
  character(*), intent(in) :: dim_name1
  character(*), intent(in) :: dim_name2
  character(*), intent(in) :: units
  character(*), intent(in) :: long_name

  integer                   :: varid
  integer                   :: dimid1
  integer                   :: dimid2
  integer                   :: status

  status = nf90_redef(ncid)
  if(status /= nf90_noerr) call netcdf_err(status)
  
  status = nf90_inq_dimid(ncid, dim_name1, dimid1)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_inq_dimid(ncid, dim_name2, dimid2)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_def_var(ncid, name, type, (/dimid1, dimid2/), varid)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_put_att(ncid, varid, 'long_name', long_name)
  if(status /= nf90_noerr) call netcdf_err(status)
  
  status = nf90_put_att(ncid, varid, 'units', units)
  if(status /= nf90_noerr) call netcdf_err(status)

  status = nf90_enddef(ncid)
  if(status /= nf90_noerr) call netcdf_err(status)

end subroutine netcdf_redef_var






