      SUBROUTINE PIKNMC (HOURS, JHOURS, STBDIF,NMCFIL,FCINCR,
     1                         FRCHRS, ERSTAT)
C                                                                      
C********************************************************************  
C                                                                      
C   NAME- PIKNMC                                                       
C                                                                      
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916    
C                                                                      
C   VERSION- 1.0    DATE- 01/20/95   PROGRAMMER- LEAH WILSON (HUGHES STX)
C   VERSION- 2.0    DATE- 07/12/95   PROGRAMMER- ALEX POZDNYAK (SMSRC)  
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK, FDC
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND L*4 TO LOGICAL.
C  
C   FUNCTION- THIS ROUTINE PICKS THE CLOSEST NMC FORECAST FILE TO THE 
C             RETRIEVAL.
C                                                                       
C   CALLING SEQUENCE PARAMETERS-                                        
C     VARIABLE      TYPE    I/O      DEFINITION                         
C     ---------     ----    ---      ----------                         
C     HOURS         INT      I       ARRAY (DIM 5) STORING THE JULIAN   
C                                    HOUR OF EACH NMC FORECAST FILE.    
C     JHOURS        INT      I       JULIAN HOUR OF THE RETRIEVAL.      
C     STBDIF        INT      I       MAXIMUM TIME DIFFERENCE ALLOWED    
C                                    IN SEARCH FOR CLOSEST FORECAST.   
C     NMCFIL        INT      O       FLAG DENOTING WHICH FORECAST FILE
C                                    IS CHOSEN.
C     FCINCR        INT      O       FORECAST INCREMENT EQUAL TO  
C                                    LENGTH OF FORECAST (EITHER 6, 12,
C                                    OR 18 HOURS).
C     FRCHRS        INT      O       JULIAN HOUR OF THE CHOSEN FORECAST.
C     ERSTAT        INT      O       ERROR STATUS CODE.
C                                                                      
C   COMMON AREA PARAMETERS- SPVAL                                      
C                                                                      
C   LOCAL PARAMTERS-                                                   
C     VARIABLE      TYPE      DESCRIPTION                              
C     --------      ----       -----------                              
C     INMC          INT       VARIABLE CONTAINING FILE NUMBER.
C     IFILE1        INT       CORRESPONDING TO THE START FILE NUMBER TO
C                             SEARCH.
C     IFILE2        INT       CORRESPONDING TO THE LAST FILE NUMBER TO
C                             SEARCH.
C     DELTAC        INT       ABSOLUTE VALUE OF JULIAN HOURS.           
C     DELTAT        INT       TIME DIFFERENCE BETWEEN RETRIEVAL & FCST. 
C     DELT2         INT       JULIAN HOUR DIFFERENCE BETWEEN NMC FILES. 
C     DLT2D3        INT       STORED JULIAN HOUR DIFFERENCE USED WHEN   
C                             SEARCHING FOR CLOSEST NMC FILE.           
C     I5CHK         INT       SET TO 0 IF THE FUTURE NMC FILE HAS NOT   
C                             YET BEEN SEARCHED.                        
C     MORE          LOG       SET TO FALSE WHEN ALL NMC FILES HAVE BEEN 
C                             SEARCHED.                                 
C     STATUS        LOG       ERROR STATUS CODE.
C                                                                       
C                                                                       
C   SUBROUTINES CALLED- PIKNM$                                          
C                                                                       
C   FUNCTIONS USED- ABS, IABS                                           
C                                                                       
C   EXIT STATES- ERSTAT = 99: FATAL ERROR - NO DATA IS AVAILABLE 
C                            IN ANY OF NMC FILES.
C                                                                      
C********************************************************************  
C                                                                      
      INCLUDE 'SPVAL' 
C                                                       
      INTEGER   HOURS(5), JHOURS, STBDIF, NMCFIL, FCINCR
      INTEGER   FRCHRS, STATUS, ERSTAT
C                                                      
      INTEGER   DELTAT, DELTAC, DELT2, DLT2D3, INMC, I5CHK
      INTEGER   IFILE1, IFILE2
      LOGICAL   MORE
C                                                                    
C  INITIALIZE VARIABLES.                                             
C     
      ERSTAT = 0    
      NMCFIL = I4SPV                                                  
      FCINCR = I4SPV                                                  
      MORE = .TRUE.
      DELTAT = 999999                                                
      FRCHRS = -1                                                    
      I5CHK = 0                                                      
      IFILE1 = 1                                                     
      IFILE2 = 4                                                     
