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 extbhss48
C----------------------------------------------------------------
C
C
	DIMENSION IM(12),bt(0:47),bz(0:47),vsw(0:47)
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,OUTHSS,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='cathss48b.txt'
	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
        bt_pre=0.
	bz_pre=0.
	vsw_pre=300.
C
C Year-to-year cycle							  ++++++++++++++++
C
	DO 122 iyear=1996,2019
C	read(12,29,end=34) ryear,ldavmax,imnvmax,idyvmax,itvminmax,vmin
C	+,iutvmax,vmax,dvmax,nn
C	iyear=int(ryear)
	iyr=iyear-iyear/100*100
	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')
	lda=0
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
C==	DO 125 lda=1,ndnr
C
C Read OMNI data for 0:47 hrs
    1	 lda=lda+1
      IF (lda.gt.ndnr) GOTO 50  ! next year =================>>>
C
       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
C
        IF ((iyear.eq.1996).and.(lda.eq.1)) GOTO 2 ! read next day
 125	CONTINUE
C==   50	close(unit=10)
C
      btmax=0.
C?	iut1=0 !Start check Vmax(1st day)
	iut1=24 !Start check Vmax(1st day)
      iut2=47  ! (2nd day)
C
	DO iut=iut1,iut2
 	if (bt(iut).gt.btmax) then
	btmax=bt(iut)
	iutbt=iut
	endif
	ENDDO
C
      vswmax=0.
	DO iut=iutbt,iut2
 	if (vsw(iut).gt.vswmax) then
	vswmax=vsw(iut)
	iutvmax=iut
	endif
	ENDDO
C
       bzmin=100.
	DO iut=iutbt,iutvmax
	if (bz(iut).lt.bzmin) bzmin=bz(iut)
	ENDDO

C++
C-        if (iutbt.lt.6) then
C-	 GOTO 2                  !next day
C-	      else
C	iut1=iutbt-6
C-	endif
C	DO 121 kk=iutbt,iut1,-1
     	kk=iutbt
	k_5=kk-5
	k_4=kk-4
	k_3=kk-3
	k_2=kk-2
	k_1=kk-1
	btmin=AMIN1(bt(k_5),bt(k_4),bt(k_3),bt(k_2),bt(k_1))
C  121	CONTINUE
    3	delbt=btmax-btmin
C
C Select t0(Btmax)
    	if (delbt.lt.3.5) GOTO 2 ! next day
C select Vswmin
       vswmin=2000.
	iut1=iutvmax-24
	do iut=iut1,iutvmax
	if (vswmin.gt.vsw(iut)) then
	vswmin=vsw(iut)
	iutvmin=iut
	endif
	enddo
	dvmax=vswmax-vswmin
	IF (dvmax.lt.50.)        GOTO 2
C
C?	if (iutbt.lt.24) then
C?	ldabt=lda-1
C	iutbt=kk
C?	exit
C?	                 else
	ldabt=lda
	iutbt=iutbt-24
C?	exit
C?	endif
C
	itvminmax=iutvmin-iutvmax
C
C?	if (iutvmax.lt.24) then
C?	ldavmax=lda-1
C	iutbt=kk
C?	exit
C?	                 else
	ldavmax=lda
	iutvmax=iutvmax-24
C?	exit
C?	endif
C++
C
C
C 
      if (delbt.ge.3.5) then
	istrm=istrm+1
	ryear=float(iyear)+(ldabt-1)*deyr
	call submmdd(ldabt,iyr,imnbt,idybt)
       write(*,129) ryear,ldabt,imnbt,idybt,iutbt,btmin,btmax,delbt
	+,bzmin,itvminmax,vswmin,iutvmax,vswmax,dvmax,istrm
       write(11,129) ryear,ldabt,imnbt,idybt,iutbt,btmin,btmax,delbt
	+,bzmin,itvminmax,vswmin,iutvmax,vswmax,dvmax,istrm
	endif
C 
   2	if (lda.le.ndnr) then
	do k=0,23
	bt(k)=bt(k+24)
	bz(k)=bz(k+24)
	vsw(k)=vsw(k+24)
	enddo
	 GOTO 1
	endif

C
   50	close(unit=10)
  122	CONTINUE				!++++++++++++++++ next year
C
  129 format(1X,F8.3,1X,I3,3(1X,I2),4(1X,F5.1),1X,I3,1X,F6.0,1X,I2
     +,2(1X,F6.0),1X,I4)
	close(unit=11)
C
   35      write(*,*) outhss
      pause ' ' 
      STOP
       END

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