!= 譌･譎よュ蝣ｱ縺ｮ蝠上＞蜷医ｏ縺�
!= Inquire information of date
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dccaldateinquire.f90,v 1.3 2010-09-24 00:28:18 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2009-. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
! 縺薙�繝輔ぃ繧､繝ｫ縺ｫ險倩ｼ峨＆繧後ｋ謇狗ｶ壹″鄒､縺ｯ dc_calendar 繝｢繧ｸ繝･繝ｼ繝ｫ縺九ｉ謠蝉ｾ帙＆繧後∪縺�.
!
! Procedures described in this file are provided from "dc_calendar" module.
!

subroutine DCCalDateInquire1( year, month, day, hour, min, sec, zone, &
  & elapse_sec, date, cal, err )
  !
  ! 譌･譎よュ蝣ｱ縺ｮ蝠上＞蜷医ｏ縺帙ｒ陦後＞縺ｾ縺�.
  !
  ! 蝠上＞蜷医ｏ縺帙�邨先棡繧�
  ! YYYY-MM-DDThh:mm:ss.sTZD 縺ｮ繧医≧縺ｪ譁�ｭ怜�
  ! (YYYY 縺ｯ蟷ｴ, MM 縺ｯ譛�, DD 縺ｯ譌･, hh 縺ｯ譎�, mm 縺ｯ蛻�, ss.s 縺ｯ遘�,
  ! TZD 縺ｯ繧ｿ繧､繝�繧ｾ繝ｼ繝ｳ) 縺ｧ蜿励￠蜿悶ｊ縺溘＞蝣ｴ蜷医↓縺ｯ,
  ! 荳玖ｨ倥�蜷悟錐縺ｮ繧ｵ繝悶Ν繝ｼ繝√Φ繧剃ｽｿ逕ｨ縺励※荳九＆縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *date* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, dc_calendar 蜀�Κ縺ｧ
  ! 菫晄戟縺輔ｌ繧区律譎ゅ↓髢｢縺吶ｋ諠��ｱ縺悟ｾ励ｉ繧後∪縺�.
  ! *date* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ險ｭ螳壹＆繧後◆譌･譎ゅ�諠��ｱ縺悟ｾ励ｉ繧後∪縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *cal* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, 邨碁℃遘呈焚 *elapse_sec*
  ! 縺ｮ蟷ｴ譛域律譎ょ�縺ｸ縺ｮ螟画鋤縺ｫ dc_calendar 蜀�Κ縺ｧ菫晄戟縺輔ｌ繧区圜縺檎畑縺�ｉ繧後∪縺�.
  ! *cal* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ險ｭ螳壹＆繧後◆證ｦ縺檎畑縺�ｉ繧後∪縺�.
  !
  ! Inquire information of date.
  !
  ! If a string like as "YYYY-MM-DDThh:mm:ss.sTZD"
  ! (YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
  ! ss.s is second, TZD is time zone) is needed,
  ! use a following homonymous subroutine.
  !
  ! If an optional argument *date* is omitted,
  ! information of date that is stored in the "dc_calendar"
  ! is returned,
  ! If *date* is not omitted, information of the variable is returned.
  !
  ! If an optional argument *cal* is omitted,
  ! information of calendar that is stored in the "dc_calendar"
  ! is used for conversion of elapsed seconds *elapse_sec* into
  ! year-month-day etc.
  ! If *cal* is not omitted, information of the variable is used.
  !
  use dc_calendar_types, only: DC_CAL, DC_CAL_DATE
  use dc_calendar_internal, only: default_cal, default_cal_set, default_date, &
    & dccaldate_normalize
  use dc_calendar_generic, only: DCCalDateToChar, DCCalToChar
  use dc_message, only: MessageNotify
  use dc_string, only: LChar
  use dc_trace, only: BeginSub, EndSub
  use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT, &
    & DC_EINCONSISTCALDATE, DC_ENEGATIVE
  use dc_types, only: STRING, DP, TOKEN
  implicit none
  integer, intent(out), optional:: year           ! 蟷ｴ. Year.
  integer, intent(out), optional:: month          ! 譛�. Month.
  integer, intent(out), optional:: day            ! 譌･. Day.
  integer, intent(out), optional:: hour           ! 譎�. Hour.
  integer, intent(out), optional:: min            ! 蛻�. Minute.
  real(DP), intent(out), optional:: sec           ! 遘�. Sec.
  character(*), intent(out), optional:: zone      ! UTC 縺九ｉ縺ｮ譎ょｷｮ. Time-zone.
  real(DP), intent(in), optional:: elapse_sec
                              ! *date* 縺九ｉ縺ｮ邨碁℃遘呈焚.
                              ! Elapsed seconds from *date*.
  type(DC_CAL_DATE), intent(in), optional, target:: date
                              ! 譌･譎よュ蝣ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              !
                              ! An object that stores information of
                              ! date and time.
  type(DC_CAL), intent(in), optional, target:: cal
                              ! 證ｦ諠��ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              !
                              ! An object that stores information of
                              ! calendar.
  logical, intent(out), optional:: err
                              ! 萓句､門�逅�畑繝輔Λ繧ｰ.
                              ! 繝�ヵ繧ｩ繝ｫ繝医〒縺ｯ, 縺薙�謇狗ｶ壹″蜀�〒繧ｨ繝ｩ繝ｼ縺�
                              ! 逕溘§縺溷�ｴ蜷�, 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�＠縺ｾ縺�.
                              ! 蠑墓焚 *err* 縺御ｸ弱∴繧峨ｌ繧句�ｴ蜷�,
                              ! 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�○縺�, 莉｣繧上ｊ縺ｫ
                              ! *err* 縺ｫ .true. 縺御ｻ｣蜈･縺輔ｌ縺ｾ縺�.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

  ! 菴懈･ｭ螟画焚
  ! Work variables
  !
  integer:: wyear, wmonth, wday, whour, wmin
  real(DP):: wsec
  character(TOKEN):: wzone
  type(DC_CAL_DATE), pointer:: datep =>null()
  type(DC_CAL), pointer:: calp =>null()
  character(STRING):: e_date_str, e_cal_str
  integer:: stat
  character(STRING):: cause_c
  character(*), parameter:: subname = 'DCCalInquire1'
