      PROGRAM VOYPROD
C
C EDR UNPACKING PROGRAM WRITTEN FOR EXECUTION UNDER VMS SYSTEM ENVIRONMENT.
C
C 1.  EDR INPUT DATA IS WRITTEN FOR THE IBM 360 ARCHITECTURE.
C 2.  NAVIGATION INPUT DATA IS EXPECTED TO BE IN VAX G_FLOATING FORMAT.
C 3.  SOURCE MUST BE COMPILED WITH DEC FORTRAN VERSION 6.0 OR HIGHER.
C 4.  SOURCE MUST BE COMPILED WITH THE /ALIGN QUALIFIER ON ALPHA VMS.
C 5.  USE THE /FLOAT=IEEE FLAG TO PRODUCE SUMMARY OUTPUT DATA IN IEEE FORMAT.
C     (IEEE ON VMS SYSTEMS IS LITTLE ENDIAN).
C
C ORIGINAL CODE WRITTEN BY SANDY KRAMER, HUGHES-STX, CODE 692, NASA GSFC.
C
C FIRST RELEASE 12/01/93    VIM-5 CRUISE MODE ONLY AVAILABLE.
C SECOND RELEASE 05/26/94   GS-X ENCOUNTER MODE ADDED.
C THIRD RELEASE 10/21/94    CR-5 CRUISE MODE ADDED (FULL WORDS ONLY).
C FOURTH RELEASE 07/09/95   PLANETARY COORDINATE TRANSFORMATIONS ADDED. 
C FIFTH RELEASE 10/11/95    CR-1 CRUISE MODE ADDED.
C SIXTH RELEASE 03/21/96    CR-6 CRUISE MODE ADDED (FULL WORDS ONLY).
C SEVENTH RELEASE 03/22/96  CR-2 CRUISE MODE ADDED.
C EIGHTH RELEASE 03/25/96   CR-3 CRUISE MODE ADDED.
C NINTH RELEASE 03/26/96    CR-4 CRUISE MODE ADDED.
C TENTH RELEASE 05/09/96    CR-5 DELTA MODULATION ADDED.
C ELEVENTH RELEASE 01/30/2006    VIM-5 COMPOSITE MAG ADDED
C TWELTH RELEASE 04/24/2006      DUMP CALIBRATION GAMMAS
C THIRTEENTH RELEASE 07/12/2006  READ SECONDS LEVEL ZERO DATA
C 
C Notes:
C     CR-6 delta modulation not properly implemented by JPL.
C     Process CR-6 data using full words only.
C
      CHARACTER TFLAG*4,SYSTIME(2)*8,ZERONAME*8,SENSDSN*8,
     &          EDRDSN(10)*8,SEDRDSN(6)*8,SYSDATE*9,DSN*50,
     &          OLDZERO*8,FILENAME*8
C
      INTEGER*2 RECLEN,EDRTIME(6),SCETIME(6),EERTIME(6),SERTIME(6),
     &          OLDTIME(6),STIME(6),ETIME(6),ZEROTIME(6)
C
      INTEGER*4 YEAR,MODE,SCMODE,OLDMODE,ZFLIGHT,MODECNT(-1:13)/15*0/
C
      LOGICAL*1 REC(11280),FIRST,LAST
C
      REAL*4 GAMMA(3,2400),GAMMA2(3,2400)
C
      REAL*8 REALTIME,START,STOP,LEAP,DELTA,SCTDIFF,
     &       DIST,OLDDIST,DISTLM,CURRTIME
C
      INCLUDE 'UNPACK.INC'
C
C GET CURRENT DATE AND TIME
C
      CALL DATE(SYSDATE)
      CALL TIME(SYSTIME(1))
C
C CREATE BIT FLIP TRANSLATION TABLE FOR LOGICAL*1 WORDS.  
C ASSIGN FLIP(0:255) THE BIT FLIP INVERSE OF VALUES 0 TO 255.
C
      DO J=0,255
       DO I=0,7
        CALL MOVBIT(J, I, 1, FLIP(J), 7-I)
       END DO
      END DO
