!=
!
!= Prepare initial surface data
!
! Authors::   Yoshiyuki O. TAKAHASHI
! Version::   $Id: initial_surface_data.f90,v 1.2 2015/01/29 12:05:42 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module initial_surface_data
  !
  !=
  !
  != Prepare initial surface data
  !
  ! 蛻晄悄蛟､繝��繧ｿ縺ｮ繧ｵ繝ｳ繝励Ν繧呈署萓帙＠縺ｾ縺�.
  !
  ! Prepare sample data of initial surface data (restart data)
  !
  !== Procedures List
  !
  ! SetInitSurfaceData :: 蛻晄悄蛟､繝��繧ｿ蜿門ｾ�
  ! ------------------ :: ------------
  ! SetInitSurfaceData :: Get initial surface data
  !
  !== NAMELIST
  !
  ! NAMELIST#initial_surface_data_nml
  !

  ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
  !

  ! 譬ｼ蟄千せ險ｭ螳�
  ! Grid points settings
  !
  use gridset, only: imax, & ! 邨悟ｺｦ譬ｼ蟄千せ謨ｰ.
                             ! Number of grid points in longitude
    &                jmax, & ! 邱ｯ蠎ｦ譬ｼ蟄千せ謨ｰ.
                             ! Number of grid points in latitude
    &                kmax    ! 驩帷峩螻､謨ｰ.
                             ! Number of vertical level

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING     ! 譁�ｭ怜�.       Strings.

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP     ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.

  ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 螳｣險譁� ; Declaration statements
  !
  implicit none
  private

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: SetInitSurfaceData
  public:: InitSurfaceDataInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !
  logical, save, public:: initial_surface_data_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  real(DP), save       :: SoilMoist
                              ! soil moisture (kg m-2)
  real(DP), save       :: SurfSnow
                              ! surface snow (kg m-2)
  real(DP), save       :: SOSeaIceMass
                              ! slab sea ice mass (kg m-2)
  real(DP), save       :: SurfMajCompIce
                              ! surface major component ice (kg m-2)

  ! 髱槫�髢句､画焚
  ! Private variables
  !

  character(*), parameter:: module_name = 'initial_surface_data'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: initial_surface_data.f90,v 1.2 2015/01/29 12:05:42 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

  !--------------------------------------------------------------------------------------

  subroutine SetInitSurfaceData(                                    &
    & xy_SurfMajCompIce, xy_SoilMoist, xy_SurfSnow, xy_SOSeaIceMass & ! (out)
    & )
    !
    ! 蛻晄悄蛟､繝��繧ｿ縺ｮ繧ｵ繝ｳ繝励Ν繧呈署萓帙＠縺ｾ縺�.
    !
    ! Prepare sample data of initial data
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 迚ｩ逅��謨ｰ蟄ｦ螳壽焚險ｭ螳�
    ! Physical and mathematical constants settings
    !
    use constants0, only: &
      & PI                    ! $ \pi $.
                              ! 蜀�捉邇�. Circular constant

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: LChar

    ! 繝輔ぃ繧､繝ｫ縺九ｉ 1 谺｡蜈��繝ｭ繝輔ぃ繧､繝ｫ繧定ｪｭ繧薙〒險ｭ螳壹☆繧�.
    ! read 1-D profile from a file and set it
    !
    use set_1d_profile, only : Set1DProfilePs, Set1DProfileAtm

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(out):: xy_SurfMajCompIce(0:imax-1, 1:jmax)
                            !
                            ! surface major component ice
    real(DP), intent(out):: xy_SoilMoist(0:imax-1, 1:jmax)
                            !
                            ! soil moisture
    real(DP), intent(out):: xy_SurfSnow(0:imax-1, 1:jmax)
                            !
                            ! surface snow amount
    real(DP), intent(out):: xy_SOSeaIceMass(0:imax-1, 1:jmax)
                            !
                            ! slab sea ice mass

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !

    integer:: j               ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude

    ! 螳溯｡梧枚 ; Executable statement

    if ( .not. initial_surface_data_inited ) call InitSurfaceDataInit

    do j = 1, jmax
!!$      if ( y_Lat(j) * 180.0_DP / PI > 0.0_DP ) then
!!$        xy_SurfMajCompIce(:,j) = 0.0_DP
!!$      else
        xy_SurfMajCompIce(:,j) = SurfMajCompIce
!!$      end if
        !
!!$      if ( y_Lat(j) * 180.0_DP / PI > 0.0_DP ) then
!!$        xy_SoilMoist(:,j) = 0.0_DP
!!$      else
        xy_SoilMoist(:,j) = SoilMoist
!!$      end if
        !
!!$      if ( y_Lat(j) * 180.0_DP / PI > 0.0_DP ) then
!!$        xy_SurfSnow(:,j) = 0.0_DP
!!$      else
        xy_SurfSnow(:,j) = SurfSnow
!!$      end if
    end do

    xy_SOSeaIceMass = SOSeaIceMass

  end subroutine SetInitSurfaceData

  !--------------------------------------------------------------------------------------

  subroutine InitSurfaceDataInit

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: LChar

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    integer:: unit_nml        ! NAMELIST 繝輔ぃ繧､繝ｫ繧ｪ繝ｼ繝励Φ逕ｨ陬�ｽｮ逡ｪ蜿ｷ.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 隱ｭ縺ｿ霎ｼ縺ｿ譎ゅ� IOSTAT.
                              ! IOSTAT of NAMELIST read

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
    namelist /initial_surface_data_nml/ &
      & SoilMoist, SurfSnow, SOSeaIceMass, SurfMajCompIce
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "initial_surface_data#InitSurfaceDataInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "initial_surface_data#InitSurfaceDataInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement

    if ( initial_surface_data_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    SoilMoist      = 0.0_DP
    SurfSnow       = 0.0_DP
    SOSeaIceMass   = 0.0_DP
    SurfMajCompIce = 0.0_DP

    ! NAMELIST 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ
    ! NAMELIST is input
    !
    if ( trim(namelist_filename) /= '' ) then
      call FileOpen( unit_nml, &          ! (out)
        & namelist_filename, mode = 'r' ) ! (in)

      rewind( unit_nml )
      read( unit_nml, &                     ! (in)
        & nml = initial_surface_data_nml, & ! (out)
        & iostat = iostat_nml )             ! (out)
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
    end if

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  SoilMoist      = %f', d = (/ SoilMoist /) )
    call MessageNotify( 'M', module_name, '  SurfSnow       = %f', d = (/ SurfSnow /) )
    call MessageNotify( 'M', module_name, '  SOSeaIceMass   = %f', d = (/ SOSeaIceMass   /) )
    call MessageNotify( 'M', module_name, '  SurfMajCompIce = %f', d = (/ SurfMajCompIce /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    initial_surface_data_inited = .true.

  end subroutine InitSurfaceDataInit

  !--------------------------------------------------------------------------------------

end module initial_surface_data