continue
  call BeginSub( subname )
  stat = DC_NOERR
  cause_c = ''

  ! 繧ｪ繝悶ず繧ｧ繧ｯ繝医�繝昴う繝ｳ繧ｿ蜑ｲ莉�
  ! Associate pointer of an object
  !
  if ( present( date ) ) then
    datep => date
  else
    datep => default_date
  end if

  if ( present( cal ) ) then
    calp => cal
  else
    calp => default_cal
    if ( .not. calp % initialized ) call default_cal_set
  end if

  ! 蛻晄悄險ｭ螳壹�繝√ぉ繝�け
  ! Check initialization
  !
  if ( .not. datep % initialized ) then
    stat = DC_ENOTINIT
    cause_c = 'DC_CAL_DATE'
    goto 999
  end if

  if ( .not. calp % initialized ) then
    stat = DC_ENOTINIT
    cause_c = 'DC_CAL'
    goto 999
  end if

  ! 蜷�ｦ∫ｴ�縺ｸ縺ｮ蛟､縺ｮ蜿ら�
  ! Refer elements
  !
  wyear  = datep % year
  wmonth = datep % month
  wday   = datep % day
  whour  = datep % hour
  wmin   = datep % min
  wsec   = datep % sec
  wzone  = datep % zone

  ! 邨碁℃譎る俣(遘�)縺ｮ霑ｽ蜉�
  ! Add elapsed time (seconds)
  !
  if ( present( elapse_sec ) ) then
