!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Module dycore_restart_mod
!
!   * Developers: Morikawa Yasuhiro
!   * Version: $Id: dycore_restart.f90,v 1.8 2005/01/09 16:47:33 morikawa Exp $
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview
!
!This module output gtool4 netCDF data for restart.
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
!
                                                                 !=end
module dycore_restart_mod
                                                                 !=begin
  !== Dependency
  use dycore_type_mod, only : STRING
                                                                 !=end
  implicit none
                                                                 !=begin
  !== Public Interface
  private
  public :: dycore_restart_init, dycore_restart_put ! subroutines
  public :: dycore_restart_end                      ! subroutines
                                                                 !=end

  logical, save :: dycore_restart_initialized = .false.
  character(STRING),parameter:: version = &
       & '$Id: dycore_restart.f90,v 1.8 2005/01/09 16:47:33 morikawa Exp $'
  character(STRING),parameter:: tagname = '$Name:  $'

contains
                                                                 !=begin
  !== Procedure Interface
  !
  !=== Initialize module and acquire NAMELIST
  !
  !モジュールを初期化し、NAMELIST から値を取得する。
  !NAMELIST から値が取得できないものに関しては上記のデフォルト値が
  !用いられる。
  !
  !NAMELIST ファイルは、メインプログラムにて ((< nmlfile_mod >)) の
  !((< nmlfile_init >)) で指定されることが想定されているが、
  !もしもこの初期化ルーチンより以前に指定されていなければ、
  !((< nmlfile_init >)) のデフォルトで指定される NAMELIST ファイルを
  !読む。
  !
  subroutine dycore_restart_init(Dims)
    use dycore_type_mod, only: DYCORE_DIMS
    use nmlfile_mod    , only : nmlfile_init, nmlfile_open, nmlfile_close
    use io_gt4_out_mod , only: io_gt4_out_init, io_gt4_out_SetDims, &
         &                     io_gt4_out_SetVars
    use dc_trace       , only: BeginSub, EndSub, DbgMessage
                                                                 !=end
    implicit none
                                                                 !=begin
    !==== Input
    !
    type(DYCORE_DIMS), intent(in):: Dims  ! 次元データ全種
                                                                 !=end
    !----- 作業用内部変数 -----
    character(len = *),  parameter:: subname = "dycore_restart_init"

  continue

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if (dycore_restart_initialized) then
       call EndSub( subname, '%c is already called.', c1=trim(subname) )
       return
    else
       dycore_restart_initialized = .true.
    endif

    !----------------------------------------------------------------
    !   Version identifier
    !----------------------------------------------------------------
    call DbgMessage('%c :: %c', c1=trim(version), c2=trim(tagname))

    !-----------------------------------------------------------------
    !   io_gt4_out_mod の初期化
    !-----------------------------------------------------------------
    call io_gt4_out_init       ! データ出力の初期設定

    !-----------------------------------------------------------------
    !   出力用の軸データ設定
    !-----------------------------------------------------------------
    call io_gt4_out_SetDims(Dims%x_Lon)       ! 経度座標重みデータ取得
    call io_gt4_out_SetDims(Dims%y_Lat)       ! 緯度座標重みデータ取得
    call io_gt4_out_SetDims(Dims%z_Sigma)     ! 整数σレベル座標データ取得
    call io_gt4_out_SetDims(Dims%r_Sigma) ! 半整数σレベル座標データ取得

    !-----------------------------------------------------------------
    !   出力用の変数データ設定
    !-----------------------------------------------------------------
    call io_gt4_out_SetVars('VelLon')
    call io_gt4_out_SetVars('VelLat')
    call io_gt4_out_SetVars('Vor')
    call io_gt4_out_SetVars('Div')
    call io_gt4_out_SetVars('Temp')
    call io_gt4_out_SetVars('QVap')
    call io_gt4_out_SetVars('Ps')

    call EndSub(subname)
  end subroutine dycore_restart_init


                                                                 !=begin
  !=== Put DYCORE_VARS Data.
  !
  !格子点データ群 Vars を出力する。
  !
  subroutine dycore_restart_put(Vars)
  !==== Dependency
    use dycore_type_mod, only : DYCORE_VARS, DBKIND, REKIND
    use io_gt4_out_mod,  only : io_gt4_out_Put
    use dc_trace,        only : BeginSub, EndSub, DbgMessage, DataDump
                                                                 !=end
    implicit none
                                                                 !=begin
    !==== Input
    !
    type(DYCORE_VARS), intent(in):: Vars  ! 格子点データ全種
                                                                 !=end
    !----- 作業用内部変数 -----
    character(len = *),  parameter:: subname = "dycore_restart_put"

  continue

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if (.not. dycore_restart_initialized) then
       call EndSub( subname, 'Call dycore_restart_init before call %c',  &
            &       c1=trim(subname) )
       return
    endif

