	SUBROUTINE GETMGF	!(I'M NOT USED YET)
C	                         AS OF 3/93 USED A'LA GETRAW FOR CALIBRATED MGF
C
C	GETMGF - GET GEOTAIL MAGNETOMETER (MGF) DATA
C	      6/94 - TEST MGF.STATUS AND SET KGOOD
C
C!
C!	     ENTRY GETB8  (B8)		GET 0.5 AVERAGE OF FO DATA
C!	     ENTRY GETBFO (FO)  	GET VECTOR OF FO(8) DATA
C!	     ENTRY GETBFI (FI)  	GET VECTOR OF FI(2) DATA
C!	     ENTRY PRNMGF (IOUT, ICODE) PRINT OUT MGF DATA
C!	     ENTRY GETSPN (DSP.SPINPH,DSP.SPNPER, DSP.MSPTIM, MGF.STATUS)
C!	     ENTRY GETBSP (MGF.TM8,   MGF.BSPIN,              MGF.STATUS)
C!
C
C	ALIGNMENT MATRIX NOTES
C	   THE DATA INITIALIZATION FOR THE ALIGNMENT MATRICES, ALIFO
C	   AND ALIFI, WERE CODED FROM A DOCUMENT AND ARE ACTUALLY
C	   THE TRANSPOSE OF WHAT SHOULD BE STORED IN THEM.
C	   TO USE THESE TRANSPOSED MATRICES, MULTIPLE THE BT VECTOR BY
C	   THE TRANSPOSE OF THE MATRIX OR MUTIPLY THE MATRIX
C	   BY THE TRANSPOSE OF THE BT VECTOR.
C	   E.G. USING VBLA ROUTINES;
C	      
C	      CALL VMULFM(ALIFO,BT, 3,3,1, 3,3, BTC,3, IER)
C	      CALL VMULFF(BT,ALIFO, 1,3,3, 1,3, BTC,1, IER)
C
C	   BTC IS THE SAME IN EITHER CASE.
C

	IMPLICIT INTEGER (A-H,O-Z)
	REAL*8 DTIME
	REAL F0(3,8), FO(3,8), FI(3,2), SC(3), BFLD, BFLDO, BFLDI, B8
	REAL ARYTIM(0:19), ARYRAD(0:19), SDRAD, SDTIM, OLDPHA
	REAL DELRAD, DELTIM
	REAL SPINPH, SPNPER,BSPIN,PHASE
	REAL DSNPL(2,2), CVSCS(3,3), ZA, TOFF, CO(3)
	REAL SPNPH, RA,DEC, SPNAX(3), COSA,SINA, COSB,SINB
C
C	Geotail MGF-FO Sensitivity Factor
C	    B[nT] = count * factor
C
	REAL SENFO (3,0:7)
	DATA SENFO /
!               X           Y           Z		RANGE
     +     0.,          0.,          0.,		!0
     +     5.41457E-4,  5.28416E-4,  5.21855E-4,	!1
     +     2.16688E-3,  2.11454E-3,  2.09009E-3,	!2
     +     8.67062E-3,  8.45580E-3,  8.35575E-3,	!3
     +     3.46785E-2,  3.42085E-2,  3.33943E-2,	!4
     +     1.38712E-1,  1.35029E-1,  1.33575E-1,	!5
     +     5.54902E-1,  5.45822E-1,  5.34274E-1,	!6
     +     2.21981E00,  2.18417E00,  2.13703E00/	!7
C
C	Geotail MGF-FO Zero Offsets in count
C
	REAL ZOFO  (3,0:7)
	DATA ZOFO /
!              X      Y      Z				range
     +         0,      0,     0,			!0
!X   +     -5269,  -3395,   -62,			!1
!X   +     -1208,  -1362,  -131,			!2
!X   +     -4000,  -2000,   -62,			!1
!X   +     -1015,   -500,  -131,			!2
     +     -4400,  -1900,   -62,			!1
     +     -1120,   -475,  -131,			!2
     +      -382,   -290,   -18,			!3
     +       -77,    -62,     4,			!4
     +       -15,    -16,     9,			!5
     +         1,     -5,    13,			!6
     +        -7,     -7,     8/			!7
C
C	Geotail MGF-FO Alignment Matrix in the cubic mirror coordinate
C
	REAL ALIFO (3,3)
	DATA ALIFO /
     +      1.00005E00,   2.67771E-3,  -3.97231E-3,
     +     -5.82318E-3,   1.00003E00,  -1.17032E-3,
     +     -2.81456E-3,  -3.57832E-3,   1.00002E00/

!          Tx :  0.16245   Fx :  0.33382
!          Ty :  0.20545   Fy : 89.84577
!          Tz : 89.76219   Fz : 16.71032
C
C	MAST ALIGNMENT MATIRX
C
	REAL ALIMA (3,3)
	DATA ALIMA /
     +     9.99993E-1, -2.16693E-3,  3.07928E-3,
     +     2.17207E-3,  9.99996E-1, -1.69003E-3,
     +    -3.07567E-3,  1.63620E-3,  9.99994E-1/
C
C	Z MAST/SPIN AXIS FIX MATRIX
C	   ZA = 1.8 DEG < 10/24/92 21:35:00, 0.6 DEG > 
C
	REAL ZAFIX (3,3)
	DATA ZAFIX /1., 8*0./
C
C	PAYLOAD COORDINATE CONVERSION MATIRX
C
	REAL PLCNV (3,3)
	DATA PLCNV /
     +     -7.06781E-1,  7.07466E-1, -1.45055E-3 ,
     +     -7.07430E-1, -7.06747E-1, -6.95138E-4 ,
     +     -1.51748E-3,  5.34960E-4,  9.99999E-1 /
C
C	Geotail MGF-FI Sensitivity Factor
C	    B[nT] = count * factor
C
	REAL SENFI (3,0:7)
	DATA SENFI /
!               X           Y           Z		RANGE
     +     0.,          0.,          0.,                !0
     +     4.51743E-4,  4.54882E-4,  4.40552E-4,	!1
     +     1.84401E-3,  1.86774E-3,  1.79701E-3,	!2
     +     7.18437E-3,  7.10485E-3,  7.05154E-3,	!3
     +     2.81978E-2,  2.79170E-2,  2.78186E-2,	!4
     +     1.10883E-1,  1.09627E-1,  1.09295E-1,	!5
     +     4.50836E-1,  4.49434E-1,  4.45397E-1,	!6
     +     1.77280E00,  1.76364E00,  1.74987E00/	!7
C
C	Geotail MGF-FI Zero Offsets in count
C
	REAL ZOFI  (3,0:7)
	DATA ZOFI /
!            X      Y      Z				RANGE
     +        0,      0,     0,  			!0
     +    -1282,  -1311,  -218,				!1
     +     -302,   -327,   -47,				!2
     +      -76,    -84,   -14,				!3
     +       75,     74,    92,				!4
     +       17,     17,    22,				!5
     +       39,     39,    40,				!6
     +        9,      9,     9/				!7
C
C	Geotail MGF-FI Alignment Matrix in the cubic mirror coordinate
C
	REAL ALIFI (3,3)
	DATA ALIFI /
     +     1.000E00,    -2.84536E-3,  -3.25557E-3,
     +     2.94837E-3,   1.000E00,    -1.0007E-2,
     +    -5.04738E-4,  -1.65782E-3,   1.000E00/
C
!          Tx : -0.02920   Fx :  0.16922
!          Ty :  0.09490   Fy : 89.83666
!          Tz : 89.39706   Fz : 71.81518 <--- ?
!
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
!P.S.
!   In order to get physical quantity from telemetry data,
!     1) extract MGF FO/FI data as 16 bit offset binary
!     2) subtracting 32768 makes data 16 bit comprementary binary.
!     3) subtract sensor offsets
!     4) multiply sensitivity factors
!     5) use sensor alignement matrix to get orthgonal vector components
!     6) use conversion matrix (which will be given later) to get vector
!        magnetic field in the satellite coord.
!
C
C	MAGNETIC FIELD (MGF) DATA
C
	STRUCTURE /MGF_DATA/
	   INTEGER*4	TM8			!MSOD @ MFC MOD 8 = 0
	   INTEGER*4	FO(3,8)
	   INTEGER*4	FI(3,2)
	   INTEGER*4	SPIN			!12 BITS
	   INTEGER*4	STATUS
	END STRUCTURE
