      program catg1g2g3
C......................................................Dec 2021
C Number of indices for categories G1, G2, G3 + common with G1(Kp),G2(Kp),G3(Kp)
C
      CHARACTER*64 infile,infilekp,outfile
      DIMENSION ires1(3,2),ires2(3,2),ires3(3,2),ires4(3,2)
	+,ITHDST(3),ITHVSW(3),ITHDVS(3),THPCN(3),THPCS(3),THWP(3)
     +,THWU(3),THWL(3),THWE(3),ireskp(3)
	 DATA ITHDST/-30,-50,-100/,ITHVSW/290,510,735/,ITHDVS/50,150,330/
C	+,THPCN/4.4,5.3,9.0/,THPCS/3.9,5.2,9.7/,THWP/2.8,4.9,7.1/
	+,THPCN/3.0,5.3,9.0/,THPCS/1.5,5.2,9.7/,THWP/2.8,4.9,7.1/
     +,THWU/2.0,2.7,3.8/,THWL/-1.9,-2.9,-4.4/,THWE/3.8,5.3,7.2/
C
	infilekp='hsskpapaa3.txt'
C+	infile='catdsthss3.txt'  ! Dst, Vsw, DVsw
C+	infile='hsspcnpcs3.txt'  ! PCN,PCS
	infile='hsswuwlwe3.txt'  ! Wp,WU,WL,WE
	outfile='catg1g2g3.txt'
	OPEN(10,file=outfile,access='APPEND')
C
      OPEN(11,file=infilekp,action='READ')
      OPEN(12,file=infile,action='READ')
	do k=1,3
	ireskp(k)=0
		do n=1,2
	ires1(k,n)=0
	ires2(k,n)=0
	ires3(k,n)=0
	ires4(k,n)=0
	    enddo
	enddo
  30	format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,1X,I4,1X,I3,2(1X,I4),1X,I3
     +,1X,F4.1)
C
      DO 50 iev=1,380
	read(11,30,end=51) ryear,lda,imn,idy,iut,ivmin,itvmax,ivmax,idvs
	+,itkp,xkp
	 kp=int(xkp*10.)
	write(*,*) ' ',iev
	 if (kp.ge.70) then
	ireskp(3)=ireskp(3)+1
	igkp=3
	goto 7
	 endif
	if ((kp.ge.30).and.(kp.lt.50)) then
	ireskp(1)=ireskp(1)+1
	igkp=1
	goto 7
	 endif
	if ((kp.ge.50).and.(kp.lt.70)) then
	ireskp(2)=ireskp(2)+1
		igkp=2
	endif
C+   7	GOTO 5  ! to PCN,PCS
   7	GOTO 8  ! to  Wp,WU,WL,WE
C   Explore Vsw
   2	continue
C
      if (ivmax.ge.ITHVSW(3)) then		 !1
	 ires1(3,1)=ires1(3,1)+1
      if (igkp.eq.3) then				   !2
          ires1(3,2)=ires1(3,2)+1
	endif                                  !2
	GOTO 3
	endif
