!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Program Arare
!
!   * Developer: SUGIYAMA Ko-ichiro 
!   * Version: $ 
!   * Tag Name: $Name: arare3j $
!   * Change History: 
!
!== Overview 
!
!$BHs@ENO3X%b%G%k(B deepconv/arare. 
!
!== Error Handling
!
!== Known Bugs
!
!  * $B1tD>J}8~$NB.EY$N<0$NIbNO9`$K@xG.8z2L$,F~$C$F$$$J$$(B
!  * $B6E7k@.J,$NHfG.$HJ,;RNL$rL5;k$7$F$$$k(B
! 
!== Note
!
!  * $BJ}Dx<07O$O=`05=L7O(B.
!
!== Future Plans
!
                                                                 !=end
!-- (2005/04/21 $B>.9b@5;L(B) --
! $BA4BNE*$J%3%a%s%H(B
! - $B%W%m%0%i%`A4BN$NN.$l$O$3$l$G$h$$$H;W$o$l$k(B.
! - $BL?L>K!(B($B<jB3$-L>(B, $BJQ?t(B, $B2>0z?t(B)$B$O8!F$$NM>CO$"$j(B
! - $B%3%a%s%HJ8$r$I$NDxEY=q$-9~$`$+(B?
!   arare.f90 $B$+$i%3!<%I2r@b$N!V%a%$%s%W%m%0%i%`$NN.$l!W$,<+F0@8@.(B
!   $B$5$l$k$h$&$K$J$C$F$$$k$H$h$$$@$m$&(B. 
!  

program arare
                                                                 !=begin
  !== Dependency
    !-- (2005/04/21 $B>.9b@5;L(B) --
    ! use $B$5$l$kJQ?t$K$b%3%a%s%H$rIU$1$?$[$&$,$h$$$H;W$&$,(B, $B$&$k$5$$(B? 
    ! $B%3%a%s%H$O3F%5%V%k!<%A%s(B/$B%b%8%e!<%kFb$N5-=R$H0lCW$5$;$k(B.
    !
  use dc_trace, only: BeginSub, &      ! $B%G%P%C%0=PNO%5%V%k!<%A%s(B  
       &              EndSub,   &      ! $B%G%P%C%0=PNO%5%V%k!<%A%s(B  
       &              DbgMessage       ! $B%G%P%C%0=PNO%5%V%k!<%A%s(B  
  use dc_message                       ! only $BB0@-ITMW(B ?
  use fileset,  only: fileset_init,  & ! I/O $B%U%!%$%kL>=i4|2=%5%V%k!<%A%s(B  
       &              ReStartFile,   & ! $BF~NOMQ%j%9%?!<%H%U%!%$%k(B
       &              NewReStartFile   ! $B=PNOMQ%j%9%?!<%H%U%!%$%k(B
  use debugset, only: debugset_init    ! $B%G%P%C%0@_Dj=i4|2=%5%V%k!<%A%s(B
!       &              DebugOn          ! $B%G%P%C%0=PNO%9%$%C%A(B
  use gridset,  only: gridset_init,  & ! $B3J;RE@>pJs=i4|2=%5%V%k!<%A%s(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
       &              RegZMin,       & ! z $BJ}8~$NJ*M}NN0h$N2<8B(B
       &              RegZMax          ! z $BJ}8~$NJ*M}NN0h$N>e8B(B 
  use timeset,  only: timeset_init,  & ! $B;~4V@QJ,@_Dj=i4|2=%5%V%k!<%A%s(B
       &              NstepShort,    & ! $BD9$$%?%$%`%9%F%C%W$N%9%F%C%W?t(B
       &              NstepLong,     & ! $BC;$$%?%$%`%9%F%C%W$N%9%F%C%W?t(B
       &              DelTimeShort,  & ! $BD9$$%?%$%`%9%F%C%W(B
       &              DelTimeLong,   & ! $BC;$$%?%$%`%9%F%C%W(B
       &              TimeDisp         ! $B%U%!%$%k=PNO$9$k;~4V4V3V(B
  use nameset,  only: nameset_init     ! $B<B83L>=i4|2=%5%V%k!<%A%s(B
!  use physset,  only: physset_init     ! $BJ*M}Dj?t$N=i4|2=%5%V%k!<%A%s(B
  use physset,  only: physset_init, PressSfc, GasRUniv, GasRDry, MolWtDry, MolWtWet, CpDry, epsilon
  use linlib,   only: linlib_init      ! $B9TNs7W;;%i%$%V%i%j=i4|2=%5%V%k!<%A%s(B
  use bcset,    only: bcset_init       ! $B6-3&>r7o@_Dj%5%V%k!<%A%s(B
  use basicset, only: basicset_file_init, &! $B4pK\>l@_Dj%5%V%k!<%A%s(B
       &              ss_PotTempBasicZ, &! $B4pK\>l$N%]%F%s%7%c%k29EY(B
       &              ss_MixRtVBasicZ,  &! $B4pK\>l$N?e>x5$:.9gHf(B
       &              ss_ExnerBasicZ     ! $B4pK\>l$N290L(B 
  use arareset, only: arareset_init,    &! $B7W;;@_Dj$rFI$_9~$`%5%V%k!<%A%s(B
       &              Min                ! $BBG$A@Z$j:G>.CM(B
  use spongelayer, only: fs_SLayer_fs, & ! $B%9%]%s%8AX(B(fs $B3J;RE@(B)
       &                 sf_SLayer_sf, & ! $B%9%]%s%8AX(B(sf $B3J;RE@(B)
       &                 ss_SLayer_ss    ! $B%9%]%s%8AX(B(ss $B3J;RE@(B)
                                                                 !=end
  !== $B0EL[$N7?@k8@6X;_(B
  implicit none

  !== $B:n6HG[Ns(B
  real(8), allocatable  :: fs_VelX_as(:,:)    ! $B?eJ?B.EY(B      [t=(n-1)$B&$&S(B]
  real(8), allocatable  :: fs_VelX_ns(:,:)    ! $B?eJ?B.EY(B      [t=n$B&$&S(B]
  real(8), allocatable  :: sf_VelZ_as(:,:)    ! $B1tD>B.EY(B      [t=(n-1)$B&$&S(B]
  real(8), allocatable  :: sf_VelZ_ns(:,:)    ! $B1tD>B.EY(B      [t=n$B&$&S(B]
  real(8), allocatable  :: ss_Exner_ns(:,:)   ! $B%(%/%9%J!<4X?t(B[t=n$B&$&S(B]
  real(8), allocatable  :: ss_Exner_as(:,:)   ! $B%(%/%9%J!<4X?t(B[t=(n-1)$B&$&S(B]

  real(8), allocatable  :: ss_Temp_bl(:,:)    ! $B290L(B [t=(n-1)$B&$(Bt]  
  real(8), allocatable  :: ss_Temp_nl(:,:)    ! $B290L(B [t=n$B&$(Bt]  
  real(8), allocatable  :: ss_PotTemp_bl(:,:) ! $B290L(B [t=(n-1)$B&$(Bt]  
  real(8), allocatable  :: ss_PotTemp_nl(:,:) ! $B290L(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: ss_PotTemp_al(:,:) ! $B290L(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: ss_Exner_bl(:,:)   ! $B%(%/%9%J!<4X?t(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_Exner_nl(:,:)   ! $B%(%/%9%J!<4X?t(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: ss_Exner_al(:,:)   ! $B%(%/%9%J!<4X?t(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: fs_VelX_bl(:,:)    ! $B?eJ?B.EY(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: fs_VelX_nl(:,:)    ! $B?eJ?B.EY(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: fs_VelX_al(:,:)    ! $B?eJ?B.EY(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: sf_VelZ_bl(:,:)    ! $B1tD>B.EY(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: sf_VelZ_nl(:,:)    ! $B1tD>B.EY(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: sf_VelZ_al(:,:)    ! $B1tD>B.EY(B [t=(n+1)$B&$(Bt]

  real(8), allocatable  :: ss_MixRtSat_bl(:,:) !$BK0OB?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtSat_nl(:,:) !$BK0OB?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtSat_al(:,:) !$BK0OB?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtV_bl(:,:) !$B?e>x5$:.9gHf(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_MixRtV_nl(:,:) !$B?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtV_al(:,:) !$B?e>x5$:.9gHf(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: ss_MixRtC_bl(:,:) !$B?e>x5$:.9gHf(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_MixRtC_nl(:,:) !$B?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtC_al(:,:) !$B?e>x5$:.9gHf(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: ss_MixRtR_bl(:,:)  !$B?e>x5$:.9gHf(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_MixRtR_nl(:,:)  !$B?e>x5$:.9gHf(B [t=n$B&$(Bt]
  real(8), allocatable  :: ss_MixRtR_al(:,:)  !$B?e>x5$:.9gHf(B [t=(n+1)$B&$(Bt]

  real(8), allocatable  :: fs_AdvDiffX_nl(:,:)! $B?eJ?J}8~$N1?F0J}Dx<0$N0\N.9`$H3H;69`(B   [t=n$B&$(Bt]
  real(8), allocatable  :: sf_AdvDiffZ_nl(:,:)! $B1tD>J}8~$N1?F0J}Dx<0$N0\N.9`$H3H;69`(B   [t=n$B&$(Bt]
  real(8), allocatable  :: sf_Buoy_nl(:,:) ! $B1tD>J}8~$N1?F0J}Dx<0$NIbNO9`(B [t=n$B&$(Bt]
  
  real(8), allocatable  :: ss_Km_bl(:,:)      ! $B12G4@-78?t(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_Km_nl(:,:)      ! $B12G4@-78?t(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: ss_Km_al(:,:)      ! $B12G4@-78?t(B [t=(n+1)$B&$(Bt]
  real(8), allocatable  :: ss_Kh_bl(:,:)      ! $B123H;678?t(B [t=(n-1)$B&$(Bt]
  real(8), allocatable  :: ss_Kh_nl(:,:)      ! $B123H;678?t(B [t=n$B&$(Bt    ]
  real(8), allocatable  :: ss_Kh_al(:,:)      ! $B123H;678?t(B [t=(n+1)$B&$(Bt]

  real(8), allocatable  :: ss_SvapPress_al(:,:)

  !--- $B>xH/$K$h$k1+?e$+$i?e>x5$$X$NJQ49(B
  real(8), allocatable  :: &
       & ss_EVrv_nl(:,:), & 
       !--- $BJ;9g@.D9$K$h$k1@?e$+$i1+?e$X$NJQ49(B
       & ss_CNcr_nl(:,:),&
       !--- $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B
       & ss_CLcr_nl(:,:),& 
       !--- $B1+N3=*C<B.EY(B
       & ss_VelZRain_nl(:,:),& 
       !--- $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B. 
       & ss_PRr_nl(:,:) 
  
  real(8)               :: Time               ! $B7W;;7QB3;~4V(B
  real(8)               :: DelTime            ! $B;~4V%9%F%C%W(B
  real(8)               :: ReStartTime(2)     ! $B%j%9%?!<%H%U%!%$%k$N;~9o(B
  integer               :: t, tau             ! $B%k!<%W$r2s$9$?$a$NJQ?t(B
  integer               :: NStep_leapfrog     ! $B%k!<%W$r2s$92s?t(B
  character(30)         :: cfgfile            ! NAMELIST $B%U%!%$%k(B


!== $B=i4|2=(B

  call getarg( 1, cfgfile )
  write(*,*) "Input NAMELIST file: ", cfgfile
  
  !=== I/O $B%U%!%$%kL>$N=i4|2=(B
  !NAMELIST $B%U%!%$%kL>$r;XDj$7(B, deepconv/arare $B$N(B
  !$B=PNO%U%!%$%kL>$r(B NAMELIST $B$+$iF@$k(B
  call fileset_init(cfgfile)

  !=== $B%G%P%0@_Dj$N=i4|2=(B
  !NAMELIST $B$+$i>pJs$rF@$F(B, $B%G%P%C%0=PNO%9%$%C%A$N@ZBX$($r9T$&(B.  
  call debugset_init(cfgfile)

  !=== $BJ*M}Dj?t$N=i4|2=(B
  !NAMELIST $B$+$iJ*M}Dj?t$rFI$_9~$`(B
  call physset_init(cfgfile)

  !=== $B3J;RE@>pJs$N=i4|2=(B
  !NAMELIST $B$+$i>pJs$rF@$F(B, $B3J;RE@$r7W;;$9$k(B
  call gridset_init(cfgfile)
  
  !=== $B;~4V@QJ,$N@_Dj$N=i4|2=(B
  !NAMELIST $B$+$iI,MW$J>pJs$rFI$_<h$j(B, $B;~4V4XO"$NJQ?t$N@_Dj$r9T$&(B. 
  call timeset_init(cfgfile)
  
  !=== $B<B83L>$N=i4|2=(B
  !netCDF $B$K=q$-9~$`<B83L>$r(B NAMELIST $B$+$iFI$_9~$`(B
  call nameset_init(cfgfile)
    
  !=== $B7W;;$N@_Dj$rFI$_9~$`(B
  !NameList $B%U%!%$%k$+$i(B arare $B<B9T$KI,MW$J%Q%i%a%?$r<hF@$9$k(B
  call arareset_init(cfgfile)

  !=== $B6-3&>r7o$r@_Dj(B
  !NAMELIST $B$+$i6-3&>r7o$N%?%$%W$r@_Dj$9$k(B. 
  call bcset_init(cfgfile)

  !=== $B=PNO%U%!%$%k$NDj5A(B
  call OpenDataFile


!== $BJQ?t$N=i4|2=(B

  !=== $BG[Ns$N3d$jEv$F(B, $B=i4|2=(B
  call ArareAlloc

  !=== $B4pK\>l$NCM$r@_Dj(B
  !$B4pK\>l$NCM$r(B netCDF $B%U%!%$%k$+$i<hF@$9$k(B. 
  call basicset_file_init( ReStartFile )

  !=== $B>qMp>l$NCM$r@_Dj(B
  !$B>qMp>l$N=i4|CM$r(B netCDF $B%U%!%$%k$+$i<hF@$9$k(B. 
  call getdisturbvar( ReStartFile, ReStartTime,   &
       & fs_VelX_bl,     sf_VelZ_bl,  ss_Exner_bl,  &
       & ss_PotTemp_bl,  ss_Km_bl,    ss_Kh_bl,     &
       & ss_MixRtSat_bl, ss_MixRtV_bl,        &
       & ss_MixRtC_bl, ss_MixRtR_bl,       &
       & fs_VelX_nl,     sf_VelZ_nl,  ss_Exner_nl,  &
       & ss_PotTemp_nl,  ss_Km_nl,    ss_Kh_nl,     &
       & ss_MixRtV_nl,        &
       & ss_MixRtC_nl, ss_MixRtR_nl )
  
  !=== $B9TNs7W;;%i%$%V%i%j$N=i4|2=(B. 
  !$BMxMQ$9$k@~7A7W;;%Q%C%1!<%8$r(B NAMELIST $B$+$i@_Dj$7(B, 
  !$BG[Ns$NBg$-$5$r7hDj$9$k(B
  !  * $B0z?t$O%9%+%i!<$N(B z $BJ}8~$N7W;;NN0h$NBg$-$5(B
  call linlib_init(cfgfile, RegZMax - RegZMin)

  !=== $B;~9o$N=i4|2=(B
  Time = ReStartTime(2)          !$B%j%9%?!<%H3+;O;~9o(B
  DelTime = 2.0 * DelTimeLong    !leap-frog $B%9%-!<%`$G$N@QJ,;~4V(B

  !=== $B%k!<%W2s?t$N=i4|2=(B
  !$B;~9o(B 0 $BIC$+$i$N7W;;(B, $B$9$J$o$A(B ReStartTime(1) = 0 $B$G$O(B, 
  !$B;~9o(B 0 $BIC$+$i@QJ,;~4V(B TimeInt $B$^$G@QJ,$9$k(B. 
  !$B$3$N$H$-(B leap-frog $B%9%-!<%`$O(B NstepLong - 1 $B2s%k!<%W$5$;$k$3$H$H$J$k(B. 
  !
  !$B7QB37W;;(B, $B$9$J$o$A(B ReStartTime(1) /= 0 $B$G$O(B, 
  !RestartTime(2) $BIC$+$i@QJ,;~4V(B TimeInt $B$^$G@QJ,$9$k(B. 
  !$B$3$N$H$-(B leap-frog $B%9%-!<%`$O(B NstepLong1 $B2s%k!<%W$5$;$k$3$H$H$J$k(B. 
  !$B7QB37W;;$N>l9g$K$O%j%9%?!<%H%U%!%$%k$N;~9o$+$i7W;;$r9T$$$?$$$+$i$G$"$k(B.

  if ( ReStartTime(1) == 0.0 ) then 
     Nstep_leapfrog = NstepLong - 1
  else
     Nstep_leapfrog = NstepLong
  end if

  !=== $B;~9o(B 0 $B$+$i$N7W;;$G$O(B, $BI,MW$K1~$8$F=i4|CM$r%U%!%$%k=PNO(B
  if ( ReStartTime(1) == 0.0 ) then 
     ss_Temp_bl =   ( ss_PotTemp_bl + ss_PotTempBasicZ ) &
          &       * ( ss_Exner_bl   + ss_ExnerBasicZ   )

     call OutputDataFile( ReStartTime(1), &
          & fs_VelX_bl, sf_VelZ_bl, ss_Exner_bl, &
          & ss_PotTemp_bl, ss_Km_bl, ss_Kh_bl, &
          & ss_MixRtV_bl, &
          & ss_MixRtC_bl, &
          & ss_MixRtR_bl, &
          & ss_Temp_bl, &
          & ss_EVrv_nl, & 
          & ss_CNcr_nl,&
          & ss_CLcr_nl,& 
          & ss_VelZRain_nl,& 
          & ss_PRr_nl, &
          & ss_MixRtSat_bl, &
          & sf_Buoy_nl &          
          )

     if ( mod( ReStartTime(2), TimeDisp ) == 0 ) then 
        ss_Temp_nl =   ( ss_PotTemp_nl + ss_PotTempBasicZ ) &
             &       * ( ss_Exner_nl   + ss_ExnerBasicZ   )
        
        call OutputDataFile( ReStartTime(2), &
             & fs_VelX_nl, sf_VelZ_nl, ss_Exner_nl, &
             & ss_PotTemp_nl, ss_Km_nl, ss_Kh_nl, &
             & ss_MixRtV_nl, &
             & ss_MixRtC_nl, &
             & ss_MixRtR_nl, &
             & ss_Temp_nl, &
             & ss_EVrv_nl, & 
             & ss_CNcr_nl,&
             & ss_CLcr_nl,& 
             & ss_VelZRain_nl,& 
             & ss_PRr_nl, &
             & ss_MixRtSat_bl, &
             & sf_Buoy_nl &
          )
     end if
  end if


!== CFL $B>r7o$N%A%'%C%/(B
  call cflcheck


!== $BD9$$;~4V%9%F%C%W(B. leap-frog $B%9%-!<%`$rMxMQ(B
  do t = 1, Nstep_leapfrog
     Time = Time + DelTimeLong
     write(*,*) Time

     !=== $B12G4@-78?t(B, $B123H;678?t$N7W;;(B
     call EddyViscosity(                            &
          & ss_Km_bl,                               & 
          & DelTime,                                & !$B;~4V4V3V(B
          & fs_VelX_nl, sf_VelZ_nl, ss_Km_nl,       & !$B0\N.(B
          & fs_VelX_bl, sf_VelZ_bl, ss_PotTemp_bl,  & !$B3H;6Ey(B
          & ss_Km_bl, ss_Exner_bl,                  &
          & ss_MixRtV_bl, ss_MixRtC_bl,   &
          & ss_MixRtR_bl,                       &
          & ss_Km_al, ss_Kh_al )

     !=== $BCH$+$$1+$N%Q%i%a%?%j%<!<%7%g%s(B
     call WarmRainPrm( &
          & DelTime, & 
          & fs_VelX_nl, sf_VelZ_nl, ss_Exner_nl, &
          & ss_PotTemp_nl, ss_PotTemp_bl, &
          & ss_MixRtV_nl, ss_MixRtV_bl, &
          & ss_MixRtC_nl, ss_MixRtC_bl, &
          & ss_MixRtR_nl,  ss_MixRtR_bl, &
          & ss_Kh_bl, &
          & ss_PotTemp_al, &
          & ss_MixRtV_al, &
          & ss_MixRtC_al, &
          & ss_MixRtR_al, &
          & ss_EVrv_nl, & 
          & ss_CNcr_nl,&
          & ss_CLcr_nl,& 
          & ss_VelZRain_nl,& 
          & ss_PRr_nl )

     !=== $BK0OB>x5$05$KH<$&J*M}NL$N7W;;(B 
     !==== $BK0OB>x5$05$N%b%kHf(B
     ss_SvapPress_al =                                      &
       & ss_svap_ss(   (ss_PotTemp_al + ss_PotTempBasicZ)   &
       &             * (ss_Exner_nl + ss_ExnerBasicZ))
  
     !==== $BK0OB>x5$05$+$iK0OB:.9gHf$r7W;;(B
     ss_MixRtSat_al = &
       & ((MolWtWet/MolWtDry) * ss_SvapPress_al) &
       & / (PressSfc * ((ss_Exner_nl + ss_ExnerBasicZ) **(CpDry / GasRDry)) &
       &     - ss_SvapPress_al)
     
     !=== $BD9$$;~4V%9%F%C%W$G$NB.EY(B u $B$N0\N.(B, $B3H;6(B, $BG4@-(B
     call AdvectDiffuseX(                     &
          & fs_VelX_nl, sf_VelZ_nl,           & !$B0\N.(B
          & fs_VelX_bl, sf_VelZ_bl, ss_Km_bl, & !$BMpN.3H;6(B, $B?tCMG4@-(B
          & fs_AdvDiffX_nl )

     !=== $BD9$$;~4V%9%F%C%W$G$NB.EY(B w $B$N0\N.(B, $B3H;6(B, $BG4@-(B
     call AdvectDiffuseZ(                          &
          & fs_VelX_nl, sf_VelZ_nl, ss_PotTemp_nl, & !$B0\N.(B, $BIbNO(B
          & fs_VelX_bl, sf_VelZ_bl, ss_Km_bl,      & !$BMpN.3H;6(B, $B?tCMG4@-(B
          & ss_MixRtV_nl, ss_MixRtC_nl, ss_MixRtR_nl, &
          & sf_Buoy_nl, &
          & sf_AdvDiffZ_nl )
     
     !=== $BC;$$%?%$%`%9%F%C%W$N=i4|CM$r:n$k(B
     fs_VelX_ns  = fs_VelX_bl
     sf_VelZ_ns  = sf_VelZ_bl
     ss_Exner_ns = ss_Exner_bl 
     
     !=== $BC;$$%?%$%`%9%F%C%W$G$N@QJ,(B
     do tau = 1, NstepShort 
        !==== $BB.EY(B u $B$N7W;;(B
        call VelocityX(                                             &
             & fs_VelX_ns,                                          &
             & DelTimeShort,                                        &
             & fs_AdvDiffX_nl, fs_VelX_ns, sf_VelZ_ns, ss_Exner_ns, &
             & fs_VelX_as )
        
        !==== $B%(%/%9%J!<4X?t$N7W;;(B
        call Exner(                                    &
             & sf_AdvDiffZ_nl, fs_VelX_ns, fs_VelX_as, &
             &   sf_VelZ_ns, ss_Exner_ns,              &
             & ss_Exner_as )

        !==== $BB.EY(B w $B$N7W;;(B
        call VelocityZ(                                &
             & sf_VelZ_ns,                             &
             & DelTimeShort,                           &
             & sf_AdvDiffZ_nl, fs_VelX_ns, sf_VelZ_ns, &
             &    ss_Exner_ns, ss_Exner_as,            &
             & sf_VelZ_as )

        !=== $B%9%]%s%8AX(B
        ss_Exner_as = ss_Exner_as - DelTime * ss_SLayer_ss( ss_Exner_ns )
        fs_VelX_as  = fs_VelX_as  - DelTime * fs_SLayer_fs( fs_VelX_ns )
        sf_VelZ_as  = sf_VelZ_as  - DelTime * sf_SLayer_sf( sf_VelZ_ns )
        
        !==== $BC;$$;~4V%9%F%C%W$N%k!<%W$r2s$9$?$a$N=hCV(B
        ss_Exner_ns  = ss_Exner_as 
        fs_VelX_ns   = fs_VelX_as
        sf_VelZ_ns   = sf_VelZ_as
        
     end do
     
     !=== $B:G=*E*$JC;$$;~4V%9%F%C%W$G$NCM$rD9$$;~4V%9%F%C%W$G$NCM$H$_$J$9(B
     ss_Exner_al  = ss_Exner_as 
     fs_VelX_al   = fs_VelX_as
     sf_VelZ_al   = sf_VelZ_as     
     
     !=== $B%"%C%;%j%s$N;~4V%U%#%k%?(B
     call AsselinTimeFilter( &
          & ss_MixRtV_al, ss_MixRtV_nl, ss_MixRtV_bl )
     call AsselinTimeFilter( &
          & ss_MixRtC_al, ss_MixRtC_nl, ss_MixRtC_bl )
     call AsselinTimeFilter( &
          & ss_MixRtR_al,  ss_MixRtR_nl,  ss_MixRtR_bl )
     call AsselinTimeFilter(ss_PotTemp_al, ss_PotTemp_nl, ss_PotTemp_bl )
     call AsselinTimeFilter( ss_Exner_al,    ss_Exner_nl,    ss_Exner_bl )
     call AsselinTimeFilter( fs_VelX_al,     fs_VelX_nl,     fs_VelX_bl )
     call AsselinTimeFilter( sf_VelZ_al,     sf_VelZ_nl,     sf_VelZ_bl )


     !=== $B6KHy>/NL$NCM$OL5;k(B. $B?^:n@.$N:]$K(B log $B$r<h$k$3$H$,$"$k$N$G(B, 
     !    $B%<%m$r5vMF$7$?$/$J$$(B. 
!     where (ss_PotTemp_al < Min)
!       ss_PotTemp_al = Min
!     end where
!     where (ss_MixRtV_al < Min)
!       ss_MixRtV_al = Min
!     end where
!     where (ss_MixRtC_al < Min)
!       ss_MixRtC_al = Min
!     end where
!     where (ss_MixRtR_al < Min)
!       ss_MixRtR_al = Min
!     end where
     
     !=== $BD9$$;~4V%9%F%C%W$N%k!<%W$r2s$9$?$a$N=hCV(B
     ss_PotTemp_bl = ss_PotTemp_nl
     ss_MixRtSat_bl   = ss_MixRtSat_nl     
     ss_MixRtV_bl = ss_MixRtV_nl
     ss_MixRtC_bl = ss_MixRtC_nl
     ss_MixRtR_bl  = ss_MixRtR_nl
     ss_Exner_bl   = ss_Exner_nl 
     fs_VelX_bl    = fs_VelX_nl
     sf_VelZ_bl    = sf_VelZ_nl
     ss_Km_bl      = ss_Km_nl
     ss_Kh_bl      = ss_Kh_nl
     ss_PotTemp_nl = ss_PotTemp_al
     ss_MixRtSat_nl   = ss_MixRtSat_al     
     ss_MixRtV_nl = ss_MixRtV_al
     ss_MixRtC_nl = ss_MixRtC_al
     ss_MixRtR_nl  = ss_MixRtR_al
     ss_Exner_nl   = ss_Exner_al 
     fs_VelX_nl    = fs_VelX_al
     sf_VelZ_nl    = sf_VelZ_al
     ss_Km_nl      = ss_Km_al
     ss_Kh_nl      = ss_Kh_al

     !=== $B%U%!%$%k=PNO(B
     if ( mod( Time, TimeDisp ) == 0 ) then 
        ss_Temp_nl =   ( ss_PotTemp_nl + ss_PotTempBasicZ ) &
             &       * ( ss_Exner_nl   + ss_ExnerBasicZ   )

        call DbgMessage(fmt="Time = %f", d=(/Time/))
        call OutputDataFile( Time, &
             &  fs_VelX_nl,    sf_VelZ_nl, ss_Exner_nl, &
             &  ss_PotTemp_nl, ss_Km_nl,   ss_Kh_nl,    &
             &  ss_MixRtV_nl, &
             &  ss_MixRtC_nl, &
             &  ss_MixRtR_nl, &
             & ss_Temp_nl, &
             & ss_EVrv_nl, & 
             & ss_CNcr_nl,&
             & ss_CLcr_nl,& 
             & ss_VelZRain_nl,& 
             & ss_PRr_nl, &
             & ss_MixRtSat_nl, &
             & sf_Buoy_nl &
             )
     end if
     
  end do
  
  !=== $B=PNO%U%!%$%k$N%/%m!<%:(B
  call CloseDataFile

  !=== $B%j%9%?!<%H%U%!%$%k$N:n@.(B
  ReStartTime(1) = Time - DelTimeLong
  ReStartTime(2) = Time
  call MakeReStartFile( NewReStartFile, ReStartTime, &
       & fs_VelX_bl,    sf_VelZ_bl,  ss_Exner_bl,    &
       & ss_PotTemp_bl, ss_Km_bl,    ss_Kh_bl,       &
       & ss_MixRtSat_bl,   ss_MixRtV_bl,         &
       & ss_MixRtC_bl, ss_MixRtR_bl, &
       & fs_VelX_nl,    sf_VelZ_nl,  ss_Exner_nl,    &
       & ss_PotTemp_nl, ss_Km_nl,    ss_Kh_nl,       &
       & ss_MixRtV_nl,         &
       & ss_MixRtC_nl, ss_MixRtR_nl    )

contains

!!!
!!!$BG[Ns$N3d$jEv$F(B, $B=i4|2=(B
!!!
  subroutine ArareAlloc

    call BeginSub("ArareAlloc", &
&                 fmt="%c",     &
&                 c1="Allocate and initialize allocatable variables.")

    allocate(ss_Exner_as(DimXMin:DimXMax, DimZMin:DimZMax),  &
         & fs_VelX_as(DimXMin:DimXMax, DimZMin:DimZMax)  , &
         & sf_VelZ_as(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_AdvDiffX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_AdvDiffZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtSat_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtSat_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtSat_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtV_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtV_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtV_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtC_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtC_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtC_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtR_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtR_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_MixRtR_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_SvapPress_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Temp_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Temp_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_Buoy_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_EVrv_nl(DimXMin:DimXMax, DimZMin:DimZMax), & 
         & ss_CNcr_nl(DimXMin:DimXMax, DimZMin:DimZMax),&
         & ss_CLcr_nl(DimXMin:DimXMax, DimZMin:DimZMax),& 
         & ss_VelZRain_nl(DimXMin:DimXMax, DimZMin:DimZMax),& 
         & ss_PRr_nl(DimXMin:DimXMax, DimZMin:DimZMax) &         
         )

    fs_VelX_al = 0.0d0
    fs_VelX_nl = 0.0d0
    fs_VelX_bl = 0.0d0

    sf_VelZ_al = 0.0d0
    sf_VelZ_nl = 0.0d0
    sf_VelZ_bl = 0.0d0

    ss_Exner_al = 0.0d0
    ss_Exner_nl = 0.0d0
    ss_Exner_bl = 0.0d0
    
    ss_PotTemp_al = 0.0d0
    ss_PotTemp_nl = 0.0d0
    ss_PotTemp_bl = 0.0d0

    ss_Temp_nl = 0.0d0
    ss_Temp_bl = 0.0d0

    fs_VelX_as = 0.0d0
    fs_VelX_ns = 0.0d0

    sf_VelZ_as = 0.0d0
    sf_VelZ_ns = 0.0d0

    ss_Exner_as = 0.0d0
    ss_Exner_ns = 0.0d0

    fs_AdvDiffX_nl = 0.0d0
    sf_AdvDiffZ_nl = 0.0d0

    ss_Km_bl = 0.0d0
    ss_Km_nl = 0.0d0
    ss_Km_al = 0.0d0
    ss_Kh_bl = 0.0d0
    ss_Kh_nl = 0.0d0
    ss_Kh_al = 0.0d0

    ss_MixRtSat_bl = 0.0d0
    ss_MixRtSat_nl = 0.0d0
    ss_MixRtV_bl = 0.0d0
    ss_MixRtV_nl = 0.0d0
    ss_MixRtV_al = 0.0d0
    ss_MixRtC_bl = 0.0d0
    ss_MixRtC_nl = 0.0d0
    ss_MixRtC_al = 0.0d0
    ss_MixRtR_bl  = 0.0d0
    ss_MixRtR_nl  = 0.0d0
    ss_MixRtR_al  = 0.0d0

    sf_Buoy_nl     = 0.0d0
    ss_EVrv_nl     = 0.0d0
    ss_CNcr_nl     = 0.0d0
    ss_CLcr_nl     = 0.0d0
    ss_VelZRain_nl = 0.0d0
    ss_PRr_nl      = 0.0d0
    Time = 0.0d0

    call EndSub("ArareAlloc")

  end subroutine ArareAlloc


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

    ! $BF~=PNOJQ?t(B
    real(8)             :: ss_svap_ss(DimXMin:DimXMax, DimZMin:DimZMax)
                                                        !$BK0OB>x5$05(B [Pa]
    real(8), intent(in) :: ss_temp(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                               !$B29EY(B [K]
    
    ! Antoine $B$N<0$rMxMQ(B. H2O $B$N78?t$O2=3X9)3XJXMw;2>H(B. 
    real(8), parameter  :: A = 23.1964d0
    real(8), parameter  :: B = 3816.44d0
    real(8), parameter  :: C = -46.13d0
    
    ss_svap_ss = dexp( A - B / (C + ss_temp) ) !Antoine $B$N<0(B
    
  end function ss_svap_ss


end program arare