C
	RECORD /MGF_DATA/ MGF
C
C	DESPUN MGF DATA
C
	STRUCTURE /DESPUN_MGF_DATA/
	   REAL         BO(3,8)
	   INTEGER*4	TBO(8)
	   REAL		BI(3,2)			!NOT YET IMPLEMENTED
	   INTEGER*4	TBI(2)
	   INTEGER*4	SPNTIM			!SPIN_TIME Fn+2 @ W112
	   REAL		SPINPH			!SPIN_PHASE
	   REAL		SPNPER			!SPIN_PERIOD
	END STRUCTURE
C
	RECORD /DESPUN_MGF_DATA/ DSP
	COMMON /MGFDAT/  MGF
	COMMON /DSPDAT/  DSP
C
	ISTP(ISX) = MOD(REF+ISX,128)		!0,...,127
	DTIME(JUL,MS) = DFLOAT(JUL-2400000) + DFLOAT(MS)/8.64D7
	
C
C	BUILD DSP INFO VIA CIRCULAR BUFFER
C
	CALL GETPTR (MFPTR)
	CALL GETMFC (MFPTR, MFC)
	CALL GETMSD (MFPTR, MSOD)
	MF8 = MOD(MFC,8)
	KGOOD = (MGF.SPIN .AND. '000F'X)*256**2 + MGF.STATUS
	IF (MF8 .EQ. 3) THEN
	IF (KGOOD .NE. '0FFFFF'X) THEN
	   OLDPHA      = DSP.SPINPH		!RADIANS
	   DSP.SPINPH  = 2.*ATAN2(0.,-1.)*(MGF.SPIN/16)/4096.
