      SUBROUTINE PARAM(FN,IFL,SNUM,MON,DATE,YEAR,INST,TYP,TIT,STR,DX,YCR
     1,JTG,YL,FIN,NPTS,CRY,DSPACE,DSP,ISP,JSP,IX,LU,JIN)
C
      COMMON /LBL/ LABX,LABY,LX0,LX1,LX2,LX3,LY0,LY1,LY2,LY3,LY4,LY5,LY6
      INTEGER SNUM,DATE,YEAR
      DIMENSION INST(2)
      BYTE LABY(8),LABX(8),LX0(8),LX1(8),LX2(8),LX3(8),DSP,ISP,JSP,IFL
      BYTE LY1(8),LY3(8),LY2(8),LY0(8),FN(9,30),SF(30),LY4(8),IX
      BYTE TYP,R(12),MNT(3),TIT(48),LY5(8),LY6(8),JIN
C
      DATA LX0/'2',' ','T','H','E','T','A',' '/
      DATA LX1/'D','S','P','A','C','E',' ',' '/
      DATA LX2/'W',' ','L','E','N','G','T','H'/
      DATA LY0/'C','N','T','S','/','S','E','C'/
      DATA LX3/'E','N','E','R','G','Y',' ',' '/
      DATA LY1/'I','N','T','E','G','R','A','L'/
      DATA LY2/'L','O','G','1','0',' ',' ',' '/
      DATA LY3/'S','Q','R','T',' ',' ',' ',' '/
      DATA LY4/'N','O','R','M',' ',' ',' ',' '/
      DATA LY5/'1','S','T',' ','D','E','R',' '/
      DATA LY6/'2','N','D',' ','D','E','R',' '/
C
      IF(LU.LE.0) LU=3
      DO 2 I=1,30
    2 SF(I)=FN(IFL,I)
      DO 1 I=1,8
      LABX(I)=' '
    1 LABY(I)=' '
      CALL CLOSE(LU)
C
      IF(TYP.EQ.1) GO TO 600    ! *.SPC FILE
      IF(TYP.EQ.0) GO TO 403      ! SPECPLOT BINARY
C
C  THIS MOD PUT IN FOR ASCII PGT DATA -- DATA TYP=6
      IF(TYP.EQ.6)THEN
      OPEN(UNIT=LU,NAME=SF,TYPE='OLD',ERR=999)
      READ(LU,*)
      READ(LU,9000)TIT
9000  FORMAT(13X,48A1)
      READ(LU,9005)STR,DX,NPTS
9005  FORMAT(21X,F6.3,4X,F6.3,5X,I5)
      DY=1
      SNUM=1
      DX=DX/1000.
      FIN=NPTS*DX+STR
      CALL IDATE(MON,DATE,YEAR)
      GO TO 13
      ENDIF
C  MOD PUT IN FOR TYP=7 -- ASCII PIXIE DATA
      IF(TYP.EQ.7)THEN
      OPEN(UNIT=LU,NAME=SF,TYPE='OLD',ERR=999)
      NPTS=1024
      READ(LU,9007)(TIT(I),I=1,29)
9007  FORMAT(29A1)
      TIT(30)=' '
      READ(LU,9008)(TIT(I),I=31,48)
9008  FORMAT(18A1)
      READ(LU,*)
      READ(LU,9009)DX,STR
9009  FORMAT(16X,2(F8.0))
      READ(LU,*)
      FIN=NPTS*DX+STR
      CALL IDATE(MON,DATE,YEAR)
      SNUM=1
      DY=1
      GO TO 13
      ENDIF
C
C  THIS MOD PUT IN FOR TYP=8 -- ASCII DENSITOMETER DATA
      IF(TYP.EQ.8)THEN
      OPEN(UNIT=LU,NAME=SF,TYPE='OLD',ERR=999)
C  THERE'S NO HEADER INFO -- SO HARDWIRE TO DISPLAY DATA
C   NUMBER OF POINTS SET IN'DATA' SUBROUTINE
C   ENDING POINT SET IN 'SPECPLOT' MAIN AFTER 'DATA' HAS BEEN CALLED
      STR=1  ! STARTING POINT IS FIRST POINT
      DX=.01
      DY=1
      CALL IDATE(MON,DATE,YEAR)
      SNUM=1
      GO TO 13
      ENDIF     
C
      IF(TYP.EQ.2.OR.TYP.EQ.3) THEN
      OPEN(UNIT=LU,NAME=SF,TYPE='OLD',ACCESS='DIRECT',RECORDSIZE=1)
