!= Matthews 縺ｮ繝��繧ｿ縺ｫ蝓ｺ縺･縺乗ヱ譏溯｡ｨ髱｢繧｢繝ｫ繝吶ラ險ｭ螳�
!
!= set surface albedo based on data by Matthews
!
! Authors::   Yoshiyuki O. Takahashi, Satoshi Noda
! Version::   $Id: albedo_Matthews.f90,v 1.10 2014/05/07 09:39:23 murashin Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module albedo_Matthews

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

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

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

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

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: SetAlbedoMatthews
  public:: ModAlbedoMatthewsCultivation
  public:: AlbedoMatthewsInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save :: albedo_matthews_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag
  logical   , save      :: flag_annual_mean
                              ! 蟷ｴ蟷ｳ蝮�ヵ繝ｩ繧ｰ.
                              ! Flag of annual mean
  real(DP)  , save      :: OceanAlbedo
                              ! 豬ｷ豢九�繧｢繝ｫ繝吶ラ.
                              ! Albedo of ocean.

  integer , parameter :: NAlbType = 32
                              ! 讀咲函縺ｮ遞ｮ鬘槭�謨ｰ.
                              ! Number of vegetation type.
  integer , parameter :: NSeason = 4
                              ! 蟄｣遽縺ｮ謨ｰ.
                              ! Number of season.
  real(DP), save      :: a_Data_DOY( NSeason )
                              ! 蜷�ｭ｣遽縺ｮ髢句ｧ区律.
                              ! Start date of each season.
  real(DP), save      :: aa_Data_Albedo( NSeason, 0:NAlbType )
                              ! 蜷�､咲函, 蜷�ｭ｣遽縺ｫ縺翫￠繧九い繝ｫ繝吶ラ.
                              ! Albedo of each vegetation type, each season.
  integer, parameter  :: IndexCultivation = 32
                              !
                              ! Index for cultivation

  !    win.  spr.  sum.  fall
  data a_Data_DOY / 0.0_DP, 90.0_DP, 181.0_DP, 273.0_DP /

  !
  ! Matthews, 1985, NASA Technical memorandum #86199
  ! ATLAS, OF ARCHIVED VEGETATION, LAND-USE AND SEASONAL ALBEDO DATA SET
  ! http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19850016197_1985016197.pdf
  !
  ! Notice: The unit of the following values is percent.
  !         0.01 is multiplied in initialization.
  !
  !    win.  spr.  sum.  fall
  !    Jan.  Apr.  Jul.  Oct.   for nothern hemisphere
  !    Jul.  Oct.  Jan.  Apr.   for southern hemisphere
  data aa_Data_Albedo / &
    10,   10,   10,   10, & !  0  The values for 0 are not included in Matthes compilation.
    11,   11,   11,   11, & !  1
    11,   11,   11,   11, & !  2
    11,   11,   11,   11, & !  3
    12,   12,   12,   12, & !  4
    12,   13,   14,   13, & !  5
    17,   14,   13,   14, & !  6
    13,   14,   16,   13, & !  7
    11,   12,   15,   12, & !  8
    18,   16,   15,   16, & !  9
    12,   15,   18,   13, & ! 10
    12,   15,   18,   13, & ! 11
    28,   32,   28,   28, & ! 12
    15,   13,   12,   13, & ! 13
    14,   14,   16,   14, & ! 14
    20,   18,   17,   18, & ! 15
    14,   14,   17,   14, & ! 16
    15,   15,   18,   15, & ! 17
    15,   15,   18,   15, & ! 18
    17,   20,   20,   17, & ! 19
    17,   20,   20,   17, & ! 20
    28,   32,   28,   28, & ! 21
    12,   12,   17,   15, & ! 22
    14,   15,   17,   15, & ! 23
    14,   15,   16,   14, & ! 24
    16,   18,   25,   20, & ! 25
    17,   17,   20,   17, & ! 26
    16,   20,   20,   18, & ! 27
    16,   20,   20,   18, & ! 28
    16,   20,   20,   18, & ! 29
    30,   30,   30,   30, & ! 30
    75,   75,   75,   75, & ! 31
    16,   18,   20,   18  & ! 32 cultivation
    /

  character(*), parameter:: module_name = 'albedo_Matthews'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: albedo_Matthews.f90,v 1.10 2014/05/07 09:39:23 murashin Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

  ! INTERFACE 譁� ; INTERFACE statements
  !