C
C OPEN PROGRAM LOG FILE
C
      WRITE(6,*) 'ENTER LOG FILE NAME'
      READ(5,'(A)') DSN
      OPEN(68,FILE=DSN,STATUS='NEW',FORM='FORMATTED',
     &     CARRIAGECONTROL='LIST')
      WRITE(68,*) 'LOG FILE NAME: ',DSN
C
C OPEN NAVIGATION FILE FOR READING.  EXPECT UNFORMATTED, VAXG FLOATING
C POINT DATA.
C
      WRITE(6,*) 'ENTER NAVIGATION FILE NAME'
      READ(5,'(A)') DSN
      OPEN(41,FILE=DSN,STATUS='OLD',CONVERT='VAXG',
     &     FORM='UNFORMATTED',RECORDTYPE='VARIABLE',READONLY)
      WRITE(68,*) 'NAVIGATION FILE NAME: ',DSN
      SEDRDSN(1) = FILENAME(DSN)
C
C OPEN POINTING VECTOR FILE FOR READING
C
      WRITE(6,*) 'ENTER POINTING VECTOR FILE NAME'
      READ(5,'(A)') DSN
      OPEN(40,FILE=DSN,STATUS='OLD',FORM='FORMATTED',READONLY)
      WRITE(68,*) 'POINTING VECTOR FILE NAME: ',DSN
C
C GET MAG ZERO OFFSETS FROM TABLE FILE
C
      WRITE(6,*) 'ENTER ZEROES FILE NAME'
      READ(5,'(A)') DSN
      OPEN(50,FILE=DSN,STATUS='OLD',FORM='FORMATTED',READONLY)
      WRITE(68,*) 'ZEROES FILE NAME: ',DSN
C
C GET MAG SENSITIVITIES
C
      WRITE(6,*) 'ENTER SENSITIVITIES FILE NAME'
      READ(5,'(A)') DSN
      OPEN(51,FILE=DSN,STATUS='OLD',FORM='FORMATTED',READONLY)
      WRITE(68,*) 'SENSITIVITIES FILE NAME: ',DSN
      CALL MAG_SENS(SENSDSN)
      CLOSE(51)
C
C GET SENSOR ALIGNMENT MATRICES
C
      WRITE(6,*) 'ENTER SENSOR ALIGNMENT FILE NAME'
      READ(5,'(A)') DSN
      OPEN(52,FILE=DSN,STATUS='OLD',FORM='FORMATTED',READONLY)
      WRITE(68,*) 'SENSOR ALIGNMENT FILE NAME: ',DSN
      CALL SENSOR_DAT()
      CLOSE(52)
C
C GET BOOM ALIGNMENT MATRICES
C
      WRITE(6,*) 'ENTER BOOM ALIGNMENT FILE NAME'
      READ(5,'(A)') DSN
      OPEN(53,FILE=DSN,STATUS='OLD',FORM='FORMATTED',READONLY)
      WRITE(68,*) 'BOOM ALIGNMENT FILE NAME: ',DSN
      CALL BOOM_DAT()
      CLOSE(53)
C
C OPEN BINARY EDR INPUT DATASET
C
      WRITE(6,*) 'ENTER EDR FILE NAME'
      READ(5,'(A)') DSN
      OPEN(54,FILE=DSN,STATUS='OLD',FORM='FORMATTED',
     &     RECORDTYPE='VARIABLE',RECL=8191,READONLY)
      WRITE(68,*) 'EDR FILE NAME: ',DSN
      EDRDSN(1) = FILENAME(DSN)
C
C OPEN OUTPUT DATA SET
C
      WRITE(6,*) 'ENTER OUTPUT DATA FILE NAME'
      READ(5,'(A)') DSN
      OPEN(66,FILE=DSN,STATUS='NEW',
     &     FORM='UNFORMATTED',RECORDTYPE='VARIABLE',RECL=8191)
      WRITE(68,*) 'OUTPUT FILE NAME: ',DSN
C
C READ SYSTEM FLAGS
C
      CALL GETFLAGS()
      CALL FLAGS()
C
C SELECT START AND STOP TIMES
C
      CALL TBOUND(START,STOP)
C 
      WRITE(6,*)
      WRITE(6,*) 'SELECT TIME TYPE (SCET,ERTS,ERTE)'
      READ(5,'(A)') TFLAG
