      program aposmooth
C..........................................................Apr 2023
C
C Smooth Apo index 1995-2023 with 7h running median centered on given UT
C
      CHARACTER*64 infile,outfile
      CHARACTER*4 AYEAR
	CHARACTER*2 AYR,AMN,ADY
	DIMENSION KCC(0:7),kx1(7),kx2(7),ires(0:23),IM(12)
	DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
CC
       infile='d:\Apkp\Hpo\Hpo19952023.txt'
	OPEN(10,file=infile,action='READ')
	outfile='d:\Apkp\Hpo\ApoYEARs.txt'
	iflag=0  !!!!!!!!!!!!!!!!!!!!!
C++++++++++++++++++++
	DO 140 iyear=1995,2023
	call blet4(iyear,AYEAR)
	outfile(16:19)=AYEAR
C
	OPEN(11,file=outfile)
C
	iyr=iyear-iyear/100*100
	call blet2(iyr,AYR)
		year=float(iyear)
 	if(int(year/4.)*4.eq.iyear) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF
C
        imn2=12
	if (iyear.eq.2023) imn2=3 !TEMPPPPPPPPPPPP
C ++++++++++++++++++++++++++
       DO 141 imn=1,imn2
	ndmn=IM(imn)
	call blet2(imn,AMN)
C +++++++++++++++++++
        DO 142 idy=1,ndmn
	call blet2(idy,ADY)
C
C++++++++++++++++++++
         DO 143 iut=0,23
      if ((iyear.eq.1995).and.(imn.eq.1).and.(idy.eq.1).and.(iut.lt.3))
	+  then
	ires(iut)=0
	goto 143
	  endif
	IF (IFLAG.eq.0) THEN
	DO kk=1,7
	read(10,*,end=55) kyear,kmn,kdy,utk,ut05,ryrx,ryry,xhpo,KCC(kk),kad
	ENDDO
	iflag=1
		                ELSE
	read(10,*,end=55) kyear,kmn,kdy,utk,ut05,ryrx,ryry,xhpo,KCC(7),kad
	ENDIF
C
C Move data up for 1 value
C
      DO mm=1,7
	KCC(mm-1)=KCC(mm)
	ENDDO
C Median-7
	      MM=0
      DO N=1,7
      KX1(N)=KCC(N-1)
	KX2(N)=0
      ENDDO 
  880 KXX=0
CREM MAX X1(N)
  890 DO N=1,7
  900 IF (KXX.lt.KX1(N)) THEN 
      KXX=KX1(N)
	ENDIF
      ENDDO      !N
  920 IF (KXX.eq.0) GOTO 980
C
  930 DO 960 N=1,7
      IF (KX1(N).lt.KXX) GOTO 960
      MM=MM+1 
      KX2(MM)=KXX 
	KX1(N)=0.
  960 CONTINUE      ! N
  970 IF (MM.lt.7) GOTO 880
  980 	MED=KX2(4)
C
C 1020 XME(M)=XMD
       ires(iut)=MED
  143 CONTINUE ! UT
C------------------------
Output 3h median
       write(*,31) AYR,AMN,ADY,(ires(k),k=0,23)
	write(11,31) AYR,AMN,ADY,(ires(k),k=0,23)
  142 CONTINUE   ! day
C------------------------
  141 CONTINUE   ! month
C------------------------
      close(11)  
  140 CONTINUE	!year
  31  format(3A2,24(1X,I4))
  55	close(10)
	pause ' '
	STOP
	END