C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_CNVTTM    CONVERT I4TIME TO SIX INTEGERS
C   PRGMMR: PATTY MILLER     ORG: FSL/PROFS  DATE: 90-06-15
C
C ABSTRACT: CONVERT I4TIME TO SIX INTEGERS WHICH REPRESENT THE
C   CORRESPONDING YEAR, MONTH, DAY, HOUR, MINUTES, AND SECOND.
C
C PROGRAM HISTORY LOG:
C   81-02-13  MURRAY      ORIGINAL VERSION
C   81-07-12  MURRAY      CORRECT FOR INVALID TIME RETURNED WHEN
C                         TIME IS IN LATE DECEMBER.
C   82-09-08  HOAGENSON   CHANGED NAME FROM CV_I4TIME_INT_G
C   88-10-03  MILLER      CHANGED TO STANDARD FORTRAN
C   98-07-10  BARTH, M.   REDEFINED I4TIME (NUMBER OF SECONDS FROM
C                         A BASELINE)
C                         OLD:  - WENT FROM 1/1/1960 THROUGH 12/31/2027
C                               - BASELINE 1/1/1960 0Z = 0 I4TIME
C                               - POSITIVE NUMBERS ONLY
C                         NEW:  - GOES FROM 1/1/1945 THROUGH 12/31/2079
C                               - BASELINE 1/1/2012 0Z = 0 I4TIME
C                               - NEGATIVE AND POSITIVE NUMBERS
C   99-09-21  M. BARTH    COPIED FROM MSAS & MODIFIED FOR MADIS.
C   08-06-13  L. BENJAMIN CHANGED "MADISCON" INCLUDE TO 
C                         "INTQC1CON1"
C
C USAGE:    CALL M_CNVTTM(I4TIME,NYEAR,NMONTH,NDAY,NHOUR,
C                         NMIN,NSEC,ISTATUS)
C   INPUT ARGUMENT LIST:
C     I4TIME   - INTEGER  NUMBER OF SECONDS
C
C   OUTPUT ARGUMENT LIST:
C     NYEAR    - INTEGER  YEAR SINCE 1900 (E.G., 1901 --> 01,
C                                          2001 --> 101)
C     NMONTH   - INTEGER  MONTH
C     NDAY     - INTEGER  DAY
C     NHOUR    - INTEGER  HOUR
C     NMIN     - INTEGER  MINUTE
C     NSEC     - INTEGER  SECOND
C     ISTATUS  - INTEGER  STATUS
C
C REMARKS:
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$


      SUBROUTINE M_CNVTTM(I4TIME,NYEAR,NMONTH,NDAY,NHOUR,
     1                    NMIN,NSEC,ISTATUS)
C
C       M_CNVTTM CONVERTS I4 TIME TO SIX INTEGERS
C
C       MURRAY          13-FEB-1981     ORIGINAL VERSION
C       MURRAY          12-JUL-1981     CORRECT FOR INVALID TIME RETURN
C                                       WHEN
C                                       TIME IS IN LATE DECEMBER.
C       HOAGENSON       08-SEP-1982     CHANGED NAME FROM CV_I4TIME_INT
C       MILLER          03-OCT-1988     CHANGED TO STANDARD FORTRAN

C
      INCLUDE 'IMPLICIT'
      INCLUDE 'INTQC1CON1'

      INTEGER I4TIME,IBASE,ISTATUS,NN,I,LEFTOVER,
     +        NYEAR,NMONTH,NDAY,NHOUR,NMIN,NSEC,NDAYS,
     +        NSECYR,NSECDA,NSECHR,NSECMN,NSECMO(12),
     +        NDAYMO(12),ISIGN,ISTART,IEND,NHOURS,NHRDA,NMINS,NMNHR
      PARAMETER (IBASE=112)
      DATA NSECYR/31536000/NSECDA/86400/NSECHR/3600/NSECMN/60/
      DATA NSECMO/2678400,0,2678400,2592000,2678400,
     1 2592000,2678400,2678400,2592000,2678400,2592000,2678400/
      DATA NDAYMO/31,0,31,30,31,30,31,31,30,31,30,31/
      DATA NHRDA/24/NMNHR/60/