C      WRITE(6,*) 'SELECT LIMITS FOR ONE WAY LIGHT TIME DEVIATION'
      WRITE(6,*) 'SELECT LIMIT FOR MAXIMUM TIME PROGRESSION (SECONDS)'
      READ(5,*) DISTLM
      WRITE(6,*) 
     & 'RANGE BIT OVERRIDE ( -1 DISABLE, 0 - 7 TO FORCE VALUE )'
      READ(5,*) RNGSET
      WRITE(6,*) 'INBOARD LFM STARTING RANGE VALUE (0,7)'
      READ(5,*) IBRNG(0)
      WRITE(6,*) 'OUTBOARD LFM STARTING RANGE VALUE (0,7)'
      READ(5,*) OBRNG(0)
      WRITE(6,*) 'MODE BIT OVERRIDE ( -1 DISABLE, 0 MANUAL, 1 AUTO )'
      READ(5,*) MODSET
      WRITE(6,*) 
     & 'INBOARD LFM STARTING RANGE MODE VALUE (T-AUTO,F-MANUAL)'
      READ(5,'(L1)') IBMODE(0)
      WRITE(6,*) 
     & 'OUTBOARD LFM STARTING RANGE MODE VALUE (T-AUTO,F-MANUAL)'
      READ(5,'(L1)') OBMODE(0)
      WRITE(6,*) 
     & 'PRIME LFM START VALUE (T-INBOARD PRIME, F-OUTBOARD PRIME)'
      READ(5,'(L1)') PRIME(0)
      WRITE(6,*) 'FILTER PARAMETER (REJECTION LEVEL)'
      READ(5,*) SIGK
      WRITE(6,*) 'FILTER PARAMETER (# PASSES)'
      READ(5,*) NPASS
C
      WRITE(68,*)
      WRITE(68,'(1X,''TIME TYPE: '',A4)') TFLAG
C      WRITE(68,'(1X,''ONE WAY LIGHT TIME DEVIATION:  '',F7.3)') DISTLM
      WRITE(68,'(1X,''MAXIMUM TIME PROGRESSION (SECONDS):  '',F8.0)') DISTLM
      WRITE(68,'(1X,''RANGE VALUE: '',I2)') RNGSET
      WRITE(68,'(1X,''IB RANGE START PRIMER: '',I2)') IBRNG(0)
      WRITE(68,'(1X,''OB RANGE START PRIMER: '',I2)') OBRNG(0)
      WRITE(68,'(1X,''MODE VALUE: '',I2)') MODSET
      WRITE(68,'(1X,''IB MODE START PRIMER: '',L1)') IBMODE(0)
      WRITE(68,'(1X,''OB MODE START PRIMER: '',L1)') OBMODE(0)
      WRITE(68,'(1X,''INBOARD MAG PRIME SWITCH: '',L1)') PRIME(0)
      WRITE(68,'(1X,''FILTER LIMIT: '',F3.1)') SIGK
      WRITE(68,'(1X,''FILTER PASSES: '',I2)') NPASS
C
C OUTPUT MAG SENSITIVITIES TO THE LOG FILE
C 
      WRITE(68,*)
      WRITE(68,'(1X,''INBOARD LFM MAG SENSITIVITIES:'')')
      DO I = 0,7
       WRITE(68,'(4X,I1,3(3X,E10.4))') I+1,(IBSENS(J,I),J=1,3)
      END DO
      WRITE(68,'(1X,''OUTBOARD LFM MAG SENSITIVITIES:'')')
      DO I = 0,7
       WRITE(68,'(4X,I1,3(3X,E10.4))') I+1,(OBSENS(J,I),J=1,3)
      END DO
C
C OUTPUT MAG SENSOR ALIGNMENT MATRICES
C
      WRITE(68,*)
      WRITE(68,*) 'INBOARD LFM SENSOR MATRIX (FLIP=0):'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VSLIB(I,J),J=1,3),I=1,3)
C     WRITE(68,*)
      WRITE(68,*) 'OUTBOARD LFM SENSOR MATRIX (FLIP=0):'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VSLOB(I,J),J=1,3),I=1,3)
C     WRITE(68,*)
      WRITE(68,*) 'INBOARD LFM SENSOR MATRIX (FLIP=180):'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VSLIB(I,J),J=1,3),I=1,3)
