	program histdmnp
c .......................................................May 2016
C Histogram of occurrence Dmp+, Dmn- for 3 categories of ICME events
C for 1999-2015
C
c
	DIMENSION THR(9),cdp(8),cdn(8),iutp(200),iutn(200)
	+,pdp(8),pdn(8),rthr(9)
	CHARACTER(10) DD
      CHARACTER(10) TT,word
      CHARACTER(5) ZZ
	CHARACTER*64,infile,outfile
	CHARACTER*4 AYEAR
	CHARACTER*2 AMN,ADY
	CHARACTER*1 sdq
	DATA THR/0,500,1000,2000,3000,4000,5000,6000,10000/ 
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
  791 FORMAT(A10,1X,I4)
		  outfile='histdmnp.res'
	OPEN(11,FILE=OUTFILE,ACCESS='APPEND')
C
   36	format(A4,2(1X,A2),1X,F5.2,3(1X,I5),1X,F5.1,3(1X,I5))

C  Cycle on 3 categories
      DO 105 ik=1,3
	if (ik.eq.1) then
	sdq='s'
	word='%Storm 1  '
	endif
	if (ik.eq.2) then
	sdq='d'
	word='%Disturb 2'
	endif
	if (ik.eq.3) then
	sdq='q'
	word='%Quiet 3  '
	endif
	infile='listicmes.txt'
	infile(9:9)=sdq
		OPEN(10,FILE=infile,ACTION='READ')
 	icntot=0 ! count of events for 17 years 
	do ii=1,200
	read(10,36,end=55) AYEAR,AMN,ADY,UT1,iae,iaa,iap,apt,idst
	+,idtp,idtn
	iutp(ii)=idtp
	iutn(ii)=idtn
      icntot=icntot+1
	enddo      
  55  CLOSE(unit=10)
	write(*,791) word,icntot
	write(11,791) word,icntot


C start
	do i=1,8
	cdp(i)=0.
	cdn(i)=0.
	rthr(i)=thr(i)/100.
	enddo

C cycle on events	
	nres=0
	DO 101 ii=1,icntot
c     
	do j=1,8
	if ((iutp(ii).ge.thr(j)).and.(iutp(ii).lt.thr(j+1))) then
	cdp(j)=cdp(j)+1.
	goto 100
 	endif
	enddo
100	do j=1,8
	if ((iutn(ii).ge.thr(j)).and.(iutn(ii).lt.thr(j+1))) then
	cdn(j)=cdn(j)+1.
	goto 101
 	endif
	enddo

101	CONTINUE	   
	do ii=1,8
	pdp(ii)=cdp(ii)/icntot*100.
	pdn(ii)=cdn(ii)/icntot*100.
	enddo
	do k=1,8
	write(*,790) k,rthr(k),cdp(k),cdn(k),pdp(k),pdn(k)
	write(11,790) k,rthr(k),cdp(k),cdn(k),pdp(k),pdn(k)
	enddo
 105	CONTINUE
 790  format(1X,I2,1X,F5.0,2(1X,F4.0),2(1X,F6.2))
C  %
c   

  800 CONTINUE             

	CLOSE(unit=11)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

