!= 蜊倅ｽ阪�隗｣驥�
!= Parse units
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: dccalparseunit.f90,v 1.2 2009-10-17 14:08:58 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 DCCalParseUnit1( unit_str, unit_sym, err )
  !
  ! 譁�ｭ怜�螟画焚縺ｮ譌･譎ょ腰菴� *unit_str* 繧呈紛謨ｰ蝙句､画焚縺ｮ譌･譎ょ腰菴�
  ! *unit_sym* 縺ｫ螟画鋤縺励∪縺�.
  ! *unit_str* 縺ｨ縺励※譛牙柑縺ｪ譁�ｭ怜�縺ｯ莉･荳九�騾壹ｊ縺ｧ縺�.
  !
  ! dc_calendar_types#UNIT_SEC          :: 遘偵�蜊倅ｽ�
  ! dc_calendar_types#UNIT_MIN          :: 蛻��蜊倅ｽ�
  ! dc_calendar_types#UNIT_HOUR         :: 譎る俣縺ｮ蜊倅ｽ�
  ! dc_calendar_types#UNIT_DAY          :: 譌･縺ｮ蜊倅ｽ�
  ! dc_calendar_types#UNIT_MONTH        :: 譛医�蜊倅ｽ�
  ! dc_calendar_types#UNIT_YEAR         :: 蟷ｴ縺ｮ蜊倅ｽ�
  !
  ! 譛牙柑縺ｪ譁�ｭ怜�縺御ｸ弱∴繧峨ｌ縺溷�ｴ蜷�, *uni_sym* 縺ｫ莉･荳九�螟画焚縺ｫ逶ｸ蠖薙☆繧�
  ! 謨ｴ謨ｰ縺瑚ｿ斐ｊ縺ｾ縺�.
  !
  ! dc_calendar_types#UNIT_SYMBOL_SEC          :: 遘偵�蜊倅ｽ�
  ! dc_calendar_types#UNIT_SYMBOL_MIN          :: 蛻��蜊倅ｽ�
  ! dc_calendar_types#UNIT_SYMBOL_HOUR         :: 譎る俣縺ｮ蜊倅ｽ�
  ! dc_calendar_types#UNIT_SYMBOL_DAY          :: 譌･縺ｮ蜊倅ｽ�
  ! dc_calendar_types#UNIT_SYMBOL_MONTH        :: 譛医�蜊倅ｽ�
  ! dc_calendar_types#UNIT_SYMBOL_YEAR         :: 蟷ｴ縺ｮ蜊倅ｽ�
  !
  ! 辟｡蜉ｹ縺ｪ蛟､縺御ｸ弱∴繧峨ｌ縺溷�ｴ蜷�, 繧ｨ繝ｩ繝ｼ繧堤函縺倥∪縺�.
  !
  ! Parse a character variable of units of date *unit_str*, and
  ! return an integer variable of units of date *unit_sym*.
  ! Valid strings as *unit_str* are as follows.
  !
  ! dc_calendar_types#UNIT_SEC          :: Units of second
  ! dc_calendar_types#UNIT_MIN          :: Units of minute
  ! dc_calendar_types#UNIT_HOUR         :: Units of hour
  ! dc_calendar_types#UNIT_DAY          :: Units of day
  ! dc_calendar_types#UNIT_MONTH        :: Units of month
  ! dc_calendar_types#UNIT_YEAR         :: Units of year
  !
  ! When a valid string is specified, an integer corresponding
  ! one of following variables is returned to *unit_sym*.
  !
  ! dc_calendar_types#UNIT_SYMBOL_SEC          :: Units of second
  ! dc_calendar_types#UNIT_SYMBOL_MIN          :: Units of minute
  ! dc_calendar_types#UNIT_SYMBOL_HOUR         :: Units of hour
  ! dc_calendar_types#UNIT_SYMBOL_DAY          :: Units of day
  ! dc_calendar_types#UNIT_SYMBOL_MONTH        :: Units of month
  ! dc_calendar_types#UNIT_SYMBOL_YEAR         :: Units of year
  !
  ! If an invalid string is specified an error is caused.
  !

  use dc_calendar_types, only: UNIT_SYMBOL_ERR
  use dc_calendar_internal, only: dccaldate_str2usym
  use dc_message, only: MessageNotify
  use dc_string, only: LChar
  use dc_trace, only: BeginSub, EndSub
  use dc_error, only: StoreError, DC_NOERR, DC_EALREADYINIT, DC_EBADCALTYPE, DC_EBADUNIT
  use dc_types, only: STRING, DP
  implicit none
  character(*), intent(in):: unit_str
  integer, intent(out):: unit_sym
  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:: stat
  character(STRING):: cause_c
  character(*), parameter:: subname = 'DCCalParseUnit1'
continue
  call BeginSub( subname )
  stat = DC_NOERR
  cause_c = ''

  ! 蜊倅ｽ阪�譁�ｭ怜�繧偵す繝ｳ繝懊Ν (謨ｴ謨ｰ蝙句､画焚) 縺ｫ螟画鋤
  ! Convert strings of units into symbols (integer variables)
  !
  unit_sym = dccaldate_str2usym( unit_str )

  ! 繧ｨ繝ｩ繝ｼ蜃ｦ逅�
  ! Error Handling
  !
  if ( unit_sym == UNIT_SYMBOL_ERR ) then
    call MessageNotify('W', subname, 'unit_str=<%c> is invalid. (ONLY day,hour,min,sec are valid)', &
      & c1 = trim(unit_str) )
    stat = DC_EBADUNIT
    goto 999
  end if

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