c     1,ERR=999)
C   DIFFRAC 11 TYPE
      READ(LU'5) (R(I),I=1,4)
      READ(LU'6) (R(I),I=5,8)    
      READ(LU'7) (R(I),I=9,12)
C
      DECODE(12,401,R) DATE,MNT,YEAR
  401 FORMAT(I2,1X,3A1,1X,I2)
      IF(MNT(1).EQ.'J'.AND.MNT(2).EQ.'A') MON=1
      IF(MNT(1).EQ.'F') MON=2
      IF(MNT(1).EQ.'M'.AND.MNT(3).EQ.'R') MON=3
      IF(MNT(1).EQ.'A'.AND.MNT(2).EQ.'P') MON=4
      IF(MNT(1).EQ.'M'.AND.MNT(3).EQ.'Y') MON=5
      IF(MNT(1).EQ.'J'.AND.MNT(3).EQ.'N') MON=6
      IF(MNT(1).EQ.'J'.AND.MNT(3).EQ.'L') MON=7
      IF(MNT(1).EQ.'A'.AND.MNT(2).EQ.'U') MON=8
      IF(MNT(1).EQ.'S') MON=9
      IF(MNT(1).EQ.'O') MON=10
      IF(MNT(1).EQ.'N') MON=11
      IF(MNT(1).EQ.'D') MON=12
C
      J1=1
      J2=J1+3
      DO 404 I=10,12
      L=I
      READ(LU'L) (TIT(K),K=J1,J2)
      J1=J1+4
  404 J2=J1+3
      READ(LU'13) JTG,I
      READ(LU'14) YL
      READ(LU'31) STR
      READ(LU'32) FIN
      READ(LU'33) DX
      READ(LU'34) DY
      IF(DY.EQ.0) DY=1
      NPTS=1+(FIN-STR)/DX
      IF(TYP.EQ.2.AND.INST(1).EQ.'XR'.AND.IFL.EQ.1.AND.JIN.EQ.0) THEN
      CALL CRYS(DSPACE)
      CRY='    '
      ENDIF
      GO TO 13
      ENDIF
C
  403 OPEN(UNIT=LU,NAME=SF,FORM='UNFORMATTED',TYPE='OLD'
     1,ACCESS='SEQUENTIAL',SHARED,ERR=999)
      CALL ERRSET(67,.TRUE.,.FALSE.,.TRUE.,.FALSE.,31)
      NPTS=0
      READ(LU,ERR=990) SNUM,MON,DATE,YEAR,INST
      IN=INST(1)
      YL=0.0
      REWIND LU
      IF(IN.EQ.'ED') THEN
	READ(LU,ERR=990) SNUM,MON,DATE,YEAR,INST,TIT,T0,DX,DY,TMAX,NPTS
	STR=T0
	FIN=TMAX
	GO TO 13
      ENDIF
      IF(IN.EQ.'DI') READ(LU,ERR=11)SNUM,MON,DATE,YEAR,INST,TIT,T0,DX,DY
     1,JTG,YL,TMAX,NPTS
      STR=T0
      FIN=TMAX
      IF(YL.LT.0.3.OR.YL.GT.3.0) GO TO 52
      GO TO 10
   11 REWIND LU
      READ(LU,ERR=990) SNUM,MON,DATE,YEAR,INST,TIT,T0,DX,DY
      STR=T0
      FIN=0
      YL=1.5405981
      JTG='CU'
   10 ITG=JTG
      XL=YL
      GO TO 13
   52 CONTINUE
      IF(IN.EQ.'XR') READ(LU,ERR=12) SNUM,MON,DATE,YEAR,INST,TIT,T0,DX
     1,DY,CRY,DSPACE
      STR=T0
      FIN=0
      GO TO 13
   12 REWIND LU
      READ(LU,ERR=990) SNUM,MON,DATE,YEAR,INST,TIT,T0,DX,DY,CRY
      IF(CRY.EQ.'200S') DSPACE=4.027
      IF(CRY.EQ.'PET ') DSPACE=8.742
      IF(CRY.EQ.'GE  ') DSPACE=6.532
      IF(CRY.EQ.'TAP ') DSPACE=25.8
      IF(CRY.EQ.'220P') DSPACE=2.848
      IF(CRY.EQ.'200P') DSPACE=4.027
      IF(CRY.EQ.'220S') DSPACE=2.848
   13 IF(DSP.NE.0) GO TO 100
      DO 110 I=1,8
  110 LABY(I)=LY0(I)
  100 IF(DSP.NE.1) GO TO 200
      DO 210 I=1,8
  210 LABY(I)=LY1(I)
  200 IF(DSP.NE.2) GO TO 300
      DO 310 I=1,8
  310 LABY(I)=LY2(I)
  300 IF(DSP.NE.3) GO TO 400
      DO 311 I=1,8
  311 LABY(I)=LY3(I)
  400 IF(DSP.NE.4) GO TO 500
      DO 410 I=1,8
  410 LABY(I)=LY4(I)
  500 IF(DSP.NE.5) GO TO 510
      DO 501 I=1,8
  501 LABY(I)=LY5(I)
  510 IF(DSP.NE.6) GO TO 520
      DO 511 I=1,8
  511 LABY(I)=LY6(I)
  520 IF(IX.EQ.1.AND.INST(1).EQ.'XR') GO TO 331
      IF(IX.EQ.1.AND.INST(1).EQ.'DI') GO TO 321
      IF(INST(1).EQ.'ED') THEN
	DO 323 I=1,8
323	LABX(I)=LX3(I)
	GO TO 380
      ENDIF
      DO 330 I=1,8
  330 LABX(I)=LX0(I)
      GO TO 380
  321 DO 322 I=1,8
  322 LABX(I)=LX1(I)
      GO TO 380
  331 DO 332 I=1,8
  332 LABX(I)=LX2(I)
  380 YCR=1./DY
      IF(INST(1).EQ.'XR') YL=DSPACE  ! IT MAKES LIFE EASIER THIS WAY
      RETURN
C
  600 OPEN(UNIT=LU,NAME=SF,TYPE='OLD',ERR=999)
      READ(LU,2000,END=999) SNUM,MON,DATE,YEAR,INST,TIT
 2000 FORMAT(1X,I5,1X,I2,1X,I2,1X,I2,1X,2A2,4X,48A1)
      READ(LU,2010) T0,DX,DY
 2010 FORMAT(13X,F8.3,15X,F6.3,7X,F5.1)
      IF(INST(1).EQ.'XR') GO TO 610
      READ(LU,2020) JTG
 2020 FORMAT(32X,A2)
      IF(JTG.EQ.'CU') YL=1.5405981
      IF(JTG.EQ.'CR') YL=2.28961
      GO TO 620
  610 READ(LU,2020) CRY
  620 RETURN
  999 TYPE *,'********************************************************'
      WRITE(6,1000) SF
 1000 FORMAT(1X,'Problem opening file ',30A1)
      TYPE *,'Does this file exist ?'
      TYPE *,'*******************************************************'
      TYPE *,' '
      STR=-1.
      RETURN
  990 TYPE *,'***************************************************'
      TYPE *,' '
      WRITE(6,1001) SF
 1001 FORMAT(1X,'Can not read header of file ',30A1)
      TYPE *,' '
      TYPE *,'*****************************************************'
      STOP
      END
C
      SUBROUTINE DATA(TYP,ISP,IFL,N,YCR,FN,IDPRC,LU2,LU3)
C
C YCR IS RECIPROCAL OF DWELL TIME
C
C LU2  *.MAT FILE
C LU3  DATA FILE
C
      DIMENSION A(32)
      DIMENSION C(8)  ! THIS ARRAY FOR TYP=7 DATA
      DIMENSION IC(16) !THIS ARRAY FOR TYP=8 DATA
      BYTE FN(9,30),IFL,F(30),TYP,ISP,IDPRC
C
      CALL ERRSET(29,.TRUE.,.FALSE.,.TRUE.,.FALSE.,31)
      CALL ERRSET(30,.TRUE.,.FALSE.,.TRUE.,.FALSE.,31)
C
     	WRITE(6,1234)
1234	FORMAT('$Data acquisition in process... Please wait...')

      CALL CLOSE(LU2)
      NPTS=N
      OPEN(UNIT=LU2,TYPE='SCRATCH',ACCESS='DIRECT',RECORDSIZE=1,ERR=999)
      IF(TYP.EQ.0) GO TO 10
      IF(TYP.EQ.2)THEN
      NOFF=35
      DO 300 I=1,N
      J=I+NOFF
      READ(LU3'J) B
      L=I
  300 WRITE(LU2'L) B
      GO TO 95
      ENDIF
C
C  THIS CODE FOR TYP=6 DATA -- ASCII PGT
      IF(TYP.EQ.6)THEN
      N=0
  100 READ(LU3,3100,END=120,ERR=120)N1,EV1,C1,N2,EV2,C2
3100  FORMAT(1X,I4,2X,F8.3,1X,F9.3,6X,I4,2X,F8.3,1X,F9.3)
      C1=C1*YCR 
      C2=C2*YCR
      WRITE(LU2'N1)C1
      WRITE(LU2'N2)C2
      N=N+2
      GO TO 100
120   CONTINUE
      GO TO 95
      ENDIF
C  THIS CODE FOR TYP=7 DATA -- ASCII PIXE
      IF(TYP.EQ.7)THEN
      N=0
      DO 122 I=1,128
      READ(LU3,3110,END=95,ERR=95)(C(J),J=1,8)
3110  FORMAT(8(2X,F6.0))
      DO 121 K=1,8
      N=N+1
      T=C(K)*YCR
      WRITE(LU2'N)T
121   CONTINUE
122   CONTINUE
      GO TO 95
      ENDIF
C  THIS CODE FOR TYP=8 -- ASCII DENSITOMETER DATA
      IF(TYP.EQ.8)THEN
      N=0
132   READ(LU3,3210,END=134,ERR=134)IC
3210  FORMAT(I3,15(1X,I3))
      DO 133 K=1,16
      N=N+1
      T=IC(K)
      WRITE(LU2'N)T
133   CONTINUE
      GO TO 132
134   NPTS=N
      GO TO 95
      ENDIF      
C    
   10 N=0
   11 READ(LU3,END=95) A
      DO 20 I=1,32
      IF(A(I).LT.0) GO TO 95
      N=N+1
      T=A(I)*YCR
      WRITE(LU2'N) T
   20 CONTINUE
      GO TO 11
   95 CONTINUE
      IF(NPTS.GT.0.AND.NPTS.LT.N) N=NPTS
      CALL CLOSE(LU3)
C
      IFL=IFL+1      ! UPDATE THE NUMBER OF THE FILE READ
C
      RETURN
  999 TYPE *,'******************************************************'
      TYPE *,'              FATAL ERROR'
      TYPE *,' '
      WRITE(6,1000) F,TYP
 1000 FORMAT(1X,30A1,'FILE IS NOT TYPE=',I1)
      TYPE *,' '
      TYPE *,'*******************************************************'
      TYPE *,'            START OVER'
      STOP 'PROBLEMS PROBLEMS'
      END
C
      SUBROUTINE ADDSUB(FN,ISP,JSP,IFL,TYP,FR,STR,FIN,NPTS,IDPRC,DSP)
C
C  FR FRACTION OF FILE TO ADD OR SUBTRACT
C
      COMMON /HEAD/ SNUM,MON,DATE,YEAR,TIT,DX,YCR,INST,ITG,CRY
     1,DSPACE,YL
      COMMON /LBL/ LABX,LABY,LX0,LX1,LX2,LY0,LY1,LY2,LY3,LY4
C
      INTEGER SNUM,DATE,YEAR,INST(2)
      BYTE TIT(48),IFL,ISP,JSP,IX,TYP,DSP,IDPRC
      BYTE LABX(8),LABY(8),LX0(8),LX1(8),LX2(8),LY1(8),LY2(8),LY0(8),
     1LY3(8),LY4(8),FN(9,30)
C
      CALL PARAM(FN,IFL,SNUM,MON,DATE,YEAR,INST,TYP,TIT,STR1,DX1,YCR
     1,ITG1,YL,FIN,N1,CRY,DSPACE,DSP,ISP,JSP,IX,3)
      CALL DATA(TYP,ISP,IFL,N1,YCR,FN,IDPRC,2,3)
      DO 100 I=1,JSP-1
      CALL PARAM(FN,IFL,SNUM,MON,DATE,YEAR,INST,TYP,TIT,STR,DX,YCR
     1,ITG,YL,FIN,N,CRY,DSPACE,DSP,ISP,JSP,IX,3)
      IF(STR1.NE.STR) GO TO 990
      IF(DX1.NE.DX) GO TO 990
      IF(ITG1.NE.ITG) GO TO 991
      CALL DATA(TYP,ISP,IFL,N,YCR,FN,IDPRC,12,3)
      IF(N1.LT.N) N=N1
      DO 100 J=1,N
      K=J
      READ(2'K) B1
      READ(12'K) B2
      IF(ISP.EQ.4) B=B1+FR*B2
      IF(ISP.EQ.5) B=B1-FR*B2
  100 WRITE(2'K) B
      CLOSE(UNIT=12)
      NPTS=N
      RETURN
  990 TYPE *,'*******************************************************'
      TYPE *,'STARTING ANGLE AND STEP SIZE HAS TO BE THE SAME FOR EACH'
      TYPE *,'FILE PROCESSED'
      TYPE *,'******************************************************'
      STOP  ' SORRY '
  991 TYPE *,'************************************************'
      TYPE *,'BOTH SPECTRA HAVE TO DONE UNDER SAME RADIATION'
      TYPE *,'************************************************'
      STOP  'SUCH IS LIFE'
       END