C
C	PRELIMINARY SPIN PERIOD DETERMINATION; EVERY 0.5 SEC
C
	   DELMGF      = MSOD + 179.7 - DSP.SPNTIM
	   DELMGF      = MSOD + (-7.8) - DSP.SPNTIM
	   IF (DELMGF .LT. 0.) DELMGF = DELMGF + 1000.*86400
	   DELRAD      = DSP.SPINPH - OLDPHA
	   IF (DELRAD .LT. 0.) DELRAD = DELRAD + 2.*ATAN2(0.,-1.)
C
	   IF (KMGF .GT. 0 .AND. 
     +        (DELMGF .GT. 495. .AND. DELMGF .LT. 505.) .AND.
     +        (DELRAD .GE. 1.0  .AND. DELRAD .LT. 1.06) !~20 RPM
     +        ) THEN
	      IXSPN       = MOD(IXSPN + 1, 20)		!0,...,19 (20 MIN.)
	      SDRAD    = SDRAD +  DELRAD - ARYRAD(IXSPN)
	      SDTIM    = SDTIM +  DELMGF - ARYTIM(IXSPN)
	      IF (SDRAD .GT. 0) THEN
	         DSP.SPNPER  = 2.*ATAN2(0.,-1.)*SDTIM/SDRAD
	      ELSE
	         PRINT *,'!!! MGF: SPINS ???', DELMGF/1000., DELRAD
	      ENDIF
	      ARYTIM(IXSPN) = DELMGF
	      ARYRAD(IXSPN) = DELRAD
	   ELSE
	      PRINT *,'... MGF: GAP ...',MFC,
     +           MSOD/1000., DELMGF/1000.,DELRAD
	   ENDIF
	   KMGF = KMGF + 1
	   DSP.SPNTIM  = MSOD + 179.7		!Fn+2 @ W112
	   DSP.SPNTIM  = MSOD + (-7.8)		!Fn+2 @ W112
CDX	   PRINT *,DSP.SPNTIM/1000.,DSP.SPNPER/1000.,DELRAD,DELMGF/1000.
	ENDIF
	ENDIF
C
	RETURN
