      PROGRAM SPECPL      !  PDP 11/44  VERSION SMOOTH ADDED 2/1985
C
C  PROGRAM WRITTEN BY RAY GOEHNER  12/83
C  THIS IS A COMPLETELY NEW VERSION OF SPECPLOT
C    REVISED FOR DIFFRAC 11 FORMAT 1/1985
C    RTEVSED FORINSTRUMENT TYPE EDS
C  INST SUPPORTED DIF,XRF,EDS
C
C DSP=0 LINEAR DISPLAY
C DSP=1 INTEGRAL DATA DISPLAY IE LAST PT IS THE TOTAL INTENSITY IN SCAN
C DSP=2 LOG BASE 10 DISPLAY  (SEMI-LOG)
C DSP=3 SQRT  SQUARE ROOT DISPLAY IE CONSTANT STATISTICS
C DSP=4 NORMALIZED INTENSITY
C DSP=5 1ST DERIVATIVE
C DSP=6 2ND DERIVATIVE
C
C IX=0 2THETA LABEL
C IX=1 DSPACING LABEL / WAVELENGTH
C
C INTENSITY IS CONVERTED INTO CNTS PER SECONDS YCR OR DY
C
C SEE COORD FOR DESCRIPTION OF COORDINATE SYSTEMS
C
C  B IS DATA MATRIX IE INTENSITY IN CPS
C  N TOTAL NUMBER OF PTS
C  ZZ IS OFFSET NEEDED FOR SQRT OR LOG DISPLAY
C  IG=0 NO GRID LINES  IG=1 GRID LINES
C  INST CONTAINS INSTRUMENT IE DIF OR XRF
C
C  ISP=1 FULL SCREEN DISPLAY
C  ISP=2 SPIT SCREEN DISPLAY
C  ISP=3 OVERLAY
C  ISP=4 ADDITION OF SPECTRUM
C  ISP=5 SUBTRACTION OF SPECTRUM
C  JSP= NUMBER OF SPECTRUM INVOLVED
C
C  YD USER SPECIFIED DISPLAY DIFFERENCE IE AMOUNT OF DISPLACEMENT
C
C ITERM=0  4010
C ITERM=1  4027
C ITERM=2  4662 PLOTTER
C ITERM=3  
C ITERM=4  
C ITERM=5  4105
C ITERM=6  VT240
C
C  TYP=0 SPECPLOT.RAW
C  TYP=1 SPECPLOT.DIF
C  TYP=2 DIFF11.RAW
C  TYP=3 DIFF11.DIF
C  TYP=3 DIF500.RAW
C  TYP=4 DIFF500.DIF
C  TYP=6 ASCII PGT DATA
C  TYP=7 ASCII PIXE DATA
C  TYP=8 RAW DENSITOMETER
C
C  JIN = 1 TOP
C  JIN = 2 ALL
C  JIN = 3 PROFILE FIT
C  JIN = 4 CURSXY ISH USED
C  JIN = 5 MARKER (ELEMENTAL MARKERS)
C  JIN = 6 JCPDS (PDF MARKERS)
C  JIN = 7 (F) DISPLAY RESULTS OF PK SEARCH (TYP=2 OR 3)
C  JIN = 8 (G) DISPLAY RESULTS OF SPC OR PKS FILE (TYP 2 OR 3)
C  JIN = 9 (B) POLYNOMIAL BKGR SUBTRACT
C
C  JIN=20  E EXPAND WINDOWED REGION
C  JIN=21  R REPLOT
C  JIN=22  RF OR # REFRESH SCREEN
C  JIN=23  I,S,N, OR Q CHANGE DISPLAY
C  JIN=24  E COMMAND FOR OVERLAYED FILES
C
C  JIN=30  RETURN TO MAIN MENU
C
C  LU-1 FILES (DEFAULT FILE)
C  LU-2 ROOT.MAT INTENSITY FILE (B MATRIX)
C  LU-3 SPECPLOT DATA FILE
C  LU-5  TTY: READ
C  LU-6  TTY: WRITE
C  LU-7  GRAPHICS
C  LU-8  RESULT FILE PEAK FINDING INTERACTIVE COMMANDS ETC *.SPC
C  LU-9  ROOT.DIF (NORMALIZED RESULT FILE)
C  LU-10 ROOT.DSP  SEARCH/MATCH INPUT FILE
C  LU-11 JCPDS INDEXFILE  D2INDEX.PDF
C  LU-11 PDF FILE SM0:[5,3]PDF.SET#
C  LU-11 ELEMENTAL DATA BASE
C  LU-11 D4CHART.HLP
C  LU-12 USED FOR ADD & SUBTRACT OF SPECTRA
C  LU-13 ROOT.BKGR (MAT TYPE FILE)
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
     1,STR,FIN,YMIN,YMAX,ZZ,CW,CH,ISP,JSP,IFL
      COMMON /XY/X,Y,X1,Y1,X2,Y2
      COMMON /LBL/LABX,LABY,LX0,LX1,LX2,LX3,LY0,LY1,LY2,LY3,LY4,LY5,LY6 !BYTE ARRAYS
      COMMON /HEAD/ SNUM,MON,DATE,YEAR,TIT,DX,YCR,INST,JTG,CRY
     1,DSPACE,YL
      COMMON /MFG/ KBKGR,KK2,KSCH,KNRM,KSTD
      COMMON /COLOR/CL,CLB,CLA,CLG   
      COMMON /DPRC/ IDPRC,IBKGR,ISCH,INRM,ISTD,NDER,MF,MB,IK2,ISM,IPF
     1,NT,SENS,INSTD,EXSTD,E2T
