!= 蝨ｰ陦ｨ髱｢繝��繧ｿ逕滓�荳ｻ繝励Ο繧ｰ繝ｩ繝�
!
!= dcpam main program for generation of surface data
!
! Authors::   Yasuhiro Morikawa, Yoshiyuki O. Takahashi
! Version::   $Id: dcpam_init_data_surface.f90,v 1.1 2013/10/06 13:42:17 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

program dcpam_init_data_surface
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 蝨ｰ陦ｨ髱｢繝��繧ｿ繝輔ぃ繧､繝ｫ繧堤函謌舌＠縺ｾ縺�.
  !
  ! Surface data file is created.
  !

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

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

  ! 蝨ｰ陦ｨ髱｢繝��繧ｿ謠蝉ｾ�
  ! Prepare surface data
  !
  use surface_data, only: SetSurfData

  !
  ! Prepare initial surface data
  !
  use initial_surface_data, only: SetInitSurfaceData

  ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜃ｺ蜉�
  ! Restart data of surface temperature input/output
  !
  use restart_surftemp_io, only: RestartSurfTempOutput

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

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

  ! 螟画焚
  ! Variables
  !
  real(DP), allocatable:: xy_SurfTemp (:,:)
                            ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ.
                            ! Surface temperature
  real(DP), allocatable:: xyz_SoilTemp (:,:,:)
                            ! 蝨溷｣梧ｸｩ蠎ｦ.
                            ! Soil temperature
  real(DP), allocatable:: xy_SurfMajCompIceB(:,:)
                            !
                            ! surface major component ice at previous time step
  real(DP), allocatable:: xy_SurfMajCompIceN(:,:)
                            !
                            ! surface major component ice at present time step
  real(DP), allocatable:: xy_SoilMoistB(:,:)
                            !
                            ! soil moisture at previous time step
  real(DP), allocatable:: xy_SoilMoistN(:,:)
                            !
                            ! soil moisture at present time step
  real(DP), allocatable:: xy_SurfSnowB(:,:)
                            !
                            ! surface snow amount at previous time step
  real(DP), allocatable:: xy_SurfSnowN(:,:)
                            !
                            ! surface snow amount at present time step

  ! 菴懈･ｭ螟画焚
  ! Work variables
  !
  integer :: k

  ! 螳溯｡梧枚 ; Executable statement
  !

  ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ蛻晄悄蛹� (蜀�Κ繧ｵ繝悶Ν繝ｼ繝√Φ)
  ! Initialization for the main program (Internal subroutine)
  !
  call MainInit

  ! 蝨ｰ陦ｨ髱｢繝��繧ｿ縺ｮ菴懈�
  ! Generate surface data
  !
  call SetSurfData( xy_SurfTemp = xy_SurfTemp )  ! (out) optional

  do k = 1, kslmax
    xyz_SoilTemp(:,:,k) = xy_SurfTemp
  end do

  !
  != Prepare initial surface data
  !
  call SetInitSurfaceData(                        &
    & xy_SurfMajCompIceB, xy_SoilMoistB, xy_SurfSnowB   & ! (out)
    & )
  xy_SurfMajCompIceN = xy_SurfMajCompIceB
  xy_SoilMoistN      = xy_SoilMoistB
  xy_SurfSnowN       = xy_SurfSnowB

  ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜃ｺ蜉�
  ! Restart data of surface temperature output
  !
  call RestartSurfTempOutput(                             &
    & xy_SurfTemp,                                        & ! (in)
    & xyz_SoilTemp,                                       & ! (in) optional
    & xy_SurfMajCompIceB, xy_SoilMoistB, xy_SurfSnowB,    & ! (in) optional
    & xy_SurfMajCompIceN, xy_SoilMoistN, xy_SurfSnowN     & ! (in) optional
    & )

  ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ邨ゆｺ��逅� (蜀�Κ繧ｵ繝悶Ν繝ｼ繝√Φ)
  ! Termination for the main program (Internal subroutine)
  !
  call MainTerminate

