      program extkp3sort
C...............................................................Aug 2021
C Sort file 'wulessn0kp3s.txt'(SSN=0 Kp>=3) with increasing sumKp3
C
	DIMENSION iev(1396),ryear(1396),iut(1396),wulekp(4,1396),kpsum(1396)
      CHARACTER*2 AYR,AMN,ADY
	CHARACTER*4	AMMDD(1396)
	CHARACTER*64 infile,outfile,infile1
		CHARACTER*4 AYEAR
C
      infile1='testskp1.txt'
	infile='wulessn0kp3s.txt'  !  events SSN=0 Kp >=3 except outliers
	outfile='wulessn0kp3s1.txt'
	      OPEN(10,file=infile,action='READ')
	OPEN(11,file=infile1,action='READ')
       OPEN(12,file=outfile)
C
   31	format(1X,I4,1X,3I2,1X,I2,3(1X,F6.2),1X,F4.1)
   32	format(3I2,22X,I3)
C cycle of SSN=0,kp>=3 days
         DO 150 nn=1,1396
	read(10,31,end=3) iev(nn),ryear(nn),ammdd(nn),iut(nn)
	+,(wulekp(k,nn),k=1,4),kpsum(nn)
	z1=iyr/4.0
      jz=int(z1)*4

      IF(jz.EQ.iyr) THEN
	dnr=366.
        ELSE
	  	dnr=365.
	       ENDIF
	delyr=1./dnr
	lda=ndoy(iyr,imn,idy)
C
	call blet2(iyr,AYR)
      call blet2(imn,AMN)
	call blet2(idy,ADY)
	AYEAR='2000'
	year=2000.
	if (iyr.gt.30) then 
	AYEAR='1900'
	year=1900.
	endif
	ryear=year+iyr+(lda-1)*delyr
C
	AYEAR(3:4)=AYR
	infilekp(16:19)=AYEAR
	      OPEN(11,file=infilekp,action='READ')
	do nn=1,lda
	read(11,32,end=52) kyr,kmn,kdy,kpsum
	enddo
   52	close(unit=11)
      write(*,33) iev,ryear,amn,ady,iut,wu,wl,we,xkp3,kpsum
      write(12,33) iev,ryear,amn,ady,iut,wu,wl,we,xkp3,kpsum
  150	CONTINUE
    3	close(unit=10)
	close(unit=12)
   33	format(1X,I4,1X,F8.3,1X,2A2,1X,I2,3(1X,F6.2),1X,F4.1,1X,I3)
      pause ' '
	STOP
	END
