      program dstphasem
C.........................................................Dec. 2021
C
C.........................................................Apr. 2021
C
C Specify storm pgase: Fi=0 (t0=SSC), Fi=1 (storm peak), Fi=0(t0+47h) 
C Include SYM data for 00,15,30,45 min
C
      CHARACTER*94 infssc,infsym,outfile,outsym
	+,txt
	CHARACTER*2 AMN,ADY,AUT,AMM,D1,D2,SM1,SM2,SMN,SDY,SUT,SMM
	CHARACTER*4	AYEAR,SYEAR
	DIMENSION idat(9,0:191),res(9,0:191),isym(0:239)
C?	+,idst(0:71) ! 1h dst for 9 storms SC23
     +,iYRs(9),iMNS(9),iDYS(9),iUTs(9),IM(12)
       DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/

C
C-      infile='dst23d.txt'
	outfile='dstphase.txt'
C
	infsym='d:\apkp\dst\SYMYEARM1D1M2D2.dat'
C
Crem	write(*,*) ' Enter SYM YEAR,M1,M2,D1,D2:'
C	read(*,*) SYEAR,SM1,SM2,D1,D2
       SYEAR='2000'   !TEMP
	SM1='07'		!TEMP
	SM2='07'		!TEMP
	D1='15'			!TEMP
	D2='17'			!TEMP
	infsym(16:19)=SYEAR
      infsym(20:21)=SM1
      infsym(22:23)=D1
	infsym(24:25)=SM1
	infsym(26:27)=D2
	outsym=infsym
	outsym(29:31)='txt'
  30	format(A4,2A2,1X,A2,240(2X,I4))				   ! read SYM
C        infsym='d:\apkp\dst\sym-dat.txt'
C        infsym='d:\apkp\dst\sym-mat.txt'
C        infsym='sym-mat.txt'
        infsym='test-out.txt'
C	OPEN(13,file='infsym',action='READ',access='SEQUENTIAL', status = 
C	OPEN(13,file='infsym',action='READ',access='DIRECT', status = 
C     +'old',BLANK='NULL',CARRIAGECONTROL='LIST'
C	+,form='FORMATTED')
	OPEN(13,file='infsym',action='READ')
		OPEN(14,file='outsym')
   32	FORMAT(A4,4(1X,A2),1X,I4)	   ! output 15 sym
Ctemp	read(13,30,err=109,end=110,iostat = ierr) txt ! extra lines
C	read(13,30,EOR=109,iostat = ierr,ADVANCE='YES') txt ! extra lines
C    3	read(13,30,ERR=109,end=108,iostat = ierr) SYEAR,SMN,SDY
C     +,(isym(k),k=0,239)
    3	read(13,*,ERR=109,end=108,iostat = ierr) txt
      if ( ierr /= 0 ) then
      write( *, * ) ierr
      stop
      endif
C Output 15-min file
       kk=0
	k15=0
	DO kut=0,23		   !UTTTTTTTT
      call blet2(kut,SUT)
	DO kmm=0,45,15	  !MINNNNNNNNNNNN
	call blet2(kmm,SMM)
	isym15=isym(k15)
	k15=k15+15
C	write(*,32) SYEAR,SMN,SDY,SUT,SMM,isym15
C	write(14,32) SYEAR,SMN,SDY,SUT,SMM,isym15
	kk=kk+1
	ENDDO         ! MINNNNNNNNNNN
	ENDDO          !UTTTTTTTT
       GOTO 3
C
  109	write(*,*) ierr
  108 continue
Crem      close(unit=13)
       close(unit=14)
  110	continue
C
	infssc='storms23c.txt'
	AMM='00'
	OPEN(12,file=infssc,action='READ')
	isym=0
	DO ii=1,9           ! EventsSSSSSSSSSSSSSSSS
	read(12,33) iyear,imn,idy,iut
	iyrs(ii)=iyear
	imns(ii)=imn
	idys(ii)=idy
	iuts(ii)=iut
C
 	z1=iyr/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyr) THEN
               IM(2)=29
	ndnyr=366
        ELSE
                IM(2)=28
	  ndnyr=365
	       ENDIF