C
C SUBTRACT OUT NUMBER OF YEARS.
C
      IF(I4TIME.LT.0)THEN
         ISIGN = -1
         ISTART = 1
      ELSE
         ISIGN = 1
         ISTART = 0
      ENDIF
      LEFTOVER = I4TIME
      NN = LEFTOVER
      DO 5 I = ISTART, 70
         IF (MOD(I,4) .EQ. 0) THEN
            NN = NN - ISIGN * (NSECYR + NSECDA)
         ELSE
            NN = NN - ISIGN * NSECYR
         END IF
         IF (ISIGN .GT. 0 .AND. NN .LT. 0) THEN
            GO TO 8
         ELSE IF (ISIGN .LT. 0 .AND. NN .GE.0) THEN
            GO TO 8
         ENDIF
         LEFTOVER = NN
5     CONTINUE
8     NYEAR = IBASE + ISIGN * I
      IF (NYEAR .LT. 0) NYEAR = NYEAR + 100
C
C SUBTRACT OUT NUMBER OF MONTHS.
C
      NSECMO(2) = 2419200
      NDAYMO(2) = 28
      IF (MOD(NYEAR,4) .NE. 0) GO TO 10
         NSECMO(2) = 2505600
         NDAYMO(2) = 29
10       NN=LEFTOVER
         IF (ISIGN .LT. 0) THEN
            ISTART = 12
            IEND = 1
         ELSE
            ISTART = 1
            IEND = 12
         ENDIF
      DO 20 I=ISTART,IEND,ISIGN
         NN = NN - ISIGN * NSECMO(I)
         IF (ISIGN .GT. 0 .AND. NN .LT. 0) THEN
            GO TO 30
         ELSE IF (ISIGN .LT. 0 .AND. NN .GE.0) THEN
            GO TO 30
         ENDIF
         LEFTOVER = NN
20    CONTINUE
30    NMONTH = I
C
C SUBTRACT OUT NUMBER OF DAYS.
C
      NDAYS = LEFTOVER / NSECDA
      IF (ISIGN .GT. 0) THEN
         NDAY = NDAYS + 1
      ELSE IF (NDAYS*NSECDA .EQ. LEFTOVER) THEN
         NDAY = NDAYMO(NMONTH) + NDAYS + 1
      ELSE
         NDAY = NDAYMO(NMONTH) + NDAYS
      ENDIF
      LEFTOVER = LEFTOVER - (NDAYS * NSECDA)
C
C SUBTRACT OUT NUMBER OF HOURS.
C
      NHOURS = LEFTOVER / NSECHR
      IF (ISIGN .GT. 0) THEN
         NHOUR = NHOURS
      ELSE IF (NHOURS*NSECHR .EQ. LEFTOVER) THEN
         NHOUR = NHRDA + NHOURS
         IF (NHOUR .EQ. NHRDA) NHOUR = 0
      ELSE
         NHOUR = NHRDA + NHOURS - 1
      ENDIF
      LEFTOVER = LEFTOVER - (NHOURS * NSECHR)
C
C SUBTRACT OUT NUMBER OF MINUTES.
C
      NMINS = LEFTOVER / NSECMN
      IF (ISIGN .GT. 0) THEN
         NMIN = NMINS
      ELSE IF (NMINS*NSECMN .EQ. LEFTOVER) THEN
         NMIN = NMNHR + NMINS
         IF (NMIN .EQ. NMNHR) NMIN = 0
      ELSE
         NMIN = NMNHR + NMINS - 1
      ENDIF
      LEFTOVER = LEFTOVER - (NMINS * NSECMN)
C
C WHAT'S LEFT OVER IS NUMBER OF SECONDS.
C
      IF (ISIGN .GT. 0) THEN
         NSEC = LEFTOVER
      ELSE
         NSEC = NSECMN + LEFTOVER
      ENDIF
      ISTATUS = SUCCESS_P
      RETURN
      END
C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