C
C
      IF ((ivmax.ge.ITHVSW(1)).and.(ivmax.lt.ITHVSW(2))) THEN !4
	 ires1(1,1)=ires1(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires1(1,2)=ires1(1,2)+1
	endif                                  !2
	GOTO 3
	endif
C
      IF ((ivmax.ge.ITHVSW(2)).and.(ivmax.lt.ITHVSW(3))) THEN !4
	 ires1(2,1)=ires1(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires1(2,2)=ires1(2,2)+1
	endif                                  !2
	endif

C   Explore DVsw
   3	continue
C
      if (idvs.ge.ITHDVS(3)) then		 !1
	 ires2(3,1)=ires2(3,1)+1
      if (igkp.eq.3) then				   !2
          ires2(3,2)=ires2(3,2)+1
	endif                                  !2
	GOTO 4
	endif
C
C
      if ((idvs.ge.ITHDVS(1)).and.(idvs.lt.ITHDVS(2))) then !4
	 ires2(1,1)=ires2(1,1)+1
      if (igkp.eq.1) then				   !2
          ires2(1,2)=ires2(1,2)+1
	endif                                  !2
	GOTO 4
	endif
C
	   if ((idvs.ge.ITHDVS(2)).and.(idvs.lt.ITHDVS(3))) then				   !2
          ires2(2,1)=ires2(2,1)+1
      if (igkp.eq.2) then				   !2
          ires2(2,2)=ires2(2,2)+1
	endif                                  !2
	endif                                  !2
C
  31	format(1X,F8.3,1X,I3,3(1X,I2),1X,I4)
C   Explore Dst
   4   read(12,31,end=52) xyear,lda1,imn1,idy1,iut1,idst
C 
       if (idst.le.ITHDST(3)) THEN           !1 
	 ires3(3,1)=ires3(3,1)+1
      if (igkp.eq.3) then				   !2
          ires3(3,2)=ires3(3,2)+1
	endif                                  !2
       GOTO 50
	endif
C
C
      if ((idst.le.ITHDST(1)).and.(idst.gt.ITHDST(2))) then !4
	 ires3(1,1)=ires3(1,1)+1
      if (igkp.eq.1) then				   !2
          ires3(1,2)=ires3(1,2)+1
	endif                                  !2
	GOTO 50
	endif

C
      if ((idst.le.ITHDST(2)).and.(idst.gt.ITHDST(3))) then !4
	 ires3(2,1)=ires3(2,1)+1
      if (igkp.eq.2) then				   !2
          ires3(2,2)=ires3(2,2)+1
	endif                                  !2
	endif
C   PCN,PCS
   34	format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,24X,F5.2,4X,F5.2)
   5  read(12,34,end=52) xyear,lda1,imn1,idy1,iut1,pcn,pcs
C
C  PCN
      if (pcn.ge.THPCN(3)) then		 !1
	 ires1(3,1)=ires1(3,1)+1
      if (igkp.eq.3) then				   !2
          ires1(3,2)=ires1(3,2)+1
	endif                                  !2
	GOTO 6
	endif
C
C
      IF ((pcn.ge.THPCN(1)).and.(pcn.lt.THPCN(2))) THEN !4
	 ires1(1,1)=ires1(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires1(1,2)=ires1(1,2)+1
	endif                                  !2
	GOTO 6
	endif
C
      IF ((pcn.ge.THPCN(2)).and.(pcn.lt.THPCN(3))) THEN !4
	 ires1(2,1)=ires1(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires1(2,2)=ires1(2,2)+1
	endif                                  !2
	endif
C
C PCS
   6	CONTINUE
      if (pcs.ge.THPCS(3)) then		 !1
	 ires2(3,1)=ires2(3,1)+1
      if (igkp.eq.3) then				   !2
          ires2(3,2)=ires2(3,2)+1
	endif                                  !2
	GOTO 50
	endif
C
C
      IF ((pcs.ge.THPCS(1)).and.(pcs.lt.THPCS(2))) THEN !4
	 ires2(1,1)=ires2(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires2(1,2)=ires2(1,2)+1
	endif                                  !2
	GOTO 50
	endif
C
      IF ((pcs.ge.THPCS(2)).and.(pcs.lt.THPCS(3))) THEN !4
	 ires2(2,1)=ires2(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires2(2,2)=ires2(2,2)+1
	endif                                  !2
	endif
C
C Wp, WU,WL,WE
   35	format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,24X,F4.1,59X,F4.2
     +,2(5X,F4.2))
   8	 read(12,35,end=52) xyear,lda1,imn1,idy1,iut1,wp,wu,wl,we
C  Wp
      if (wp.ge.THWP(3)) then		 !1
	 ires1(3,1)=ires1(3,1)+1
      if (igkp.eq.3) then				   !2
          ires1(3,2)=ires1(3,2)+1
	endif                                  !2
	GOTO 9
	endif
C
C
      IF ((wp.ge.THWP(1)).and.(wp.lt.THWP(2))) THEN !4
	 ires1(1,1)=ires1(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires1(1,2)=ires1(1,2)+1
	endif                                  !2
	GOTO 9
	endif
C
      IF ((wp.ge.THWP(2)).and.(wp.lt.THWP(3))) THEN !4
	 ires1(2,1)=ires1(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires1(2,2)=ires1(2,2)+1
	endif                                  !2
	endif
C	 WU
    9  if (wu.ge.THWU(3)) then		 !1
	 ires2(3,1)=ires2(3,1)+1
      if (igkp.eq.3) then				   !2
          ires2(3,2)=ires2(3,2)+1
	endif                                  !2
	GOTO 10
	endif
C
C
      IF ((wu.ge.THWU(1)).and.(wu.lt.THWU(2))) THEN !4
	 ires2(1,1)=ires2(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires2(1,2)=ires2(1,2)+1
	endif                                  !2
	GOTO 10
	endif
C
      IF ((wu.ge.THWU(2)).and.(wu.lt.THWU(3))) THEN !4
	 ires2(2,1)=ires2(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires2(2,2)=ires2(2,2)+1
	endif                                  !2
	endif
C        WL
   10	continue
       if (wl.le.THWL(3)) THEN           !1 
	 ires3(3,1)=ires3(3,1)+1
      if (igkp.eq.3) then				   !2
          ires3(3,2)=ires3(3,2)+1
	endif                                  !2
       GOTO 11
	endif
C
C
      if ((wl.le.THWL(1)).and.(wl.gt.THWL(2))) then !4
	 ires3(1,1)=ires3(1,1)+1
      if (igkp.eq.1) then				   !2
          ires3(1,2)=ires3(1,2)+1
	endif                                  !2
	GOTO 11
	endif

C
      if ((wl.le.THWL(2)).and.(wl.gt.THWL(3))) then !4
	 ires3(2,1)=ires3(2,1)+1
      if (igkp.eq.2) then				   !2
          ires3(2,2)=ires3(2,2)+1
	endif                                  !2
	endif
C  WE
   11	 if (we.ge.THWE(3)) then		 !1
	  ires4(3,1)=ires4(3,1)+1
      if (igkp.eq.3) then				   !2
          ires4(3,2)=ires4(3,2)+1
	endif                                  !2
	GOTO 50
	endif
C
C
      IF ((we.ge.THWE(1)).and.(we.lt.THWE(2))) THEN !4
	 ires4(1,1)=ires4(1,1)+1
	 if (igkp.eq.1) then				   !2
          ires4(1,2)=ires4(1,2)+1
	endif                                  !2
	GOTO 50
	endif
C
      IF ((we.ge.THWE(2)).and.(we.lt.THWE(3))) THEN !4
	 ires4(2,1)=ires4(2,1)+1
	 if (igkp.eq.2) then				   !2
          ires4(2,2)=ires4(2,2)+1
	endif                                  !2
	endif
   50 CONTINUE
C 
   51 close(unit=11)
   52 close(unit=12)
C
   32	format(1X,A32)
   33	format(3(1X,I4))
C output results
        write(*,32) infile
	write(10,32) infile
	 write(*,33) (ireskp(k),k=1,3)
	write(10,33) (ireskp(k),k=1,3)
	DO n=1,2
	 write(*,33) (ires1(k,n),k=1,3)
	write(10,33) (ires1(k,n),k=1,3)
	 write(*,33) (ires2(k,n),k=1,3)
	write(10,33) (ires2(k,n),k=1,3)
	 write(*,33) (ires3(k,n),k=1,3)
	write(10,33) (ires3(k,n),k=1,3)
	 write(*,33) (ires4(k,n),k=1,3)
	write(10,33) (ires4(k,n),k=1,3)
	enddo
      close(unit=10)
	pause ' '
	STOP
	END