!!$    if ( elapse_sec < 0.0_DP ) then
!!$      stat = DC_ENEGATIVE
!!$      cause_c = 'elapse_sec'
!!$      goto 999
!!$    end if

    wsec = wsec + elapse_sec
  end if

  ! 譌･譎ゅ�豁｣隕丞喧
  ! Normalize date and time
  !
  stat = dccaldate_normalize( wyear, wmonth, wday, whour, wmin, wsec, & ! (inout)
    &                         calp )                                    ! (in)
  if ( stat == DC_EINCONSISTCALDATE ) then
    e_cal_str = DCCalToChar( calp )
    e_date_str = DCCalDateToChar( wyear, wmonth, wday, whour, wmin, wsec, wzone )
    call MessageNotify('W', subname, 'cal=<%c> and date=<%c> are inconsistency', &
      & c1 = trim(e_cal_str), c2 = trim(e_date_str) )
    goto 999
  end if

  ! 蠑墓焚縺ｸ縺ｮ莉｣蜈･
  ! Substitute arguments
  !
  if ( present(year ) ) year  = wyear
  if ( present(month) ) month = wmonth
  if ( present(day  ) ) day   = wday
  if ( present(hour ) ) hour  = whour
  if ( present(min  ) ) min   = wmin
  if ( present(sec  ) ) sec   = wsec
  if ( present(zone ) ) zone  = wzone

  ! 邨ゆｺ��逅�, 萓句､門�逅�
  ! Termination and Exception handling
  !
999 continue
  nullify( calp, datep )
  call StoreError( stat, subname, err, cause_c )
  call EndSub( subname )
end subroutine DCCalDateInquire1