C
      BYTE IBKGR,IK2,ISCH,INRM,IDPRC,ISTD,NDER,MF,MB,ISM,INSTD(30)
     1,EXSTD(30),KBKGR,KK2,KSCH,KNRM,KSTD
      BYTE DSP,ISP,JSP,IG,ITERM,IX,LABX(8),LABY(8),JIN,IFL,ISH,CRB
      BYTE D1(12),D2(12),D3(12),D4(12),LOGO(12),TYP,VER(72)
      BYTE FN(9,30),TIT(48),CL,CLB,CLA,CLG(5),F(30),DLT(5)
      BYTE LX0(8),LX1(8),LX2(8),LY0(8),LY1(8),LY2(8),LY3(8),LY4(8),
     1LY5(8),LY6(8),LX3(8)
      DIMENSION IPX(100),IWDL(10),IWDR(10)
      INTEGER SNUM,DATE,YEAR,INST(2),C1,WSPC
C  VMS MOD
      OPEN(UNIT=7,FILE='SYS$OUTPUT')
C
      CRB=29  ! GS CHARACTER IE CTRL ]
      CALL DFAULT(FN,DSP,IG,YD,ITERM,TYP,IX,D1,D2,D3,D4,LOGO,FR,VER,DLT
     1,INST)
C
      IF(ITERM.EQ.2) CALL T4662(0,0)
      IF(ITERM.EQ.0) CALL OUTPUT(CRB,1)
   11 DO 10 I=1,100
   10 IPX(I)=-1
C
      HY=0 ! YAXIS CURRENT OFFSET
      IFL=1 ! IFL IS CURRENT ROOT.MAT FILE
      JIN=0 ! NO INTERACTIVE COMMANDS
      ICL=2 ! 4662 COLOR
C
      CALL MENU(FN,DSP,IG,YD,ITERM,TYP,IX,D1,D2,D3,D4,LOGO,FR,VER
     1,INST)
C
  300 IF(ISP.EQ.4.OR.ISP.EQ.5) GO TO 301
C
      CALL PARAM(FN,IFL,SNUM,MON,DATE,YEAR,INST,TYP,TIT,STR,DX,YCR,JTG
     1,YL,FIN,N,CRY,DSPACE,DSP,ISP,JSP,IX,3,JIN)
C
C
C  DATA   IFL=IFL+1
      ITYP=1 ! DIF TYPE FILE
      IF(TYP.EQ.0.OR.TYP.EQ.2.OR.TYP.EQ.4.OR.TYP.EQ.6) ITYP=0 !RAW TYPE
      IF(TYP.EQ.7.OR.TYP.EQ.8)ITYP=0  !RAW TYPE
      IF(ITYP.EQ.0) CALL DATA(TYP,ISP,IFL,N,YCR,FN,IDPRC,2,3) 
      IF(TYP.EQ.8) FIN=N
      IF(ITYP.EQ.0.AND.ISM.GT.0)   CALL SMOOTH(N,ISM,2)