C
C	GET DSP SPIN INFO
C
	ENTRY GETSPN (SPINPH,SPNPER, MSPTIM, IBSPST)
C
	SPINPH  = DSP.SPINPH				!RADS
	SPNPER  = DSP.SPNPER				!MSEC
	MSPTIM  = DSP.SPNTIM				!MSEC
	IBSPST  = (MGF.SPIN .AND. '0000000F'X)*256**2 + MGF.STATUS
	RETURN
C
C	GET MGF SPIN INFO
C
	ENTRY GETBSP (ITIM8,BSPIN,MSPST)
	ITIM8 = MGF.TM8
	BSPIN = 2.0*(MGF.SPIN/16)*ATAN2(0.,-1.)/4096.
	MSPST = (MGF.SPIN .AND. '0000000F'X)*256**2 + MGF.STATUS
	RETURN
C
	ENTRY GETSPH (PHASE)
	PHASE = SPNPH
	RETURN
C
	
	ENTRY GETB8 (B8)
C
C	GETB8 = GET 0.5 SEC. AVERAGE OF FO MAGNETOMETER DATA
C	        NOTE: IT TAKES EDITOR B DATA 8 MINOR FRAMES OR
C	              1/2 SECOND TO GET A FULL SET OF MCA DATA.
C
	KRANGE = (MGF.STATUS .AND. '00E0'X)/32	!0,...7
C
	B8  = 0.
	IF (MGF.STATUS .EQ. 'FFFF'X) RETURN
	IF (KGOOD .EQ. 1) RETURN
	NB8 = 0
	DO I=1,8
	   BFLD = 0.
C
	   DO J=1,3
	      BFLD = BFLD + (SENFO(J,KRANGE)*
     +           (MGF.FO(J,I) - 32768 - ZOFO(J,KRANGE)))**2
	   ENDDO
C
	   IF (BFLD .GT. 0) THEN
	      B8  = B8  + SQRT(BFLD)
	      NB8 = NB8 + 1
	   ENDIF
	ENDDO
	IF (NB8 .GT. 0) B8 = B8/FLOAT(NB8)	!> 0 ==> OK
	RETURN
C
C
	ENTRY GETBF0 (F0)
C
C	GETBFO = GET FO MAGNETOMETER DATA
C
	KRNGFO = (MGF.STATUS .AND. '00E0'X)/32	!0,...7
C
	DO I=1,8
	   DO J=1,3
	      F0(J,I) = SENFO(J,KRNGFO)*
     +           (MGF.FO(J,I) - 32768 - ZOFO(J,KRNGFO))
	   ENDDO
	ENDDO
	RETURN
	
	ENTRY GETBFO (FO)
C
C	GETBFO = GET OUTBOARD FLUXGATE MAGNETOMETER DATA (PREFERRED DATA)
C
	KRANGE = (MGF.STATUS .AND. '00E0'X)/32	!0,...7
C
C	PREPARE AD HOC ROTATION MATRIX
C
	CALL GETPTR (MFPTR)
	CALL GETJUL (MFPTR,JUL)
	CALL GETMSD (MFPTR,MSOD)
	CALL GETMFC (MFPTR,MFC)
	CALL JDAY (24,10,1992,JULZ)
C
	MSZ = 1000*(21*60 + 55)*60
	IF (DTIME(JUL,MSOD) .LT. DTIME(2448920,77700)) THEN
	   ZA = 1.80		!10/24/92 21:35:00
	ELSE
	   ZA = 0.60
	ENDIF
C
	ZA = ATAN2(0.,-1.)*ZA/180.
	ZAFIX(1,1) = 1.
	ZAFIX(2,2) = COS(ZA)
	ZAFIX(2,3) = SIN(ZA)
	ZAFIX(3,2) =-SIN(ZA)
	ZAFIX(3,3) = COS(ZA)
C
	IF (MGF.STATUS .AND. '0500'X .EQ. '0500'X) THEN
	   TOFF = -31.25 - 3.2			!MS (AUTO) Fn-1, W64 (1/2)
	ELSE
	   TOFF = -62.5 + 58.6			!MS (BC) Fn-1, W120
	ENDIF
