C........................................................Feb.2020
C Extract events of delB = B(1hmax)-B(-3hmin) > 3.8 nT hourly 0...47 hrs UT hrs 
C with t0 at B(1hmax) including delVsw = Vswmax(t0 : t0+23h)-Vswmin(t0 : t0+20h)
C Use 1305 HSS events 'cathss48m.txt'  to fins t0 at delB >3.8 during t(Vswmax)-24 h
C extract : to(Bmax), Bmin(to-3h),Bmax,delB,Bzmin at [t0: t(Vswmax)]
C 
C 
      PROGRAM entbhss48
C----------------------------------------------------------------
C
C
	DIMENSION IM(12),bt(0:47),bz(0:47),vsw(0:47)
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,OUTHSS,INFHSS,txt
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	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
	outhss='catbhss48m.txt'
	infhss='cathss48m.txt'
	OPEN(12,file=infhss,action='READ')
	OPEN(11,file=outhss,access='APPEND')

	infile='d:\apkp\OMNI\swYEAR.txt'

C
C HSS storms from 1996 to 2019
C    1       2  3  4   5    6    7    8      9     10
C----------------------------------------------------
C 1996.005   3  1  3  -6   459. 11   546.    87.    1
   28 format(1X,F8.3,2(1X,I2),3(1X,F6.0))
   29 format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,1X,F6.0,1X,I2,2(1X,F6.0),1X
     +,I4)
   30	format(A50)
C
       istrm=0
C
C Events cycle							  ++++++++++++++++
C
	DO 122 ihss=1,1305
	read(12,29,end=34) ryear,ldavmax,imnvmax,idyvmax,itvminmax,vmin
	+,iutvmax,vmax,dvmax,nn
	iyear=int(ryear)
	iyr=iyear-iyear/100*100
C      lda1=ndoy(iyr,imn,idy) ! 
	call blet4(iyear,AYEAR)
	infile(16:19)=AYEAR
	write(*,*) ' YEAR=',AYEAR
C
C
	xyear=float(iyear)
		if(int(xyear/4.)*4.eq.iyear) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF
	deyr=1./ndnr
	iflag=0
C day-to day cycle
C							   >>>>>>>>>>>>>>>>>
	OPEN(10,file=infile,action='READ')
CYEAR DOY HR    B     Bz     Te     Np    Vsw 
C1996   1  0   5.7   4.1   92006.   7.2  423.
	do i=1,13
	read(10,30) txt ! extra lines
	enddo
   31	format(I4,1X,I3,1X,I2,2(1X,F5.1),16X,F5.0)
C
C READ OMNI file
C
	DO 125 lda=1,ldavmax
C
C Read OMNI data for 0:47 hrs
        bt_pre=0.
	bz_pre=0.
	vsw_pre=300.
	DO ii=0,23
   	read(10,31,end=50) kyear,kday,kut,bt(ii+24),bz(ii+24),vsw(ii+24)
	if (bt(ii+24).gt.999.) then
	 bt(ii+24)=bt_pre
	                       else
	bt_pre=bt(ii+24)
	endif
	if (bz(ii+24).gt.999.) then
	 bz(ii+24)=bz_pre
	                       else
	bz_pre=bz(ii+24)
	endif
	if (vsw(ii+24).gt.9990.) then
	 vsw(ii+24)=vsw_pre
	                       else
	vsw_pre=vsw(ii+24)
	endif
	ENDDO
	if (lda.lt.ldavmax) then
	do k=0,23
	bt(k)=bt(k+24)
	bz(k)=bz(k+24)
	vsw(k)=vsw(k+24)
	enddo
	endif
 125	CONTINUE
   50	close(unit=10)
C
      btmax=0.
	iut1=iutvmax !Start check at iutmax(1st day)
	icn999=0
      iut2=24+iutvmax  ! Vmax occur at iutmax(2nd day)
C
	DO iut=iut1,iut2
 	if (bt(iut).gt.btmax) then
	btmax=bt(iut)
	iutbt=iut
	endif
	ENDDO
C
       bzmin=100.
	DO iut=iut1,iut2
	if (bz(iut).lt.bzmin) bzmin=bz(iut)
	ENDDO

C++
        if (iutbt-iut1.lt.4) then
	iut1=iutbt-4
	endif
	DO 121 kk=iutbt,iut1,-1
	k_3=kk-3
	k_2=kk-2
	k_1=kk-1
	btmin=AMIN1(bt(k_3),bt(k_2),bt(k_1))
C	btmax=bt(kk)
	delbt=btmax-btmin
C
C Select t0(Btmax)
	if (delbt.gt.3.5) then
	if (kk.lt.24) then
	ldabt=ldavmax-1
C	iutbt=kk
	exit
	                 else
	ldabt=ldavmax
	iutbt=iutbt-24
	exit
	endif
	       endif
  121	CONTINUE
C++
C
C
C 
      if (delbt.ge.3.5) then
	istrm=istrm+1
       write(*,129) ryear,ldavmax,imnvmax,idyvmax,itvminmax,vmin
	+,iutvmax,vmax,dvmax,ldabt,iutbt,btmin,btmax,delbt,bzmin,istrm
       write(11,129) ryear,ldavmax,imnvmax,idyvmax,itvminmax,vmin
	+,iutvmax,vmax,dvmax,ldabt,iutbt,btmin,btmax,delbt,bzmin,istrm
	endif
C 
C
	close(unit=10)
  122	CONTINUE				!++++++++++++++++
C
  129 format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,1X,F6.0,1X,I2,2(1X,F6.0)
     +,1X,I3,1X,I2,4(1X,F5.1),1X,I4)
	close(unit=11)
   34		close(unit=12)
C
   35      write(*,*) outhss
      pause ' ' 
      STOP
       END

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