      program extwulessn0
C......................................................Aug. 2021
C extract WU, WL, WE for SSN2=0 (5 periods ndays>30) if kp < 3
C
      character*128 infilkp,infilwu,outfilwu,outfilwl,outfilwe,txt
      character*2 AYR,AMN,AMN1,AMN2,ADY
	character*4 AYEAR
	dimension iwpos(0:7),iwneg(0:7),iwran(0:7),inkp(0:7)
	+,resp(0:7),resn(0:7),resr(0:7)
C
      infilkp='d:\apkp\kpap\kpYEAR'
	infilwu='d:\web\graf\datw\qtcyr.txt'
	outfilwu='wuYRMN0.txt'
	outfilwl='wlYRMN0.txt'
	outfilwe='weYRMN0.txt'
C
        write(*,*) ' Enter iyear:'
	read(*,*) iyear
	call blet4(iyear,AYEAR)
	iyr=iyear-(iyear/100)*100
	AYR=AYEAR(3:4)
	infilkp(16:19)=AYEAR
	OPEN(10,file=infilkp,action='READ')
	infilwu(21:22)=AYR
      OPEN(11,file=infilwu,action='READ')
	read(11,*) txt      ! title line
	outfilwu(3:4)=AYR
      outfilwl(3:4)=AYR
	outfilwe(3:4)=AYR
C
        write(*,*) ' Enter imn1,idy1:'
	read(*,*) imn1,idy1
      call blet2(imn1,AMN1)
	outfilwu(5:6)=AMN1
      outfilwl(5:6)=AMN1
	outfilwe(5:6)=AMN1
      OPEN(12,file=outfilwu,access='APPEND')
	OPEN(13,file=outfilwl,access='APPEND')
	OPEN(14,file=outfilwe,access='APPEND')
C
        write(*,*) ' Enter imn2,idy2:'
	read(*,*) imn2,idy2
      call blet2(imn2,AMN2)
	lda1=ndoy(iyr,imn1,idy1)
	lda2=ndoy(iyr,imn2,idy2)
C
   30	format(3I2,6X,8(I2))
   31	format(3I2,8(1X,I4),8(1X,I5),8(1X,I4))
      iflag=0
C      
       DO 50 lda=lda1,lda2
	call submmdd(lda,iyr,jmn,idy)
	if (idy.eq.1) then
	imn=imn2
	AMN=AMN2
	iflag=1
	else
	if (iflag.eq.0) then
	AMN=AMN1
	imn=imn1
	       else
	imn=imn2
	AMN=AMN2
         endif
	endif
	call blet2(idy,ADY)
   1	read(10,30,end=150) kyr,kmn,kdy,inkp
   2	read(11,31,end=151) nyr,nmn,ndy,iwpos,iwneg,iwran
	if ((kyr.eq.iyr).and.(kmn.eq.imn).and.(kdy.eq.idy)) then
	GOTO 3
	else
	GOTO 1
	endif
C exclude Kp >=3
   3     DO 51 j=0,7
         if (inkp(j).ge.30) then
	iwpos(j)=0
	iwneg(j)=0
	iwran(j)=0
	   endif
   51	CONTINUE
C
C output results
       do j=0,7
	resp(j)=iwpos(j)/100.
	resn(j)=iwneg(j)/100.
	resr(j)=iwran(j)/100.
	 enddo
      write(*,33) AYR,AMN,ADY,(resp(k),k=0,7)
	write(12,33) AYR,AMN,ADY,(resp(k),k=0,7)
      write(*,33) AYR,AMN,ADY,(resn(k),k=0,7)
	write(13,33) AYR,AMN,ADY,(resn(k),k=0,7)
      write(*,33) AYR,AMN,ADY,(resr(k),k=0,7)
	write(14,33) AYR,AMN,ADY,(resr(k),k=0,7)
   50 CONTINUE
   33	format(1X,3A2,8(1X,F6.2))
  150 close(unit=10)
  151 close(unit=11)
      close(unit=12)
	close(unit=13)
	close(unit=14)
	pause ' '
	STOP
	END