CX8
C
C	GET ATTITUDE INFO FOR SPIN AXIS ORIENTATION AND NEAREST GUESS FOR RPM
C
	   CALL GETRAD(JUL,DSP.TBO(1), RA,DEC,RPM, IRET)
	   IF (IRET .EQ. 5) THEN
	      PRINT *,'!!! GETMGF:ATTITUDE FILE ???',JUL,DSP.TBO(1)
	      RETURN
	   ELSE IF (IRET .EQ. -5) THEN
	      PRINT *,'!!! GETMGF:ATT:LOCATION ???',JUL,DSP.TBO(1)
	      DO I=1,8
	      DO J=1,3
	         FO(J,I) = 0.
	      ENDDO
	      ENDDO
	      GOTO 555
	   ENDIF
CX55	WRITE (55,*) JUL,DSP.TBO(1),RA,DEC,RPM
C
	   SPNAX(1) = COS(DEC)*COS(RA)
	   SPNAX(2) = COS(DEC)*SIN(RA)
	   SPNAX(3) = SIN(DEC)
C
	   COSA    = SQRT(SPNAX(2)**2 + SPNAX(3)**2)
	   SINA    = SPNAX(1)
	   COSB    = SPNAX(3)/COSA
	   SINB    = SPNAX(2)/COSA
C
	   CVSCS(1,1) =  COSA
	   CVSCS(1,2) =  0.
	   CVSCS(1,3) =  SINA
	   CVSCS(2,1) = -SINA*SINB
	   CVSCS(2,2) =  COSB
	   CVSCS(2,3) =  COSA*SINB
	   CVSCS(3,1) = -SINA*COSB
	   CVSCS(3,2) = -SINB
	   CVSCS(3,3) =  COSA*COSB
CX8
C
	DO I=1,8
	   DO J=1,3
	      FO(J,I) = 0.
	   ENDDO
C
C	   GET RAW B FIELD COMPONENTS
C
	   DO J=1,3
CD	      PRINT *,I*100+J*10+KRANGE,MGF.FO(J,I),SENFO(J,KRANGE),ZOFO(J,KRANGE)
	      FO(J,I) = SENFO(J,KRANGE)*
     +           (MGF.FO(J,I) - 32768 - ZOFO(J,KRANGE))
	   ENDDO
