      program histkpapaa1
C..............................................................Dec. 2019
C
      character*64 infile,outfile 
	character*4 AYEAR
	character*2 AYR
	dimension ithkp(5),icnkp(5),icnap(5),icnaa(5)
C	+,minap(5),minaa(5)
	+,ithap(5),ithaa(5)	   ! add new: ap, aa thresholds
	data ithkp/30,40,60,70,80/
C	+,ithap/15,48,80,132,207/,ithaa/37,73,105,154,276/ !add new:thap(thkp),thaa(ap) thresholds
	+,ithap/15,27,80,132,207/,ithaa/24,35,90,145,229/ !add new:thap(thkp),thaa(ap) thresholds
	infile='d:/apkp/aa/aakpYEAR'
	outfile='histkpapaa.txt'
	OPEN(11,FILE=outfile,access='APPEND')
C
Crem		iyear=1963   !START+++++++++++++++++++++
		iyear=1995   !START+++++++++++++++++++++
	icntot=0
	icnkpt=0
	icnapt=0
	icnaat=0
       a =  5.885
       b =  0.6732
	rmse=17.5

	do i=1,5
	icnkp(i)=0
	icnap(i)=0
	icnaa(i)=0
Crem	thaa=a*ithap(i)**b-rmse
Crem	ithaa(i)=nint(thaa)
C	minap(i)=0
C	minaa(i)=0
	enddo
Crem	do 400 KK=1,56	! 1964-2019
	do 400 KK=1,24	! 1964-2019
	iyear=iyear+1
	call blet4(iyear,AYEAR)
	ryear=float(iyear)
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
		if(int(ryr/4.)*4.eq.iyr) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
	infile(18:19)=AYR
	if (iyear.lt.2000) then
	infile(16:17)='19'
	else
      infile(16:17)='20'
	endif
36	format(I4,3(1X,I2),1X,I2,2(1X,I3))
	OPEN(10,FILE=infile,action='READ')
    1 read(10,36,end=21) kyear,kmn,kdy,kut,kpind,iap,iaa
      icntot=icntot+1
	if (kpind.ge.ithkp(1)) icnkpt=icnkpt+1
	if (iap.ge.ithap(1)) icnapt=icnapt+1
	if (iaa.ge.ithaa(1)) icnaat=icnaat+1
C
	if (kpind.ge.ithkp(5)) icnkp(5)=icnkp(5)+1
	if (iap.ge.ithap(5)) icnap(5)=icnap(5)+1
	if (iaa.ge.ithaa(5)) icnaa(5)=icnaa(5)+1
C=      minap(5)=minap(5)+iap
C=      minaa(5)=minaa(5)+iaa
C=	      goto 1
C
	do k=1,4
	if ((kpind.ge.ithkp(k)).and.(kpind.lt.ithkp(k+1))) 
     +icnkp(k)=icnkp(k)+1
	if ((iap.ge.ithap(k)).and.(iap.lt.ithap(k+1))) 
     +icnap(k)=icnap(k)+1
	if ((iaa.ge.ithaa(k)).and.(iaa.lt.ithaa(k+1))) 
     +icnaa(k)=icnaa(k)+1
c=      minap(k)=minap(k)+iap
c=       minaa(k)=minaa(k)+iaa
	enddo
C
	goto 1
   21 close(unit=10)
 400  continue
C
      iqkp=icntot-icnkpt
	iqap=icntot-icnapt
	iqaa=icntot-icnaat
       write(*,*) icntot,iqkp,icnkpt,iqap,icnapt,iqaa,icnaat
	 write(11,*) icntot,iqkp,icnkpt,iqap,icnapt,iqaa,icnaat
      do kk=1,5
	perkp=float(icnkp(kk))/icnkpt*100.0
      perap=float(icnap(kk))/icnapt*100.0
	peraa=float(icnaa(kk))/icnaat*100.0
c=	minap(kk)=minap(kk)/icnap(kk)
c=	minaa(kk)=minaa(kk)/icnaa(kk)
	write(*,35) ithkp(kk),icnkp(kk),perkp,ithap(kk),icnap(kk),perap
	+,ithaa(kk),icnaa(kk),peraa
	write(11,35) ithkp(kk),icnkp(kk),perkp,ithap(kk),icnap(kk),perap
	+,ithaa(kk),icnaa(kk),peraa
	enddo
   35 format(1X,3(1X,I3,1X,I8,1X,F7.2))
      pause ' '
	stop
	end

