      PROGRAM BPLOT
      INTEGER*2 IFLAG,INUNIT,OUTUNIT,IOPEN1, IOPEN7
      DIMENSION APA(4)
      CHARACTER*30 INFILE,OUTFILE
      CHARACTER*1 ANS, FPM(3)
      DATA FPM/'F','P','M'/
      WRITE(*,1208)
1208  FORMAT(' FLOATING-POINT MAGNETIC FIELD DATA  FILE NAME?',/,
     1 ' (MUST HAVE UT,BX,BY,BZ IN FLOATING POINT IN THAT ORDER)',/,
     2 ' SUFFIX MUST BE "FPM"',/)
      READ(*,1004)INFILE
      INUNIT=1
      OUTUNIT=7
C
      CALL SUFIX(INFILE,FPM)
      OPEN(UNIT=INUNIT,FILE=INFILE,FORM='UNFORMATTED',STATUS='OLD',
     1 ACCESS='TRANSPARENT')
C
      IFLAG=0
      IOPEN1=1
5     WRITE(*,1009)
1009  FORMAT(' OUTPUT FILE NAME?',/)
      READ(*,1004)OUTFILE
1004  FORMAT(A)
      OPEN(UNIT=OUTUNIT,FILE=OUTFILE,STATUS='NEW',FORM='FORMATTED')
      IOPEN7=1
      CALL FIRSTPA(INUNIT,UT,APA,IFLAG)
      IF(IFLAG.EQ.1)STOP
      WRITE(*,1002)UT
1002  FORMAT(' FIRST TIME IN MAG FILE IS ',F10.3,/)
      WRITE(*,1005)
1005  FORMAT(' START AND STOP TIMES? (2F7.0)',/)
      READ(*,1008)TSTRT,TSTOP
1008  FORMAT(2F7.0)
      IF(TSTRT.LT.UT)TSTRT=UT
      TFRST=TSTRT
      TTOTAL=TSTOP-TSTRT
      WRITE(*,1006)
1006  FORMAT(' DO YOU WANT FIELD COMPONENTS OR TOTAL FIELD? (1,2)   ')
      READ(*,1007)ITYPE
1007  FORMAT(I1)
      IF(ITYPE.EQ.1)GO TO 1100
      PRINT*,' INTERVAL (IN SECONDS) TO AVERAGE B OVER? (F7.3)'
      READ(*,1078)AIT
1078  FORMAT(F7.3)
      WRITE(*,1079)TSTRT,TSTOP,AIT
1079  FORMAT(' START AND STOP TIMES:',2F9.1,', INTERVAL:',F7.3)
1050  NT=0
      UT=TFRST
      TLAST=UT+AIT
      BAVE=0.
      DO 1060 IUY=1,50000
      CALL GETPA(INUNIT,UT,APA,IFLAG)
      IF(IFLAG.EQ.1)GO TO 140
      BEE=APA(4)
      IF(NT.EQ.0)GO TO 1051
      IF(BEE.GT.AOLD2)GO TO 1058
      IF(BEE.LT.AOLD1)GO TO 1058
1051  BAVE=BAVE+BEE
      AOLD1=0.9*BEE
      AOLD2=1.1*BEE
      NT=NT+1
1058  UT=UT+0.128
      IF(UT.LT.TLAST)GO TO 1060
      IF(NT.EQ.0)GO TO 1050
      BAVE=BAVE/FLOAT(NT)
      UT1=UT-0.5*AIT
      WRITE(7,1059)UT1,BAVE
      TFRST=UT
      NRIT=NRIT+1
      IF(NRIT.LT.100)GO TO 1050
      TAMT=100.*(UT-TSTRT)/TTOTAL
      WRITE(*,1038)TAMT
1038  FORMAT(1X,F4.1,'% COMPLETED')
      NRIT=0
1059  FORMAT(F9.2,','F7.1)
      IF(UT.GT.TSTOP)GO TO 135
      GO TO 1050
1060  CONTINUE
      IF(UT.GT.TSTOP)GO TO 135
      GO TO 1050
1100  PRINT*,' INTERVAL BETWEEN SAMPLES (SECONDS)? (F6.3)'
      READ(*,1008)AIT
      print*,' this part of the code has not been debugged'
      UT=TSTRT
1101  DO 1135 JJ=1,50000
      CALL GETPA(INUNIT,UT,APA,IFLAG)
      IF (IFLAG.EQ.1)GOTO140
      WRITE(7,1130)UT,APA
1130  FORMAT(F9.2,4(',',F7.1))
      UT=UT+AIT
      IF(UT.GT.TSTOP)GO TO 135
1135  CONTINUE
      GO TO 1101
135   CONTINUE
      CLOSE(INUNIT)
      CLOSE(OUTUNIT)
      STOP
140   CLOSE(OUTUNIT)
      STOP
      END
C   
      SUBROUTINE GETPA(INUNIT,UT,APA,IFLAG)
      DIMENSION ARRAY(4,33),BCMP(3),APA(4)
      INTEGER*2 INDX,IFLAG,NDX
1     NDX=INDX

      DO 100 J=NDX,33
      IF(UT.GT.ARRAY(1,J))GO TO 100
      APA(1)=ARRAY(2,J-1)
      APA(2)=ARRAY(3,J-1)
      APA(3)=ARRAY(4,J-1)
      APA(4)=SQRT(DOT(APA,APA))
      RETURN
100   INDX=INDX+1
      DO 110 IJ=1,4
110   ARRAY(IJ,1)=ARRAY(IJ,33)
      CALL GETSECT(INUNIT,ARRAY(1,2),IFLAG)
      IF(IFLAG.EQ.1)THEN
         PRINT*,' MAGNETIC FIELD FILE EXHAUSTED'
         RETURN
      ENDIF
      INDX=2
      GO TO 1
      ENTRY FIRSTPA
      CALL GETSECT(INUNIT,ARRAY(1,2),IFLAG)
        IF(IFLAG.EQ.1)THEN
           PRINT*,' MAGNETIC FIELD FILE CAN NOT BE READ'
           RETURN
        ENDIF
      DO 120 IJ=1,4
120   ARRAY(IJ,1)=ARRAY(IJ,2)
      INDX=2
      UT=ARRAY(1,1)
      RETURN
      END


      SUBROUTINE GETSECT(INUNIT,BREC,IFLAG)
C     A subroutine to read sectors from disk.
C     IFLAG = 1 if reached end of file.
      LOGICAL*1 BREC(512)
      INTEGER*2 INUNIT,IFLAG
      IFLAG=0
      READ(UNIT=INUNIT,END=1000)BREC
      RETURN
1000  IFLAG=1
      PRINT *,' END OF DATA ON INPUT FILE'
      CLOSE (UNIT=INUNIT)
      RETURN
      END


      SUBROUTINE SUFIX(NAME,SUF)
      CHARACTER*1 NAME(51),SUF(3),SUF1
      DATA SUF1/'.'/
      DO 1 I=1,51
      IF(NAME(I).EQ.'.')GOTO4
1     IF(NAME(I).EQ.' ')GOTO3
      WRITE(*,2)NAME
2     FORMAT(' ***** ERROR ***** ERROR ***** ERROR *****',//,
     1 A,//,' exceeds the limit on file names (50 characters)')
      STOP
3     NAME(I)=SUF1
4     NAME(I+1)=SUF(1)
      NAME(I+2)=SUF(2)
      NAME(I+3)=SUF(3)
      RETURN
      END

      FUNCTION DOT(A,B)
      DIMENSION A(3),B(3)
      DOT=A(1)*B(1)+A(2)*B(2)+A(3)*B(3)
      RETURN
      END