CD	   IF (I .EQ. 1) PRINT *,'MGFSP',(FO(J,I), J=1,3)
C
C	ALIGN DATA WRT ZA, ETC.
C
	   CALL VMULFM(ALIFO,FO(1,I), 3,3,1, 3,3, CO,3, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'ALIFO',(FO(J,I), J=1,3)
C
	   CALL VMULFM(ALIMA,FO(1,I), 3,3,1, 3,3, CO,3, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'ALIMA',(FO(J,I), J=1,3)
C
	   CALL VMULFF(ZAFIX,FO(1,I), 3,3,1, 3,3, CO,3, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'ZAFIX',(FO(J,I), J=1,3)
C
	   CALL VMULFM(PLCNV,FO(1,I), 3,3,1, 3,3, CO,3, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'PLCNV', (FO(J,I), J=1,3)
C
C	ASSIGN AQUISITION TIMES TO DATA SAMPLES WRT RANGE = {BC, AUTO}
C
	   DSP.TBO(I) = MGF.TM8 + (I-1)*62.5 + TOFF
C
	IF (DSP.SPNPER .EQ. 0) DSP.SPNPER = 60./RPM	!GUESS > 0 ?!?
C
C	DESPIN DATA
C
	   IF (DSP.SPNPER .GT. 0) THEN 
	      SPNPH = -(DSP.TBO(I) - DSP.SPNTIM)*2.*ATAN2(0.,-1.)/
     +           DSP.SPNPER - DSP.SPINPH + 101.97*ATAN2(0.,-1.)/180.
	   ELSE 	!IF (DSP.SPNTIM .GT. 0) THEN
CW	      PRINT *,'!!! WACKY SPIN PERIOD ???',DSP.SPNPER,DSP.SPNTIM
	      DO J=1,3
	         FO(J,I) = 0.
	      ENDDO
	      GOTO 555
	   ENDIF
C
	   DSNPL(1,1) =  COS(SPNPH)
	   DSNPL(1,2) =  SIN(SPNPH)
	   DSNPL(2,1) = -SIN(SPNPH)
	   DSNPL(2,2) =  COS(SPNPH)
C
	   CALL VMULFF(DSNPL,FO(1,I), 2,2,1, 2,2, CO,2, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'DSNPL',(FO(J,I), J=1,3)
C
C	OK, NOW CONVERT DESPUN PAYLOAD COORDINATES TO SCS
C
	   CALL VMULFF(CVSCS,FO(1,I), 3,3,1, 3,3, CO,3, IER)
	   CALL VCOPY (3, CO,1, FO(1,I),1)
CD	   IF (I .EQ. 1) PRINT *,'CVSCS',(FO(J,I), J=1,3)
C
C	EOL, SHOULD WE DO THIS HERE?
C
555	   DO J=1,3
	      DSP.BO(J,I) = FO(J,I)
	   ENDDO
	ENDDO
	RETURN
C
	ENTRY GETBFI (FI)
C
C	GETBFI = GET INBOARD FLUXGATE MAGNETOMETER DATA
C
	KRANGE = (MGF.STATUS .AND. '001C'X)/4	!0,...7
C
	DO I=1,2
	   DO J=1,3
	      FI(J,I) = SENFI(J,KRANGE)*
     +           (MGF.FI(J,I) - 32768 - ZOFI(J,KRANGE))
	   ENDDO
	ENDDO
	RETURN
C
	ENTRY PRNMGF (IOUT,ICODE)
C
C	PRNMGF - PRINT MGF DATA ON IOUT WRT IOUT
C
	IF (ICODE .EQ. 10) THEN
	   WRITE (IOUT,'(1X,A/4X,I9,Z9.6)') 'MGF     SPIN,  STATUS :',
     +        MGF.SPIN/16,(MGF.SPIN .AND. '000F'X)*256**2 + MGF.STATUS
	   WRITE (IOUT, '(1X,6(7X,A3))')
     +        'FOX', 'FOY', 'FOZ',  'FIX', 'FIY', 'FIZ'
	   DO I=1,8
	      IF (I .LE. 2) THEN
	         WRITE (IOUT, '(1X,6I10)') 
     +             (MGF.FO(J,I),J=1,3), (MGF.FI(J,I), J=1,3)
	      ELSE
	         WRITE (IOUT, '(1X,3I10)') (MGF.FO(J,I),J=1,3)
	      ENDIF
	   ENDDO
	ELSE IF (ICODE .EQ. 11) THEN
C
	   KRANGE = (MGF.STATUS .AND. '00E0'X)/32	!0,...7
C
	   BFLDO = 0.
	   DO I=1,8
	        BFLD = 0.
	      DO J=1,3
	         BFLD = BFLD + (SENFO(J,KRANGE)*
     +              (MGF.FO(J,I) - 32768 - ZOFO(J,KRANGE)))**2
	      ENDDO
	      BFLDO = BFLDO + SQRT(BFLD)/8.
	   ENDDO
C
	   KRANGE = (MGF.STATUS .AND. '00E0'X)/32	!0,...7
C
	   BFLDI = 0.
	   DO I=1,2
	        BFLD = 0.
	      DO J=1,3
	         BFLD = BFLD + (SENFI(J,KRANGE)*
     +              (MGF.FI(J,I) - 32768 - ZOFI(J,KRANGE)))**2
	      ENDDO
	      BFLDI = BFLDI + SQRT(BFLD)/2.
	   ENDDO
C
	   WRITE (IOUT,'(1X,A/4X,I9,Z9.6,2F12.2)') 
     +        'MGF     SPIN,  STATUS        B:FO        B:FI (nT)',
     +        MGF.SPIN/16,(MGF.SPIN .AND. '000F'X)*256**2 + MGF.STATUS,
     +        BFLDO,BFLDI
C
	ENDIF
	RETURN
	END
