      program wulephasem
C.........................................................May. 2021
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 infsym,outfile
	CHARACTER*2 ANN
	CHARACTER*4	AYEAR
	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)

C
	outfile='wulephasnnsm.txt'
C
	infsym='testwuxxnns.txt'
C
C       DO 123 nn=12,24
       DO 123 nn=10,10
	call blet2(nn,ANN)
	infsym(9:10)=ANN
	OPEN(10,file=infsym,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 
       DO nt=0,191
	read(10,30,end=40) IYRs(nt),imns(nt),idys(nt),uts(nt),wu(nt)
	+,wl(nt),we(nt)
	ENDDO
   40	close(unit=10)
C
	 wu_max=0.
	wl_min=0.
	we_max=0.
C
      DO nt=0,191 ! Search max & min during SSC+48h
	if ((wu(nt).gt.wu_max).and.(nt.lt.92)) then	!Search WUmax during SSC+24h
	wu_max=wu(nt)
	kwu=nt
	endif
	if (wl(nt).lt.wl_min) then	 
	wl_min=wl(nt)
	kwl=nt
	endif
	if (we(nt).gt.we_max) then
 	we_max=we(nt)
	kwe=nt
	endif
      ENDDO
C
C Phase fi=[0,1]:
	dwumax=float(kwu)
	dwlmin=float(kwl)
	dwemax=float(kwe)
C WU Phase
       DO k=0,kwu
	FI=float(k)/dwumax  ! Main Phase
	phwu(k)=FI
	 enddo
	DO k=kwu+1,191
	FI=(float(k)-192.)/abs(dwumax-192.)	! Recovery phase during 2days after max
	phwu(k)=FI
       ENDDO
C WL Phase
       DO k=0,kwl
	FI=float(k)/dwlmin  ! Main Phase
	phwl(k)=FI
	 enddo
	DO k=kwl+1,191
	FI=(float(k)-192.)/abs(dwlmin-192.)	! Recovery phase during 2days after min
	phwl(k)=FI
       ENDDO
C WE Phase
       DO k=0,kwe
	FI=float(k)/dwemax  ! Main Phase
	phwe(k)=FI
	 enddo
	DO k=kwe+1,191
	FI=(float(k)-192.)/abs(dwemax-192.)	! Recovery phase during 2days after max
	phwe(k)=FI
       ENDDO
   50	CONTINUE
C
C Output results
C
        DO 75 iev=0,191	  !15min data
C	write(*,34) iev
C	write(11,34) iev
	iyr=iyrs(iev)
      imn=imns(iev)
	idy=idys(iev)
	rut=uts(iev)
	call blet4(iyr,AYEAR)
	ryear=float(iyear)
c
	write(*,36) iYR,iMN,iDY,rUT,wu(iev),phwu(iev),wl(iev),phwl(iev)
	+,we(iev),phwe(iev)
	write(11,36) iYR,iMN,iDY,rUT,wu(iev),phwu(iev),wl(iev),phwl(iev)
	+,we(iev),phwe(iev)
   75	CONTINUE		 !15min data
	close(unit=11)
  123 CONTINUE              ! END events
   36 FORMAT(1X,I4,2(1X,I2),1X,F5.2,3(1X,F6.2,1X,F6.3))
	pause ' '
	STOP
	END

      	
	 