	program cnticmewp
c .......................................................May 2016
C Check Wp storms (a) at ICME (b) none ICME events
C for 1999-2015
C
c
	DIMENSION iyear(410),imn(410),idy(410),ut1(410),lda(410)
	CHARACTER(10) DD
      CHARACTER(10) TT,word
      CHARACTER(5) ZZ
	CHARACTER*67,infilewp,infile,outfile1,outfile2,txt
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
	word='%ICME num:'
C
  789 FORMAT(I3,1X,A67)
  790 FORMAT(4X,A67)
  791 FORMAT(1X,A10,I4)
		  outfile1='icmewps.txt'
	outfile2='icmewpq.txt'
	OPEN(11,FILE=OUTFILE1)
	OPEN(12,FILE=OUTFILE2)
C
   35	format(4X,I4,2(1X,I2),1X,F2.0,2X,I4,2(1X,I2),1X,F2.0)
   36	format(I4,2(1X,I2),1X,F5.2)

C
	infile='listicme1.txt'
		OPEN(10,FILE=infile,ACTION='READ')
      infilewp='tecstorm1.txt'
 	icntot=0 ! count of events for 17 years 
C
C Listicme1.txt:
C
	do ii=1,409
	read(10,36,end=55) IYEAR(ii),IMN(ii),IDY(ii),UT1(ii)
	iyr=iyear(ii)-iyear(ii)/100*100
	lda(ii)=ndoy(iyr,imn(ii),idy(ii))
      icntot=icntot+1
	enddo      
  55  CLOSE(unit=10)
	write(*,791) word,icntot
	write(11,791) word,icntot
	write(12,791) word,icntot

		OPEN(10,FILE=infilewp,ACTION='READ')
C start
C cycle on Wp storms	
	icnts=0
	icntq=0
	DO 105 iwp=1,296
	read(10,35,end=106) jyears,jmns,jdys,uts,jyearp,jmnp,jdyp,utp
	jyrs=jyears-jyears/100*100
	ldas=ndoy(jyrs,jmns,jdys)
	jyrp=jyearp-jyearp/100*100
	ldap=ndoy(jyrp,jmnp,jdyp)
	iflag=0
C Check icme precursor:
	DO 101 ii=1,icntot
	if (jyears.ne.iyear(ii)) goto 101
	lda1=lda(ii)+1
	if (((ldas.eq.lda(ii)).and.(uts.ge.ut1(ii))).or.
	+((ldas.eq.lda1).and.(uts.lt.12).and.(ut1(ii).gt.12))
     +.or.((ldap.eq.lda(ii)).and.(utp.ge.(ut1(ii))).or.(ldap.eq.lda1)))
     + then
	backspace(10)
	read(10,790) txt
	icnts=icnts+1
	write(*,789) icnts,txt
	write(11,789) icnts,txt
      iflag=1
	exit
	endif
101	CONTINUE	   
	if (iflag.eq.0) then
	backspace(10)
	read(10,790,end=106) txt
	icntq=icntq+1
	write(*,789) icntq,txt
	write(12,789) icntq,txt
	endif
c     
 105	CONTINUE
c   
 106     	CLOSE(unit=10)
	CLOSE(unit=11)
	CLOSE(unit=12)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

