	program histsscbz
c .......................................................Jan 2016
C Histogram of occurrence Bzmin, dB3h, dB/dt, dVsw for SSC events
C for 1999-2015
C
c
	DIMENSION SSC(4,460),TB3(7),TDB(7),TBZ(7),TSW(7)
	+,cb3(7),cdb(7),cbz(7),csw(7),iutmm(5,460)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64,infile,outfile,txt
	CHARACTER*6 YRMNDY,YMD(460)
	DATA TB3/0.0,3.0,6.0,9.0,12.0,15.0,18.0/ 
	DATA TDB/0.0,3.0,6.0,9.0,12.0,15.0,18.0/ 
	DATA TBZ/-25.0,-20.0,-15.0,-10.0,-5.0,0.0,5.0/
	DATA TSW/0.0,10.0,20.0,40.0,60.0,100.0,150.0/  
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
  790 FORMAT(1X,I3,1X,A6,1X,I4,4(1X,F7.2,I4))
  791 FORMAT(A64)
		  outfile='histsscbz.res'
	OPEN(11,FILE=OUTFILE,ACCESS='APPEND')
	infile='sscbz.txt'
		OPEN(10,FILE=infile,ACTION='READ')
	read(10,791) txt
 	icntot=0 ! count of SSC for 17 years =445
	do ii=1,460
	read(10,790,end=55) n,YRMNDY,iutmm(1,ii),x1,i1,x2,i2,x3,i3,x4,i4
	ssc(1,ii)=x1
	ssc(2,ii)=x2
	ssc(3,ii)=x3
	ssc(4,ii)=x4
	YMD(ii)=YRMNDY
	iutmm(2,ii)=i1
	iutmm(3,ii)=i2
	iutmm(4,ii)=i3
	iutmm(5,ii)=i4
      icntot=icntot+1
c	write(*,790)  n,YRMNDY,x1,x2,x3,x4
	enddo      
  55  CLOSE(unit=111)


C start
	do i=1,7
	cb3(i)=0.
	cdb(i)=0.
	cbz(i)=0.
	csw(i)=0. ! Hist
	enddo

C cycle on ssc	
	nres=0
	DO 105 ii=1,icntot

c     
C dB3h:
	if (ssc(1,ii).ge.tb3(7)) then
	cb3(7)=cb3(7)+1.
	goto 101
	endif
	do j=1,6
	if ((ssc(1,ii).ge.tb3(j)).and.(ssc(1,ii).lt.tb3(j+1))) then
	cb3(j)=cb3(j)+1.
	goto 101
 	endif
	enddo
101	CONTINUE	   
C	 dB/dt
	if (ssc(2,ii).ge.tdb(7)) then
	cdb(7)=cdb(7)+1.
	goto 102
	endif
	do j=1,6
	if ((ssc(2,ii).ge.tdb(j)).and.(ssc(2,ii).lt.tdb(j+1))) then
	cdb(j)=cdb(j)+1.
	goto 102
 	endif
	enddo
102	CONTINUE
C Bz	   
	if (ssc(3,ii).ge.tbz(7)) then
	cbz(7)=cbz(7)+1.
	goto 103
	endif
	do j=1,6
	if ((ssc(3,ii).ge.tbz(j)).and.(ssc(3,ii).lt.tbz(j+1))) then
	cbz(j)=cbz(j)+1.
	exit
 	endif
	enddo
103	CONTINUE	   
C Vsw
	if (ssc(4,ii).ge.tsw(7)) then
	csw(7)=csw(7)+1.
	goto 104
	endif
	do j=1,6
	if ((ssc(4,ii).ge.tsw(j)).and.(ssc(4,ii).lt.tsw(j+1))) then
	csw(j)=csw(j)+1.
	exit
 	endif
	enddo
104	CONTINUE	   
	if ((ssc(1,ii).lt.3.0).or.(ssc(2,ii).lt.3.0).or.(ssc(3,ii).ge.0.0)
	+.or.(ssc(4,ii).le.5.0)) goto 105
	nres=nres+1
	write(*,790) nres,YMD(ii),iutmm(1,ii),ssc(1,ii),iutmm(2,ii)
	+,ssc(2,ii),iutmm(3,ii),ssc(3,ii),iutmm(4,ii),ssc(4,ii),iutmm(5,ii) 
crem	write(11,790) nres,YMD(ii),iutmm(1,ii),ssc(1,ii),iutmm(2,ii)
crem	+,ssc(2,ii),iutmm(3,ii),ssc(3,ii),iutmm(4,ii),ssc(4,ii),iutmm(5,ii) 
 105	CONTINUE
	
C  %
	do k=1,7
	per1=cb3(k)/icntot*100.
	per2=cdb(k)/icntot*100.
	per3=cbz(k)/icntot*100.
	per4=csw(k)/icntot*100.
	write(*,72) tb3(k),cb3(k),per1,tdb(k),cdb(k),per2
	+,tbz(k),cbz(k),per3,tsw(k),csw(k),per4
	write(11,72) tb3(k),cb3(k),per1,tdb(k),cdb(k),per2
	+,tbz(k),cbz(k),per3,tsw(k),csw(k),per4
	enddo
c   

  800 CONTINUE             


   72 format(1X,4(1X,F5.0,1X,F5.0,1X,F6.3))
	CLOSE(unit=11)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

