	program aedgecoc
c .......................................................Aug 2016
C Select dGEC+max, DGEC-min for AE storms 
C+Select dGEC+max, DGEC-min for Dst storms 
C for 1999-2015
C
c
	DIMENSION 
     +iaestart(1840,4),iaepeak(1840,4),aepeak(1840),idgec(0:23,3)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64,infilae,infile,outfile
	CHARACTER*2 AMN,ADY,AYR
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
C+  791 FORMAT(1X,'AE>500: ',1X,I4)
C    1 DST9901*13 17  DST9901*13 23 -112.  DST9901*15 20   52
  791 FORMAT(1X,'Dst<-500: ',1X,I4)
C+		  outfile='aedgecoc.res'
		  outfile='dstdgecoc.res'
	OPEN(12,FILE=OUTFILE,ACCESS='APPEND')
C    1 AE 9901*06 19  AE 9901*06 19   504.  AE 9901*06 21    3
C+  36	format(9X,2I2,2(1X,I2),5X,2(I2),2(1X,I2),2X,F5.0) !AE
  36	format(9X,2I2,2(1X,I2),5X,2(I2),2(1X,I2),1X,F5.0) !Dst

C  Cycle on 3 categories
C+	infilae='AEstorm9915.txt'
	infilae='dststorm9915.txt'
		OPEN(10,FILE=infilae,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) icntot
	write(12,791) icntot


C start
C cycle on events	
	nres=0
	DO 101 ii=1,icntot
c     
	iyr1=iaestart(ii,1)
	imn1=iaestart(ii,2)
	idy1=iaestart(ii,3)
	lda1=ndoy(iyr1,imn1,idy1)
	iyr2=iaepeak(ii,1)
	imn2=iaepeak(ii,2)
	idy2=iaepeak(ii,3)
	lda2=ndoy(iyr2,imn2,idy2)
	call blet2(iyr2,AYR)
	call blet2(imn2,AMN)
	call blet2(idy2,ADY)
      aevent=aepeak(ii)
	ryr=float(iyr1)
		if(int(ryr/4.)*4.eq.iyr1) THEN 
c  	im(2)=29
	ndr=366
	                          ELSE
c	IM(2)=28
	ndr=365
	ENDIF
 
     
      infile='d:\web\dat0\YR\eoYRdgecj.txt'
	infile(13:14)=AYR
      infile(18:19)=AYR
	OPEN(11,file=infile,action='READ')
  37	format(3I2,24(1X,I4))
      lda3=lda2+1
	if(lda3.gt.ndr) lda3=lda2
C
	do j=1,lda3
		read(11,37,end=55) JYR,JMN,JDY,(idgec(k,3),k=0,23)
	if (j.eq.lda1) then
	do k=0,23
	idgec(k,1)=idgec(k,3)
	enddo
	endif
	if (j.eq.lda2) then
	do k=0,23
	idgec(k,2)=idgec(k,3)
	enddo
	endif
	enddo
C
	mindgec=0
	maxdgec=0
100	do j=1,3
          do k=0,23
	if ((idgec(k,j).lt.0).and.(idgec(k,j).lt.mindgec)) then
	mindgec=idgec(k,j)
 	endif
	if ((idgec(k,j).gt.0).and.(idgec(k,j).gt.maxdgec)) then
	maxdgec=idgec(k,j)
 	endif
	     enddo
	enddo
	write(*,790) ayr,amn,ady,lda2,aevent,maxdgec,mindgec
	write(12,790) ayr,amn,ady,lda2,aevent,maxdgec,mindgec
	CLOSE(unit=11)

101	CONTINUE	   !next AE event
      CLOSE(unit=12)
 790  format(1X,3(A2),1X,I3,1X,F5.0,2(1X,I4))
c   
  800 CONTINUE             

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