C
       IF(STR.EQ.-1.) GO TO 11
      GO TO 302
C
  301 CALL ADDSUB(FN,ISP,JSP,IFL,TYP,FR,STR,FIN,N,IDPRC)
C
  302 IF(ISP.EQ.2.OR.ISP.EQ.3) GO TO 311
C
      CALL OPEN8(STR,FN,F,ISP,IFL,JIN)  ! F IS FN(1,-)
C
      IF(IDPRC.EQ.0) GO TO 311  ! DATA PROCESSING SECTION
C
      CALL BKGR(2,YMIN,YMAX,AM,N,MF,MB,IBKGR,F)  ! AM CALCULATED HERE FOR SEARCH
C
      IF(IK2.GT.0.AND.INST(1).EQ.'DI') CALL KALP2(2,STR,DX,N,ITG,IK2)
C
      IF(ISCH.GT.0) THEN
	 CALL SEARCH(2,N,DX,YCR,STR,IPX,IWDL,IWDR,NWD,INST(1)
     1,ISCH,SENS,1,IPF,NT,YL,AM)
	 CALL OPEN8(STR,FN,F,ISP,IFL,1) !DON'T CREATE NEW VERSION
      ENDIF
C
C
  311 CALL MINMAX(N,DSP,NDER,TYP,ZZ,YMIN,YMAX,STR,FIN,DX,YN,2)
C
      CALL TERM(ITERM,CL,DSP,ISP,JSP,IFL,JIN,HY,CW,CH)
      IF(ISP.EQ.3) HY=HY+YD ! INCREMENT FOR OVERLAY
C
      IF(IFL.GT.2.AND.ISP.LT.4) GO TO 100
      IF(JIN.EQ.24) GO TO 100
C
   12 CALL SSUD(STR,FIN,CSTR,CFIN,YMIN,YMAX,CYMIN,CYMAX,YCR,ISP,JSP
     1,DSP,IFL,INST(1),YL,DSPACE,IX)
C
  100 CALL COORD(TYP)
C
      IF(IFL.GT.2.AND.ISP.LT.4) GO TO 200
      IF(ITERM.EQ.6) CALL VT240(2)
      CALL ERASE(ITERM,DSP,XW,YW,YF,CH,NN)
  200 IF(ITERM.EQ.3.OR.ITERM.EQ.4) CALL ENVIS(2)
      WSPC='33'
      C1='C1'
      IF(ITERM.EQ.1) CALL T4027(C1,WSPC,2)
      IF(ITERM.EQ.2) CALL T4662(1,1)
C
      CALL XAXIS(LABX,IG,IX,YL,DSPACE,INST(1),LOGO)
C
      CALL YAXIS(LABY,IG)
C
      C1='C3'
      IF(ITERM.EQ.1) CALL T4027(C1,WSPC,3)
      IF(ITERM.EQ.2) CALL T4662(1,ICL)
      ICL=ICL+1
      IF(ICL.GT.7) ICL=1
      IF(ITERM.EQ.3.OR.ITERM.EQ.4) CALL ENVIS(0)
C
      CALL HEADER(7,IFL,ISP,JSP,CH,YF,TYP,FN)
C
      IF(ITYP.EQ.0) CALL PLOT(IPX,IG,YN,2,NDER)
C
      IF(ITYP.NE.0) CALL PKPLOT(TYP,JIN,FN,XW,YW,INST(1)) ! DIF TYPE
C
      IF(IFL.GT.JSP) GO TO 310
      IF(ISP.LT.4) GO TO 300 ! MULTIPLE DISPLAY
C
  310 IF(ITERM.EQ.3.OR.ITERM.EQ.4) CALL ENVIS(0)
      C1='C4'
      IF(ITERM.EQ.1) CALL T4027(C1,WSPC,3)
      C1='C1'
C
      CALL INTER(NN,TYP,INST(1),YL,DSPACE,ID,BG,DB2,XH,JIN,ISH,IX,D4)