subroutine DCCalDateInquire2( date_str, elapse_sec, date, cal, err )
  !
  ! 譌･譎よュ蝣ｱ縺ｮ蝠上＞蜷医ｏ縺帙ｒ陦後＞縺ｾ縺�.
  ! 蝠上＞蜷医ｏ縺帷ｵ先棡縺ｯ YYYY-MM-DDThh:mm:ss.sTZD 縺ｮ繧医≧縺ｪ譁�ｭ怜�
  ! (YYYY 縺ｯ蟷ｴ, MM 縺ｯ譛�, DD 縺ｯ譌･, hh 縺ｯ譎�, mm 縺ｯ蛻�, ss.s 縺ｯ遘�,
  ! TZD 縺ｯ繧ｿ繧､繝�繧ｾ繝ｼ繝ｳ) 縺ｧ霑斐ｊ縺ｾ縺�.
  ! 譌･譎ゅ�譁�ｭ怜�蠖｢蠑上�
  ! gtool4 netCDF 隕冗ｴ��5.5 譌･譎ょｽ｢蠑上阪↓貅匁侠縺励※縺�∪縺�.
  !
  ! 蝠上＞蜷医ｏ縺帙�邨先棡繧貞ｹｴ譛域律譎ょ�遘偵〒蜷�句､画焚縺ｧ蜿励￠蜿悶ｊ縺溘＞蝣ｴ蜷医�
  ! 荳願ｨ倥�蜷悟錐縺ｮ繧ｵ繝悶Ν繝ｼ繝√Φ繧剃ｽｿ逕ｨ縺励※荳九＆縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *date* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, dc_calendar 蜀�Κ縺ｧ
  ! 菫晄戟縺輔ｌ繧区律譎ゅ↓髢｢縺吶ｋ諠��ｱ縺悟ｾ励ｉ繧後∪縺�.
  ! *date* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ險ｭ螳壹＆繧後◆譌･譎ゅ�諠��ｱ縺悟ｾ励ｉ繧後∪縺�.
  !
  ! 逵∫払蜿ｯ閭ｽ蠑墓焚 *cal* 縺檎怐逡･縺輔ｌ縺溷�ｴ蜷医↓縺ｯ, 邨碁℃遘呈焚 *elapse_sec*
  ! 縺ｮ蟷ｴ譛域律譎ょ�縺ｸ縺ｮ螟画鋤縺ｫ dc_calendar 蜀�Κ縺ｧ菫晄戟縺輔ｌ繧区圜縺檎畑縺�ｉ繧後∪縺�.
  ! *cal* 縺檎怐逡･縺輔ｌ縺ｪ縺��ｴ蜷医↓縺ｯ縺昴�螟画焚縺ｫ險ｭ螳壹＆繧後◆證ｦ縺檎畑縺�ｉ繧後∪縺�.
  !
  ! Inquire information of date.
  ! A result is returned as a string like as
  ! YYYY-MM-DDThh:mm:ss.sTZD
  ! (YYYY is year, MM is month, DD is day, hh is hour, mm is minute,
  ! ss.s is second, TZD is time zone).
  ! Format of date is conformed to gtool4 netCDF Convention "5.5 Expression of date and time"
  !
  ! If individual variables (year, month, day, hour, minute, second, zone)
  ! are needed, use a foregoing homonymous subroutine.
  !
  ! If an optional argument *date* is omitted,
  ! information of date that is stored in the "dc_calendar"
  ! is returned,
  ! If *date* is not omitted, information of the variable is returned.
  !
  ! If an optional argument *cal* is omitted,
  ! information of calendar that is stored in the "dc_calendar"
  ! is used for conversion of elapsed seconds *elapse_sec* into
  ! year-month-day etc.
  ! If *cal* is not omitted, information of the variable is used.
  !
  use dc_calendar_generic, only: DCCalDateToChar
  use dc_calendar_types, only: DC_CAL, DC_CAL_DATE
  use dc_calendar_internal, only: default_cal, default_cal_set, default_date, &
    & dccaldate_normalize
  use dc_calendar_generic, only: DCCalDateToChar, DCCalToChar
  use dc_message, only: MessageNotify
  use dc_string, only: LChar
  use dc_trace, only: BeginSub, EndSub
  use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT, &
    & DC_EINCONSISTCALDATE, DC_ENEGATIVE
  use dc_types, only: STRING, DP, TOKEN
  implicit none
  character(*), intent(out):: date_str
                              ! 譌･譎よュ蝣ｱ繧定｡ｨ縺呎枚蟄怜�.
                              ! 陦ｨ遉ｺ蠖｢蠑上↓縺､縺�※縺ｯ gtool4 netCDF 隕冗ｴ�
                              ! 5.5 譌･譎ょｽ｢蠑上ｒ蜿ら�縺ｮ縺薙→.
                              !
                              ! Strings that express date and time.
                              ! See gtool4 netCDF Convention
                              ! 5.5 Expression of date and time for details.
  real(DP), intent(in), optional:: elapse_sec
                              ! *date* 縺九ｉ縺ｮ邨碁℃遘呈焚.
                              ! Elapsed seconds from *date*.
  type(DC_CAL_DATE), intent(in), optional, target:: date
                              ! 譌･譎よュ蝣ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              !
                              ! An object that stores information of
                              ! date and time.
  type(DC_CAL), intent(in), optional, target:: cal
                              ! 證ｦ諠��ｱ繧貞庶繧√◆繧ｪ繝悶ず繧ｧ繧ｯ繝�.
                              !
                              ! An object that stores information of
                              ! calendar.
  logical, intent(out), optional:: err
                              ! 萓句､門�逅�畑繝輔Λ繧ｰ.
                              ! 繝�ヵ繧ｩ繝ｫ繝医〒縺ｯ, 縺薙�謇狗ｶ壹″蜀�〒繧ｨ繝ｩ繝ｼ縺�
                              ! 逕溘§縺溷�ｴ蜷�, 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�＠縺ｾ縺�.
                              ! 蠑墓焚 *err* 縺御ｸ弱∴繧峨ｌ繧句�ｴ蜷�,
                              ! 繝励Ο繧ｰ繝ｩ繝�縺ｯ蠑ｷ蛻ｶ邨ゆｺ�○縺�, 莉｣繧上ｊ縺ｫ
                              ! *err* 縺ｫ .true. 縺御ｻ｣蜈･縺輔ｌ縺ｾ縺�.
                              !
                              ! Exception handling flag.
                              ! By default, when error occur in
                              ! this procedure, the program aborts.
                              ! If this *err* argument is given,
                              ! .true. is substituted to *err* and
                              ! the program does not abort.

  ! 菴懈･ｭ螟画焚
  ! Work variables
  !
  integer:: year, month, day, hour, min
  real(DP):: sec
  character(TOKEN):: zone
  type(DC_CAL_DATE), pointer:: datep =>null()
  type(DC_CAL), pointer:: calp =>null()
  character(STRING):: e_date_str, e_cal_str
  integer:: stat
  character(STRING):: cause_c
  character(*), parameter:: subname = 'DCCalInquire2'
