	program extpchmag4
C....................................................Oct. 2019
C Wp-storm from GEOMAG. coord. GIM-TEC  from 04/2011 - to 09/2019
C Pch,Vsw,Dst,Ae,aa,ap,Wpm
C
C Wp = magn coord. map
C....................................................Jan 2017
C outfile='extpchmag4.txt' for Pchg dmax+5 days
C....................................................Dec 2016
C
C Extract geom. indices for 2011.04-2019.09 at Pch-peaks>100i.u. (Dst_min <= -50 nT, Dst <=-30 nT) 
C                    Include AE,aa,ap,Vp (potocYR.txt),Vn(notocYR.txt) indices extraction
C select EXTREME values for time to=nint(to_Pch):to+5 days
C outfile='extpchmag4.txt'
c
	integer IM(12)
	dimension iaa(0:371),iap(0:371)  ! +5 days from next year
     +,indaa(0:7),indap(0:7) ! Input 3h indices => select peak
     +,ind24(0:23),wp24(0:23) ! Input 1h indices AE,Dst,Wp,(Vp,Vn) => select peak
     +,iae(0:371)
     +,idst(0:371),wp(371),isw(371)
C	+,ivp(371),ivn(371)
C	nind ! peak index for d0+5 days
C	nday !  day of delay from d0 to d0+5 days: (nday=0,1,...5)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*65 infaa,infpch,infae,infdst,infwp,infsw
C	+,infvn
	+,outfile,txt
	CHARACTER*2 AYR
	CHARACTER*4 AYEAR,AYEAR_nex
C	DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/ 
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C

	infaa='d:/apkp/AA/aaYEAR' !aa, ap
	infdst='d:/apkp/dst/dstYEAR'	 ! Dst
	infae='d:/apkp/AE/aeYEAR'	 ! AE
	infsw='d:/apkp/OMNI/swYEAR.txt'	 ! Vsw
	infwp='d:/web/grif/datw/wptemYR.txt' ! Wp geom.
C>>	infvp='c:/msdev/projects/vsigma/pstocYR.txt' ! Vp
C>>	infvn='c:/msdev/projects/vsigma/nstocYR.txt' ! Vn
C?	write(*,*) ' Enter YEAR:'
C?	read(*,*) IYEAR
CREM	outfile='extrgicme1.txt'  ! 	total file for 1999-2016 =376 ICME List t0+47 hrs
	outfile='extpchcat4.txt' !total file for 2011-2019 =349 Pch-peak(-3d+3d) List t0+5days
C Enter Pch peak list for 2011-2018
   30	format(A50)
  31	format(39X,I4)
   36 format(1X,I4,A1)
   37 format(20X,24(I4))	 ! AE
  137 format(13X,2(1X,I3)) ! ap,aa
  237 format(6X,8(1X,F5.1))  ! ap(tau)
C   34 format(6X,24(1X,I4))  ! (Vp, Vn) 
   34 format(6X,24(1X,F4.1))  !  Wp
      icntot=0
C Cycle on years 1999-2016
	iyear=2010   !TEMP	  Year_pre
C	iyear=2015	 !Temp
	call blet4(iyear,AYEAR)
c//	outfile(6:9)=AYEAR
		OPEN(16,FILE=OUTFILE,access='APPEND')
	nicmes=0	   ! total Pch-peak events 2011/03-2018/12
C
C start cycle on year: =====================================
C
C+	do 400 KK=1,52	! 1964-2015
	do 400 KK=1,9	! 2011-2019
C-	kk=1 !Temp
C//	if (kk.gt.1) ndyr0=ndnyr
	iyear=iyear+1
Crem	iyear=2015 !TEMP
	call blet4(iyear,AYEAR)
C
      if (iyear.lt.2019) then
	iyear_nex=iyear+1
	else
	iyear_nex=iyear
	endif
      call blet4(iyear_nex,AYEAR_nex)
	ryear=float(iyear)
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
		if(int(ryr/4.)*4.eq.iyr) THEN 
		 im(2)=29
	ndnyr=366
	                          ELSE
	IM(2)=28
	ndnyr=365
	ENDIF
C       !==============================================
	do nn=1,ndnyr+5
	iap(nn)=0
	iaa(nn)=0
	iae(nn)=0
	idst(nn)=0
	wp(nn)=0.
C	ivp(nn)=0
C	ivn(nn)=0
	enddo
C Reading aa & ap file
	infaa(14:17)=AYEAR
	OPEN(10,FILE=infaa,action='READ')
	do idd=1,ndnyr
	iaamax=0
	iapmax=0
	do ii=0,7
	read(10,137,end=139) indap(ii),indaa(ii)
	if (iaamax.lt.indaa(ii)) iaamax=indaa(ii) 
	if (iapmax.lt.indap(ii)) iapmax=indap(ii) 
	enddo
	iaa(idd)=iaamax
	iap(idd)=iapmax
	enddo
  139	close(unit=10)
