      SUBROUTINE GETZERO(IUNIT,ZERONAME,FLIGHT,EDRTIME,ZEROTIME,ISTAT)
C
C SEARCH ASCII ZERO TABLE DATASET FOR DESIRED EDR TIME.
C RETRIEVE ZERO TABLE WITH TIME CLOSEST BUT NOT GREATER
C THAN EDR TIME.  
C
C WRITTEN BY S.KRAMER, CODE 692  05/13/94
C MOD TO READ TIME TAG TO SECONDS LEVEL, SBK 07/12/2006
C
      INTEGER*4 RANGE
C
      CHARACTER LINE*72,ZERONAME*8,CURRNAME*8
      INTEGER*2 EDRTIME(6),ZEROTIME(6),CURRTIME(6)
      INTEGER*4 FLIGHT,ISTAT,ITAB
      LOGICAL*1 LOAD,FORCE
      REAL*8 REALTIME,ZTIME,ETIME
      DATA LOAD/.FALSE./,ICALL/0/,ITAB/0/,FORCE/.FALSE./
C
      INCLUDE 'UNPACK.INC'
C
      FORCE = SYS2(15)
C
C GET DECIMAL YEAR TIME VALUE OF EDR TIME
C
      ETIME = REALTIME(EDRTIME)
C
      ICALL = ICALL + 1
      IF ( ICALL.GT.1.AND.FORCE ) RETURN
      IF ( ICALL.GT.1 ) GOTO 50
C
C READ ZERO OFFSET TABLES
C
      ISTAT = 0
      ICNT = 0
   10 CONTINUE
       READ(IUNIT,'(A72)',END=900) LINE
       ICNT = ICNT + 1
C
C LOOK FOR NEW TABLE DELIMITER
C
       IF ( LINE(1:1).EQ.'#' ) THEN
        ICNT = 0
        ITAB = ITAB + 1
       END IF
C
C READ FORCED ZERO TABLE
C
       IF ( FORCE.AND.ITAB.GT.1 ) THEN
        WRITE(6,*)
        WRITE(6,'(1X,''ZERO TABLE FORCED'')')
        WRITE(6,'(1X,''FIRST TABLE USED'')')
        RETURN
       END IF
C
C GET SPACECRAFT ID
C
       IF ( ICNT.EQ.1.AND.LINE(2:8).EQ.'VOYAGER' ) 
     &  READ(LINE,'(8X,I1)') FLIGHT
C
C READ ZERO TABLE TIME TAG AND NAME
C
       IF ( ICNT.EQ.2 ) THEN
        write(6,*) 'LINE = ', line
        READ(LINE,'(8X,I2,1X,I3,1X,I2,10X,A8)') 
     &   (CURRTIME(I),I=1,3),CURRNAME
       WRITE(6,800) CURRTIME
        ZTIME = REALTIME(CURRTIME)
       END IF
C
   50  CONTINUE
C
C USE THE CLOSEST ZERO TABLE TIME NOT GREATER THAN THE EDR TIME
C
       IF ( ICNT.EQ.2.AND.ZTIME.GT.ETIME.AND..NOT.FORCE ) THEN
        IF ( ITAB.EQ.1 ) THEN
         WRITE(6,*)
         WRITE(6,'(1X,''ALL ZERO TABLE TIMES EXCEED EDR TIME'')')
         WRITE(6,'(1X,''NO ZERO TABLE LOADED'')')
         WRITE(6,'(1X,''PRODUCTION EXECUTION STOPPED'')')
         WRITE(6,*) 
         STOP
        END IF
        RETURN
       END IF
C
       IF ( ICNT.EQ.3 ) CONTINUE
       IF ( ICNT.EQ.4 ) CONTINUE
