!= O3 分布の設定
!
!= Set O3 distribution
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: set_o3.f90,v 1.5 2013/03/01 14:14:16 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module set_o3
  !
  != O3 分布の設定
  !
  != Set O3 distribution
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! O3 分布.
  !
  ! Set O3 distribution. 
  !
  !== References
  !
!!$  !  Roewe, D., and K.-N. Liou, Influence of cirrus clouds on the infrared cooling 
!!$  !    rate in the troposphere and lower stratosphere, J. Appl. Met., 17, 92-106, 1978.
  !
  !== Procedures List
  !
!!$  ! RadiationFluxDennouAGCM :: 放射フラックスの計算
!!$  ! RadiationDTempDt        :: 放射フラックスによる温度変化の計算
!!$  ! RadiationFluxOutput     :: 放射フラックスの出力
!!$  ! RadiationFinalize       :: 終了処理 (モジュール内部の変数の割り付け解除)
!!$  ! ------------            :: ------------
!!$  ! RadiationFluxDennouAGCM :: Calculate radiation flux
!!$  ! RadiationDTempDt        :: Calculate temperature tendency with radiation flux
!!$  ! RadiationFluxOutput     :: Output radiation fluxes
!!$  ! RadiationFinalize       :: Termination (deallocate variables in this module)
  !
  !== NAMELIST
  !
!!$  ! NAMELIST#radiation_DennouAGCM_nml
  !

  ! USE statements
  !

  ! 
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! Double precision.
    &                 STRING, &  ! Strings.
    &                 TOKEN      ! Keywords.

  ! メッセージ出力
  ! Message output
  !
  use dc_message, only: MessageNotify


  ! Declaration statements
  !
  implicit none
  private

  ! 
  ! Public procedure
  !
  public:: SetO3
  public:: SetO3Init


  character(*), parameter:: module_name = 'set_o3'
                              ! モジュールの名称.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: set_o3.f90,v 1.5 2013/03/01 14:14:16 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

  logical          , save:: FlagO3
  logical          , save:: Flag1D

  character(STRING), save:: O3File

  logical, save :: set_o3_inited = .false.
                              ! 初期設定フラグ.
                              ! Initialization flag

contains

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

  subroutine SetO3( &
    & xyz_Press, & ! (in )
    & xyz_QO3    & ! (out)
    & )


    ! 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

    ! ヒストリデータ出力
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable, HistoryAutoPut

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! ステップ $ t $ の時刻.
                              ! Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! 時系列データの読み込み
    ! Reading time series
    !
    use read_time_series, only: SetValuesFromTimeSeriesWrapper

    ! ファイルから 1 次元プロファイルを読んで設定する. 
    ! read 1-D profile from a file and set it 
    !
    use set_1d_profile, only : Set1DProfileO3


    real(DP), intent(in ):: xyz_Press(0:imax-1, 1:jmax, 1:kmax)
    real(DP), intent(out):: xyz_QO3  (0:imax-1, 1:jmax, 1:kmax)


    !
    ! Work variables
    !


    ! 初期化確認
    ! Initialization check
    !
    if ( .not. set_o3_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if


    ! 計算時間計測開始
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )


    if ( FlagO3 ) then

      if ( Flag1D ) then

        call Set1DProfileO3( &
          & xyz_Press,             & ! (in )
          & xyz_QO3                & ! (out)
          & )

      else

        call SetValuesFromTimeSeriesWrapper( &
          & "O3",                  & ! (in)
          & O3File, "O3",          & ! (in)
          & xyz_Press,             & ! (in)
          & xyz_QO3,               & ! (inout)
          & .true., .true., .true. & ! (in)
          & )

      end if

    else

      xyz_QO3 (:,:,:) = 0.0d0

    end if

    call HistoryAutoPut( TimeN, "QO3", xyz_QO3 )


    ! 計算時間計測一時停止
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )


  end subroutine SetO3

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

  subroutine SetO3Init


    ! ファイル入出力補助
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! ヒストリデータ出力
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid



    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT.
                              ! IOSTAT of NAMELIST read

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
    namelist /set_O3_nml/ &
      & FlagO3,  &
      & Flag1D,  &
      & O3File
          !
          ! デフォルト値については初期化手続 "set_o3#SetO3Init"
          ! のソースコードを参照のこと.
          !
          ! Refer to source codes in the initialization procedure
          ! "set_o3#SetO3Init" for the default values.
          !

    if ( set_o3_inited ) return


    ! デフォルト値の設定
    ! Default values settings
    !

    FlagO3 = .true.

    Flag1D = .false.

    O3File = ""


    ! 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 = set_O3_nml,               & ! (out)
        & iostat = iostat_nml )             ! (out)
      close( unit_nml )

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


    call HistoryAutoAddVariable( "QO3",        & ! (in)
      & (/ 'lon ', 'lat ', 'sig ', 'time' /),  & ! (in)
      & "ozone", 'kg kg-1' )                     ! (in)

    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  FlagO3 = %b', l = (/ FlagO3 /) )
    call MessageNotify( 'M', module_name, '  Flag1D = %b', l = (/ Flag1D /) )
    call MessageNotify( 'M', module_name, '  O3File = %c', c1 = trim( O3File ) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )


    set_o3_inited = .true.

  end subroutine SetO3Init

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

end module set_o3
