C........................................................Mar. 2022
C nev=481 Kp storms1 / 86 Kpstprms2 // 1994-2021
C........................................................Jan.2020
C Extract peak for all indices from -24 < t0< +48h for Kp storm events 
C 
C Select period of 48hrs starting from t0-24h, ending t0+48h; t0 @ Kp peak
C 
      PROGRAM extpeakskpmd1
C add: select from -24h < to <+48h for Kp-events
C
C----------------------------------------------------------------
C
	DIMENSION xind(0:71),ind(0:71),IM(12) ! 3h indices for 24h (0r 48h file for 1h indices)
C
      CHARACTER*128 INFHSS
C+	+,infds
c-	+,infsw
C     +,infkp,infap
C     +,infae,infds,infal,infau
     +,infwp,infweg,infwlo,infwup
	+,outfile
C-     +,outhss  !
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*1 kpx
	DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C++
C
      ICALLS=0
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
c-       write(*,*) ' Enter m, n, s:'
c-	read(*,*) gns
	outfile='catkpstx.res'  ! Kp events t0 at Kp storm peak
	infhss='catkpstx.txt' !includes Kp & Dst peaksa
	infwp='kpwp3d_x.txt' ! x= 1 ~ 2			   input Wp
      infwup='kpwu3dmx.txt' ! x= 1 ~ 2         ! input WU
	      infwlo='kpwl3dmx.txt' ! x= 1 ~ 2         ! input WL
	      infweg='kpwe3dmx.txt' ! x= 1 ~ 2         ! input WE
C++
C Start kpstorm1 or kpstorm2
       kpx='1'
C
   1  CONTINUE
   	infwup(8:8)=kpx
      open(11,file=infwup,action='READ')
	   	infwlo(8:8)=kpx
      open(12,file=infwlo,action='READ')
	   	infweg(8:8)=kpx
      open(13,file=infweg,action='READ')
	   	infwp(8:8)=kpx
      open(14,file=infwp,action='READ')

	outfile(8:8)=kpx
	infhss(8:8)=kpx
C
   37 format(72(1X,F4.2))	 ! WE,WU,WL
   31 format(72(1X,I3))	 ! kp,ap,aa,
   33 format(72(1X,I4))	 ! ae,dst,sw,au,PCN,PCS
   32 format(72(1X,F4.1))	 ! Wp
  133 format(72(1X,I5))	 ! al
C
CC	outfile='hsspeakdx.txt' !total file for 1996-2021 =391 lines x 72 cols superposed epoch
CC	outfile(9:9)=gns
	OPEN(20,FILE=OUTFILE)
C
C Kp storms from 1994 to 2021
C
   28 format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,1X,F6.0,1X,I2,2(1X,F6.0))
C   29 format(1X,F8.3,3(1X,I2),3(1X,F6.0),1X,I4)
c   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,F3.1,2(1X,I3),2(1X,I4))
	open(10,file=infhss,action='READ')
C
C Cycle on events ====================================================
C
       nout=0
	newout=0
	nev=481
	if (kpx.eq.'2') nev=86
	DO 177 iev=1,nev
      read(10,29,end=27) ryear1,lda1,imn1,idy1,iut1,xkp,ikpsum
	+,iutvs,idsmin,ninp
	iyear=int(ryear1)
	iyr=iyear-iyear/100*100
	xyear=float(iyear)
	if(int(xyear/4.)*4.eq.iyear) THEN 
 	im(2)=29
	                          ELSE
	im(2)=28
	ENDIF
C=========================================
C		   Read WU data:
C
189       read(11,37,end=52) (xind(k),k=0,71)  ! WU
C+	ivsmax=0
       wumax=0.
	iutwu=0
	do iut=0,71
	iut0=iut-24
	if (wumax.lt.xind(iut)) then
	wumax=xind(iut)
	iutwu=iut0
	endif
	enddo
