      program rewritehss4
C...................................................Feb 2020
C combine 707 events visCHH+Hss 2010/03/-2019/09+ cathss48m1.txt
C
      CHARACTER*47 infile1,infile2,outfile,txthss
	CHARACTER*8 epoch
	CHARACTER*2 AYR,AMN,ADY,CYR
	CHARACTER*3 CLDA
	infile1='viscatcchpart.txt'   ! Vis-CCH-cat
	infile2='cathss48m1.txt'		!HSS - Cat1
	outfile='cathssviscch.txt'
	OPEN(10,file=infile1,action='READ')
C	
	OPEN(12,file=outfile)
C
   30 FORMAT(A47)
C   31 FORMAT(A40)
   32 FORMAT(A47,1X,A8,1X,I2,3(1X,F7.3),1X,I4)
		icch=0
C
		DO 70 ii=1,707
 	read(10,*,end=40) nn,epoch,ith,chper,comlat,smag
	ayr=epoch(3:4)
 	amn=epoch(5:6)
	ady=epoch(7:8)
	read(ayr,*) ryr
	iyr=int(ryr)
	read(amn,*) rmn
	imn=int(rmn)
	read(ady,*) rdy
	idy=int(rdy)
	 ldacch=ndoy(iyr,imn,idy)
C	ldahss=ldacch+5
      OPEN(11,file=infile2,action='READ')
   1	read(11,30,end=40) txthss
      CYR=txthss(4:5)
	read(cyr,*) xyr
	jyr=int(xyr)
	if (jyr.gt.iyr) GOTO 40 !======>
	if (jyr.lt.iyr) then								!++2
	goto 1
	                else								 !++2
      CLDA=txthss(11:13)
      if (CLDA(1:1).eq.' ') CLDA(1:1)='0'
	if (CLDA(2:2).eq.' ') CLDA(2:2)='0'
	read(clda,*) rldahss
	ldahss=int(rldahss)
	ldanex=ldacch+4
	if (ldanex.gt.365) then
	write(*,*) ' Check year-next in txthxx ',cyr,ldanex
	pause ' '
	goto 40
	endif

	if ((ldahss.lt.ldacch).or.(ldahss.gt.ldanex)) then   !1
	goto 1
	                else								   !1
		icch=icch+1
	write(*,32) txthss,epoch,ith,chper,comlat,smag,icch
	write(12,32) txthss,epoch,ith,chper,comlat,smag,icch
                        endif								   !1

	endif                                     !++2
   40	close(unit=11)
C
   70 CONTINUE
   41 close(unit=10)
      close(unit=12)
	pause ' '
	STOP
	end


