      program wulephasavm
C.........................................................Oct. 2021
C Average WU WL (Phase) fi=[0,0.1],[0.1 0.2],...,[0.9 1] [1.0.9]...[0.2 0.1]
C
C Specify storm pgase: Fi=0 (t0=SSC), Fi=1 (storm peak), Fi=0(t0+47h) 
C Include WU,WL,WE data for 00,15,30,45 min
C
      CHARACTER*94 infile,outfile
	CHARACTER*2 ANN
	DIMENSION iYRs(0:191),iMNS(0:191),iDYS(0:191),UTs(0:191)
     +,wu(0:191),wl(0:191),we(0:191)
	+,phwu(0:191),phwl(0:191),phwe(0:191)
     +,thr1(11),thr2(11),avwu(20),avwl(20),avwe(20)
	+,wu_sum(20),wu_cnt(20),wl_sum(20),wl_cnt(20),we_sum(20),we_cnt(20)
C
	infile='wulephasnnsm.txt'
	outfile='wulephasnnav.txt'
C
        thr1(1)=0.0
	thr2(1)=-1.0
        do k=1,10
	thr1(k+1)=thr1(k)+0.1
      thr2(k+1)=thr2(k)+0.1
	  enddo
	thr2(11)=-0.00001
C
C       DO 123 nn=31,39      ! Training set
C       DO 123 nn=9,10      ! Testing set
       DO 123 nn=23,24      ! Testing set
	call blet2(nn,ANN)
	infile(9:10)=ANN
	OPEN(10,file=infile,action='READ') 
      outfile(9:10)=ANN
	OPEN(11,file=outfile)
C
  30	format(1X,I4,2(1X,I2),1X,F5.2,3(1X,F6.2))				   ! read WU,WL,WE
   32	FORMAT(A4,4(1X,A2),1X,I4)	   ! output 
   36 FORMAT(1X,I4,2(1X,I2),1X,F5.2,3(1X,F6.2,1X,F6.3))
C
        do k=1,20
	avwu(k)=0.0
	avwl(k)=0.0
	avwe(k)=0.0
	 enddo
C
       DO nt=0,191
	read(10,36,end=40) IYRs(nt),imns(nt),idys(nt),uts(nt),wu(nt)
	+,phwu(nt),wl(nt),phwl(nt),we(nt),phwe(nt)
	ENDDO
   40	close(unit=10)
C
	do k=1,20
	 wu_sum(k)=0.
	wl_sum(k)=0.
	we_sum(k)=0.
	wu_cnt(k)=0.
	wl_cnt(k)=0.
	we_cnt(k)=0.
	enddo
C
      DO nt=0,191 ! aver (fi)
C
        IF (phwu(nt).ge.0.) THEN
	do k=1,10
	if ((phwu(nt).ge.thr1(k)).and.(phwu(nt).le.thr1(k+1))) then
	wu_sum(k)=wu_sum(k)+wu(nt)
	wu_cnt(k)=wu_cnt(k)+1.0
	endif
	enddo
	wu_sum(11)=wu(nt)  ! fi=-1.0
	wu_cnt(11)=1.0
	                     ELSE
	do k=1,10
	if ((phwu(nt).gt.thr2(k)).and.(phwu(nt).le.thr2(k+1))) then
	wu_sum(k+10)=wu_sum(k+10)+wu(nt)
	wu_cnt(k+10)=wu_cnt(k+10)+1.0
	endif
	enddo
	                   ENDIF
        ENDDO
C
      DO nt=0,191 ! aver (fi)
C
        IF (phwl(nt).ge.0.) THEN
	do k=1,10
	if ((phwl(nt).ge.thr1(k)).and.(phwl(nt).le.thr1(k+1))) then
	wl_sum(k)=wl_sum(k)+wl(nt)
	wl_cnt(k)=wl_cnt(k)+1.0
	endif
	enddo
	wl_sum(11)=wl(nt)  ! fi=-1.0
	wl_cnt(11)=1.0
	                     ELSE
	do k=1,10
	if ((phwl(nt).gt.thr2(k)).and.(phwl(nt).le.thr2(k+1))) then
	wl_sum(k+10)=wl_sum(k+10)+wl(nt)
	wl_cnt(k+10)=wl_cnt(k+10)+1.0
	endif
	enddo
	                   ENDIF
        ENDDO
C
      DO nt=0,191 ! aver (fi)
C
        IF (phwe(nt).ge.0.) THEN
	do k=1,10
	if ((phwe(nt).ge.thr1(k)).and.(phwe(nt).le.thr1(k+1))) then
	we_sum(k)=we_sum(k)+we(nt)
	we_cnt(k)=we_cnt(k)+1.0
	endif
	enddo
	we_sum(11)=we(nt)  ! fi=-1.0
	we_cnt(11)=1.0
	                     ELSE
	do k=1,10
	if ((phwe(nt).gt.thr2(k)).and.(phwe(nt).le.thr2(k+1))) then
	we_sum(k+10)=we_sum(k+10)+we(nt)
	we_cnt(k+10)=we_cnt(k+10)+1.0
	endif
	enddo
	                   ENDIF
        ENDDO
C
C
   50	CONTINUE
C
C Output results
C
        DO 75 ifi=1,20	  !Phase
	if (ifi.lt.11) then
	thr=(thr1(ifi)+thr1(ifi+1))/2.0
	else
	thr=(thr2(ifi-10)+thr2(ifi-9))/2.0
	endif
      if (wu_cnt(ifi).gt.0.) then
	avwu(ifi)=wu_sum(ifi)/wu_cnt(ifi)
	endif
      if (wl_cnt(ifi).gt.0.) then
	avwl(ifi)=wl_sum(ifi)/wl_cnt(ifi)
	endif
      if (we_cnt(ifi).gt.0.) then
	avwe(ifi)=we_sum(ifi)/we_cnt(ifi)
	endif
c
	write(*,37) thr,avwu(ifi),avwl(ifi),avwe(ifi)
	write(11,37) thr,avwu(ifi),avwl(ifi),avwe(ifi)
   75	CONTINUE		 !15min data
	close(unit=11)
C
  123 CONTINUE              ! END events
   37	format(1X,F5.2,3(1X,F6.2))
	pause ' '
	STOP
	END

      	
	 