!= å˜ä½ã®è§£é‡ˆ
!= 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