C     WRITE(68,*)
      WRITE(68,*) 'OUTBOARD LFM SENSOR MATRIX (FLIP=180):'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VSLOB(I,J),J=1,3),I=1,3)
C
C OUTPUT MAG BOOM ALIGNMENT MATRICES
C
      WRITE(68,*) 'INBOARD LFM BOOM ALIGNMENT MATRIX:'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VBLIB(I,J),J=1,3),I=1,3)
C     WRITE(68,*)
      WRITE(68,*) 'OUTBOARD LFM BOOM ALIGNMENT MATRIX:'
      WRITE(68,*)
      WRITE(68,'(3(3(1X,E10.4)/))') ((VBLOB(I,J),J=1,3),I=1,3)
C
C OUTPUT RECONSTRUCTED COUNTS IF FLAGGED
C
C REMOVED TO ALLOW VMS DCL REDIRECTION OF FORTRAN UNIT 80
C OUTPUT TO DESIRED DISK/DIRECTORY/FILENAME.  SBK 05/18/2003
C
C      IF ( SYS2(31) ) THEN
C       OPEN(80,FILE='COUNTS.DAT',STATUS='NEW',FORM='FORMATTED',
C     &      RECORDTYPE='VARIABLE',RECL=8191)
C      END IF
C
C MINIMUM RATIO OF DIFFERENCE BETWEEN TIME TAGS OF TWO ADJACENT MAG RECORDS
C AND THE PERIOD OF THE FIRST MAG RECORD IN THAT DIFFERENCE
C
      TIMECHK = 0.99
C
C INITIALIZE MAIN LOOP
C
      ITDFLG = 0
      RATIO = 1
      IBADTIM = 0
      FIRST = .TRUE.
      LAST = .FALSE.
      BAD = 999.0
      NCNT = 0
      GOTO 10
C
C INPUT ERROR CHECK
C
    5 CONTINUE
       WRITE(6,*) '*VOYPROD*  EDR READ ERROR!'
   10 CONTINUE
C
C USE DEC FORTRAN 'Q' FORMAT SPECIFIER TO RETURN RECORD LENGTH FROM
C UNFORMATTED EDR FILE OPENED AS FORMATTED FILE.
C
       READ(54,'(Q,<RECLEN>A1)',END=100,ERR=5) 
     &  RECLEN,(REC(I),I=1,RECLEN)
       NCNT = NCNT + 1
C
C UNPACK HEADER BLOCK
C
       CALL UNHEAD(REC)
C
C CHECK HEADER DATA
C 
       CALL HEADCHK(RECLEN,NCNT,ISTAT)
       IF ( ISTAT.NE.0 .AND. .NOT.SYS2(29) ) GOTO 10
C
C GET SUMMARY TELEMETRY MODE IDENTIFIER
C
       SCMODE = MODE(NCNT) 
C
C ASSIGN SELECTED TIME TYPE TO EDRTIME 
C
       CALL GETTIME(TFLAG,SERTIME,EERTIME,SCETIME,EDRTIME)
       CALL DISPLAY(RECLEN,NCNT,TFLAG,EDRTIME)
C
C FILL HEADER BLOCK OF SUMMARY RECORD
C
       CALL HEADFILL(TFLAG,EDRTIME,SCMODE)
C
C WRITE HDR1 SUMMARY RECORD
C
       IF ( FIRST .AND. ( .NOT.SYS2(26) ) )
     &  CALL HDR1OUT(66,EDRTIME,EDRDSN,SEDRDSN,ZERONAME,SENSDSN)
       FIRST = .FALSE.
C
C UNPACK DECOMMUTATION MAP
C
       IF ( RECID.EQ.15 .AND. SYS2(3) ) THEN
        MODECNT(SCMODE) = MODECNT(SCMODE) + 1
        CALL UNPACKDEC(REC)
        GOTO 10
C
C UNPACK ENGINEERING RECORD
C
       ELSE IF ( RECID.EQ.11 .AND. SYS2(3) ) THEN
        MODECNT(SCMODE) = MODECNT(SCMODE) + 1
        CALL UNPACKENG(REC)
        GOTO 10
       END IF
C
C CALCULATE TIME DIFFERENCE BETWEEN SEQUENTIAL MAG RECORDS AND
C REMOVE OUT OF BOUND TIME TAGS
C
       IF ( RECID.EQ.4 ) THEN
