      SUBROUTINE ENGOUT(ENG)
C
C READ 60 MINOR FRAMES OF ENGINEERING DATA.  EACH MINOR FRAME CONTAINS 60
C ENGINEERING WORDS REPRESENTING COMMUTATOR POSITIONS 100 THROUGH 159.
C THE 60 COMMUTATOR POSITIONS ARE MAPPED TO 223 DECK WORDS.  COMMUTATOR 
C WORDS 116, 120, 125, 130, 146, 150 AND 156 ARE MAPPED TO SPECIFIC DECK
C WORDS ACCORDING TO ENGINEERING MINOR FRAME COUNT.  ONCE ENGINEERING WORDS
C ARE MAPPED TO CORRESPONDING DECK WORDS, THE DECOMMUTATION MAP PROVIDES
C IDENTIFICATION OF A PARTICULAR ENGINEERING WORD.
C
C ORIGINAL SOURCE WRITTEN BY SANDY KRAMER, CODE 692, 06/09/94
C ENHANCED TO MAP ENGINEERING DATA - SBK  05/13/96
C ADD ASCII OUTPUT OF MAG E CHANNELS  SBK 12/7/2006
C
C VARIABLES:
C
C            TABLE - A TABLE OF 223 DECK NUMBERS MAPPED TO THE 60
C                    WORDS OF EACH ENGINEERING DATA MINOR FRAME
C
C            MAP - DECOMMUTATION MAP, 223 E-NUMS TO BE MAPPED INTO
C                  223 DECK NUMBERS FOUND IN ARRAY "TABLE" EXTRACTED
C                  FROM ENGINEERING RECORD BY ROUTINE DECOMM
C
C            COM116 - CONTAINS FIRST HALF OF THE 300 DECK.  30 DECK 
C                     NUMBERS REPEATED ONCE TO YIELD 60 DECK NUMBERS, 
C                     ONE FOR EACH OF THE 60 MINOR FRAMES OF ENGINEERING 
C                     DATA
C
C            COM120 - CONTAINS THE A00 DECK.  15 DECK NUMBERS REPEATED 
C                     THREE TIMES TO YIELD 60 DECK NUMBERS, ONE FOR EACH 
C                     OF THE 60 MINOR FRAMES OF ENGINEERING DATA
C
C            COM125 - CONTAINS FIRST HALF OF THE 200 DECK.  10 DECK NUMBERS 
C                     REPEATED FIVE TIMES TO YIELD 60 DECK NUMBERS, ONE FOR 
C                     EACH OF THE 60 MINOR FRAMES OF ENGINEERING DATA
C
C            COM130 - CONTAINS 400 DECK.  60 DECK NUMBERS, ONE FOR EACH 
C                     OF THE 60 MINOR FRAMES OF ENGINEERING DATA
C
C            COM146 - CONTAINS SECOND HALF OF THE 300 DECK.  30 DECK 
C                     NUMBERS REPEATED ONCE TO YIELD 60 DECK NUMBERS, 
C                     ONE FOR EACH OF THE 60 MINOR FRAMES OF ENGINEERING 
C                     DATA
C
C            COM150 - CONTAINS THE B00 DECK.  15 DECK NUMBERS REPEATED 
C                     THREE TIMES TO YIELD 60 DECK NUMBERS, ONE FOR EACH 
C                     OF THE 60 MINOR FRAMES OF ENGINEERING DATA
C
C            COM155 - CONTAINS SECOND HALF OF THE 200 DECK.  10 DECK 
C                     NUMBERS REPEATED ONCE TO YIELD 60 DECK NUMBERS, 
C                     ONE FOR EACH OF THE 60 MINOR FRAMES OF ENGINEERING 
C                     DATA
C
C E NUMBERS: THE DATA CHANNELS CONTAINING INSTRUMENT AND SYSTEM DATA
C            EACH E NUMBER REPRESENTS THE VALUES OF A SPECIFIC COMPONENT
C           
C            660 HOLDS VALUE OF E NUMS 665-672 DEPENDING UPON MOD60 AND
C                LINE COUNT EXTRACTED FROM FRAME IDENTIFICATION FIELD 
C            661 OB LFM TEMP
C            662 IB LFM TEMP
C            663 OB HFM TEMP
C            664 IB HFM TEMP
C            665 MAG INSTRUMENT SERIAL NUMBER
C            666 +12 V DC VOLTAGE
C            667 MAG INBOARD ELECTRONICS TEMP
C            668 -12 V DC VOLTAGE
C            669 MAG OUTBOARD ELECTRONICS TEMP
C            670 A ADC REFERENCE VOLTAGE
C            671 POWER CONVERTER TEMP
C            672 B ADC REFERENCE VOLTAGE
C
      CHARACTER DATAID*4
      LOGICAL*1 ENG(3600),COM(60),EDAT(224,60),EVAL(0:720)/721*0/
      INTEGER*2 DATATYPE(2),WORD32(2),LINECT,EMOD60,EMOD16,
     &          DECK660,MAGMUX,EDRTIME(6)
      INTEGER*2 COM116(60),COM120(60),COM125(60),COM130(60),
     &          COM146(60),COM150(60),COM155(60)
