ParseTimeUnits Subroutine

public subroutine ParseTimeUnits(TimeUnits, year, month, day, hour, min, sec, rc)

ParseTimeUnits – Parse the COARDS time units string

This subroutine takes as input the COARDS metadata time units string and parses out the date included in the string. This date typically represents the first time in a COARDS HDF files.

History

  • 2000.10.18 Lucchesi Initial coding.
  • 2001.08.13 Lucchesi Modified to better parse time:units string (needed for lats4d support)

Arguments

Type IntentOptional Attributes Name
character(len=MAXCHR) :: TimeUnits

Units metadata string from the Time coord var

integer :: year

4-digit year

integer :: month

month

integer :: day

day

integer :: hour

hour

integer :: min

minute

integer :: sec

second

integer :: rc

return code 0 = no error -1 = problem parsing string


Called by

proc~~parsetimeunits~~CalledByGraph proc~parsetimeunits ParseTimeUnits proc~getbegdatetime GetBegDateTime proc~getbegdatetime->proc~parsetimeunits proc~getdatetimevec GetDateTimeVec proc~getdatetimevec->proc~parsetimeunits proc~cfio_getvar CFIO_GetVar proc~cfio_getvar->proc~getdatetimevec proc~cfio_sgetvar CFIO_SGetVar proc~cfio_sgetvar->proc~getdatetimevec proc~esmf_cfiosdffileopen ESMF_CFIOSdfFileOpen proc~esmf_cfiosdffileopen->proc~getbegdatetime proc~esmf_cfiofileopen ESMF_CFIOFileOpen proc~esmf_cfiofileopen->proc~esmf_cfiosdffileopen none~find~31 CFIOCollection%find none~find~31->proc~esmf_cfiofileopen proc~mapl_cfioopenwrite MAPL_CFIOOpenWrite proc~mapl_cfioopenwrite->proc~esmf_cfiofileopen program~test test program~test->proc~esmf_cfiofileopen program~test~10 test program~test~10->proc~esmf_cfiofileopen program~test~12 test program~test~12->proc~esmf_cfiofileopen program~test~5 test program~test~5->proc~esmf_cfiofileopen

Source Code

      subroutine ParseTimeUnits ( TimeUnits, year, month, day, hour, min, sec, rc )
!
! !USES:
!
      implicit none
!
! !INPUT PARAMETERS:
!
      character(len=MAXCHR) TimeUnits      !! Units metadata string from the Time coord var
!
! !OUTPUT PARAMETERS:
!
      integer        year               !! 4-digit year
      integer        month              !! month
      integer        day                !! day
      integer        hour               !! hour
      integer        min                !! minute
      integer        sec                !! second
      integer        rc                 !! return code
                                        !!  0 = no error
                                        !! -1 = problem parsing string

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


      ! Local variables

      integer ypos(2), mpos(2), dpos(2), hpos(2), spos(2)
      integer strlen
      integer firstdash, lastdash
      integer firstcolon, lastcolon
      integer lastspace

      strlen = LEN_TRIM (TimeUnits)

      firstdash = index(TimeUnits, '-')
      lastdash  = index(TimeUnits, '-', BACK=.TRUE.)

      if (firstdash .LE. 0 .OR. lastdash .LE. 0) then
        rc = -1
        return
      endif

      ypos(2) = firstdash - 1
      mpos(1) = firstdash + 1
      ypos(1) = ypos(2) - 4

      mpos(2) = lastdash - 1
      dpos(1) = lastdash + 1
      dpos(2) = dpos(1) + 2

      read ( TimeUnits(ypos(1):ypos(2)), * ) year
      read ( TimeUnits(mpos(1):mpos(2)), * ) month
      read ( TimeUnits(dpos(1):dpos(2)), * ) day

      firstcolon = index(TimeUnits, ':')

      if (firstcolon .LE. 0) then

        ! If no colons, check for hour.

        ! Logic below assumes a null character or something else is after the hour
        ! if we do not find a null character add one so that it correctly parses time
        if (TimeUnits(strlen:strlen) /= char(0)) then
           TimeUnits = trim(TimeUnits)//char(0)
           strlen=len_trim(TimeUnits)
        endif
        lastspace = index(TRIM(TimeUnits), ' ', BACK=.TRUE.)
        if ((strlen-lastspace).eq.2 .or. (strlen-lastspace).eq.3) then
          hpos(1) = lastspace+1
          hpos(2) = strlen-1
          read (TimeUnits(hpos(1):hpos(2)), * ) hour
          min  = 0
          sec  = 0
        else
          print *, 'ParseTimeUnits: Assuming a starting time of 00z'
          hour = 0
          min  = 0
          sec  = 0
        endif

      else
        hpos(1) = firstcolon - 2
        hpos(2) = firstcolon - 1
        lastcolon =  index(TimeUnits, ':', BACK=.TRUE.)
        if ( lastcolon .EQ. firstcolon ) then
          mpos(1) = firstcolon + 1
          mpos(2) = firstcolon + 2
          read (TimeUnits(hpos(1):hpos(2)), * ) hour
          read (TimeUnits(mpos(1):mpos(2)), * ) min
          sec = 0
        else
          mpos(1) = firstcolon + 1
          mpos(2) = lastcolon - 1
          spos(1) = lastcolon + 1
          spos(2) = lastcolon + 2
          read (TimeUnits(hpos(1):hpos(2)), * ) hour
          read (TimeUnits(mpos(1):mpos(2)), * ) min
          read (TimeUnits(spos(1):spos(2)), * ) sec
        endif
      endif

      rc = 0
      return
      end subroutine ParseTimeUnits