contains

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

  subroutine SetAlbedoMatthews( xy_SurfType, xy_SurfAlbedo )

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; 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

    ! 譌･莉倥♀繧医�譎ょ綾縺ｮ蜿悶ｊ謇ｱ縺�
    ! Date and time handler
    !
    use dc_calendar, only: DCCalInquire, DCCalDateEvalSecOfYear

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

    ! 螳｣險譁� ; Declaration statements
    !
    integer , intent(in ) :: xy_SurfType  ( 0:imax-1, 1:jmax )
                              ! 讀咲函縺ｮ繧､繝ｳ繝�ャ繧ｯ繧ｹ
                              ! Index of vegetation
    real(DP), intent(out) :: xy_SurfAlbedo( 0:imax-1, 1:jmax )
                              ! 蝨ｰ陦ｨ繧｢繝ｫ繝吶ラ.
                              ! Surface albedo

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP):: SecOfYear
    real(DP):: a_Data_SOY_Ex( 0:nseason+1 )
                              ! 蜷�ｭ｣遽縺ｮ髢句ｧ区凾蛻ｻ (蜀�諺縺ｮ縺溘ａ縺ｫ諡｡蠑ｵ).
                              ! Start time of each season (extended for interpolation).
    real(DP):: xya_SurfAlbedoLocal( 0:imax-1, 1:jmax, 1:2 )
    integer :: i              ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in longitude
    integer :: j              ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude
    integer :: l              ! 蟄｣遽譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in season
    integer :: t
    integer :: tindex
    integer :: a_tindex(1:2)

    integer:: hour_in_day, min_in_hour, day_in_year
    integer, pointer:: day_in_month_ptr(:) => null()
    real(DP):: sec_in_min, sec_in_day

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. albedo_matthews_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( flag_annual_mean ) then

      !
      ! Now, annual mean value is used, temporarily.
      !
      xy_SurfAlbedo = 0.0_DP

      do l = 1, nseason
        do j = 1, jmax
          do i = 0, imax-1
            xy_SurfAlbedo(i,j) = xy_SurfAlbedo(i,j) + aa_Data_Albedo( l, xy_SurfType(i,j) )
          end do
        end do
      end do

      xy_SurfAlbedo = xy_SurfAlbedo / dble( nseason )

    else

      SecOfYear = DCCalDateEvalSecOfYear( TimeN, date = InitialDate )

      call DCCalInquire( &
        & day_in_month_ptr = day_in_month_ptr , & ! (out)
        & hour_in_day      = hour_in_day  , &     ! (out)
        & min_in_hour      = min_in_hour  , &     ! (out)
        & sec_in_min       = sec_in_min )         ! (out)

      day_in_year = sum( day_in_month_ptr )
      deallocate( day_in_month_ptr )
      sec_in_day  = hour_in_day * min_in_hour * sec_in_min

      if ( SecOfYear > day_in_year * sec_in_day ) SecOfYear = day_in_year * sec_in_day

      a_Data_SOY_Ex(0) = ( 0.0_DP - ( day_in_year - a_Data_DOY(nseason) ) ) * sec_in_day
      do t = 1, nseason
        a_Data_SOY_Ex(t) = a_Data_DOY(t) * sec_in_day
      end do
      a_Data_SOY_Ex(nseason+1) = ( day_in_year + a_Data_DOY(1) ) * sec_in_day

      a_tindex(1) = 0
      a_tindex(2) = 1
      do t = 1, nseason
        if ( a_Data_DOY(t) * sec_in_day <= SecOfYear ) then
          a_tindex(1) = t
          a_tindex(2) = t+1
        end if
      end do

      do t = 1, 2
        ! for northern hemisphere
        tindex = a_tindex(t)
        if ( tindex == 0 ) then
          tindex = nseason
        else if ( tindex == nseason+1 ) then
          tindex = 1
        else
          tindex = tindex
        end if
        do j = jmax/2+1, jmax
          do i = 0, imax-1
            xya_SurfAlbedoLocal(i,j,t) = aa_Data_Albedo( tindex, xy_SurfType(i,j) )
          end do
        end do
        ! for southern hemisphere
        tindex = a_tindex(t) + nseason / 2
        if ( tindex > nseason ) tindex = tindex - nseason
        if ( tindex == 0 ) then
          tindex = nseason
        else if ( tindex == nseason+1 ) then
          tindex = 1
        else
          tindex = tindex
        end if
        do j = 1, jmax/2
          do i = 0, imax-1
            xya_SurfAlbedoLocal(i,j,t) = aa_Data_Albedo( tindex, xy_SurfType(i,j) )
          end do
        end do
      end do

      xy_SurfAlbedo =                                                   &
        &   ( xya_SurfAlbedoLocal(:,:,2) - xya_SurfAlbedoLocal(:,:,1) ) &
        & / ( a_Data_SOY_Ex(a_tindex(2)) - a_Data_SOY_Ex(a_tindex(1)) ) &
        & * ( SecOfYear                  - a_Data_SOY_Ex(a_tindex(1)) ) &
        & + xya_SurfAlbedoLocal(:,:,1)

    end if

  end subroutine SetAlbedoMatthews

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

  subroutine ModAlbedoMatthewsCultivation( &
    & xy_SurfType, xy_SurfCulInt,          &
    & xy_SurfAlbedo                        &
    & )

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; 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

    ! 譌･莉倥♀繧医�譎ょ綾縺ｮ蜿悶ｊ謇ｱ縺�
    ! Date and time handler
    !
    use dc_calendar, only: DCCalInquire, DCCalDateEvalSecOfYear

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

    ! 螳｣險譁� ; Declaration statements
    !
    integer , intent(in   ) :: xy_SurfType  ( 0:imax-1, 1:jmax )
                              ! 讀咲函縺ｮ繧､繝ｳ繝�ャ繧ｯ繧ｹ
                              ! Index of vegetation
    real(DP), intent(in   ) :: xy_SurfCulInt( 0:imax-1, 1:jmax )
                              ! ...
                              ! Cultivation index
    real(DP), intent(inout) :: xy_SurfAlbedo( 0:imax-1, 1:jmax )
                              ! 蝨ｰ陦ｨ繧｢繝ｫ繝吶ラ.
                              ! Surface albedo

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP):: SecOfYear
    real(DP):: a_Data_SOY_Ex( 0:nseason+1 )
                              ! 蜷�ｭ｣遽縺ｮ髢句ｧ区凾蛻ｻ (蜀�諺縺ｮ縺溘ａ縺ｫ諡｡蠑ｵ).
                              ! Start time of each season (extended for interpolation).
    real(DP):: SurfAlbedoCul
    real(DP):: xy_SurfAlbedoCul ( 0:imax-1, 1:jmax )
    real(DP):: xya_SurfAlbedoCul( 0:imax-1, 1:jmax, 1:2 )
    integer :: i              ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in longitude
    integer :: j              ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude
    integer :: l              ! 蟄｣遽譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in season
    integer :: t
    integer :: tindex
    integer :: a_tindex(1:2)

    integer:: hour_in_day, min_in_hour, day_in_year
    integer, pointer:: day_in_month_ptr(:) => null()
    real(DP):: sec_in_min, sec_in_day

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. albedo_matthews_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( flag_annual_mean ) then

      !
      ! Now, annual mean value is used, temporarily.
      !
      SurfAlbedoCul = 0.0_DP
      do l = 1, nseason
        SurfAlbedoCul = SurfAlbedoCul + aa_Data_Albedo( l, IndexCultivation )
      end do
      SurfAlbedoCul = SurfAlbedoCul / dble( nseason )

      do j = 1, jmax
        do i = 0, imax-1
          if ( xy_SurfType(i,j) > 0 ) then
            xy_SurfAlbedo(i,j) =                                       &
              &   ( 1.0_DP - xy_SurfCulInt(i,j) ) * xy_SurfAlbedo(i,j) &
              & + xy_SurfCulInt(i,j)              * SurfAlbedoCul
          end if
        end do
      end do

    else

      SecOfYear = DCCalDateEvalSecOfYear( TimeN, date = InitialDate )

      call DCCalInquire( &
        & day_in_month_ptr = day_in_month_ptr , & ! (out)
        & hour_in_day      = hour_in_day  , &     ! (out)
        & min_in_hour      = min_in_hour  , &     ! (out)
        & sec_in_min       = sec_in_min )         ! (out)

      day_in_year = sum( day_in_month_ptr )
      deallocate( day_in_month_ptr )
      sec_in_day  = hour_in_day * min_in_hour * sec_in_min

      if ( SecOfYear > day_in_year * sec_in_day ) SecOfYear = day_in_year * sec_in_day

      a_Data_SOY_Ex(0) = ( 0.0_DP - ( day_in_year - a_Data_DOY(nseason) ) ) * sec_in_day
      do t = 1, nseason
        a_Data_SOY_Ex(t) = a_Data_DOY(t) * sec_in_day
      end do
      a_Data_SOY_Ex(nseason+1) = ( day_in_year + a_Data_DOY(1) ) * sec_in_day

      a_tindex(1) = 0
      a_tindex(2) = 1
      do t = 1, nseason
        if ( a_Data_DOY(t) * sec_in_day <= SecOfYear ) then
          a_tindex(1) = t
          a_tindex(2) = t+1
        end if
      end do

      do t = 1, 2
        ! for northern hemisphere
        tindex = a_tindex(t)
        if ( tindex == 0 ) then
          tindex = nseason
        else if ( tindex == nseason+1 ) then
          tindex = 1
        else
          tindex = tindex
        end if
        do j = jmax/2+1, jmax
          do i = 0, imax-1
            xya_SurfAlbedoCul(i,j,t) = aa_Data_Albedo( tindex, IndexCultivation )
          end do
        end do
        ! for southern hemisphere
        tindex = a_tindex(t) + nseason / 2
        if ( tindex > nseason ) tindex = tindex - nseason
        if ( tindex == 0 ) then
          tindex = nseason
        else if ( tindex == nseason+1 ) then
          tindex = 1
        else
          tindex = tindex
        end if
        do j = 1, jmax/2
          do i = 0, imax-1
            xya_SurfAlbedoCul(i,j,t) = aa_Data_Albedo( tindex, IndexCultivation )
          end do
        end do
      end do

      xy_SurfAlbedoCul =                                                &
        &   ( xya_SurfAlbedoCul(:,:,2)   - xya_SurfAlbedoCul(:,:,1)   ) &
        & / ( a_Data_SOY_Ex(a_tindex(2)) - a_Data_SOY_Ex(a_tindex(1)) ) &
        & * ( SecOfYear                  - a_Data_SOY_Ex(a_tindex(1)) ) &
        & + xya_SurfAlbedoCul(:,:,1)

      do j = 1, jmax
        do i = 0, imax-1
          if ( xy_SurfType(i,j) > 0 ) then
            xy_SurfAlbedo(i,j) =                                          &
              &   ( 1.0_DP - xy_SurfCulInt(i,j) ) * xy_SurfAlbedo(i,j)    &
              & + xy_SurfCulInt(i,j)              * xy_SurfAlbedoCul(i,j)
          end if
        end do
      end do

    end if

  end subroutine ModAlbedoMatthewsCultivation

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

  subroutine AlbedoMatthewsInit

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

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

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

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

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

    if ( albedo_matthews_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    flag_annual_mean = .false.
    OceanAlbedo      = 0.1_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 = albedo_Matthews_nml, &  ! (out)
        & iostat = iostat_nml )        ! (out)
      close( unit_nml )

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

    aa_Data_Albedo = aa_Data_Albedo * 1.0e-2_DP

    aa_Data_Albedo(:,0) = OceanAlbedo

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  flag_annual_mean = %b', l = (/ flag_annual_mean /) )
    call MessageNotify( 'M', module_name, '  OceanAlbedo      = %f', d = (/ OceanAlbedo      /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    albedo_matthews_inited = .true.

  end subroutine AlbedoMatthewsInit

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

end module albedo_Matthews