contains

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

  subroutine MainInit
    !
    ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ蛻晄悄蛹匁焔邯壹″.
    !
    ! Initialization procedure for the main program.
    !

    ! MPI
    !
    use mpi_wrapper, only : MPIWrapperInit

    use dc_message, only: MessageNotify

    ! 繧ｳ繝槭Φ繝峨Λ繧､繝ｳ蠑墓焚蜃ｦ逅�
    ! Command line option parser
    !
    use option_parser, only: OptParseInit

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

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: TimesetInit
                              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾. Time of step $ t $.

    ! 蜃ｺ蜉帙ヵ繧｡繧､繝ｫ縺ｮ蝓ｺ譛ｬ諠��ｱ邂｡逅�
    ! Management basic information for output files
    !
    use fileset, only: FilesetInit

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

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    use constants, only: ConstantsInit

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    use axesset, only: AxessetInit

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜃ｺ蜉�
    ! Restart data of surface temperature input/output
    !
    use restart_surftemp_io, only: RestartSurfTempInit, RestartSurfTempOpen, RestartSurfTempGet

    ! 蝨ｰ陦ｨ髱｢繝��繧ｿ謠蝉ｾ�
    ! Prepare surface data
    !
    use surface_data, only : SurfDataInit

    !
    ! Prepare initial surface data
    !
    use initial_surface_data, only: InitSurfaceDataInit

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    character(*), parameter:: prog_name = 'dcpam_init_data_surface'
                              ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�蜷�.
                              ! Main program name
    character(*), parameter:: version = &
      & '$Name:  $' // &
      & '$Id: dcpam_init_data_surface.f90,v 1.1 2013/10/06 13:42:17 yot Exp $'
                              ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Main program version
    character(STRING)      :: namelist_filename
                              ! NAMELIST 繝輔ぃ繧､繝ｫ縺ｮ蜷咲ｧｰ.
                              ! NAMELIST file name

    character(STRING):: brief
                              ! 螳溯｡後ヵ繧｡繧､繝ｫ縺ｮ邁｡貎斐↑隱ｬ譏�
                              ! Brief account of executable file

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! Initialize MPI
    !
    call MPIWrapperInit

    brief = 'SST data generation'
    call MessageNotify( 'M', prog_name, 'Run: %c', c1 = trim(brief) )
    call MessageNotify( 'M', prog_name, '-- version = %c', c1 = trim(version) )

    ! 繧ｳ繝槭Φ繝峨Λ繧､繝ｳ蠑墓焚蜃ｦ逅�
    ! Command line option parser
    !
    call OptParseInit(       &
      & namelist_filename,   & ! (out)
      & prog_name,           & ! (in )
      & brief                & ! (in )
      & )

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜷榊�蜉�
    ! Input NAMELIST file name
    !
    call NmlutilInit( &
      & namelist_filename  & ! (in)
      & )

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    call TimesetInit

    ! 蜃ｺ蜉帙ヵ繧｡繧､繝ｫ縺ｮ蝓ｺ譛ｬ諠��ｱ邂｡逅�
    ! Management basic information for output files
    !
    call FilesetInit

    ! 譬ｼ蟄千せ險ｭ螳�
    ! Grid points settings
    !
    call GridsetInit

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    call ConstantsInit

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    call AxessetInit

    ! 蝨ｰ陦ｨ髱｢繝��繧ｿ謠蝉ｾ�
    ! Prepare surface data
    !
    call SurfDataInit

    !
    ! Prepare initial surface data
    !
    call InitSurfaceDataInit

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ螟画焚縺ｮ蜑ｲ莉�
    ! Allocation of a variable of surface temperature
    !
    allocate( xy_SurfTemp       (0:imax-1, 1:jmax) )
    allocate( xyz_SoilTemp      (0:imax-1, 1:jmax, 1:kslmax) )
    allocate( xy_SurfMajCompIceB(0:imax-1, 1:jmax) )
    allocate( xy_SurfMajCompIceN(0:imax-1, 1:jmax) )
    allocate( xy_SoilMoistB     (0:imax-1, 1:jmax) )
    allocate( xy_SoilMoistN     (0:imax-1, 1:jmax) )
    allocate( xy_SurfSnowB      (0:imax-1, 1:jmax) )
    allocate( xy_SurfSnowN      (0:imax-1, 1:jmax) )

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ縺ｮ蛻晄悄蛹�
    ! Initialization of restart data file of surface temperature
    !
    call RestartSurfTempInit
    call RestartSurfTempOpen( flag_init_data = .true. ) ! (in) optional

  end subroutine MainInit

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

  subroutine MainTerminate
    !
    ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ邨ゆｺ��逅�焔邯壹″.
    !
    ! Termination procedure for the main program.
    !

    ! MPI
    !
    use mpi_wrapper, only : MPIWrapperFinalize

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: TimesetClose

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜃ｺ蜉�
    ! Restart data of surface temperature input/output
    !
    use restart_surftemp_io, only: RestartSurfTempClose

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use history_file_io, only: HistoryFileClose

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

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ繧ｯ繝ｭ繝ｼ繧ｺ
    ! Close restart data file of surface temperature
    !
    call RestartSurfTempClose

    ! 譎ょ綾邂｡逅�ｵゆｺ��逅�
    ! Termination of time control
    !
    call TimesetClose

    ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ螟画焚縺ｮ蜑ｲ莉倩ｧ｣髯､
    ! Deallocation of a variable of surface temperature
    !
    deallocate( xy_SurfTemp )

    ! Finalize MPI
    !
    call MPIWrapperFinalize

  end subroutine MainTerminate

end program dcpam_init_data_surface