      program sorticme3
C.......................................................May 2016
      DIMENSION iap(0:24),iaa(0:24),iae(0:24),idst(0:24),apt(0:24)
 	+,idmp(0:24),idmn(0:24)

	CHARACTER*65 infilae,infildst,infilaa,infilap,infilapt,infeq
     +,inptdmp,inptdmn,outfiles,outfiled,outfileq
	CHARACTER*4 AYEAR,BYEAR
		CHARACTER*2 AMN,ADY,BMN,BDY

	infilae='icmaei24.txt'  !  AE for ICME 1999- during 12 hrs 
	infildst='icmdst24.txt'  ! Dst for ICME 1999- during 12 hrs 
	infilaa='icmaai24.txt'  !  aa for ICME 1999- during 12 hrs 
	infilap='icmapi24.txt'  !  ap for ICME 1999- during 12 hrs 
	infilapt='icmapt24.txt'  ! apt for ICME 1999- during 12 hrs 
	inptdmp='icmdmp24.txt'  ! Dm+ for ICME 1999- during 12 hrs 
	inptdmn='icmdmn24.txt'  ! Dm- for ICME 1999- during 12 hrs 
	OPEN(11,FILE=infilAE,action='READ')
	OPEN(12,FILE=infilDST,action='READ')
	OPEN(13,FILE=infilAA,action='READ')
	OPEN(14,FILE=infilAP,action='READ')
	OPEN(15,FILE=infilAPT,action='READ')
	OPEN(16,FILE=inptDMP,action='READ')
	OPEN(17,FILE=inptDMN,action='READ')
C
	      infeq='listicme1.txt'
	OPEN(10,file=infeq,action='READ')
C
      outfiles='listicmes.txt'
      outfiled='listicmed.txt'
	outfileq='listicmeq.txt'

      OPEN(18,file=outfiles)
	 OPEN(19,file=outfiled)
	 OPEN(20,file=outfileq)
C
   33 format(1X,A4,2A2,1X,F5.2,25(1X,I5))
   34 format(1X,A4,2A2,1X,F5.2,25(1X,F5.1))
   35	format(A4,2(1X,A2),1X,F5.2)
   36	format(A4,2(1X,A2),1X,F5.2,3(1X,I5),1X,F5.1,3(1X,I5))
C ,iaem,iaam,iapm,aptm,idstm,idmpm,idmnm
	icnts=0
	icntd=0
	icntq=0
	icntaes=0
	icntdst=0
C
	 jj=0
  101	read(10,35,end=27,err=2) AYEAR,AMN,ADY,UT1
      jj=jj+1
	iev=jj
	do k=0,24
	iap(k)=0
	iaa(k)=0
	iae(k)=0
	idst(k)=0
	apt(k)=0.
	idmp(k)=0
	idmn(k)=0
	enddo
C
C Read selected geom. and ion. indices:
	read(11,33) BYEAR,BMN,BDY,BUT1,(iae(k),k=0,24) 
	read(12,33) BYEAR,BMN,BDY,BUT1,(idst(k),k=0,24) 
	read(13,33) BYEAR,BMN,BDY,BUT1,(iaa(k),k=0,24) 
	read(14,33) BYEAR,BMN,BDY,BUT1,(iap(k),k=0,24) 
	read(15,34) BYEAR,BMN,BDY,BUT1,(apt(k),k=0,24) 
	read(16,33) BYEAR,BMN,BDY,BUT1,(idmp(k),k=0,24) 
	read(17,33) BYEAR,BMN,BDY,BUT1,(idmn(k),k=0,24) 
C
	iaem=0
	idstm=0
	iaam=0
	iapm=0
	aptm=0.
	idmpm=0
	idmnm=0
C select max or min index:
      do k=0,24
	if (iae(k).gt.iaem) iaem=iae(k)
      if (idst(k).lt.idstm) idstm=idst(k)
	if (iaa(k).gt.iaam) iaam=iaa(k)
	if (iap(k).gt.iapm) iapm=iap(k)
	if (apt(k).gt.aptm) aptm=apt(k)
	if (idmp(k).gt.idmpm) idmpm=idmp(k)
	if (idmn(k).gt.idmnm) idmnm=idmn(k)
	enddo
C
	if (iaem.ge.1000) icntaes=icntaes+1
	if (idstm.le.-100) icntdst=icntdst+1

C Sort events to 3 categories : S -storm, d - disturbed, q -quiet
C
      if ((iaem.ge.1000).or.(idstm.le.-100).or.(iaam.ge.90).or.
	+(iapm.ge.60).or.(aptm.ge.28)
Crem     +.or.(idmpm.ge.1000).or.(idmnm.ge.1000)
     +) then
	write(*,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	write(18,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	 icnts=icnts+1
	 goto 101
                                        else
      if ((iaem.gt.500).or.(idstm.le.-50).or.(iaam.ge.45).or.
	+(iapm.ge.30).or.(aptm.ge.14)
C     +.or.(idmpm.ge.500).or.(idmnm.ge.500)
     +) then
	write(*,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	write(19,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	 icntd=icntd+1
	 goto 101
	                    else
	write(*,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	write(20,36) BYEAR,BMN,BDY,BUT1,iaem,iaam,iapm,aptm,idstm,idmpm
	+,idmnm
	 icntq=icntq+1
	 goto 101
	endif
	                                  endif
  27  close(unit=10)
      close(unit=11)
	close(unit=12)
	close(unit=13)
	close(unit=14)
	close(unit=15)
	close(unit=16)
	close(unit=17)
	close(unit=18)
	close(unit=19)
	close(unit=20)
	write(*,*) jj,icnts,icntd,icntq,icntaes,icntdst
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
	stop
	END