C
        CURRTIME = REALTIME(EDRTIME)
C
C CHECK ALL MAG RECORDS FOLLOWING THE FIRST
C
        IF ( ITDFLG.EQ.1 ) THEN
C
C COMPUTE TIME DIFFERENCE IN SECONDS
C
         DELTA = (CURRTIME-REALTIME(OLDTIME))*
     &            LEAP(OLDTIME(1))*24.0D0*3600.0D0  
C
C COMPARE DELTA TO PERIOD OF PREVIOUS MAG RECORD
C
         RATIO = REAL(DELTA/SCTDIFF(OLDMODE))
C
C REMOVE MAG RECORDS WITH RETROGRADE TIME TAGS
C
         IF ( RATIO.LT.TIMECHK ) THEN
          IBADTIM = IBADTIM + 1
          WRITE(6,'(1X,''*VOYPROD*  BAD RETROGRADE TIME AT: '',
     &          I2,1X,I3.3,3(1X,I2.2),1X,I3.3,
     &          ''   DELTA t = '',E10.4)') 
     &     EDRTIME,DELTA
          IF ( .NOT.SYS2(14) ) GOTO 10
         END IF
C 
C CHECK TIME TAGS FOR EXCESSIVE FORWARD ADVANCE
C
         IF ( DELTA.GT.DISTLM ) THEN 
          IBADTIM = IBADTIM + 1
          WRITE(6,'(1X,''*VOYPROD*  EXCESSIVE TIME ADVANCE AT:  '',
     &          I2,1X,I3.3,3(1X,I2.2),1X,I3.3,
     &          ''   DELTA t = '',E10.4)') 
     &     EDRTIME,DELTA
          IF ( .NOT.SYS2(16) ) GOTO 10
         END IF
C
        ELSE
C
C FLAG FIRST MAG TIME LOAD
C
         ITDFLG = 1
C
C GET ZERO TABLE FOR FIRST MAG TIME 
C
         CALL GETZERO(50,ZERONAME,ZFLIGHT,EDRTIME,ZEROTIME,ISTAT)
         OLDZERO = ZERONAME
         WRITE(68,*)
         WRITE(68,*) 'ZERO TABLE INFO'
         WRITE(68,'(1X,''EDR TIME:         '',
     &    I2,1X,I3.3,3(1X,I2.2),1X,I3.3)') EDRTIME
         WRITE(68,'(1X,''ZERO TABLE TIME:  '',
     &    I2,1X,I3.3,3(1X,I2.2),1X,I3.3)') ZEROTIME
         WRITE(68,'(1X,''ZERO TABLE NAME:  '',A8)') ZERONAME
         WRITE(68,'(1X,''FLIGHT:           '',I2.2)') ZFLIGHT
         WRITE(68,'(1X,''INBOARD LFM ZERO OFFSETS:'')') 
         DO I = 0,7
          WRITE(68,'(4X,I1,3(3X,I4))') I+1,(IBOFF(J,I),J=1,3)
         END DO
         WRITE(68,'(1X,''OUTBOARD LFM ZERO OFFSETS:'')')
         DO I = 0,7
          WRITE(68,'(4X,I1,3(3X,I4))') I+1,(OBOFF(J,I),J=1,3)
         END DO
C
        END IF
C
C SAVE CURRENT MAG EDR TIME FOR COMPARISON WITH THE NEXT MAG EDR TIME
C
        DO JJ = 1,6
         OLDTIME(JJ) = EDRTIME(JJ)
        END DO
C
C SAVE CURRENT MAG EDR TELEMETRY MODE FOR COMPARISON WITH THE NEXT 
C MAG EDR TELEMETRY MODE 
C
        OLDMODE = SCMODE
C
C CHECK DATA AGAINST START AND STOP TIMES
C
        IF ( CURRTIME.LT.START ) GOTO 10
C
C CHECK ALL DATA TO EXTRACT ANY IMBEDDED PLAYBACK RECORDS
C
        IF ( CURRTIME.GE.STOP ) THEN
         IF ( SYS2(14) ) THEN
          GOTO 10
         ELSE 
          WRITE(6,*) '*VOYPROD*  STOP TIME REACHED, PROCESSING HALTED.'
          WRITE(6,*) '*VOYPROD*  REALTIME(EDRTIME) = ',CURRTIME
          WRITE(6,*) '*VOYPROD*  STOP TIME = ',STOP
          GOTO 100
         END IF
        END IF
