C Copyright(c) 1997, Space Science and Engineering Center, UW-Madison
C Refer to "McIDAS Software Acquisition and Distribution Policies"
C in the file  mcidas/data/license.txt

C *** $Id: rnvblk.f,v 1.9 1997/10/10 20:18:45 dglo Exp $ ***

C $ RNVBLK
C $ THIS ROUTINE RETURNS THE PAGE OF NAVIGATION INFORMATION SPECIFIED
C $ BY SATYD AND HHMMSS FROM THE SYSTEM NAV FILE SPECIFIED BY FILNUM.
C $ IF AN EXACT MATCH IS NOT FOUND, THE MOST RECENT BLOCK OF THE SAME
C $ DAY IS RETURNED.
C $
C $ INPUT:
C $   FILNUM | MASTER NAVIGATION FILE NUMBER
C $   SATYD  | SATELLITE-YEAR-DAY OF DATA
C $   HHMMSS | TIME OF DATA
C $ OUTPUT:
C $   HHMMSS | TIME OF DATA RETURNED (FROM DIRECTORY ENTRY)
C $   NAVBLK | DATA
C $   STATUS | RESULT OF READ OPERATION
C $            -1 => NO NAV FOR THAT DAY IN FILE
C $             1 => EXACT MATCH OF SATYD AND HHMMSS FOUND
C $             2 => DATA RETURNED IS NOT THAT EXACTLY ASKED FOR

      SUBROUTINE RNVBLK(FILNUM, SATYD, HHMMSS, NAVBLK, STATUS)
      IMPLICIT NONE
      INTEGER FILNUM
      INTEGER SATYD
      INTEGER HHMMSS
      INTEGER NAVBLK(*)
      INTEGER STATUS

      ! symbolic constants & shared data

      INCLUDE 'hex80.inc'
      INCLUDE 'syscon.inc'
      INCLUDE 'sysnav.inc'

      INTEGER COMFIL
      INTEGER COMSYD
      INTEGER DIRHMS
      INTEGER DATHMS
      INTEGER LOCALE
      INTEGER DRCTRY(MAXENT,ENTSIZ)
      INTEGER COMPAG(PGSIZE)
      LOGICAL DFLAG
      LOGICAL PFLAG
      COMMON / SYSNAV / COMFIL, COMSYD, DIRHMS, DATHMS, LOCALE, DRCTRY,
     &                                          COMPAG, DFLAG , PFLAG

      ! local variables

      CHARACTER*12 NAVFIL
      INTEGER      DIFF
      INTEGER      LOOP
      INTEGER      PLACE

C----------------------------- RNVBLK -----------------------------

C ASSEMBLE FILE NAME
      CALL NVNAME(FILNUM, NAVFIL)

C DETERMINE IF THE SAME PAGE OF DATA IS WANTED AS ON THE LAST CALL
      IF (PFLAG.AND.FILNUM.EQ.COMFIL.AND.SATYD.EQ.COMSYD.AND.HHMMSS.EQ.
     &                                                     DIRHMS) THEN
        CALL MOVW(PGSIZE,COMPAG,NAVBLK)
        STATUS = 1
        RETURN
      ENDIF

C READ THE DIRECTORY
      IF (.NOT.DFLAG.OR.COMFIL.NE.FILNUM) THEN
        CALL LWI(NAVFIL, DIRLOC, DIRSIZ, DRCTRY)
        call swbyt4( DRCTRY, DIRSIZ )
        DFLAG  = .TRUE.
      ENDIF
      STATUS = -1

C SEARCH THE DIRECTORY TO SEE IF THE DATA IS HERE
      DIFF = 240000
      DO 1 LOOP = 1, MAXENT
        IF (DRCTRY(LOOP,PRIKEY).EQ.SATYD) THEN

C EXACT MATCH
          IF (DRCTRY(LOOP,SECKEY).EQ.HHMMSS) THEN
            STATUS = 1
            PLACE = LOOP
            GOTO 2
          ENDIF

C NOT EXACT MATCH, SO SEE HOW GOOD IT IS
          IF (DRCTRY(LOOP,SECKEY).LT.HHMMSS.AND.(HHMMSS
     &                              -DRCTRY(LOOP,SECKEY)).LT.DIFF) THEN
            PLACE = LOOP
            DIFF = HHMMSS-DRCTRY(LOOP,SECKEY)
            STATUS = 2
          ENDIF
        ENDIF
 1    CONTINUE
      IF (STATUS.NE.2) RETURN

C PUT INFO ON THIS DATA INTO COMMON
 2    IF (LOCALE.EQ.PLACE.AND.COMFIL.EQ.FILNUM.AND.PFLAG) THEN
        DATHMS = HHMMSS
        CALL MOVW(PGSIZE,COMPAG,NAVBLK)
        RETURN
      ENDIF
      COMFIL = FILNUM
      COMSYD = SATYD
      DIRHMS = DRCTRY(PLACE,SECKEY)
      LOCALE = PLACE
      PFLAG  = .TRUE.

C READ THE BLOCK FROM THE NAV FILE
      CALL LWI(NAVFIL, ONALOC+(PLACE-1)*PGSIZE, PGSIZE, NAVBLK)
      call swbyt4( NAVBLK, PGSIZE )
      CALL MOVW(PGSIZE,NAVBLK,COMPAG)

      RETURN
      END

      BLOCK DATA NAVBLK

      INCLUDE 'hex80.inc'
      INCLUDE 'syscon.inc'
      INCLUDE 'sysnav.inc'

      INTEGER COMFIL
      INTEGER COMSYD
      INTEGER DIRHMS
      INTEGER DATHMS
      INTEGER LOCALE
      INTEGER DRCTRY(MAXENT,ENTSIZ)
      INTEGER COMPAG(PGSIZE)
      LOGICAL DFLAG
      LOGICAL PFLAG
      COMMON / SYSNAV / COMFIL, COMSYD, DIRHMS, DATHMS, LOCALE, DRCTRY,
     &                                          COMPAG, DFLAG , PFLAG

      DATA COMFIL / -1 /
      DATA COMSYD / -1 /
      DATA DIRHMS / -1 /
      DATA DATHMS / -1 /
      DATA LOCALE / -1 /
      DATA DFLAG  / .FALSE. /
      DATA PFLAG  / .FALSE. /

      END