C
      IF(JIN.EQ.30) GO TO 420  ! 0 RETURN TO MENU
C
      IF(ITERM.EQ.1) CALL T4027(C1,WSPC,3)
      IF(ITERM.EQ.2) CALL T4662(1,ICL)
      ICL=ICL+1
      IF(ICL.GT.7) ICL=1
C
C   NN IS CURRENT NUMBER OF CURSOR COMMAND
C
      IF(TYP.EQ.1.OR.TYP.EQ.3.OR.TYP.EQ.5) THEN
      IF(JIN.EQ.1) GO TO 310
      IF(JIN.EQ.2) GO TO 310
      ENDIF
C
      IF(JIN.EQ.1) CALL TOP(2,DX,N,ASMX,BSMX,PX,NN,XW,YW,YL,DSPACE
     1,INST(1),IPF)
C
      IF(JIN.EQ.2) CALL ALL(2,DX,PX,XIN,DB2,BG,XH,NN,XW,YW,ISH,YL
     1,DSPACE,INST(1))
C
      IF(JIN.EQ.4) CALL CURSXY(X0,Y0,NN,XW,YW,ASUX,BSUX
     1,ASUY,BSUY,DSPACE,ISH,YL)
C
      IF(JIN.EQ.5) CALL MARKER(INST(1),XW,YW,NN,DSPACE,JIN,D3)
C
      IF(JIN.EQ.6) CALL JCPDS(INST(1),XW,YW,NN,D2,YL,CL,JIN)
C
      IF(JIN.EQ.7.OR.JIN.EQ.8) CALL PKPLOT(TYP,JIN,FN,XW,YW,INST(1))
C
      IF(JIN.EQ.9) CALL POLYBG(2,13,5,3,XW,YW,YN) ! POLY INTERACTIVE BKGR 
C
      IF(JIN.NE.23) GO TO 400  ! CHANGE DISPLAY
      CALL MINMAX(N,DSP,NDER,TYP,ZZ,YMIN,YMAX,STR,FIN,DX,YN,2)
      CYMIN=YMIN
      CYMAX=YMAX
      CXMIN=XMIN
      CXMAX=XMAX
      GO TO 100
  400 IF(JIN.NE.24) GO TO 401  ! EXPAND OVERLAY USING E
      IFL=1
      HY=0
      GO TO 300
C
  401 IF(JIN.GE.20) GO TO 100
      IF(JIN.GT.0) GO TO 310
  420 CALL ERASE(ITERM,DSP,XW,YW,YF,CH,NN)
      IF(ITERM.EQ.1) CALL T4027(0,0,1)
      IF(ITERM.EQ.2) CALL T4662(1,8)
      IF(ITERM.EQ.2) CALL T4662(0,0)
      IF(ITERM.EQ.3.OR.ITERM.EQ.4) CALL ENVIS(1)
      IF(ITERM.EQ.6) CALL VT240(1)
C
      IF(IDPRC.EQ.0) GO TO 403
      IF(INST(1).EQ.'XR'.OR.INST(1).EQ.'ED') GO TO 403
      IF(INRM.GT.0) CALL NORM(D2,YL,F,STR,FIN)  ! F IS ROOT FOR *.DSP AND *.DIF
C
  403 IQ=0
      IF(IBKGR.GT.0) CALL LDBKGR(F,IQ)
      IF(IQ.EQ.2) GO TO 410
C
  402 WRITE(6,1001)
 1001 FORMAT(1H$,'STOP -0, START OVER -1, DISPLAY -2 > ')
      READ(5,2000) IQ
 2000 FORMAT(I1)
  410 IFL=1
      HY=0
      JIN=0
      IF(IQ.EQ.2.AND.ISP.EQ.2) GO TO 300
      IF(IQ.EQ.2.AND.ISP.EQ.3) GO TO 300
      IFL=2  ! SUB DATA NOT READ
      IF(IQ.EQ.2) GO TO 12
      CALL CLFILE(DLT,ITERM)  ! CLOSE FILES
      IF(IQ.EQ.1) GO TO 11
C
  999 STOP  '  HAVE A GOOD DAY '
      END