C
C END MAG RECORD TIME CHECK
C
       END IF
C
C PROCESS MAG RECORDS FOR SELECTED TIME INTERVAL
C
       IF ( RECID.EQ.4.AND.SCID.LE.1 ) THEN
C
C CALL UNPACKING PROCEDURE APPROPRIATE TO MODE AND RECORD TYPE
C
        MODECNT(SCMODE) = MODECNT(SCMODE) + 1
        IF ( SCMODE.EQ.13 ) THEN
         CALL UNPACKVIM5(REC)
        ELSE IF ( SCMODE.EQ.6 ) THEN
         CALL UNPACKCR6(REC)
        ELSE IF ( SCMODE.EQ.5 ) THEN
         CALL UNPACKCR5(REC)
        ELSE IF ( SCMODE.EQ.4 ) THEN
         CALL UNPACKCR4(REC)
        ELSE IF ( SCMODE.EQ.3 ) THEN
         CALL UNPACKCR3(REC)
        ELSE IF ( SCMODE.EQ.2 ) THEN
         CALL UNPACKCR2(REC)
        ELSE IF ( SCMODE.EQ.1 ) THEN
         CALL UNPACKCR1(REC)
        ELSE IF ( SCMODE.EQ.0 ) THEN
         CALL UNPACKGS3(REC)
        ELSE 
         GOTO 10
        END IF
C
C GET ZERO OFFSET FROM TABLE FILE
C
        CALL GETZERO(50,ZERONAME,ZFLIGHT,EDRTIME,ZEROTIME,ISTAT)
C
C OUTPUT ZERO TABLE INFO IF NEW TABLE
C
        IF ( OLDZERO.NE.ZERONAME ) THEN
         WRITE(68,*)
         WRITE(68,*) 'ZERO TABLE INFO'
         WRITE(68,'(1X,''EDR TIME:         '',
     &    I2,1X,I3.3,3(1X,I2.2),1X,I3.3)') EDRTIME
         WRITE(68,'(1X,''ZERO TABLE TIME:  '',
     &    I2,1X,I3.3,3(1X,I2.2),1X,I3.3)') ZEROTIME
         WRITE(68,'(1X,''ZERO TABLE NAME:  '',A8)') ZERONAME
         WRITE(68,'(1X,''FLIGHT:           '',I2.2)') ZFLIGHT
         WRITE(68,'(1X,''INBOARD LFM ZERO OFFSETS:'')') 
         DO I = 0,7
          WRITE(68,'(4X,I1,3(3X,I4))') I+1,(IBOFF(J,I),J=1,3)
         END DO
         WRITE(68,'(1X,''OUTBOARD LFM ZERO OFFSETS:'')')
         DO I = 0,7
          WRITE(68,'(4X,I1,3(3X,I4))') I+1,(OBOFF(J,I),J=1,3)
         END DO
         OLDZERO = ZERONAME
        END IF
C
C CONVERT COUNTS TO GAMMAS AND PERFORM NECESSARY ROTATIONS
C
        IF ( SCMODE.EQ.13 ) THEN
          CALL VIM5LFM(GAMMA,GAMMA2,BAD,EDRTIME) 
        ELSE IF ( SCMODE.EQ.6 ) THEN
          CALL CR6LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.5 ) THEN
          CALL CR5LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.4 ) THEN
          CALL CR4LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.3 ) THEN
          CALL CR3LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.2 ) THEN
          CALL CR2LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.1 ) THEN
          CALL CR1LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        ELSE IF ( SCMODE.EQ.0 ) THEN
          CALL GS3LFM(GAMMA,GAMMA2,BAD,EDRTIME)
        END IF