C
C+ 5 days from next year
	infaa(14:17)=AYEAR_nex
	OPEN(10,FILE=infaa,action='READ')
	do idd=ndnyr+1,ndnyr+5
	iaamax=0
	iapmax=0
	do ii=0,7
	read(10,137,end=1139) indap(ii),indaa(ii)
	if (iaamax.lt.indaa(ii)) iaamax=indaa(ii) 
	if (iapmax.lt.indap(ii)) iapmax=indap(ii) 
	enddo
	iaa(idd)=iaamax
	iap(idd)=iapmax
	enddo
 1139	close(unit=10)
C
C Reading Vsw file
	infsw(16:19)=AYEAR
	OPEN(10,FILE=infsw,action='READ')
	do i=1,13
	read(10,30) txt ! extra lines
	enddo
C
	do idd=1,ndnyr
		iswmax=0
	do ii=0,23
	read(10,31,end=239) ind24(ii)
	if((ind24(ii).ne.9999).and.(iswmax.lt.ind24(ii))) iswmax=ind24(ii) ! Vsw max (except 9999)
	enddo
	isw(idd)=iswmax
	enddo
  239	close(unit=10)
C
C+ 5 days from next year
	infsw(16:19)=AYEAR_nex
	OPEN(10,FILE=infsw,action='READ')
	do i=1,13
	read(10,30) txt ! extra lines
	enddo
C
	do idd=ndnyr+1,ndnyr+5
		iswmax=0
	do ii=0,23
	read(10,31,end=1239) ind24(ii)
	if((ind24(ii).ne.9999).and.(iswmax.lt.ind24(ii))) iswmax=ind24(ii) 
	enddo
	isw(idd)=iswmax
	enddo
 1239	close(unit=10)

C
C Reading AE file
C
	infae(14:17)=AYEAR
	OPEN(10,FILE=infae,action='READ')
	do idd=1,ndnyr
		iaemax=0
	read(10,37,end=339) (ind24(ii),ii=0,23)
	do ii=0,23
	if (iaemax.lt.ind24(ii)) iaemax=ind24(ii) 
	enddo
	iae(idd)=iaemax
	enddo
  339	close(unit=10)
C
C+ 5 days from next year
	infae(14:17)=AYEAR_nex
	OPEN(10,FILE=infae,action='READ')
	do idd=ndnyr+1,ndnyr+5
		iaemax=0
	read(10,37,end=1339) (ind24(ii),ii=0,23)
	do ii=0,23
	if (iaemax.lt.ind24(ii)) iaemax=ind24(ii) 
	enddo
	iae(idd)=iaemax
	enddo
 1339	close(unit=10)

C Reading Dst file
C
	infdst(16:19)=AYEAR
	OPEN(10,FILE=infdst,action='READ')
	do idd=1,ndnyr
	idstmin=1000
	read(10,37,end=439) (ind24(ii),ii=0,23)
	do ii=0,23
	if (idstmin.gt.ind24(ii)) idstmin=ind24(ii) 
	enddo
	idst(idd)=idstmin
	enddo
  439	close(unit=10)
C
C+ 5 days from next year
	infdst(16:19)=AYEAR_nex
	OPEN(10,FILE=infdst,action='READ')
	do idd=ndnyr+1,ndnyr+5
	idstmin=1000
	read(10,37,end=1439) (ind24(ii),ii=0,23)
	do ii=0,23
	if (idstmin.gt.ind24(ii)) idstmin=ind24(ii) 
	enddo
	idst(idd)=idstmin
	enddo
 1439	close(unit=10)
C
C Reading Wp file
C
	infwp(23:24)=AYR
	OPEN(10,FILE=infwp,action='READ')
	read(10,*) txt ! extra line

	do idd=1,ndnyr
	read(10,34,end=539) (wp24(ii),ii=0,23)
		wpmax=0.
		do ii=0,23
	if (wpmax.lt.wp24(ii)) wpmax=wp24(ii) 
	enddo
	wp(idd)=wpmax
	enddo
  539	close(unit=10)
C
C+ 5 days from next year
C Reading Wp file
	infwp(23:24)=AYEAR_nex(3:4)
	OPEN(10,FILE=infwp,action='READ')
	read(10,*) txt ! extra line
		
	do idd=ndnyr+1,ndnyr+5
	wpmax=0.
	read(10,34,end=1539) (wp24(ii),ii=0,23)
	do ii=0,23
	if (wpmax.lt.wp24(ii)) wpmax=wp24(ii) 
	enddo
	wp(idd)=wpmax
	enddo
 1539	close(unit=10)