C
C  WL
       read(12,37,end=53) (xind(k),k=0,71)  ! WL
	wlmin=100.
	iutwl=0
	do iut=0,71
	iut0=iut-24
	if (wlmin.gt.xind(iut)) then
	wlmin=xind(iut)
	iutwl=iut0
	endif
	enddo
C
C  WE
       read(13,37,end=54) (xind(k),k=0,71)  ! WE
	wemax=0
	iutwe=0
	do iut=0,71
	iut0=iut-24
	if (wemax.lt.xind(iut)) then
	wemax=xind(iut)
	iutwe=iut0
	endif
	enddo
C		 
C  Wp
       read(14,32,end=55) (xind(k),k=0,71)  ! Wp
	wpmax=0
	iutwp=0
	do iut=0,71
	iut0=iut-24
	if (wpmax.lt.xind(iut)) then
	wpmax=xind(iut)
	iutwp=iut0
	endif
	enddo
C		   Avoid next data:
C==========================================
	GOTO 123
C==========================================
C  AE
       read(15,33,end=155) (ind(k),k=0,71)  ! AE
	iaemax=0
	iutae=0
	do iut=0,71
	iut0=iut-24
	if (ind(iut).lt.0) exit
	if (iaemax.lt.ind(iut)) then
	iaemax=ind(iut)
	iutae=iut0
	endif
	enddo
C  Dst
  155     read(16,33,end=56) (ind(k),k=0,71)  ! Dst
	idsmin=0
	iutds=0
	do iut=0,71
	iut0=iut-24
	if (idsmin.gt.ind(iut)) then
	idsmin=ind(iut)
	iutds=iut0
	endif
	enddo
C  AL
       read(17,133,end=157) (ind(k),k=0,71)  ! AL
	ialmin=0.
	iutal=0
	do iut=0,71
	iut0=iut-24
	if (ialmin.gt.ind(iut)) then
	ialmin=ind(iut)
	iutal=iut0
	endif
	enddo
C  AU
  157     read(18,33,end=58) (ind(k),k=0,71)  ! AU
	iaumax=0.
	iutau=0
	do iut=0,71
	iut0=iut-24
	if (iaumax.lt.ind(iut)) then
	iaumax=ind(iut)
	iutau=iut0
	endif
	enddo
C End extra parameters  ................>>>>>>>>>>>>>>>>>>>>>>
C
C Output matrices:
  123	continue
      nout=nout+1
C
  138 format(1X,F8.3,1X,I3,3(1X,I2),1X,F3.1,2(1X,I3),1X,I4
     +,3(1X,I3,1X,F5.2),1X,I3,1X,F4.1,1X,I4) ! Kp,Dst,WU,WL,WE,Wp
	write(*,138) ryear1,lda1,imn1,idy1,iut1,xkp,ikpsum
	+,iutvs,idsmin,iutwu,wumax,iutwl,wlmin,iutwe,wemax,iutwp,wpmax,ninp
	write(20,138) ryear1,lda1,imn1,idy1,iut1,xkp,ikpsum
	+,iutvs,idsmin,iutwu,wumax,iutwl,wlmin,iutwe,wemax,iutwp,wpmax,ninp

  177	CONTINUE
C+   51 close(unit=11)
   52 close(unit=11)  !+++
   53 close(unit=12)  !+++
   54 close(unit=13)  !+++
   55 close(unit=14)
CC       close(unit=15)
   56 continue
CC       close(unit=16)
   57 continue
CC       close(unit=17)
   58 continue
CC       close(unit=18)
   59 continue
CC      close(unit=19)
   60 continue
CC       close(unit=20)
   61 continue
CC      close(unit=21)
   27 close(unit=10)		!+
      close(unit=20)		!+
C
        if (kpx.eq.'1') then
	kpx='2'
	goto 1
	  endif
C
   35      write(*,*) outfile
      pause ' ' 
      STOP
       END

C***********************************************************