C
C BUFFER AND FILTER DATA WITH POPULATION FILTER
C
        IF ( SYS2(12) ) THEN
         IF ( SCMODE.EQ.13 ) THEN 
          CALL QUE(GAMMA,400,GAMMA2,8,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.6 ) THEN 
          CALL QUE(GAMMA,750,GAMMA2,375,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.5 ) THEN 
          CALL QUE(GAMMA,2400,GAMMA2,600,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.4 ) THEN 
          CALL QUE(GAMMA,640,GAMMA2,80,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.3 ) THEN 
          CALL QUE(GAMMA,400,GAMMA2,100,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.2 ) THEN 
          CALL QUE(GAMMA,400,GAMMA2,200,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.1 ) THEN 
          CALL QUE(GAMMA,800,GAMMA2,800,LAST,SIGK,NPASS,BAD)
         ELSE IF ( SCMODE.EQ.0 ) THEN 
          CALL QUE(GAMMA,800,GAMMA2,400,LAST,SIGK,NPASS,BAD)
         END IF
        ELSE
         IF ( SCMODE.EQ.13 ) THEN
          CALL VIM5AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.6 ) THEN
          CALL CR6AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.5 ) THEN
          CALL CR5AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.4 ) THEN
          CALL CR4AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.3 ) THEN
          CALL CR3AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.2 ) THEN
          CALL CR2AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.1 ) THEN
          CALL CR1AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         ELSE IF ( SCMODE.EQ.0 ) THEN
          CALL GS3AVE(GAMMA,GAMMA2,BAD,EDRTIME)
         END IF
        END IF
C
C END MAG RECORD PROCESSING
C
       END IF
C
      GOTO 10
  100 CONTINUE
      WRITE(6,*)
      WRITE(6,'(1X,''NUMBER OF BAD RECORD TIMES: '',I5)') 
     & IBADTIM
      WRITE(6,*)
C
C PROCESS MAG RECORDS REMAINING IN ROBERT'S FILTER BUFFER
C
      LAST = .TRUE.
      IF ( SYS2(12) ) THEN
       IF ( SCMODE.EQ.13 ) THEN
        CALL QUE(GAMMA,400,GAMMA2,8,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.6 ) THEN 
        CALL QUE(GAMMA,750,GAMMA2,375,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.5 ) THEN 
        CALL QUE(GAMMA,2400,GAMMA2,600,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.4 ) THEN 
        CALL QUE(GAMMA,640,GAMMA2,80,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.3 ) THEN 
        CALL QUE(GAMMA,400,GAMMA2,100,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.2 ) THEN 
        CALL QUE(GAMMA,400,GAMMA2,200,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.1 ) THEN 
        CALL QUE(GAMMA,800,GAMMA2,800,LAST,SIGK,NPASS,BAD)
       ELSE IF ( SCMODE.EQ.0 ) THEN 
        CALL QUE(GAMMA,800,GAMMA2,400,LAST,SIGK,NPASS,BAD)
       END IF
      END IF
C
C WRITE HDR1 SUMMARY RECORD
C
      IF ( .NOT.SYS2(26) ) 
     &  CALL HDR1OUT(66,EDRTIME,EDRDSN,SEDRDSN,ZERONAME,SENSDSN)
C
      CLOSE(40)
      CLOSE(41)
      CLOSE(50)
      CLOSE(54)
      CLOSE(66)
C
      WRITE(68,*)
      WRITE(68,*)
      WRITE(68,*) '***EDR PROCESSING SUMMARY***'
      WRITE(68,*)
      WRITE(68,800) 
      WRITE(68,*)
      WRITE(68,801) MODECNT(8),(MODECNT(I),I=0,6),MODECNT(9),
     &              MODECNT(13),MODECNT(-1)
      CALL TIME(SYSTIME(2))
      WRITE(68,*)
      WRITE(68,'(1X,''RUN DATE:  '',A9)') SYSDATE
      WRITE(68,'(1X,''START CLOCK TIME:  '',A8)') SYSTIME(1)
      WRITE(68,'(1X,''END CLOCK TIME:  '',A8)') SYSTIME(2)
      WRITE(68,*)
      CLOSE(68)
C
  999 CONTINUE
C    
      STOP
  800 FORMAT(1X,' ENG',3X,'GS-X',3X,'CR-1',3X,'CR-2',3X,'CR-3',3X,
     &       'CR-4',3X,'CR-5',3X,'CR-6',3X,'CR-7',3X,'VIM5',3X,'????')
  801 FORMAT(11(I5,2X))
      END