C
C LOAD INBOARD LFM RANGES
C
       IF ( ICNT.EQ.5 ) READ(LINE,*) RANGE,(IBOFF(J,0),J=1,3)
       IF ( ICNT.EQ.6 ) READ(LINE,*) RANGE,(IBOFF(J,1),J=1,3)
       IF ( ICNT.EQ.7 ) READ(LINE,*) RANGE,(IBOFF(J,2),J=1,3)
       IF ( ICNT.EQ.8 ) READ(LINE,*) RANGE,(IBOFF(J,3),J=1,3)
       IF ( ICNT.EQ.9 ) READ(LINE,*) RANGE,(IBOFF(J,4),J=1,3)
       IF ( ICNT.EQ.10 ) READ(LINE,*) RANGE,(IBOFF(J,5),J=1,3)
       IF ( ICNT.EQ.11 ) READ(LINE,*) RANGE,(IBOFF(J,6),J=1,3)
       IF ( ICNT.EQ.12 ) READ(LINE,*) RANGE,(IBOFF(J,7),J=1,3)
C
C LOAD INBOARD HFM RANGES
C
       IF ( ICNT.EQ.13 ) READ(LINE,*) RANGE,(IBOFF(J,8),J=1,3)
       IF ( ICNT.EQ.14 ) READ(LINE,*) RANGE,(IBOFF(J,9),J=1,3)
C
       IF ( ICNT.EQ.15 ) CONTINUE
       IF ( ICNT.EQ.16 ) CONTINUE
C
C LOAD OUTBOARD LFM RANGES
C
       IF ( ICNT.EQ.17 ) READ(LINE,*) RANGE,(OBOFF(J,0),J=1,3)
       IF ( ICNT.EQ.18 ) READ(LINE,*) RANGE,(OBOFF(J,1),J=1,3)
       IF ( ICNT.EQ.19 ) READ(LINE,*) RANGE,(OBOFF(J,2),J=1,3)
       IF ( ICNT.EQ.20 ) READ(LINE,*) RANGE,(OBOFF(J,3),J=1,3)
       IF ( ICNT.EQ.21 ) READ(LINE,*) RANGE,(OBOFF(J,4),J=1,3)
       IF ( ICNT.EQ.22 ) READ(LINE,*) RANGE,(OBOFF(J,5),J=1,3)
       IF ( ICNT.EQ.23 ) READ(LINE,*) RANGE,(OBOFF(J,6),J=1,3)
       IF ( ICNT.EQ.24 ) READ(LINE,*) RANGE,(OBOFF(J,7),J=1,3)
C
C LOAD OUTBOARD HFM RANGES
C
       IF ( ICNT.EQ.25 ) READ(LINE,*) RANGE,(OBOFF(J,8),J=1,3)
       IF ( ICNT.EQ.26 ) READ(LINE,*) RANGE,(OBOFF(J,9),J=1,3)
C
C SAVE CURRENT ZERO TABLE TIME TAG
C
       IF ( ICNT.EQ.2) THEN
        DO I = 1,6
         ZEROTIME(I) = CURRTIME(I) 
        END DO
        ZERONAME = CURRNAME
C       WRITE(6,1) ZERONAME,ZEROTIME,EDRTIME
       END IF
       LOAD = .TRUE.
C
      GOTO 10
C
  900 CONTINUE
      WRITE(6,*) 
      WRITE(6,*) 'END OF FILE REACHED'
      IF (LOAD) THEN 
       WRITE(6,'( 1X,''LAST ZERO TABLE TAGGED '',
     &  I2,1X,I3,1X,I2)') (ZEROTIME(I),I=1,3)
       ISTAT = 1
      END IF
      IF (.NOT.LOAD) THEN
       WRITE(6,'(1X,''EMPTY ZERO TABLE FILE'')')
       WRITE(6,'(1X,''NO ZERO TABLE LOADED'')')
       WRITE(6,'(1X,''PRODUCTION EXECUTION STOPPED'')')
       WRITE(6,*)
       STOP
      END IF
C
      RETURN
    1 FORMAT(1X,A8,3X,6(1X,I3),3X,6(1X,I3))
  800 FORMAT(1X,I2,1X,I3,3(1X,I2),1X,I3)
      END