C
      INTEGER*2 TABLE(223)/ 100,101,102,103,104,105,106,107,108,109,110,
     &  111,112,113,114,115,300,301,302,303,304,305,306,307,308,309,310,
     &  311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,
     &  327,328,329,117,118,119,500,501,502,503,504,505,506,507,508,509,
     &  510,511,512,513,514,121,122,123,124,200,201,202,203,204,205,206,
     &  207,208,209,126,127,128,129,400,401,402,403,404,405,406,407,408,
     &  409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,
     &  425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,
     &  441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,
     &  457,458,459,131,132,133,134,135,136,137,138,139,140,141,142,143,
     &  144,145,330,331,332,333,334,335,336,337,338,339,340,341,342,343,
     &  344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,
     &  147,148,149,600,601,602,603,604,605,606,607,608,609,610,611,612,
     &  613,614,151,152,153,154,210,211,212,213,214,215,216,217,218,219,
     &  156,157,158,159/
C
      REAL*4 HDR(32)
C
      EQUIVALENCE (SUMOUT,       HDR(1)), (HDR(1),       DATAID),
     &            (HDR(12),      TIMEPD), (HDR(17), DATATYPE(1)), 
     &            (HDR(32),    WORD32(1)                       ),
     &            (HDR(4),    EDRTIME(1)                       )
C
      DATA COM116
     &      /300,301,302,303,304,305,306,307,308,309,
     &       310,311,312,313,314,315,316,317,318,319,
     &       320,321,322,323,324,325,326,327,328,329,
     &       300,301,302,303,304,305,306,307,308,309,
     &       310,311,312,313,314,315,316,317,318,319,
     &       320,321,322,323,324,325,326,327,328,329/
C
C A00 DECK NUMBERS ALIASED AS 5XX
C
      DATA COM120
     &      /500,501,502,503,504,505,506,507,508,509,
     &       510,511,512,513,514,500,501,502,503,504,
     &       505,506,507,508,509,510,511,512,513,514,
     &       500,501,502,503,504,505,506,507,508,509,
     &       510,511,512,513,514,500,501,502,503,504,
     &       505,506,507,508,509,510,511,512,513,514/
C
      DATA COM125
     &      /200,201,202,203,204,205,206,207,208,209,
     &       200,201,202,203,204,205,206,207,208,209,
     &       200,201,202,203,204,205,206,207,208,209,
     &       200,201,202,203,204,205,206,207,208,209,
     &       200,201,202,203,204,205,206,207,208,209,
     &       200,201,202,203,204,205,206,207,208,209/
C
      DATA COM130
     &      /400,401,402,403,404,405,406,407,408,409,
     &       410,411,412,413,414,415,416,417,418,419,
     &       420,421,422,423,424,425,426,427,428,429,
     &       430,431,432,433,434,435,436,437,438,439,
     &       440,441,442,443,444,445,446,447,448,449,
     &       450,451,452,453,454,455,456,457,458,459/
