      program wulephasavm1
C.........................................................Dec 2021
C Global, North, South	 Phase av results PHI=0,0.1,...1 -0.9... -0.1, 0
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 infile1,infile2,outfile
C/	CHARACTER*2 ANN
	DIMENSION iYRs(0:71),iMNS(0:71),iDYS(0:71),UTs(0:71)
     +,wu(0:71),wl(0:71)
	+,phwu(0:71),phwl(0:71)
     +,thr1(11),thr2(11),avwu(20),avwl(20),avwe(20)
	+,wu_sum(20),wu_cnt(20),wl_sum(20),wl_cnt(20)
C
	infile1='wulephasem13.txt' !global
		infile2='tesiwuwl13m.dat' !global
	outfile='wulephasem13av.txt' !global
C+	infile1='wulephasen13.txt' !north
C+		infile2='tesiwuwl13n.dat' !north
C+	outfile='wulephasen13av.txt' !north
C+	infile1='wulephases13.txt' !south
C+		infile2='tesiwuwl13s.dat' !south
C+	outfile='wulephases13av.txt' !south

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
C       DO 123 nn=23,24      ! Testing set
C	call blet2(nn,ANN)
C	infile(9:10)=ANN
	OPEN(10,file=infile1,action='READ') 
	OPEN(12,file=infile2,action='READ') 
C/      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 
   35 FORMAT(1X,I4,2(I2),1X,I2,2(1X,F6.3))
   36 FORMAT(1X,I4,2(1X,I2),1X,F5.2,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
C/	read(10,36,end=40) IYRs(nt),imns(nt),idys(nt),uts(nt),wu(nt)
C/	+,phwu(nt),wl(nt),phwl(nt),we(nt),phwe(nt)
	read(10,35,end=40) IYRs(nt),imns(nt),idys(nt),iutin
	+,phwu(nt),phwl(nt)
	read(12,36,end=40) IYRs(nt),imns(nt),idys(nt),uts(nt),wu(nt)
	+,wl(nt)
	ENDDO
   40	close(unit=10)
C
	do k=1,20
	 wu_sum(k)=0.
	wl_sum(k)=0.
C	we_sum(k)=0.
	wu_cnt(k)=0.
	wl_cnt(k)=0.
C	we_cnt(k)=0.
	enddo
C
      DO nt=0,71 ! 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,71 ! 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
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
C/      if (we_cnt(ifi).gt.0.) then
C/	avwe(ifi)=we_sum(ifi)/we_cnt(ifi)
C/	endif
c
C/	write(*,37) thr,avwu(ifi),avwl(ifi),avwe(ifi)
C/	write(11,37) thr,avwu(ifi),avwl(ifi),avwe(ifi)
	write(*,37) thr,avwu(ifi),avwl(ifi)
	write(11,37) thr,avwu(ifi),avwl(ifi)
   75	CONTINUE		 !15min data
	close(unit=11)
C
  123 CONTINUE              ! END events
   37	format(1X,F5.2,2(1X,F6.2))
	pause ' '
	STOP
	END

      	
	 