!= åœ°è¡¨é¢ãƒ‡ãƒ¼ã‚¿ç”Ÿæˆä¸»ãƒ—ãƒã‚°ãƒ©ãƒ
!
!= 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
    &                ksimax  ! æµ·æ°·ã®é‰›ç›´å±¤æ•°.
                             ! Number of sea ice 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:: xyz_SOSeaIceTemp (:,:,:)
                            ! æµ·æ°·æ¸©åº¦.
                            ! Sea ice temperature
  real(DP), allocatable:: xy_SOSeaIceMass(:,:)
                            !
                            ! slab sea ice mass
  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
  do k = 1, ksimax
    xyz_SOSeaIceTemp(:,:,k) = xy_SurfTemp
  end do

  !
  != Prepare initial surface data
  !
  call SetInitSurfaceData(                                              &
    & xy_SurfMajCompIceB, xy_SoilMoistB, xy_SurfSnowB, xy_SOSeaIceMass  & ! (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
    & xyz_SOSeaIceTemp, xy_SOSeaIceMass,               & ! (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
      &                ksimax  ! æµ·æ°·ã®é‰›ç›´å±¤æ•°.
                               ! Number of sea ice 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

    ! åœ°è¡¨é¢ãƒ‡ãƒ¼ã‚¿æä¾›
    ! 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( xyz_SOSeaIceTemp  (0:imax-1, 1:jmax, 1:ksimax) )
    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) )
    allocate( xy_SOSeaIceMass   (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