C
      DATA COM146
     &      /330,331,332,333,334,335,336,337,338,339,
     &       340,341,342,343,344,345,346,347,348,349,
     &       350,351,352,353,354,355,356,357,358,359,
     &       330,331,332,333,334,335,336,337,338,339,
     &       340,341,342,343,344,345,346,347,348,349,
     &       350,351,352,353,354,355,356,357,358,359/
C
C B00 DECK NUMBERS ALIASES AS 6XX
C
      DATA COM150
     &      /600,601,602,603,604,605,606,607,608,609,
     &       610,611,612,613,614,600,601,602,603,604,
     &       605,606,607,608,609,610,611,612,613,614,
     &       600,601,602,603,604,605,606,607,608,609,
     &       610,611,612,613,614,600,601,602,603,604,
     &       605,606,607,608,609,610,611,612,613,614/
C
      DATA COM155
     &      /210,211,212,213,214,215,216,217,218,219,
     &       210,211,212,213,214,215,216,217,218,219,
     &       210,211,212,213,214,215,216,217,218,219,
     &       210,211,212,213,214,215,216,217,218,219,
     &       210,211,212,213,214,215,216,217,218,219,
     &       210,211,212,213,214,215,216,217,218,219/
C
      DATA ICNT/0/
C
      INCLUDE 'UNPACK.INC'
C
      ICNT = ICNT + 1
      RECWRITE = RECWRITE + 1
      DATAID = 'ENG '
      TIMEPD = 0.0
      DATATYPE(1) = 7
      WORD32(1) = RECWRITE
      WORD32(2) = 3416
C
      IF ( ICNT.EQ.1 ) 
     & OPEN(87,FILE='ENG.DAT',STATUS='NEW',FORM='FORMATTED',
     &      CARRIAGECONTROL='LIST')
C
C READ 60 MINOR FRAMES OF ENGINEERING DATA
C
      DO J = 1,60
C
C EXTRACT 8-BIT ENGINEERING DATA.  FIRST EIGHT WORDS CORRESPOND TO MEASUREMENT
C NUMBERS E-160 THROUGH E-167.  E-160 THROUGH E-163 CONTAIN THE HEX FRAME
C SYNCHRONIZATION CODE 03915ED3.
C
C READ 60 BYTES OF ENGINEERING MINOR FRAME
C
       DO I = 1,60
C
        COM(I) = 0
        K = (J-1)*480 + (I-1)*8
        CALL MOVBIT(ENG, K, 8, COM(I), 0)
        L = I + 99  ! commuter number
        IF ( L.EQ.116 ) L = COM116(J)
        IF ( L.EQ.120 ) L = COM120(J)
        IF ( L.EQ.125 ) L = COM125(J)
        IF ( L.EQ.130 ) L = COM130(J)
        IF ( L.EQ.146 ) L = COM146(J)
        IF ( L.EQ.150 ) L = COM150(J)
        IF ( L.EQ.155 ) L = COM155(J)
C
C MAP ENGINEERING NUMBERS TO DECK NUMBERS
C
        DO IMAP = 1,223
         INDEX = TABLE(IMAP)  ! get deck number
         IF ( L.EQ.INDEX ) THEN
          IF ( MAP(IMAP).EQ.660) DECK660 = L
          EVAL(MAP(IMAP)) = COM(I)  ! save measurementS w/E-num indexing
          EDAT(IMAP,J) = COM(I)  ! save measurements from each MF
c          write(87,888) imap,index,map(imap),com(i)
  888  format(3(1x,i3.3),3x,z2.2)
         END IF
        END DO
C 
       END DO  
C
       if ( eval(160).eq.'03'x .and.
     &      eval(161).eq.'91'x .and.
     &      eval(162).eq.'5e'x .and.
     &      eval(163).eq.'d3'x ) then
        linect = 0
        emod60 = 0
        emod16 = 0
        call movbit(eval(165), 0, 8, emod16, 0)
        call movbit(eval(166), 2, 6, emod60, 0)
        call movbit(eval(166), 0, 2, linect, 8)
        call movbit(eval(167), 0, 8, linect, 0) 
        write(87,'(1x,I2,1X,I3.3,3(1X,I2.2),1X,I3.3, 
     &   8(1x,z2.2),9(1x,i3.3))') (edrtime(mm),mm=1,6),
     &   (eval(n),n=160,167),emod60,linect,
     &   deck660,magmux(deck660,emod60,linect),
     &   (zext(eval(nn)),nn=660,664)
       end if
