      SUBROUTINE PLOT(IPX,IG,YN,LU,ND)
C
      COMMON /CRD/AUSX,BUSX,AUSY,BUSY,ASUX,BSUX,ASUY,BSUY,AMSX,BMSX
     1,ASMX,BSMX,AUMX,BUMX,AMUX,BMUX,DSP,ITERM,N,NS,NF
      COMMON /SCN/ XS,XF,YS,YF,XXS,XXF,YYS,YYF,CSTR,CFIN,CYMIN,CYMAX,STR
     1,FIN,YMIN,YMAX,ZZ,CW,CH,ISP,JSP,IFL
C
      BYTE DSP,ISP,JSP,IG,ITERM,IFL,ND
C
C  IPX IS MATRIX CONTAINING PEAK NUMBER FROM SEARCH
C  IPX(M) M IS PEAK NUMBER 1 TO 100
C  IPX CONTAINS PEAK POSITION AS INDEX IN B MATRIX 1 TO ?
C
      DIMENSION IPX(1)
C
      IF(ISP.EQ.3.AND.YYF.GT.(YF-3*CH)) YYF=YF-3*CH !NEEDED FOR OVERLAY
      DL=1.0
      M=1
      IS=1
      IF(NS.LT.1) READ(LU'1) B
      IF(NS.GE.1) READ(LU'NS) B
      YX=B
      IF(DSP.EQ.2) YX=ALOG10(B+ZZ)
      IF(DSP.EQ.3) YX=SQRT(B+ZZ)
      IF(DSP.EQ.4) YX=B*YN
      IF(DSP.GT.4) YX=0.
      Y=AUSY*YX+BUSY
      X=NS
      IF(X.LT.1.0) X=1.0
      X=AMSX*X+BMSX
      IF(Y.GT.YYF) Y=YYF
      IF(Y.LT.YYS) Y=YYS
      IF(X.LT.XXS) X=XXS
      IF(X.GT.XXF) X=XXF
      CALL TPLOT(0,X,Y)
C PLOT THE DATA
      IF(NF.GT.N) NF=N
      YX=CYMIN
      IF(YX.LT.0.0) YX=0.0
C
      DO 10 I=NS,NF
      IF(IPX(M).LT.0) GO TO 23
   24 IF(IPX(M).GT.I) GO TO 23
      IF(M.GT.99) GO TO 23
      IF(IPX(M).EQ.I) GO TO 23
      M=M+1
      GO TO 24
   23 CONTINUE
      J=I
      IF(J.LT.1) GO TO 10
      READ(LU'J) B
      X=I
      X=AMSX*X+BMSX
      IF(DSP.EQ.0) YX=B
      IF(DSP.EQ.1) YX=YX+B
      IF(DSP.EQ.2) YX=ALOG10(B+ZZ)
      IF(DSP.EQ.3) YX=SQRT(B+ZZ)
      IF(DSP.EQ.4) YX=YN*B
      IF(DSP.GT.4) CALL DER(DSP,LU,ND,YX,J,N,IS)
      IF(IS.EQ.0) GO TO 20
      Y=AUSY*YX+BUSY
      IF(IPX(M).NE.I) GO TO 20
      X1=X-CW
      Y1=Y+CH/2
      IF(FLOAT(M/2).EQ.(FLOAT(M)/2)) Y1=Y+CH
      IF(Y1.GT.YYF) Y1=YYF
      CALL TPLOT(2,X1,Y1)
      WRITE(7,15) M
   15 FORMAT(1H+,$,I2)
      M=M+1
      CALL TPLOT(0,XXL,YYL)
   20 CONTINUE
      IF(Y.GT.YYF) Y=YYF
      IF(Y.LT.YYS) Y=YYS
      IF(X.GT.XXF) X=XXF
      IF(X.LT.XXS) X=XXS
      TY=ABS(YYL-Y)
      TX=ABS(XXL-X)
      IF(TY.LT.DL.AND.TX.LT.DL) GO TO 10
      CALL TPLOT(1,X,Y)
      XXL=X
      YYL=Y
   10 CONTINUE
C
      CALL TPLOT(1,X,Y)
      CALL TPLOT(2,0,0)
      RETURN
      END
C
      SUBROUTINE PKPLOT(TYP,JIN,FN,XW,YW,IN)
C
      COMMON /CRD/AUSX,BUSX,AUSY,BUSY,ASUX,BSUX,ASUY,BSUY,AMSX,BMSX
     1,ASMX,BSMX,AUMX,BUMX,AMUX,BMUX,DSP,ITERM,N,NS,NF
      COMMON /SCN/ XS,XF,YS,YF,XXS,XXF,YYS,YYF,CSTR,CFIN,CYMIN,CYMAX,STR
     1,FIN,YMIN,YMAX,ZZ,CW,CH,ISP,JSP,IFL
C
      BYTE DSP,ITERM,TYP,FN(9,30),F(30),IFL
      BYTE JIN,ISP,JSP,DUM
      DIMENSION TT(100),XIN(100)
C
	DO 1 I=1,30
1	F(I)=FN(1,I)
C
      IF(YYF.GT.(YF-66.)) YYF=YF-66.
      YNM=1.0
      IFL=1
      IF(TYP.EQ.4) GO TO 41
      CALL CLOSE(3)
      IF(TYP.EQ.0.OR.TYP.EQ.2.AND.JIN.EQ.7) GO TO 200     ! SPECPLOT BINARY
      IF(JIN.EQ.7) GO TO 4
      CALL TPLOT(2,XW,YW)
      WRITE(7,2010)
 2010 FORMAT('+',$,'SPC FILE=')
      READ(7,2011) IQ,F
 2011 FORMAT(Q,30A1)
      F(IQ+1)=0
      W=(10+IQ)*CW
      XW=XW+W
      CALL XWYW(XW,YW,XS,XF,W,CW,CH)
      IF(IQ.EQ.0) RETURN
    4 J=0
      DO 20 I=1,30
   20 IF(F(I).EQ.'.') J=I
      IF(J.GT.0) GO TO 5
      IF(J.EQ.0) F(IQ+1)='.'
      IF(J.EQ.0) J=IQ+1
      F(J+1)='S'
      F(J+2)='P'
      F(J+3)='C'
      F(J+4)=0
C
      CALL CLOSE(8)
      OPEN(UNIT=8,FILE=F,STATUS='OLD',SHARED,ERR=900
     1,RECL=76,FORM='FORMATTED')   ! CHANGHED RPG 7/1985
C
    5 IF(TYP.EQ.0.OR.TYP.EQ.2) GO TO 200
      OPEN(UNIT=3,NAME=F,TYPE='OLD',SHARED,ACCESS='DIRECT',RECORDSIZE=1
     1,ERR=900)
   41 READ(3'42) NPK
      NN=50+3*NPK
      RIMAX=0.0
      DO 101 I=51,NN,3
      J=I
      READ(3'J) T2
      IF(T2.LT.CSTR) GO TO 101
      IF(T2.GT.CFIN) GO TO 101
      K=J+1
      READ(3'K) RI
      YX=RI
      IF(DSP.EQ.2) YX=ALOG10(RI+ZZ)
      IF(DSP.EQ.3) YX=SQRT(RI+ZZ)
      IF(YX.GT.RIMAX) RIMAX=YX
  101 CONTINUE
      IF(RIMAX.LE.0.0) RIMAX=100.0  ! JUST IN CASE
C      YNM=CYMAX/RIMAX
      YNM=1.0 
      DO 100 I=51,NN,3
      J=I
      READ(3'J) T2
      IF(T2.LT.CSTR) GO TO 100
      IF(T2.GT.CFIN) GO TO 100
      K=J+1
       READ(3'K) RI
      YX=RI
      IF(DSP.EQ.2) YX=ALOG10(RI+ZZ)
      IF(DSP.EQ.3) YX=SQRT(RI+ZZ)
       RI=YNM*YX
      X=AUSX*T2+BUSX
      Y=AUSY*RI+BUSY
      IF(Y.GT.YYF) Y=YYF
      IF(Y.LT.YYS) GO TO 100
      CALL TPLOT(0,X,YYS)
      CALL TPLOT(1,X,Y)
  100 CONTINUE
      CALL TPLOT(2,X,Y)
      IFL=IFL+1
      RETURN
  200 IF(TYP.EQ.1.OR.TYP.EQ.3) RETURN
	REWIND 8  
  300 RIMAX=0.0
      DO 310 I=1,6
  310 READ(8,2000,END=900) A
 2000 FORMAT(A1)
      NPK=1
  301 IF(IN.NE.'ED') READ(8,2100,END=320) TT(NPK),XIN(NPK)
 2100 FORMAT(4X,F9.0,18X,F9.0)
      IF(IN.EQ.'ED') READ(8,2101,END=320) TT(NPK),XIN(NPK)
 2101 FORMAT(4X,F9.0,9X,F9.0)
      IF(TT(NPK).LT.CSTR) GO TO 311
      IF(TT(NPK).GT.CFIN) GO TO 311
      IF(XIN(NPK).GT.RIMAX) RIMAX=XIN(NPK)
  311 NPK=NPK+1
      GO TO 301
C
  320 NPK=NPK-1
      IF(RIMAX.LE.0.0) RIMAX=100.0 ! WHY NOT
      YNM=1.0
C      YNM=CYMAX/RIMAX
C
      DO 330 I=1,NPK
      IF(TT(I).LT.CSTR) GO TO 330
      IF(TT(I).GT.CFIN) GO TO 330
      IF(DSP.EQ.0) YX=XIN(I)
      IF(DSP.EQ.1) YX=YX+XIN(I)
      IF(DSP.EQ.2) YX=ALOG10(XIN(I)+ZZ)
      IF(DSP.EQ.3) YX=SQRT(XIN(I)+ZZ)
      IF(DSP.EQ.4) YX=YN*XIN(I)
      IF(DSP.GT.4) CALL DER(DSP,LU,ND,YX,J,N,IS)
      RI=YNM*YX
      X=AUSX*TT(I)+BUSX
      Y=AUSY*RI+BUSY
      IF(Y.GT.YYF) Y=YYF
      CALL TPLOT(0,X,YYS)
      CALL TPLOT(1,X,Y)
  330 CONTINUE
      CALL TPLOT(2,X,Y)
      IFL=IFL+1
      RETURN
  900 TYPE *,'***************************************************'
      WRITE(6,1000) F
 1000 FORMAT(1X,'PROBLEM OPENING FILE ',30A1)
      TYPE *,'DOES IT EXIST  ?'
      TYPE *,'***************************************************'
      STOP 'WHAT CHOICE'
      END
