      program wulephasem24
C.........................................................June 2021
C Produce Storm-Phase for training storms SC2231h time step (0:1:47 h)
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,txt
	CHARACTER*2 ANN,AYR,AMN,ADY,AUT
CSC24	+,ASET(7)
	CHARACTER*4	AYEAR
	DIMENSION phwu(0:71),phwl(0:71),it(2,0:71) ! IDY,IUT
CSC24	DATA ASET/'09','10','11','14','18','23','24'/
C Read t(WUmax),t(WLmax): YR,MN,D0,UT0=0,DYu,UTu,DYl,UTl,DY2=DY0+2,UT2=23
C
	outfile='wuwlphasnn.txt'
C
Crem	infsym='wuwlpeak22.txt'	   ! Modeling set SC22
C	infsym='wuwlpeak24.txt'	   ! Testing set  SC24
	infsym='wuwlpeak23.txt'	   ! Training set  SC23
C
	OPEN(10,file=infsym,action='READ')
      read(10,*) txt       ! title line
C
C Cycle on events
C
Crem       DO 123 nn=1,8		  ! Modeling set SC22
C       DO 123 nn=1,7			  ! Testing set SC24
        DO 123 nn=9,10			  ! Testing set SC24 TEMP
C        DO 123 nn=1,9			  ! Training set SC23
      
	call blet2(nn,ANN)  ! Modeling set SC22
CSC24       ANN=ASET(nn)			 ! Testing set
	nnres=nn+30		 ! 
	call blet2(nnres,ANN)
	outfile(9:10)=ANN
	OPEN(11,file=outfile)
C
  30	format(I2,2(I2),1X,I2,2(5X,I2,1X,I2))  ! read t(WUmax),t(WLmin)
	read(10,30,end=40) IYR,imn,id0,iut0,idu,iutu,idl,iutl
	lda=ndoy(iyr,imn,id0)
	lda2=lda+2
	imn1=imn
	imn2=imn
	if (nn.eq.3) then
	imn2=imn1+1
	idy2=2
	endif
	iut2=23
	call blet2(iyr,AYR)
	if (iyr.gt.50) then
      AYEAR='1900'
	               else
	AYEAR='2000'
	endif
	AYEAR(3:4)=AYR
	call blet2(imn,AMN)
C
C  Search tmax & tmin during 72h
        kwu=0
	kwl=0
         kt=-1
C        DO idy=id0,id2
        DO lda=lda,lda2
	call submmdd(lda,iyr,imn,idy)
       DO iut=0,23
       kt=kt+1
	it(1,kt)=idy
	it(2,kt)=iut
	if ((idy.eq.idu).and.(iut.eq.iutu)) then
	kwu=kt
	endif
	if ((idy.eq.idl).and.(iut.eq.iutl)) then
	kwl=kt
	endif
	 ENDDO
	  ENDDO
C
C
C
C Phase fi=[0,1]:
	dwumax=float(kwu)
	dwlmin=float(kwl)
C WU Phase
       DO k=0,kwu
	FI=float(k)/dwumax  ! Main Phase
	phwu(k)=FI
	 enddo
	DO k=kwu+1,71
	FI=(float(k)-72.)/abs(dwumax-72.)	! 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,71
	FI=(float(k)-72.)/abs(dwlmin-72.)	! Recovery phase during 2days after min
	phwl(k)=FI
       ENDDO
C
C Output results
C
        DO 75 iev=0,71	  !1h data
C	write(*,34) iev
C	write(11,34) iev
	idy=it(1,iev)
	call blet2(idy,ADY)
	iut=it(2,iev)
	call blet2(iut,AUT)
c
	write(*,36) AYEAR,AMN,ADY,AUT,phwu(iev),phwl(iev)
	write(11,36) AYEAR,AMN,ADY,AUT,phwu(iev),phwl(iev)
   75	CONTINUE		 !1h data for 3 days
	close(unit=11)
  123 CONTINUE              ! END events
   40	close(unit=10)

   36 FORMAT(1X,A4,2A2,1X,A2,2(1X,F6.3))
	pause ' '
	STOP
	END

      	
	 