C
      END DO
C
C output 14016 byte record (3504 words)
C
      WRITE(66) HDR, MAP, EDAT 
C
      RETURN
      END
      INTEGER*2 FUNCTION MAGMUX(DECK660,EMOD60,LINECT)
C
      INTEGER*2 DECK660,EMOD60,LINECT
C
      MAGPOS = -1
      IF ( DECK660.EQ.334 ) THEN
         IF ( EMOD60.EQ.1 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 4
         ELSE IF (EMOD60.EQ.8 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 2
         ELSE IF (EMOD60.EQ.16 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 0
         ELSE IF (EMOD60.EQ.23 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 6
         ELSE IF (EMOD60.EQ.31 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 4
         ELSE IF (EMOD60.EQ.38 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 2
         ELSE IF (EMOD60.EQ.46 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 0
         ELSE IF (EMOD60.EQ.53 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 6
         ELSE
            WRITE(6,*) '*ENGOUT/MAGMUX* INVALID MOD60/LINECT VALUE',
     &       EMOD60,LINECT
         END IF
      ELSE IF ( DECK660.EQ.202 ) THEN
          IF ( MOD(EMOD60,10).EQ.0 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 2
          ELSE IF ( MOD(EMOD60+7,10).EQ.0 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 4
          ELSE IF ( MOD(EMOD60+5,10).EQ.0 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 6
          ELSE IF ( MOD(EMOD60+2,10).EQ.0 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 0
          ELSE 
            WRITE(6,*) '*ENGOUT/MAGMUX* INVALID MOD60/LINECT VALUE',
     &       EMOD60,LINECT
          END IF
      ELSE IF ( (DECK660-100).LT.100 ) THEN
          IF ( MOD(EMOD60,2).EQ.0 .AND. LINECT.EQ.1 ) THEN
            MAGPOS = 0
          ELSE IF ( MOD(EMOD60,2).EQ.0 .AND. LINECT.EQ.201 ) THEN
            MAGPOS = 1
          ELSE IF ( MOD(EMOD60,2).EQ.0 .AND. LINECT.EQ.401 ) THEN
            MAGPOS = 2
          ELSE IF ( MOD(EMOD60,2).EQ.0 .AND. LINECT.EQ.601 ) THEN
            MAGPOS = 3
          ELSE IF ( LINECT.EQ.1 ) THEN
            MAGPOS = 4
          ELSE IF ( LINECT.EQ.201 ) THEN
            MAGPOS = 5
          ELSE IF ( LINECT.EQ.401 ) THEN
            MAGPOS = 6
          ELSE IF ( LINECT.EQ.601 ) THEN
            MAGPOS = 7
          ELSE 
            WRITE(6,*) '*ENGOUT/MAGMUX* INVALID MOD60/LINECT VALUE',
     &       EMOD60,LINECT
          END IF
      ELSE
          WRITE(6,*) '*ENGOUT/MAGMUX* INVALID DECK NUMBER', DECK660
      END IF
C     
      IF ( MAGPOS.EQ.0 ) THEN
          MAGMUX = 665
      ELSE IF ( MAGPOS.EQ.1 ) THEN
          MAGMUX = 666
      ELSE IF ( MAGPOS.EQ.2 ) THEN
          MAGMUX = 667
      ELSE IF ( MAGPOS.EQ.3 ) THEN
          MAGMUX = 668
      ELSE IF ( MAGPOS.EQ.4 ) THEN
          MAGMUX = 669
      ELSE IF ( MAGPOS.EQ.5 ) THEN
          MAGMUX = 670
      ELSE IF ( MAGPOS.EQ.6 ) THEN
          MAGMUX = 671
      ELSE IF ( MAGPOS.EQ.7 ) THEN
          MAGMUX = 672
      ELSE
          MAGMUX = 999
      END IF
C
      RETURN
      END