C Read SYM H for 48h 00,15,30,45 min
C
   31 FORMAT(I4,4(1X,I2),46X,F8.2)
      nn=0
  
    2	   DO imm=0,45,15	 !MINNNNNNNNNNNNNN
    1	read(13,31,end=111) kyear,kmn,kdy,kut,kmm,sym
C    1	read(13,*,end=111) kyear,kmn,kdy,kut,kmm,xx,kk,s1,s2,s3,sym
      if ((kyear.eq.iyear).and.(kmn.eq.imn).and.(kdy.eq.idy)
	+.and.(kut.eq.iut).and.(kmm.eq.imm)) then
      idat(ii,nn)=int(sym)*(-1)
	nn=nn+1
	 if (nn.eq.192) GOTO 111 	  
	                   else
	goto 1
	endif
	   ENDDO			 !MINNNNNNNNNNNN
	iut=iut+1
	IF (iut.gt.23) THEN
	iut=0
	idy=idy+1
	if (idy.gt.im(imn)) then
	idy=1
	imn=imn+1
	endif
	ENDIF
	if (nn.lt.192) GOTO 2
  111	close(unit=13)
      ENDDO             !EVENTSssssssssssssss
C
C-      OPEN(10,file=infile,action='READ')
      OPEN(11,file=outfile)  ! Phase FI of storm & Dst from to(SSC) to to+47
   33	FORMAT(6X,I4,2I2,1X,I2)
   35	FORMAT(72(1X,I4))
C
C-      DO iev=1,9
C-	read(10,35) (idst(k),k=0,71)
C-	do k=0,47
C-	idat(iev,k)=idst(k+24)
C-	enddo
C-	ENDDO
C-	close(unit=10)
C
      DO 50 iev=1,9
C Select peak DST
      mindst=100.
Crem	do k=0,47
	do k=0,95			 ! Search SUM-min during SSC+24h
	if (idat(iev,k).lt.mindst) then
	mindst=idat(iev,k)
	k1=k
	endif
	enddo
C Phase fi=[0,1]:
	dmin=float(k1)
       DO k=0,k1+1
	FI=float(k)/dmin  ! Main Phase
	res(iev,k)=FI
	 enddo
	DO k=k1+1,191
	FI=(float(k)-192.)/abs(dmin-192.)	! Recovery phase during 2days after SYM-min
	res(iev,k)=FI
       ENDDO
   50	CONTINUE
C
C Output results
C
        DO iev=1,9	  !EventsSSSSSSSSSSSSS
	write(*,34) iev
	write(11,34) iev
	iyr=iyrs(iev)
      imn=imns(iev)
	idy=idys(iev)
	iut=iuts(iev)
	call blet4(iyr,AYEAR)
	ryear=float(iyear)
c
       imm=0       
	  DO 75 ii=0,191
      call blet2(imn,AMN)
	call blet2(idy,ADY)
	call blet2(iut,AUT)
	call blet2(imm,AMM)
	write(*,36) AYEAR,AMN,ADY,AUT,AMM,res(iev,ii),idat(iev,ii)
	write(11,36) AYEAR,AMN,ADY,AUT,AMM,res(iev,ii),idat(iev,ii)
C+	if (res(iev,ii).eq.1) then 
C+	fi=-1.
C+	write(*,36) AYEAR,AMN,ADY,AUT,AMM,fi,idat(iev,ii)
C+	write(11,36) AYEAR,AMN,ADY,AUT,AMM,fi,idat(iev,ii)
C+      endif
       imm=imm+15
	if (imm.gt.45) then           !IMM Corr
	imm=0
	iut=iut+1
	IF (iut.gt.23) THEN
	iut=0
	idy=idy+1
	if (idy.gt.im(imn)) then
	idy=1
	imn=imn+1
	endif
	ENDIF
	             endif
   75	CONTINUE
	ENDDO		 !EventsSSSSSSSSSSSSS
	close(unit=11)
   34 FORMAT(1X,'fi',I1)
   36 FORMAT(1X,A4,2A2,1X,2A2,1X,F6.3,1X,I4)
	pause ' '
	STOP
	END

      	
	 