	program aedgecoc
c .......................................................Aug 2016
C Select dGEC+max, DGEC-min for AE storms 
C for 1999-2015
C
c
	DIMENSION THR(11),cdp(10),cdn(10),iutp(200),iutn(200)
	+,pdp(10),pdn(10),rthr(11)
     +,iaestart(1840,4),iaepeak(1840,4),aepeak(1840)
	CHARACTER(10) DD
      CHARACTER(10) TT,word
      CHARACTER(5) ZZ
	CHARACTER*64,infilae,infile,outfile
	CHARACTER*4 AYEAR
	CHARACTER*2 AMN,ADY
	CHARACTER*1 sdq
	DATA THR/0,1000,2000,3000,4000,5000,6000,7000,8000,9000,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='aedgecoc.res'
	OPEN(11,FILE=OUTFILE,ACCESS='APPEND')
C    1 AE 9901*06 19  AE 9901*06 19   504.  AE 9901*06 21    3
   36	format(9X,2I2,2(1X,I2),5X,2(I2),2(1X,I2),2X,F5.0)

C  Cycle on 3 categories
C-      DO 105 ik=1,3
	infilae='AEstorm9915.txt'
		OPEN(10,FILE=infile,ACTION='READ')
 	icntot=0 ! count of events for 17 years 
	do ii=1,1840
	read(10,36,end=55) JYEAR1,JMN1,JDY1,JUT1,JYEAR2,JMN2,JDY2,JUT2
	+,aepeak(ii)
	iaestart(ii,1)=jyear1
	iaestart(ii,2)=jmn1
	iaestart(ii,3)=jdy1
	iaestart(ii,4)=jut1
	iaepeak(ii,1)=jyear2
	iaepeak(ii,2)=jmn2
	iaepeak(ii,3)=jdy2
	iaepeak(ii,4)=jut2
      icntot=icntot+1
	enddo      
  55  CLOSE(unit=10)
	write(*,791) word,icntot
	write(11,791) word,icntot


C start
	do i=1,10
	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,10
	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,10
	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,10
	pdp(ii)=cdp(ii)/icntot*100.
	pdn(ii)=cdn(ii)/icntot*100.
	enddo
	do k=1,10
	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