continue
  call BeginSub( subname )
  stat = DC_NOERR
  cause_c = ''

  ! 繧ｪ繝悶ず繧ｧ繧ｯ繝医�繝昴う繝ｳ繧ｿ蜑ｲ莉�
  ! Associate pointer of an object
  !
  if ( present( date ) ) then
    datep => date
  else
    datep => default_date
  end if

  if ( present( cal ) ) then
    calp => cal
  else
    calp => default_cal
    if ( .not. calp % initialized ) call default_cal_set
  end if

  ! 蛻晄悄險ｭ螳壹�繝√ぉ繝�け
  ! Check initialization
  !
  if ( .not. datep % initialized ) then
    stat = DC_ENOTINIT
    cause_c = 'DC_CAL_DATE'
    goto 999
  end if

  if ( .not. calp % initialized ) then
    stat = DC_ENOTINIT
    cause_c = 'DC_CAL'
    goto 999
  end if

  ! 蜷�ｦ∫ｴ�縺ｮ蜿門ｾ�
  ! Get elements
  !
  year  = datep % year
  month = datep % month
  day   = datep % day
  hour  = datep % hour
  min   = datep % min
  sec   = datep % sec
  zone  = datep % zone

  ! 邨碁℃譎る俣(遘�)縺ｮ霑ｽ蜉�
  ! Add elapsed time (seconds)
  !
  if ( present( elapse_sec ) ) then
!!$    if ( elapse_sec < 0.0_DP ) then
!!$      stat = DC_ENEGATIVE
!!$      cause_c = 'elapse_sec'
!!$      goto 999
!!$    end if

    sec = sec + elapse_sec
  end if

  ! 譌･譎ゅ�豁｣隕丞喧
  ! Normalize date and time
  !
  stat = dccaldate_normalize( year, month, day, hour, min, sec, & ! (inout)
    &                         calp )                              ! (in)
  if ( stat == DC_EINCONSISTCALDATE ) then
    e_cal_str = DCCalToChar( calp )
    e_date_str = DCCalDateToChar( year, month, day, hour, min, sec, zone )
    call MessageNotify('W', subname, 'cal=<%c> and date=<%c> are inconsistency', &
      & c1 = trim(e_cal_str), c2 = trim(e_date_str) )
    goto 999
  end if

  ! 譌･譎り｡ｨ險假ｼ�gtool4 netCDF 隕冗ｴ� 5.5 譌･譎ょｽ｢蠑擾ｼ峨∈縺ｮ螟画鋤
  ! Convert expression of date (gtool4 netCDF Convention 5.5 Expression of date and time)
  !
  date_str = DCCalDateToChar( year, month, day, hour, min, sec, zone )

  ! 邨ゆｺ��逅�, 萓句､門�逅�
  ! Termination and Exception handling
  !
999 continue
  nullify( calp, datep )
  call StoreError( stat, subname, err, cause_c )
  call EndSub( subname )
end subroutine DCCalDateInquire2