C
C Reading Vn file
C	infvn(31:32)=AYR
C	OPEN(10,FILE=infvn,action='READ')
C	read(10,*) txt ! extra line
c		do idd=1,ndnyr
c	read(10,34,end=639) (ind24(ii),ii=0,23)
c	ivnmax=0
c	do ii=0,23
c	if (ivnmax.lt.ind24(ii)) ivnmax=ind24(ii) 
c	enddo
c	ivn(idd)=ivnmax
c	enddo
c  639	close(unit=10)
C+ 5 days from next year
c	infvn(31:32)=AYEAR_nex(3:4)
c	OPEN(10,FILE=infvn,action='READ')
c	read(10,*) txt ! extra line
c	do idd=ndnyr+1,ndnyr+5
c	read(10,34,end=1639) (ind24(ii),ii=0,23)
c		ivnmax=0
c	do ii=0,23
c	if (ivnmax.lt.ind24(ii)) ivnmax=ind24(ii) 
c	enddo
c	ivn(idd)=ivnmax
c	enddo
c 1639	close(unit=10)

C
   35 format(1X,F8.3,2(1X,I2),3(1X,F7.2),1X,I4)
C
C+
	infpch='delpchcat100.txt'  ! 2011-2019
C
	OPEN(15,file=infpch,action='READ')
	read(15,*) txt ! extra line
C
C Cycle on Pch peak events
C
	iev=0
C
	do 28 jj=1,349           ! Catalogue Pch peaks for 2011-2019
  228 read(15,35,end=27,err=27) ryear,kmn,kdy,pchmin,pchmax,dpch,kev
      kyear=int(ryear)
C
C Remove -2018.04.15 
	if (kev.ge.281) kev=kev-1
	if (kyear.ne.iyear) GOTO 28
C
	kyr=kyear-(kyear/100)*100
	klda=ndoy(kyr,kmn,kdy)
	klda2=klda+5
C
C Select peak indices during from currend day of PCH-max +5 days
C
C+	nxxmax ! peak daily index XX for d0+5 days
C+	nxxday !  nday of delay of XX from d0 to d0+5 days: (nday=0,1,...5)
C
C Select peak Vsw
	nswday=0
	nswmax=0
	do lda=klda,klda2
	if (isw(lda).gt.nswmax) then
	nswmax=isw(lda)
      nswday=lda-klda
	endif
	enddo
C
C Select peak AE
	naeday=0
	naemax=0
	do lda=klda,klda2
	if (iae(lda).gt.naemax) then
	naemax=iae(lda)
      naeday=lda-klda
	endif
	enddo
C
C Select peak dst
	ndsday=0
	ndsmin=1000
	do lda=klda,klda2
	if (idst(lda).lt.ndsmin) then
	ndsmin=idst(lda)
      ndsday=lda-klda
	endif
	enddo
C Select peak aa
	naaday=0
	naamax=0
	do lda=klda,klda2
	if (iaa(lda).gt.naamax) then
	naamax=iaa(lda)
      naaday=lda-klda
	endif
	enddo
C Select peak ap
	napday=0
	napmax=0
	do lda=klda,klda2
	if (iap(lda).gt.napmax) then
	napmax=iap(lda)
      napday=lda-klda
	endif
	enddo
C Select peak Wp
	nwpday=0
	wpmax=0.
	do lda=klda,klda2
	if (wp(lda).gt.wpmax) then
	wpmax=wp(lda)
      nwpday=lda-klda
	endif
	enddo
C Select peak Vn
c	nvnday=0
c	nvnmax=0
c	do lda=klda,klda2
c	if (ivn(lda).gt.nvnmax) then
c	nvnmax=ivn(lda)
c      nvnday=lda-klda
c	endif
c	enddo
C
C Output results
C
	write(*,33) ryear,kmn,kdy,pchmin,pchmax,dpch,nswday,nswmax
	+,naeday,naemax,naaday,naamax,napday,napmax,ndsday,ndsmin
C     +,nvpday,nvpmax,nvnday,nvnmax,kev
     +,nwpday,wpmax,kev
	write(16,33) ryear,kmn,kdy,pchmin,pchmax,dpch,nswday,nswmax
	+,naeday,naemax,naaday,naamax,napday,napmax,ndsday,ndsmin
C     +,nvpday,nvpmax,nvnday,nvnmax,kev
     +,nwpday,wpmax,kev
C Remove -2018.04.15 
  28	CONTINUE
   27 close(unit=15)
c
C
C   33 format(1X,F8.3,2(1X,I2),3(1X,F7.2),6(1X,I2,1X,I5),1X,I4)
   33 format(1X,F8.3,2(1X,I2),3(1X,F7.2),5(1X,I2,1X,I5),1X,I2,1X,F4.1
     +,1X,I4)
C?   31 format(1X,A4,2(1X,I5))
C
C
C
  400	CONTINUE ! next year
C	
      close(unit=16)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

