!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
!---------------------------------------------------------------------
!= Subroutine 
!
!   * Developer: SUGIYAMA Ko-ichiro
!   * Version: $Id: $
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview
!
!$BCH$+$$1+$N%P%k%/K!$rMQ$$$?(B, $B?e>x5$$H1+(B, $B1@$H1+$N:.9gHf$NJQ4978?t$r5a$a$k(B.
! 
!== Error Handling
!
!== Bugs
!
!== Note
!
!== Future Plans
!
!
subroutine 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, &
  & ss_MixRtVSat_al    )

  !== Dependency
  use dc_trace, only: BeginSub, EndSub
  use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax, &
    &                 RegXMin, RegXMax, RegZMin, RegZMax
  use bcset,    only: ss_BC
  use average,  only: ss_avr_fs, ss_avr_sf, fs_avr_ss, sf_avr_ss
  use basicset, only: ss_ExnerBasicZ, &
       &              ss_DensBasicZ,  &
       &              ss_PotTempBasicZ, &
       &              ss_MixRtVBasicZ,  &
       &              ss_PressBasicZ  
  use physset,  only: PressSfc, GasRUniv, GasRDry, MolWtDry, MolWtWet, CpDry, epsilon
  use arareset, only: Min
  use satvappress, only: ss_Svap_ss, Svap, ss_LatentHeat_ss, LatentHeat, DSvapDTemp
  use differentiate_center2, only: sf_dz_ss


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

  !== Input
  real(8), intent(in) :: DelTime !$B@QJ,;~4V(B
  real(8), intent(in) :: ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B%(%/%9%J!<4X?t(B
  real(8), intent(in) :: ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B290L(B
  real(8), intent(in) :: ss_PotTemp_bl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B290L(B
  real(8), intent(in) :: ss_MixRtV_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B?e>x5$NL(B
  real(8), intent(in) :: ss_MixRtV_bl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B?e>x5$NL(B
  real(8), intent(in) :: ss_MixRtC_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B1@N3:.9gHf(B
  real(8), intent(in) :: ss_MixRtC_bl(DimXMin:DimXMax, DimZMin:DimZMax)
                                                          !$B1@N3:.9gHf(B
  real(8), intent(in) :: ss_MixRtR_nl(DimXMin:DimXMax, DimZMin:DimZMax)  
                                                          !$B1+N3:.9gHf(B
  real(8), intent(in) :: ss_MixRtR_bl(DimXMin:DimXMax, DimZMin:DimZMax)  
                                                          !$B1+N3:.9gHf(B
  real(8), intent(in) :: fs_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax)   
                                                          !$BB.EY(B u
  real(8), intent(in) :: sf_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax)   
                                                          !$BB.EY(B w
  real(8), intent(in) :: ss_Kh_bl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                          !$B12G4@-78?t(B

  !== Out
  real(8), intent(Out)  :: ss_PotTemp_al(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$B290L(B
  real(8), intent(Out)  :: ss_MixRtV_al(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$B?e>x5$:.9gHf(B
  real(8), intent(Out)  :: ss_MixRtC_al(DimXMin:DimXMax, DimZMin:DimZMax)
                                            !$B1@N3:.9gHf(B
  real(8), intent(Out)  :: ss_MixRtR_al(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$B1+N3:.9gHf(B
  real(8), intent(Out)  :: ss_EVrv_nl(DimXMin:DimXMax, DimZMin:DimZMax)
                                            !$B>xH/$K$h$k1+?e$+$i?e>x5$$X$NJQ49(B
  real(8), intent(Out)  :: ss_CNcr_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$BJ;9g@.D9$K$h$k1@?e$+$i1+?e$X$NJQ49(B
  real(8), intent(Out)  :: ss_CLcr_nl(DimXMin:DimXMax, DimZMin:DimZMax)
                                            !$B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B
  real(8), intent(Out)  :: ss_VelZRain_nl(DimXMin:DimXMax, DimZMin:DimZMax)
                                            !$B1+N3=*C<B.EY(B
  real(8), intent(Out)  :: ss_PRr_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B. 
  real(8), intent(Out)  :: ss_MixRtVSat_al(DimXMin:DimXMax, DimZMin:DimZMax) 
                                            !$BK0OB>x5$05(B

  !== Work       
  real(8) :: ss_Gamma_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                             ! $B@xG.(B / ( $BHfG.(B * $B%(%/%9%J!<4X?t(B )
  real(8) :: ss_PressAll_nl(DimXMin:DimXMax, DimZMin:DimZMax)   
                                             ! $B05NO(B($B>qMp(B+$BJ?6Q(B)
  real(8) :: ss_ExnerAll_nl(DimXMin:DimXMax, DimZMin:DimZMax)   
                                             ! $BL5<!8505NO(B($B>qMp(B+$BJ?6Q(B)
  real(8) :: ss_TempAll_nl(DimXMin:DimXMax, DimZMin:DimZMax)    
                                             ! $B29EY(B($B>qMp(B+$BJ?6Q(B)
  real(8) :: ss_PotTempAll_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
                                             ! $B290L(B($B>qMp(B+$BJ?6Q(B)
  real(8) :: ss_MixRtVAll_nl(DimXMin:DimXMax, DimZMin:DimZMax)  
                                             ! $B?e>x5$:.9gHf(B($B>qMp(B+$BJ?6Q(B)
  real(8) :: ss_MixRtVSat_nl(DimXMin:DimXMax, DimZMin:DimZMax)  
                                             ! $BK0OB>x5$05:.9gHf(B
  real(8) :: ss_SvapPress_nl(DimXMin:DimXMax, DimZMin:DimZMax)  
                                             ! $BK0OB>x5$05%b%kHf(B
  real(8) :: ss_PotTempAll_al(DimXMin:DimXMax, DimZMin:DimZMax) 
                                             ! $BA4290L(B
  real(8) :: ss_MixRtVAll_al(DimXMin:DimXMax, DimZMin:DimZMax)  
                                             ! $BA4?e>x5$(B

  real(8) :: MixRtV_tmp
  real(8) :: MixRtC_tmp
  real(8) :: PotTemp_tmp

  real(8) :: PressAll_adj
  real(8) :: ExnerAll_adj
  real(8) :: DelMixRtV_adj

  real(8) :: TempAll_tmp
  real(8) :: PotTempAll_tmp
  real(8) :: MixRtVAll_tmp

  real(8) :: SvapPress_tmp
  real(8) :: MixRtVSat_tmp
  real(8) :: Gamma_tmp

  real(8) :: PotTemp_adj
  real(8) :: MixRtV_adj
  real(8) :: MixRtC_adj

  real(8) :: DMixRtVSatDPotTemp_tmp
  real(8) :: DMixRtVSatDSvapPress_tmp 

  real(8) :: DensGround         ! $BCOI=LLL)EY(B
  integer :: i, k, num          ! $BE:$(;z(B  
  real(8), parameter :: K1 = 1.0d-3  !$B%1%9%i!<$N78?t(B
  real(8), parameter :: K2 = 2.2d0   !$B%1%9%i!<$N78?t(B
  real(8), parameter :: A  = 1.0d-3  !$B1@?eNL$NogCM(B
  real(8), parameter :: N0 = 5.0d7
  real(8), parameter :: D0 = 3.66d-1

  
  !=== $B%5%V%k!<%A%s$N=i4|2=(B
  call BeginSub("Scalar", &
    &           fmt="%c",              &
    &           c1="Time integration of scalar component.")
  

  !=== $BJQ?t$KNm$rBeF~(B
  call warmrainprm_init
  
  !=== $B29EY(B, $B05NO(B, $B:.9gHf$NA4NL$r5a$a$k(B
  !==== $B>qMp@.J,$HJ?6Q@.J,$NB-$7;;(B
  ss_PotTempAll_nl = ss_PotTemp_nl  + ss_PotTempBasicZ
  ss_ExnerAll_nl   = ss_Exner_nl    + ss_ExnerBasicZ
  ss_MixRtVAll_nl  = ss_MixRtV_nl   + ss_MixRtVBasicZ
  ss_TempAll_nl    = ss_ExnerAll_nl * ss_PotTempAll_nl
  
  !=== $BK0OB>x5$05$KH<$&J*M}NL$N7W;;(B 
  !==== $BK0OB>x5$05$N%b%kHf(B
  ss_SvapPress_nl = ss_Svap_ss(ss_TempAll_nl)
  
  !==== $BK0OB>x5$05$+$iK0OB:.9gHf$r7W;;(B
  ss_MixRtVSat_nl = &
    & ((MolWtWet/MolWtDry) * ss_SvapPress_nl) &
    & / (PressSfc * (ss_ExnerAll_nl **(CpDry / GasRDry)) - ss_SvapPress_nl)
 
  !=== $BJQ?t$N=i4|2=(B
  ss_Gamma_nl   = &
    & ss_LatentHeat_ss(ss_TempAll_nl) / (ss_ExnerAll_nl * CpDry) 
  DensGround = ss_DensBasicZ(RegXMin, 0)   ! X $B:BI8$O$I$3$G$bF1$8(B
    

!!! 
!!! $B7W;;$KI,MW$H$J$k(B, $B?e>x5$(B--$B1@N3(B--$B1+N34V$NJQ4978?t$r5a$a$k(B
!!!
  !=== $BJ;9g@.D9$K$h$k1@?e$+$i1+?e$X$NJQ49(B
  !==== Berry (1968) $B$N%Q%i%a%?%j%<!<%7%g%s(B
!  where (ss_MixRtC_nl > A)
!    ss_CNcr_nl = K1 * (ss_MixRtC_nl - A)
!  end where
  where (ss_MixRtC_nl > Min)
    ss_CNcr_nl =  &
      & ss_DensBasicZ * (ss_MixRtC_nl ** 3.0d0) * 1.0d6 &
      & / (60.0d0 * 2.0d0 * ss_MixRtC_nl                &
      &    + 60.0d0 * 2.66d-8 * N0 / (ss_DensBasicZ * D0)) 
  end where


  !=== $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B
  !==== Kessler (1969) $B$N%Q%i%a%?%j%<!<%7%g%s(B
  where (ss_MixRtC_nl > Min .AND. ss_MixRtR_nl > Min)
    ss_CLcr_nl = K2 * ss_MixRtC_nl * (ss_MixRtR_nl ** 0.875d0)
  end where


  !=== $B>xH/$K$h$k1+?e$+$i?e>x5$$X$NJQ49(B
  !====== $BCfEg7r2p(B (1994) $B$GMxMQ$7$?Dj<0(B. 
  where (ss_MixRtVSat_nl > ss_MixRtVAll_nl  .AND. ss_MixRtR_nl > Min )
    ss_EVrv_nl = &
      &  4.85d-2                                        &
      &  * (ss_MixRtVSat_nl - ss_MixRtVAll_nl )         &
      &  * ((ss_DensBasicZ * ss_MixRtR_nl) ** 0.65d0 )
  end where

  
  !=== $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B. $B=*C<B.EY$r7W;;$7$F$+$iI>2A(B.
  !==== $B1+N3=*C<B.EY(B
  where (ss_MixRtR_nl > Min)
    ss_VelZRain_nl = 12.2d0 * (ss_MixRtR_nl ** 0.125d0) 
  end where

  !==== $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B. 
  !===== Dens $B$N(B avr $B$r<h$C$F$+$i3d$k$H(B, $B%<%m3d$,@8$8$k$N$GCm0U(B
  where (ss_MixRtR_nl > Min)
    ss_PRr_nl &
      & = ss_avr_sf(  &
      &     sf_dz_ss(ss_DensBasicZ * ss_VelZRain_nl * ss_MixRtR_nl) &
      &   ) / ss_DensBasicZ 
  end where

  
end subroutine WarmRainPrm
