subroutine MAPL_NCIOParseTimeUnits ( TimeUnits, year, month, day, hour, min, sec, rc )
implicit none
!
! !INPUT PARAMETERS:
!
character(len=*) 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
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) - 3
mpos(2) = lastdash - 1
dpos(1) = lastdash + 1
dpos(2) = dpos(1) + 1
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 MAPL_NCIOParseTimeUnits