C........................................................Apr 2021
C
C...................................................
C Extract kp for -24h<t0< +48h for kp/(Dst<-250nT) events (ev=8,SC22), (ev=9, SC23)
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ Kpmax_3hour
C 
      PROGRAM decodws5kp
C Input storm22.txt 1986-1996, storm23.txt 1997-2008...........................Apr 2021
C add: select from -24h < to <+48h for kp_storms-events
C
C----------------------------------------------------------------
C
	DIMENSION inkp(0:71),inap(0:71),inaa(0:71) ! 3h indices for 24h (0r 72h file for 1h indices)
	+,kkp(0:23),kap(0:23),kaa(0:23)
C
C	CHARACTER*2 AYR,AMN,ADY,AUT,AMM
	CHARACTER*4 AYEAR
C	+,DYR(1132),DMN(1132),DDY(1132),DUT(1132)
      CHARACTER*128 INFILE,INFSTORM
	+,outfilkp,outfilap,outfilaa !kp,ap,aa
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
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
	infile='d:/apkp/AA/aakpYEAR' !kp
C
crem	outfilkp='kp23d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch
crem		outfilap='ap23d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch
crem	outfilaa='aa23d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch
	outfilkp='kp22d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch
		outfilap='ap22d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch
	outfilaa='aa22d.txt' !total file for 1997-2009 =9 lines x 24 cols superposed epoch

	OPEN(11,FILE=OUTFILKP)
	OPEN(12,FILE=OUTFILAP)
	OPEN(13,FILE=OUTFILAA)
C
C kp/Dst 8(9) storms from 1996 to 2019
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
C   29 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,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
   29 format(6X,I4,2I2,1X,I2,2X,I4,2I2,1X,I2,1X,F6.1,1X,I4,2I2,1X,I2)

crem	INFSTORM='storms23.txt'
	INFSTORM='storms22.txt'

	open(10,file=INFSTORM,action='READ')
C	read(10,*) txt

C
C Cycle on events ====================================================
C
crem	DO 177 iev=1,9        ! SC23
	DO 177 iev=1,8        ! SC22
C	read(10,28,end=27) ryear1,imn1,idy1,iut1,v1,v2,delv
C	read(10,29,end=27) ryear1,ldam,imn1,idy1,iutm,v1,iut1,v2,delv
      read(10,29,end=27) iyear0,imn0,idy0,iut0,iyear1,imn1,idy1,iut1,pkp
	+,iyear2,imn2,idy2,iut2
	iutcnt=0
	do k=0,71
	inkp(k)=0
	inap(k)=0
	inaa(k)=0
	enddo
	iyear=iyear1
	iyr=iyear-iyear/100*100
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
      lda0=lda1-1 ! -1d
	lda2=lda1+2
C
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	DO 101 k=1,ndnyr
	  do i=0,7
	i1=i*3
C	read(15,137,end=102) jyear,jmn,jdy,jut,jkp,jap,jaa
	read(15,*,end=102) jyear,jmn,jdy,jut,jkp,jap,jaa
	kkp(i1)=jkp
	kap(i1)=jap
	kaa(i1)=jaa
	kkp(i1+1)=jkp
	kkp(i1+2)=jkp
	kap(i1+1)=jap
	kap(i1+2)=jap
	kaa(i1+1)=jaa
	kaa(i1+2)=jaa
	  enddo
  101	CONTINUE
  102	close(unit=15)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
       inkp(iutcnt)=kkp(ii)
       inap(iutcnt)=kap(ii)
       inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
  137 format(I4,4(1X,I2),2(1X,I3)) ! kp,ap,aa 
C Prec. day from current year:
       call blet4(iyear,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	  do i=0,7
	i1=i*3
	read(15,137,end=103) jyear,jmn,jdy,jut,kkp(i1),kap(i1),kaa(i1)
	kkp(i1+1)=kkp(i1)
	kkp(i1+2)=kkp(i1)
	kap(i1+1)=kap(i1)
	kap(i1+2)=kap(i1)
	kaa(i1+1)=kaa(i1)
	kaa(i1+2)=kaa(i1)
	  enddo
  109	CONTINUE
       DO ii=iut1,23
       inkp(iutcnt)=kkp(ii)
	inap(iutcnt)=kap(ii)
	inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	  do i=0,7
	i1=i*3
	read(15,137,end=103) jyear,jmn,jdy,jut,kkp(i1),kap(i1),kaa(i1)
	kkp(i1+1)=kkp(i1)
	kkp(i1+2)=kkp(i1)
	kap(i1+1)=kap(i1)
	kap(i1+2)=kap(i1)
	kaa(i1+1)=kaa(i1)
	kaa(i1+2)=kaa(i1)
	  enddo
C	  1st day :
       DO ii=0,23
       inkp(iutcnt)=kkp(ii)
	inap(iutcnt)=kap(ii)
	inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
	ENDDO
C 2nd day from current year:
	  do i=0,7
	i1=i*3
	read(15,137,end=103) jyear,jmn,jdy,jut,kkp(i1),kap(i1),kaa(i1)
	kkp(i1+1)=kkp(i1)
	kkp(i1+2)=kkp(i1)
	kap(i1+1)=kap(i1)
	kap(i1+2)=kap(i1)
	kaa(i1+1)=kaa(i1)
	kaa(i1+2)=kaa(i1)
	  enddo
C	  2nd day :
       DO ii=0,23
       inkp(iutcnt)=kkp(ii)
	inap(iutcnt)=kap(ii)
	inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
	ENDDO
C 3rd day from current year:
	  do i=0,7
	i1=i*3
	read(15,137,end=103) jyear,jmn,jdy,jut,kkp(i1),kap(i1),kaa(i1)
	kkp(i1+1)=kkp(i1)
	kkp(i1+2)=kkp(i1)
	kap(i1+1)=kap(i1)
	kap(i1+2)=kap(i1)
	kaa(i1+1)=kaa(i1)
	kaa(i1+2)=kaa(i1)
	  enddo
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inkp(iutcnt)=kkp(ii)
	inap(iutcnt)=kap(ii)
	inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  103	close(unit=15)
C
C Next year if necessary:
       if (iutcnt.lt.72) then   !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	iyear1=iyear+1
	call blet4(iyear1,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
C
	  do i=0,7
	i1=i*3
	read(15,137,end=104) jyear,jmn,jdy,jut,kkp(i1),kap(i1),kaa(i1)
	kkp(i1+1)=kkp(i1)
	kkp(i1+2)=kkp(i1)
	kap(i1+1)=kap(i1)
	kap(i1+2)=kap(i1)
	kaa(i1+1)=kaa(i1)
	kaa(i1+2)=kaa(i1)
	  enddo
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inkp(iutcnt)=kkp(ii)
	inap(iutcnt)=kap(ii)
	inaa(iutcnt)=kaa(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  104	close(unit=15)
      endif	                !>>>>>>>>>>>>>>>>>>>>>
C
  138 format(72(1X,I3))
C Output matrices:
 	write(*,138) (inkp(k),k=0,71)
	write(11,138) (inkp(k),k=0,71)
 	write(*,138) (inap(k),k=0,71)
	write(12,138) (inap(k),k=0,71)
 	write(*,138) (inaa(k),k=0,71)
	write(13,138) (inaa(k),k=0,71)
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
	close(unit=12)
	close(unit=13)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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