	PROGRAM GSASMH
C
C
C	VERSION 2.1 FEB - 85 G. GUELFI QEII
C
C	TO TEMPORAL SMOOTH GATED STUDIES
C
C
C
	DIMENSION IQ1(1024),IQ2(1024),IQ3(1024)
	DIMENSION B(3,42)
	INTEGER*2 ADMN(256),IP(75)
	LOGICAL*1 IQ1B(2048),IQ2B(2048),IQ3B(2048)
	LOGICAL*1 TITLE(12),IB(512)
	EQUIVALENCE (IQ1,IQ1B),(IQ2,IQ2B,IB,ADMN),(IQ3,IQ3B,B)
	REAL*8 SVAR00,PROCES
	DATA SVAR00/12RSVASVAR00SYS/,PROCES/12RSY1PROCESX00/
	DATA TITLE/'T','E','M','P','O','R','A','L',' ','S','M','1'/
C
C	OPEN SVAR00
C
	CALL SAVOP(0,ISAVE0,ADMN,IERR)
	CALL CRTFIL(ADMN,NB,ISTUDY,1)
C
C	READ ADMIN BLOCK
C
	ITP = 0
	CALL READW(256,ADMN,ITP,ISTUDY)
	CALL FGADM1(ADMN,IP,B)
	NSBLKS = IP(3)		!STUDY LENGTH
	MDOFF = IP(6)
	NFRAME = IP(11)
	NBKFRM = 2**IP(12)
	NMDWDS = NBKFRM*256
C	TYPE *,NMDWDS,NBKFRM,IP(11),IP(12),IP(3),NSBLKS,MDOFF
C
C	CHECK IF PROCES.X00 ON SY1:
C
	CALL CRTFIL(PROCES,NBLKS,ISMTH,1)
	IF (NBLKS.EQ.-2.OR.NBLKS.LT.NSBLKS)
	1 CALL CRTFIL(PROCES,NSBLKS,ISMTH,0)
C
C	MOVE HEADER AND DATA TO MDOFF
C
	IRD = 0
	IWT = 0
	DO 17 I = 1,MDOFF
	CALL READW(256,ADMN,IRD,ISTUDY)
	IF (I.EQ.1) CALL HDCHG(ADMN,TITLE)
17	CALL WRITW(256,ADMN,IWT,ISMTH)
C
C	PERFORM THREE POINT TEMPORAL SMOOTH
C
	IRD1 = MDOFF
	IRD2 = MDOFF + NBKFRM
	IRD3 = MDOFF + NBKFRM*2
	IWT = MDOFF
	IBUFSZ = 1024
	DO 50 JJJ = 1,NFRAME
	J = JJJ
	DO 30 M = 1,NMDWDS/IBUFSZ
	CALL READW(IBUFSZ,IQ1,IRD1,ISTUDY)
	CALL READW(IBUFSZ,IQ2,IRD2,ISTUDY)
	IF (J.NE.NFRAME) CALL READW(IBUFSZ,IQ3,IRD3,ISTUDY)
C
C	WORD OR BYTE
C
	IF (MOD(IP(12),2).EQ.0) GOTO 39
C
C	BYTE DATA
C
	DO 20 I = 1,IBUFSZ/2
	IF (J.EQ.1.OR.J.EQ.NFRAME) GOTO 25
C	CNT = CNT + IQ1B(I) + IQ2B(I) + IQ3B(I)
	IAV = (IBYTE(IQ1B(I)) + (IBYTE(IQ2B(I)) * 2) + IBYTE(IQ3B(I)))/4
	IQ1B(I) = LBYTE(IAV)
	GOTO 20
C
C	FOR FIRST AND LAST FRAME
C
25	IF (J.EQ.1) IAV = (IBYTE(IQ1B(I)) * 2 + IBYTE(IQ2B(I)))/3
	IF (J.EQ.NFRAME) IAV = (IBYTE(IQ1B(I)) + IBYTE(IQ2B(I))*2)/3
	IQ1B(I) = LBYTE(IAV)
20	CONTINUE
	GOTO 30
C
C	WORD DATA
C
39	DO 40 I = 1,IBUFSZ
	IF (J.EQ.1.OR.J.EQ.NFRAME) GOTO 35
C	CNT = CNT + IQ1(I) + IQ2(I) + IQ3(I)
	IQ1(I) = (IQ1(I) + IQ2(I) * 2 + IQ3(I))/4
	GOTO 40
C
C	FOR FIRST AND LAST FRAME
C
35	IF (J.EQ.1) IQ1(I) = (IQ1(I) * 2 + IQ2(I))/3
	IF (J.EQ.NFRAME) IQ1(I) = (IQ1(I) + IQ2(I)*2)/3
40	CONTINUE
30	CALL WRITW(IBUFSZ,IQ1,IWT,ISMTH)
C	WRITE(7,200)CNT
200	FORMAT(F10.0)
	IRD1 = MDOFF + NBKFRM*(J-1)
	IRD2 = MDOFF + NBKFRM + NBKFRM*(J-1)
	IRD3 = MDOFF + NBKFRM + NBKFRM*(J-1)
	IF (IRD3.GT.(NBKFRM*NFRAME+MDOFF)) GOTO 55
50	CONTINUE
55	CALL FIXSVH(ISAVE0,ISMTH,PROCES,TITLE,IQ1,IQ1B
	1,IQ2,IB,NMDWDS,IBUFSZ,IP(12))
C
C	CLOSE ALL CHANNELS
C
	DO 56 J = 1,10
	I = J - 1
	CALL CLOSEC(I)
56	CALL IFREEC(I)
	CALL BGAMMA('CA')
	END