C
C  USING THE JULIAN HOUR OF EACH NMC FILE, SELECT THE ONE WITH THE   
C  SMALLEST TIME DIFFERENCES.                                        
C                                                                    
      DO 200 WHILE (MORE)                                                  
         IF(IFILE1.EQ.IFILE2) MORE = .FALSE.                          
C                                                                     
         DO 100 INMC = IFILE1, IFILE2
C                                                                    
C  COMPUTE TIME DIFFERENCE IF THE CORRESPONDING NMC FILE IS AVAILABLE.
C                                                                   
             IF(HOURS(INMC).NE.I4SPV) THEN                         
                DELTAC = ABS(JHOURS - HOURS(INMC))                
                DELT2 = JHOURS - HOURS(INMC)                     
                IF (DELTAC .LT. DELTAT) THEN                    
                  DELTAT = DELTAC                              
                  DLT2D3 = DELT2                              
                  NMCFIL = INMC                              
                  FRCHRS = HOURS(INMC)                      
                ENDIF                                      
             ENDIF                                        
  100    CONTINUE                                        
C                                                                    
C  CHECK THAT RETRIEVAL - FORECAST TIME DIFFERENCE IS WITHIN STBDIF  
C  HOURS.  IF NOT, SEARCH 5TH FILE = 18-HOUR "FUTURE" FILE.
C                                                                      
         IF(I5CHK.EQ.0) THEN                                          
            IF(IABS(DLT2D3).GT.STBDIF) THEN                          
               IFILE1 = 5                                           
               IFILE2 = 5                                          
               I5CHK = 1                                          
            ELSE                                                 
               MORE = .FALSE.                                       
            ENDIF                                                   
         ENDIF                                                      
  200 END DO                                                        
C                                                                 
      IF (NMCFIL .EQ. I4SPV) THEN 
         CALL PIKNM$(101,STATUS)
         ERSTAT = 99  
C                                                                  
C  DETERMINE FORECAST INCREMENT BASED ON FILE SELECTED.           
C                                                                
      ELSEIF(NMCFIL.EQ.1.OR.NMCFIL.EQ.3) THEN 
         FCINCR = 6
      ELSEIF(NMCFIL.EQ.2.OR.NMCFIL.EQ.4) THEN 
         FCINCR = 12  
      ELSEIF(NMCFIL.EQ.5) THEN
         FCINCR = 18        
      ENDIF  
C                                                                     
C     RETURN TO CALLING ROUTINE.                                     
C                                                                   
      RETURN                                                       
      END                                                         
C**********************************************************************         
      SUBROUTINE PIKNM$ ( MSGCOD, STAT )                                        
C**********************************************************************         
C   NAME- PIKNM$                                                                
C                                                                               
C   LANGUAGE- VS FORTRAN    TYPE- SUBROUTINE       COMPUTER- CRAY J916
C                                                                               
C   VERSION- 1.0    DATE- 06/24/95   PROGRAMMER- ALEX POZDNYAK (SMSRC)          
C   VERSION- 3.0    DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C                                                                               
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR                 
C             ROUTINE PIKNMC.                                                   
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C
C   FILES-
C     DSN                    I/O   UNIT    FUNCTIONAL DESCRIPTION
C     ---                    ---  ------   -----------------------
C     ...KIDS                 O    KUNIT   KRAY INTERACTIVE DOCUMENTATION
C                                                                               
C   CALLING SEQUENCE PARAMETERS-                                                
C                                                                               
C     ARGUMENT               TYPE  I/O  FUNCTION                                
C     --------               ----  ---  --------                                
C     MSGCOD                  I*4   I   MESSAGE CODE USED TO                    
C                                       IDENTIFY THE ERROR.                     
C
C     STAT                    I*4   O   STATUS CODE IF AN ERROR                 
C                                       OCCURS DURING I/O.                      
C                                                                               
C                                                                               
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     I            INT       IMPLIED DO INDEX.                                  
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBROUTINES CALLED- KIDS
C                                                                               
C**********************************************************************         
C                                                                               
      INTEGER   MSGCOD, STAT                                                  
C                                                                               
      INTEGER   I                                                             
      CHARACTER*4   C4TEMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
      STAT = 0                                                                  
C                                                                               
C     CHECK THE MSGCOD TO DETERMINE WHICH MESSAGE TO PUT INTO THE KIDS FILE.    
C
      IF(MSGCOD .EQ. 101) THEN                                              
        KMSG=' * FATAL ERROR * No data available in any NMC file'
        STAT = 99
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C 
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END