!!$    call io_gt4_out_Put(  'VelLon', real(Vars%xyz_VelLon(:,:,:), REKIND)  )
!!$    call io_gt4_out_Put(  'VelLat', real(Vars%xyz_VelLat(:,:,:), REKIND)  )
!!$    call io_gt4_out_Put(  'Vor' , real(Vars%xyz_Vor(:,:,:) , REKIND)  )
!!$    call io_gt4_out_Put(  'Div' , real(Vars%xyz_Div(:,:,:) , REKIND)  )
!!$    call io_gt4_out_Put(  'Temp', real(Vars%xyz_Temp(:,:,:), REKIND)  )
!!$    call io_gt4_out_Put(  'QVap', real(Vars%xyz_QVap(:,:,:), REKIND)  )
!!$    call io_gt4_out_Put(  'Ps'  , real(Vars%xy_Ps(:,:)     , REKIND)  )

    call io_gt4_out_Put(  'VelLon', real(Vars%xyz_VelLon(:,:,:), DBKIND)  )
    call io_gt4_out_Put(  'VelLat', real(Vars%xyz_VelLat(:,:,:), DBKIND)  )
    call io_gt4_out_Put(  'Vor' , real(Vars%xyz_Vor(:,:,:) , DBKIND)  )
    call io_gt4_out_Put(  'Div' , real(Vars%xyz_Div(:,:,:) , DBKIND)  )
    call io_gt4_out_Put(  'Temp', real(Vars%xyz_Temp(:,:,:), DBKIND)  )
    call io_gt4_out_Put(  'QVap', real(Vars%xyz_QVap(:,:,:), DBKIND)  )
!!$    call io_gt4_out_Put(  'Ps'  , real(Vars%xy_Ps(:,:)     , DBKIND)  )

    ! テスト用に、一時 hPa で出力
    call io_gt4_out_Put(  'Ps'  , real(Vars%xy_Ps(:,:)     , DBKIND)/100.0d0  )

    call EndSub(subname)
  end subroutine dycore_restart_put



                                                                 !=begin
  !=== Terminate module
  !
  !((< io_gt4_out_mod >)) の終了処理を行なう。
  !
  subroutine dycore_restart_end
  !==== Dependency
    use io_gt4_out_mod, only : io_gt4_out_end
    use dc_trace,       only : BeginSub, EndSub, DbgMessage
                                                                 !=end
    implicit none

    !-------------------------------------------------------------------
    !   変数定義
    !-------------------------------------------------------------------
    !----- 作業用内部変数 -----
    character(len = *),  parameter:: subname = "dycore_restart_end"

  continue

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if ( .not. dycore_restart_initialized) then
       call EndSub( subname, 'dycore_restart_init was not called', &
            &       c1=trim(subname) )
       return
    else
       dycore_restart_initialized = .false.
    endif

    call io_gt4_out_end

    call EndSub(subname)
  end subroutine dycore_restart_end

end module dycore_restart_mod
