      SUBROUTINE POTTEM(UNITNM,INDEX,DELTIME,FCSTYM,STATUS)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- POTTEM
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 03/12/93   PROGRAMMER- FRANK TILLEY (SMSRC)
C   VERSION- 3.0    DATE- 05/21/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C     FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C     INTEGER.
C   VERSION- 3.1    DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 12/09/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.3    DATE- 11/03/98   PROGRAMMER- BRIAN BARIL (ITSS)
C   VERSION- 3.4    DATE- 05/02/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.5    DATE- 01/11/05   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.6    DATE- 10/16/06   PROGRAMMER- FRANK TILLEY (IMSG)
C
C    FUNCTION- POTTEM GETS THE DATE AND TIME OF A HIRS DATA FRAME
C        AND DETERMINES WHICH OF THE FOUR MAPPARM CDB POTENTIAL
C        TEMPERATURE MAPS MOST CLOSELY MATCHES THE DATA FRAME TIME.
C        THE FOUR MAPS ARE NAMED POTEMP00, POTEMP06, POTEMP12, AND
C        POTEMP18.  THE TWO DIGITS AT THE END OF THESE MAP NAMES
C        REFLECT THE HOUR OF THE FORECAST CONTAINED IN THAT MAP.
C        FOR EACH POTENTIAL TEMPERATURE MAP THERE IS AN ASSOCIATED
C        SURFACE PRESSURE MAP.  THE NAMES OF THESE MAPS ARE IN TWO
C        ARRAYS IN MAPDVR.  POTTEM DETERMINES WHICH MAP TO USE AND
C        RETURNS THE ARRAY INDEX OF THAT MAP NAME TO MAPDVR.  IF THE
C        DATE OF THE MAP FOR THE HOUR OF THE DATA FRAME DOES NOT
C        MATCH THE DATE OF THE DATA FRAME, THE DATE OF THE MAP FOR
C        THE NEXT SIX-HOUR PERIOD IS CHECKED.  IF THAT DATE DOES NOT
C        MATCH, AN ERROR MESSAGE IS WRITTEN TO THE KIDS
C        MESSAGE LOG FILE AND STATUS CODE 77 IS RETURNED TO MAPDVR.
C   VERSION- 3.1 RTOVS UPGRADE TO SELECT CLOSEST FORECAST TIME TO
C                THE CURRENT TIME OF THE DATA FRAME.  ALSO, THE CDB
C                LOOP WAS UNROLLED TO SPEED UP PROCESSING TIMES, AND
C                MKNAME WAS REMOVED.  FCSTYM WAS ADDED TO THE CALL
C                TO STORE THE FORECAST YEAR, AND THE LOCAL VARIABLES
C                YR,MO,DAY WERE EXPANDED TO ARRAYS OF DIM 4.
C   VERSION- 3.2 RTOVS UPGRADE TO SELECT THE MOST RECENT FORECAST
C                TIME IF THE FORECAST FILES ARE AT LEAST 1 DAY OLD.
C                ADD 2 CALLS TO POTTE$ TO DISPLAY WARNINGS IF THIS
C                HAPPENS.
C   VERSION- 3.3 REPLACED CALL TO IGTCDB WITH INTCOEF TO ACCESS
C                NEW 8-BYTE MAPPARM CDB.
C   VERSION- 3.4 FOR WR #98 THE CALCULATION OF THE DATA FRAME
C                START TIME WAS CHANGED FROM THE TIME OF THE
C                FIRST LINE IN THE EXTENDED DATA FRAME TO THE
C                FIRST NON-MISSING SCAN TIME IN THE DATA FRAME.
C   VERSION- 3.5 MADE SMALL MODIFICATION IN ERROR RETURN SECTION.
C   VERSION- 3.6 REMOVED ERROR MESSAGE GENERATED WHEN THE 0Z
C                FORECAST FILE IS USED WITH RETRIEVALS FROM THE
C                PREVIOUS DAY. THIS IS A NORMAL CONDITION AND
C                SHOULD NOT HAVE BEEN USED TO GENERATE AN ERROR
C                MESSAGE. THE CALL TO POTTE$ WITH MESSAGE CODE
C                102 WAS COMMENTED OUT (WR #303)
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE  I/O  DESCRIPTION
C     --------          ----  ---  --------
C     UNITNM             INT  I/O  THE MAPPARM CDB UNIT NUMBER.
C
C     DELTIME            REAL I/O  POTENTIAL TEMPERATURE TIME MINUS
C                                  NCEP FORECAST TIME.
C
C     INDEX              INT  O    THE INDEX OF THE NAME OF THE
C                                  CDB POTENTIAL TEMPERATURE MAP
C                                  IN THE MAP NAME ARRAY.
C
C     FCSTYM             INT  O    FORECAST TIME AS YYMM,DDHH
C
C     STATUS             INT  O    SUBROUTINE COMPLETION STATUS.
C
      INTEGER        INDEX, FCSTYM(2), STATUS, UNITNM
      REAL           DELTIME
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  DESCRIPTION
C     --------          ----  --------
C     DAY(4)            INT   TWO DIGIT DAY FROM THE CDB.
C     DFRDAY            INT   DATE OF THE DATA FRAME IN CENTURY DAY
C                             FORMAT.
C     FRHOUR            INT   HOUR EXTRACTED FROM THE HIRS DATA FRAME
C                             DATE AND TIME.
C     FRTIME            INT   VECTOR(3) HOLDING DATE AND TIME OF THE
C                             HIRS DATA FRAME IN FORM YYMM, DDHH, MMSS.
C     I, J              INT   LOOP INDEX.
C     MAPDAY            INT   ARRAY HOLDING THE YEAR, MONTH, AND DAY
C                             OF UPDATE FOR EACH OF THE FOUR POTENTIAL
C                             TEMPERATURE MAPS IN THE CDB.  DIMENSION
C                             4.
C     MO(4)             INT   TWO DIGIT MONTH FROM THE CDB.
C     MPTIME            INT   FORECAST HOUR.
C                             RETRIEVING MAPDAY FROM THE MAPPARM CDB.
C     SCLINE            INT   SCAN LINE INDEX USED IN CALCULATING THE
C                             DATA FRAME TIME.
C     STAT              INT   STATUS CODE RETURNED FROM THE CALL TO
C                             IGTCDB.
C     YR(4)             INT   TWO DIGIT YEAR FROM THE CDB.
C
      INTEGER DFRDAY,FRHOUR,FRTIME(3),MAPDAY(4),MPTIME,I,J
      INTEGER STAT,YR(4),MO(4),DAY(4),FRMIN,FRSEC,DELDAY,FCSTHR(4)
      INTEGER SCLINE,K,DELDAY2
C
      REAL    DELMIN
C
C
C   FUNCTIONS CALLED-
C     INTRINSIC  MOD
C
C   SUBPROGRAMS CALLED- INTCOEF, CENDAY, ODBTIM, POTTE$
C
C
C   EXIT STATES- 77 = NO FORECAST FILE TO MATCH ORBIT TIME.
C                99 = FATAL ERROR IN IGTCDB OR IN FILE MANAGEMENT
C                     UTILITIES.
C
C   COMMENTS-  SINCE THE FORECAST POTENTIAL TEMPERATURE MAPS FOR
C              THE NORTHERN AND SOUTHERN HEMISPHERES AND THE
C              FORECAST SURFACE PRESSURE MAPS FOR BOTH HEMISPHERES
C              ARE ALL UPDATED USING VALUES FROM A SINGLE FILE,
C              IT IS ASSUMED THAT ALL FOUR OF THESE MAPS FOR A
C              GIVEN 6-HOUR PERIOD ARE UPDATED AT THE SAME TIME.
C
C********************************************************************
C
      DATA FCSTHR /0, 6, 12, 18/
      INDEX = 0
      STATUS = 0
      DELMIN = 999.0
C
C   FOR EACH POTENTIAL TEMPERATURE MAP, GET THE UPDATE YEAR,
C   MONTH, AND DAY FOR EACH OF THESE MAPS FROM THE CDB. CONVERT
C   THIS DATE TO CENTURY DAY FOR COMPARISON AGAINST THE DATA
C   FRAME DAY.
C
      CALL INTCOEF(UNITNM,'POTEMP00','NORTHERN','UPDAT   ',1,0,0,0,
     1             MAPDAY(1),'GET',STAT)
      IF(STAT.NE.0) GOTO 110
      YR(1) = MAPDAY(1) / 10000
      MO(1) = (MOD(MAPDAY(1),10000)) / 100
      DAY(1) = MOD(MAPDAY(1),100)
      CALL CENDAY(YR(1),MO(1),DAY(1),MAPDAY(1))
      CALL INTCOEF(UNITNM,'POTEMP06','NORTHERN','UPDAT   ',1,0,0,0,
     1            MAPDAY(2),'GET',STAT)
      IF(STAT.NE.0) GOTO 110
      YR(2) = MAPDAY(2) / 10000
      MO(2) = (MOD(MAPDAY(2),10000)) / 100
      DAY(2) = MOD(MAPDAY(2),100)
      CALL CENDAY(YR(2),MO(2),DAY(2),MAPDAY(2))
      CALL INTCOEF(UNITNM,'POTEMP12','NORTHERN','UPDAT   ',1,0,0,0,
     1            MAPDAY(3),'GET',STAT)
      IF(STAT.NE.0) GOTO 110
      YR(3) = MAPDAY(3) / 10000
      MO(3) = (MOD(MAPDAY(3),10000)) / 100
      DAY(3) = MOD(MAPDAY(3),100)
      CALL CENDAY(YR(3),MO(3),DAY(3),MAPDAY(3))
      CALL INTCOEF(UNITNM,'POTEMP18','NORTHERN','UPDAT   ',1,0,0,0,
     1            MAPDAY(4),'GET',STAT)
      IF(STAT.NE.0) GOTO 110
      YR(4) = MAPDAY(4) / 10000
      MO(4) = (MOD(MAPDAY(4),10000)) / 100
      DAY(4) = MOD(MAPDAY(4),100)
      CALL CENDAY(YR(4),MO(4),DAY(4),MAPDAY(4))
C
C   GET THE HIRS DATA FRAME TIME FROM COMMON FRMINF:HSCTIM AND
C   CONVERT IT FROM CENTURY DAY TO THE ODB TIME FORMAT.
C   USE THE FIRST NON-MISSING SCAN TIME IN THE OVERLAP PORTION
C   (FIRST 9 LINES) OF THE EXTENDED FRAME BUFFER (WR #98).
C
      SCLINE = 0
      DO J = 1, 9
         IF (HSCTIM(1,J).GT.0) THEN
            SCLINE = J
            GO TO 10
         END IF
      END DO
C
   10 CONTINUE
C
C   IF NO SCAN LINE TIME IS AVAILABLE, NO DATA FRAME DATE OR
C   TIME CAN BE CALCULATED. SET THE RETURN CODE TO 77 AND
C   RETURN TO THE CALLING ROUTINE. (WR #98)
C
      IF (SCLINE.EQ.0) THEN
         STATUS = 77
         RETURN
      ELSE
         CALL ODBTIM(HSCTIM(1,SCLINE),HSCTIM(2,SCLINE),FRTIME)
      END IF
C
C   THE POTENTIAL TEMPERATURE MAPS HAVE FORECASTS FOR HOURS
C   00, 06, 12, AND 18.  EACH FORECAST IS FOR SIX HOURS.  DETERMINE
C   THE MAP TIME FRAME OF THE DATA FRAME.  FRTIME(2) CONTAINS THE
C   DAY AND HOUR OF THE DATA FRAME.
C
      FRHOUR = MOD(FRTIME(2),100)
      FRMIN = (FLOAT(FRTIME(3)/100))/60.0
      FRSEC = (FLOAT(MOD(FRTIME(3),100)))/3600.0
      FRHOUR = FRHOUR + FRMIN + FRSEC
C
C   DETERMINE WHICH MAP SHOULD BE CHECKED FIRST, BASED ON THE HOUR
C   OF THE DATA FRAME.   FIND THE MINIMUM DIFFERENCE BETWEEN THE
C   FORECAST HOUR AND THE DATA FRAME HOUR AND USE IT TO DETERMINE THE
C   INDEX INTO THE MAPDAY ARRAY.
C
      DO I=1,4
         IF(FRHOUR.LT.20) THEN
            DELTIME = FRHOUR - FCSTHR(I)
         ELSE
            DELTIME = FRHOUR - 24.0
         ENDIF
         IF(ABS(DELTIME) .LT. ABS(DELMIN)) THEN
            DELMIN = DELTIME
            MPTIME = I
         ENDIF
      ENDDO
C
C   USE THE MINIMUM DELTA TIME.
C
      DELTIME = DELMIN
C
C   ASSIGN THE CENTURY DAY OF THE FIRST NON-MISSING SCAN IN THE
C   EXTENDED DATA BUFFER TO VARIABLE 'DFRDAY'. IF NO SCAN TIME
C   IS AVAILABLE, SET DFRDAY TO A 10 DAYS BEFORE THE MAP DAY.
C
      IF (SCLINE.NE.0) THEN
         DFRDAY = HSCTIM(1,SCLINE)
      ELSE
         DFRDAY = MAPDAY(MPTIME) -10
      END IF
C
C   IF THE DATA FRAME DAY IS EQUAL TO THE MAP DAY, RETURN THE
C   INDEX OF THAT MAP TO THE CALLING ROUTINE.
C
C *****************************************************************
C *  DATA FRAME DAY AND MAP DAY MATCH. RETURN.                    *
C *****************************************************************
C
      IF (DFRDAY .EQ. MAPDAY(MPTIME)) THEN
         INDEX = MPTIME
         GOTO 110
      END IF
C
C ****************************************************************
C * IF ORBITAL DATA FRAME TIMES HAPPEN TO BE BEFORE THE FORECAST *
C * FILE GETS UPDATED, CHOSE THE MOST RECENT FORECAST TIME MAP   *
C * AVAILABLE, IGNORING THE AGE OF THE FILE.                     *
C ****************************************************************
C
      DELDAY = DFRDAY - MAPDAY(MPTIME)
      DELMIN = 30000.0
      IF (DELDAY .GE. 1.0) THEN
         CALL POTTE$(101, STAT)
         DO K=1,4
            DELDAY2 = DFRDAY - MAPDAY(K)
            DELTIME = FRHOUR - FCSTHR(K) + 24*DELDAY2
            IF(ABS(DELTIME).LT.ABS(DELMIN)) THEN
               DELMIN = DELTIME
               MPTIME = K
            ENDIF
         ENDDO
         DELTIME = DELMIN
         INDEX = MPTIME
         GO TO 110
      ENDIF
C
C ****************************************************************
C THIS STATEMENT ADDED BECAUSE THE 24HR (0Z) FORECAST FILE YIELDS
C A MAPDAY VALUE OF 1 GREATER THAN THE DATA FRAME DAY WHEN
C THE DATA FRAME TIME IS GREATER THAN OR EQUAL TO 21.0 HOURS.  THIS
C WILL ALLOW THE DATA TO BE PROCESSED.
C ****************************************************************
C
      IF(DELDAY.EQ.-1.0) THEN
c       CALL POTTE$(102, STAT)
        DFRDAY = DFRDAY + 1
      ENDIF
C
C ****************************************************************
C AGAIN CHECK IF THE MAP DAY OF THE FORECAST MAP MATCHES THE DATA
C FRAME DAY, AND RETURN THAT MAP INDEX TO MAPDVR.  IF THE DATES
C STILL DON'T MATCH, WRITE A WARNING MESSAGE TO THE KIDS FILE STATING
C THAT NO FORECAST MATCH COULD BE FOUND AND RETURN STATUS 77 TO MAPDVR.
C ****************************************************************
C
      IF(DFRDAY.EQ.MAPDAY(MPTIME)) THEN
         INDEX = MPTIME
         GO TO 110
      ELSE
         STATUS = 77
      ENDIF
C
C ****************************************************************
C *             ERROR PROCESSING.                                *
C ****************************************************************
C
      CALL POTTE$(100, STAT)
      IF (STAT.EQ.99) GOTO 120
      GO TO 130
C
C
C ****************************************************************
C   STORE TIME OF THE MAP SELECTED FOR OUTPUT TO THE ODF.
C ****************************************************************
C
  110 CONTINUE
      FCSTYM(1) = YR(INDEX)*100 + MO(INDEX)
      FCSTYM(2) = DAY(INDEX)*100 + FCSTHR(INDEX)
      GO TO 130
C
C RETURN TO THE ERROR LABEL.
C
  120 CONTINUE
      STATUS = 99
  130 CONTINUE
      RETURN
C
      END
C**********************************************************************
      SUBROUTINE POTTE$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- POTTE$
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 3/12/93    PROGRAMMER- FRANK TILLEY (SMSRC)
C   VERSION- 3.0    DATE- 8/04/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             KIDS MESSAGE LOG FILE FOR SUBROUTINE POTTEM.
C             THE ERROR MESSAGE IS ONLY WRITTEN OUT ONCE PER ORBIT.
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS CONVENTION.
C   VERSION- 3.1 ADD MESSAGE CODES 101 AND 102.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     .../KIDS                  O    KUNIT KRAY I/A DOCUMENTATION SYSTEM
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     MSGCOD             INT  I   MESSAGE CODE OF ERROR DETECTED.
C     STAT               INT  O   ERROR STATUS CODE
C                                  = 0 IF NO/NON-FATAL ERROR OCCURRED,
C                                   99 IF FATAL ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     COUNT              INT  COUNTER FOR NUMBER OF TIMES $-ROUTINE
C                             HAS BEEN INVOKED.
C     C4TEMP            CH*4  TEMPORARY FOR WRITING INTO KMSG.
C     C6TEMP            CH*6  TEMPORARY FOR WRITING INTO KMSG.
C     KMSG             CH*100 KIDS MESSAGE BUFFER.
C
C   SUBPROGRAMS CALLED-  KIDS
C
C   EXIT STATES-
C       STAT =  0 INDICATES NO OR NON-FATAL ERROR WAS DETECTED.
C       STAT = 99 INDICATES A FATAL ERROR WAS DETECTED.
C
C***********************************************************************
C
      INTEGER     STAT, MSGCOD
C
      INTEGER     I, COUNT
      CHARACTER*4 C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
C
      DATA COUNT /0/
      SAVE COUNT
C
C   SET THE MAIN MODULE NAME SINCE KIDS SETS THE SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
      COUNT = COUNT + 1
C
C   ONLY WRITE OUT ONE ERROR MESSAGE PER ORBIT.
C
      IF (COUNT .LT. 2) THEN
C
C        CHECK FOR VALID MESSAGE CODES.
C
         IF (MSGCOD .EQ. 100) THEN
            KMSG='* WARNING * No forecast found for orbit       '
            WRITE(C6TEMP,'(I6)') SORB(1)
            KMSG(41:46) = C6TEMP
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
         ELSE IF(MSGCOD.EQ.101) THEN
            KMSG(1:50)='*WARNING* FORECAST FILES MORE THAN 1 DAY OLD!!'
            KMSG(51:100)='WARNING! WARNING! WARNING! WARNING!'
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
            KMSG = ' Check FTP of Forecast Files from CEMSCS!'
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
         ELSE IF(MSGCOD.EQ.102) THEN
            KMSG(1:50)='* Warning * Data frame day increased by '
            KMSG(51:100)='1 to account for a 24 hour forecast.'
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
            KMSG = ' Check FTP of Forecast Files from CEMSCS.'
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
         ELSE
C
C NOT A VALID MSGCOD
C
           STAT=0
           KMSG = 'Invalid message code      '
           WRITE(C4TEMP,'(I4)') MSGCOD
           KMSG(22:25) = C4TEMP
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
         ENDIF
      ENDIF
      RETURN
      END
