!= 蛻晄悄蛟､繝��繧ｿ繝輔ぃ繧､繝ｫ逕滓�荳ｻ繝励Ο繧ｰ繝ｩ繝�
!
!= dcpam main program for generation of initial data file
!
! Authors::   Yasuhiro Morikawa, Yoshiyuki O. Takahashi
! Version::   $Id: dcpam_init_data.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
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 蛻晄悄蛟､繝��繧ｿ繝輔ぃ繧､繝ｫ繧堤函謌舌＠縺ｾ縺�.
  !
  ! Initial data file is created.
  !

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

  ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
  ! Settings of array for atmospheric composition
  !
  use composition, only : &
    &                ncmax, & ! 謌仙�縺ｮ謨ｰ
                              ! Number of composition
    &                IndexH2OVap
                              ! 豌ｴ闥ｸ豌励�繧､繝ｳ繝�ャ繧ｯ繧ｹ
                              ! Index for water vapor

  ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
  ! Prepare initial data (restart data)
  !
  use initial_data, only: SetInitData

  ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜃ｺ蜉�
  ! Restart data input/output
  !
  use restart_file_io, only: InitialFileOutPut

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

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

  ! 莠亥�ｱ螟画焚 (繧ｹ繝�ャ繝� $ t-\Delta t $ , $ t $ , $ t+\Delta t $ )
  ! Prediction variables  (Step $ t-\Delta t $ , $ t $ , $ t+\Delta t $ )
  !
  real(DP), allocatable:: xyz_U (:,:,:)
                              ! $ u $ .   譚ｱ隘ｿ鬚ｨ騾�. Eastward wind
  real(DP), allocatable:: xyz_V (:,:,:)
                              ! $ v $ .   蜊怜圏鬚ｨ騾�. Northward wind
  real(DP), allocatable:: xyz_Temp (:,:,:)
                              ! $ T $ .   貂ｩ蠎ｦ. Temperature
  real(DP), allocatable:: xyzf_QVap (:,:,:,:)
                              ! $ q $ .   豈疲ｹｿ. Specific humidity
  real(DP), allocatable:: xy_Ps (:,:)
                              ! $ p_s $ . 蝨ｰ陦ｨ髱｢豌怜悸. Surface pressure

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

  ! 螳溯｡梧枚 ; Executable statement
  !

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

  ! 蛻晄悄蛟､繝��繧ｿ縺ｮ菴懈�
  ! Generate initial data
  !
  call SetInitData( &
    & xyz_U, xyz_V, xyz_Temp, xyzf_QVap, xy_Ps )  ! (out)

  ! 蛻晄悄蛟､繝��繧ｿ縺ｮ蜃ｺ蜉�
  ! Output initial data
  !
  call InitialFileOutput( &
    & xyz_U, xyz_V, xyz_Temp, xyzf_QVap, xy_Ps )  ! (in)

  ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�縺ｮ邨ゆｺ��逅� (蜀�Κ繧ｵ繝悶Ν繝ｼ繝√Φ)
  ! 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
      &                kmax    ! 驩帷峩螻､謨ｰ.
                               ! Number of vertical level

    ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
    ! Settings of array for atmospheric composition
    !
    use composition, only : &
      &                CompositionInit, &
      &                ncmax   ! 謌仙�縺ｮ謨ｰ
                               ! Number of composition

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

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

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

    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜃ｺ蜉�
    ! Restart data input/output
    !
    use restart_file_io, only: RestartFileInit, RestartFileOpen

    ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
    ! Prepare initial data (restart data)
    !
    use initial_data, only: InitDataInit

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    character(*), parameter:: prog_name = 'dcpam_init_data'
                              ! 荳ｻ繝励Ο繧ｰ繝ｩ繝�蜷�.
                              ! Main program name
    character(*), parameter:: version = &
      & '$Name:  $' // &
      & '$Id: dcpam_init_data.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 = 'Initial 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

    ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
    ! Settings of array for atmospheric composition
    !
    call CompositionInit

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

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

    ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
    ! Prepare initial data (restart data)
    !
    call InitDataInit

    ! 莠亥�ｱ螟画焚縺ｮ蜑ｲ莉�
    ! Allocation of prediction variables
    !
    allocate( xyz_U    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_V    (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyz_Temp (0:imax-1, 1:jmax, 1:kmax) )
    allocate( xyzf_QVap(0:imax-1, 1:jmax, 1:kmax, 1:ncmax) )
    allocate( xy_Ps    (0:imax-1, 1:jmax) )

    ! 繝輔ぃ繧､繝ｫ縺九ｉ 1 谺｡蜈��繝ｭ繝輔ぃ繧､繝ｫ繧定ｪｭ繧薙〒險ｭ螳壹☆繧�.
    ! read 1-D profile from a file and set it
    !
    call Set1DProfileInit

    ! 蛻晄悄蛟､繝��繧ｿ蜃ｺ蜉�
    ! Initial data output
    !
    call RestartFileInit
    call RestartFileOpen( 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 input/output
    !
    use restart_file_io, only: RestartFileClose

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

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ繧ｯ繝ｭ繝ｼ繧ｺ
    ! Close restart data input
    !
    call RestartFileClose

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

    ! 莠亥�ｱ螟画焚縺ｮ蜑ｲ莉倩ｧ｣髯､
    ! Deallocation of prediction variables
    !
    deallocate( xyz_U     )
    deallocate( xyz_V     )
    deallocate( xyz_Temp  )
    deallocate( xyzf_QVap )
    deallocate( xy_Ps     )

    ! Finalize MPI
    !
    call MPIWrapperFinalize

  end subroutine MainTerminate

end program dcpam_init_data