!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004, 2005, 2006. All rights reserved.
!---------------------------------------------------------------------
!= Module ReStartFileIO
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: restartfileio.f90,v 1.1.1.1 2006/04/25 03:43:58 deepconv Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!$B%j%9%?!<%HMQ$N>l$N>pJs$r(B netCDF $B%U%!%$%k$K=PNO$9$k$?$a$N%k!<%A%s(B
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!$BB.EY$rI>2A$9$k3J;R0LCV$H(B, $B<4$H$7$FF~NO$7$F$"$k:BI8CM$O@09gE*$G$J$$$3$H$KCm0U(B
!
!== Future Plans
!

module ReStartFileIO
  !
  !$B%j%9%?!<%HMQ$N>l$N>pJs$r(B netCDF $B%U%!%$%k$K=PNO$9$k$?$a$N%k!<%A%s(B
  !

  !$B%b%8%e!<%kFI$_9~$_(B
  use gt4_history
  use gridset,  only: s_X,               &!X $B:BI8<4(B($B%9%+%i!<3J;RE@(B)
    &                 s_Z,               &!Z $B:BI8<4(B($B%9%+%i!<3J;RE@(B)
    &                 FileNX,            &!X $BJ}8~$N3J;RE@(B($B%U%!%$%k=PNO(B)
    &                 FileNZ,            &!Z $BJ}8~$N3J;RE@(B($B%U%!%$%k=PNO(B)
    &                 FileXMin,          &!X $BJ}8~$NG[Ns$N2<8B(B($B%U%!%$%k=PNO(B)
    &                 FileXMax,          &!X $BJ}8~$NG[Ns$N>e8B(B($B%U%!%$%k=PNO(B)
    &                 FileZMin,          &!Z $BJ}8~$NG[Ns$N2<8B(B($B%U%!%$%k=PNO(B)
    &                 FileZMax,          &!Z $BJ}8~$NG[Ns$N>e8B(B($B%U%!%$%k=PNO(B)
    &                 DimXMin,           &!X $BJ}8~$NG[Ns$N2<8B(B
    &                 DimXMax,           &!X $BJ}8~$NG[Ns$N>e8B(B
    &                 DimZMin,           &!Z $BJ}8~$NG[Ns$N2<8B(B
    &                 DimZMax,           &!Z $BJ}8~$NG[Ns$N>e8B(B
    &                 SpcNum              !$B6E=L@.J,$N?t(B
  use fileset,  only: HistoryFile,       &!$B%R%9%H%j%U%!%$%kL>(B
    &                 exptitle,          &!$B%G!<%?$NI=Bj(B
    &                 expsrc,            &!$B%G!<%?$r:n@.$9$k<j=g(B
    &                 expinst             !$B:G=*JQ99<T!&AH?%(B
  use basicset, only: SpcWetID,          &!$B6E=L@.J,$N(B ID
    &                 xz_ExnerBasicZ,    &!$B4pK\>l$N%(%/%9%J!<4X?t(B
    &                 xz_DensBasicZ,     &!$B4pK\>l$NL)EY(B
    &                 xz_PotTempBasicZ,  &!$B4pK\>l$N290L(B
    &                 xz_VelSoundBasicZ, &!$B4pK\>l$N2;B.(B
    &                 xz_PressBasicZ,    &!$B4pK\>l$N05NO(B
    &                 xz_TempBasicZ,     &!$B4pK\>l$N29EY(B
    &                 xza_MixRtBasicZ,    &!$B4pK\>l$N:.9gHf(B
    &                 xz_EffMolWtBasicZ   !$B4pK\>l$NJ,;RNL8z2L(B

  !$B0EL[$N7?@k8@6X;_(B
  implicit none
  
  !$BB0@-$N;XDj(B
  private

  !$B4X?t$r(B public $B$K;XDj(B
  public ReStartFile_Open
  public ReStartFile_OutPut
  public ReStartFile_Close
  public ReStartFile_Get

contains

  subroutine ReStartFile_Open(  )
    !
    !$B%j%9%?!<%H%U%!%$%k$N=q$-=P$7(B
    !
    
    !$B%b%8%e!<%kFI$_9~$_(B
    use gt4_history
    use gridset,  only: s_X, s_Z, SpcNum
    use fileset,  only: exptitle, expsrc, expinst, ReStartFile
    use basicset, only: xz_ExnerBasicZ,   xz_DensBasicZ, &
      &                 xz_PotTempBasicZ, xz_VelSoundBasicZ, &
      &                 xz_PressBasicZ,   xz_TempBasicZ,     &
      &                 xza_MixRtBasicZ,  xz_EffMolWtBasicZ, &
      &                 SpcWetID
    
    !$B0EL[$N7?@k8@6X;_(B
    implicit none
    
    !$BJQ?tDj5A(B
    integer             :: N, M
    
    N = size(s_X, 1)
    M = size(s_Z, 1)
    
    !-------------------------------------------------------------    
    ! $B%R%9%H%j!<:n@.(B
    !-------------------------------------------------------------  
    call HistoryCreate(                              &
      & file = ReStartFile,                          &
      & title = exptitle,                            &
      & source = expsrc,                             &
      & institution = expinst,                       &
      & dims=(/'x','z','s','t'/),                    &
      & dimsizes=(/N, M, SpcNum, 2/),                & !$B;~4VJ}8~$OG[NsMWAG(B 2 $B$D(B
      & longnames=(/'X-coordinate',                  &
      &             'Z-coordinate',                  &
      &             'Species Num ',                  &
      &             'Time        '/),                &
      & units=(/'m','m','1','t'/), origin=0.0,       &
      & interval=0.0 )
    
    !-------------------------------------------------------------  
    ! $BJQ?t=PNO(B
    !-------------------------------------------------------------
    call HistoryPut('x', s_X )
    call HistoryPut('z', s_Z )
    call HistoryPut('s', real(SpcWetID, 4) )
    
    !$BL5<!8505NO$N4pK\>l(B
    call HistoryAddVariable(                             &
      & varname='ExnerBasicZ', dims=(/'x','z'/),      &
      & longname='nondimensional pressure', units='1',&
      & xtype='double' )
    
    !$B290L$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='PotTempBasicZ', dims=(/'x','z'/),    &
      & longname='potential temperature',            &
      & units='K', xtype='double' )
    
    !$BL)EY$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='DensBasicZ', dims=(/'x','z'/),      &
      & longname='density',                          &
      & units='Kg/m^3', xtype='double' )
    
    !$B2;GHB.EY$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='VelSoundBasicZ', dims=(/'x','z'/),  &
      & longname='sound velocity',                   &
      & units='m/s|2', xtype='double' )
    
    !$B29EY$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='TempBasicZ', dims=(/'x','z'/),  &
      & longname='Temperature of basic state',       &
      & units='K', xtype='double' ) 
    
    !$B05NO$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='PressBasicZ', dims=(/'x','z'/),  &
      & longname='Pressure of basic state',             &
      & units='Pa', xtype='double' ) 
    
    !$B?e>x5$:.9gHf$N4pK\>l(B
    call HistoryAddVariable(                            &
      & varname='MixRtBasicZ', dims=(/'x','z','s'/),  &
      & longname='Mixing ratio of Condensible volatiles',        &
      & units='kg/kg', xtype='double' ) 
    
    !$BJ,;RNL8z2L(B
    call HistoryAddVariable(                         &
      & varname='EffMolWtBasicZ', dims=(/'x','z'/),  &
      & longname='Effect of Mole Weight',            &
      & units='1', xtype='double' ) 
    
    !$BL5<!8505NO(B
    call HistoryAddVariable(                         &
      & varname='Exner', dims=(/'x','z','t'/),       &
      & longname='nondimensional pressure',          &
      & units='1',                                   &
      & xtype='double' )
    
    !$B290L$N>qMp(B
    call HistoryAddVariable(                         &
      & varname='PotTemp', dims=(/'x','z','t'/),     &
      & longname='virtual potential temperature',    &
      & units='K',                                   &
      & xtype='double' )
    
    !$BB.EY(B
    call HistoryAddVariable(                         &
      & varname='VelX', dims=(/'x','z','t'/),        &
      & longname='zonal velocity',                   &
      & units='m/s',                                 &
      & xtype='double' )
    
    !$BB.EY(B
    call HistoryAddVariable(                         &
      & varname='VelZ', dims=(/'x','z','t'/),        &
      & longname='vertical velocity',                &
      & units='m/s',                                 &
      & xtype='double' )
    
    !$B12G4@-78?t(B
    call HistoryAddVariable(                         &
      & varname='Km', dims=(/'x','z','t'/),          &
      & longname='Km',                               &
      & units='1',                                   &
      & xtype='double' )
    
    !$B?e>x5$:.9gHf(B
    call HistoryAddVariable(                         &
      & varname='MixRt', dims=(/'x','z','s','t'/),  &
      & longname='Mixing Ratio of Water Vaper',      &
      & units='1',                                   & 
      & xtype='double' )
    
    !-------------------------------------------------------------
    ! $B4pK\>l$N%U%!%$%k=PNO(B
    !-------------------------------------------------------------
    call HistoryPut( 'DensBasicZ',     xz_DensBasicZ     )
    call HistoryPut( 'ExnerBasicZ',    xz_ExnerBasicZ    )
    call HistoryPut( 'PotTempBasicZ',  xz_PotTempBasicZ  )
    call HistoryPut( 'VelSoundBasicZ', xz_VelSoundBasicZ )
    call HistoryPut( 'TempBasicZ',     xz_TempBasicZ     )
    call HistoryPut( 'PressBasicZ',    xz_PressBasicZ    )
    call HistoryPut( 'MixRtBasicZ',    xza_MixRtBasicZ   )
    call HistoryPut( 'EffMolWtBasicZ', xz_EffMolWtBasicZ )
    
  end subroutine ReStartFile_Open
  
  
  subroutine ReStartFile_OutPut(        &
    & Time,                             &
    & pz_VelX,    xr_VelZ, xz_Exner,    &
    & xz_PotTemp, xz_Km,   xza_MixRt )
    !
    !$B%j%9%?!<%H%U%!%$%k$KM=JsJQ?t$r=q$-=P$9(B
    !
    
    !$B%b%8%e!<%kFI$_9~$_(B
    use gt4_history
    use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax, SpcNum

    !$B0EL[$N7?@k8@6X;_(B
    implicit none
    
    !$BJQ?tDj5A(B
    real(8), intent(in)  :: Time
    real(8), intent(in)  :: pz_VelX(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xr_VelZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_Km(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xza_MixRt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    
  
    !------------------------------------------------------------------
    ! $B%U%!%$%k=PNO(B
    !------------------------------------------------------------------
    call HistoryPut( 't',       Time       )
    call HistoryPut( 'VelX',    pz_VelX    )
    call HistoryPut( 'VelZ',    xr_VelZ    )
    call HistoryPut( 'Exner',   xz_Exner   ) 
    call HistoryPut( 'PotTemp', xz_PotTemp )
    call HistoryPut( 'Km',      xz_Km      )
    call HistoryPut( 'MixRt',   xza_MixRt   )
    
  end subroutine ReStartFile_OutPut
  
  
  subroutine ReStartFile_Close
    !
    !$B%j%9%?!<%H%U%!%$%k$N%/%m!<%:(B
    !
    
    !$B%b%8%e!<%kFI$_9~$_(B
    use gt4_history
    
    !$B0EL[$N7?@k8@6X;_(B
    implicit none

    !$B%U%!%$%k$rJD$8$k(B
    call HistoryClose
    
  end subroutine ReStartFile_Close


  subroutine ReStartFile_Get(   &
    & ReStartTime,                            &
    & pz_VelX_b,    xr_VelZ_b,   xz_Exner_b,  &
    & xz_PotTemp_b, xz_Km_b,     xza_MixRt_b,  &
    & pz_VelX_n,    xr_VelZ_n,   xz_Exner_n,  &
    & xz_PotTemp_n, xz_Km_n,     xza_MixRt_n )
    !
    !$B%j%9%?!<%H%U%!%$%k$+$i>pJs<hF@(B
    !

    !$B%b%8%e!<%kFI$_9~$_(B
    use gt4_history
    use dc_message ,only: MessageNotify
    use timeset,    only: DelTimeLong
    use fileset,    only: InitFile
    use gridset,    only: DimXMin, DimXMax, DimZMin, DimZMax, SpcNum

    !$B0EL[$N7?@k8@6X;_(B
    implicit none

    !$BJQ?tDj5A(B
    real(8), intent(out) :: ReStartTime(2)
    real(8), intent(out) :: pz_VelX_n(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xr_VelZ_n(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_Exner_n(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_PotTemp_n(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_Km_n(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xza_MixRt_n(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8), intent(out) :: pz_VelX_b(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xr_VelZ_b(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_Exner_b(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_PotTemp_b(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_Km_b(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xza_MixRt_b(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: DelTime
    real(8)              :: Var3D(DimXMin:DimXMax, DimZMin:DimZMax, 2)
!    real(8)              :: Var4D(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum, 2)
    character(30)        :: name            !$BJQ?tL>(B

    
    !-------------------------------------------------------------
    !Get a Value from netCDF File
    !-------------------------------------------------------------
    name = "t"
    call HistoryGet( InitFile, name, ReStartTime )
    
    !$B;~4V9o$_$N%A%'%C%/(B
    DelTime = ReStartTime(2) - ReStartTime(1)
    if ( DelTime /= real(DelTimeLong, 4) ) then 
      write(*,*) DelTime, DelTimeLong
      call MessageNotify("Error", "getdistrbvar", &
        &    "DelTime for InitFile is not the same as DelTimeLong")
      stop
    end if
    
    !-------------------------------------------------------------    
    ! Get a Value from netCDF File 
    !-------------------------------------------------------------
    name = "VelX"
    call HistoryGet( InitFile, name, Var3D )    
    pz_VelX_b = Var3D(:,:,1)
    pz_VelX_n = Var3D(:,:,2)
    
    name = "VelZ"
    call HistoryGet( InitFile, name, Var3D )
    xr_VelZ_b = Var3D(:,:,1)
    xr_VelZ_n = Var3D(:,:,2)
    
    name = "Exner"
    call HistoryGet( InitFile, name, Var3D )
    xz_Exner_b = Var3D(:,:,1)
    xz_Exner_n = Var3D(:,:,2)
    
    name = "PotTemp"
    call HistoryGet( InitFile, name, Var3D )
    xz_PotTemp_b = Var3D(:,:,1)
    xz_PotTemp_n = Var3D(:,:,2)
    
    name = "Km"
    call HistoryGet( InitFile, name, Var3D )
    xz_Km_b = Var3D(:,:,1)
    xz_Km_n = Var3D(:,:,2)
    
    name = "MixRt"
!    call HistoryGet( InitFile, name, Var4D )
!    xza_MixRt_b = Var4D(:,:,:,1)
!    xza_MixRt_n = Var4D(:,:,:,2)
    
  end subroutine ReStartFile_Get

end module ReStartFileIO
