	program delpchAE2
c .......................................................Nov 2019
C Extract (1) intense storms with AE>=1000; (2) moderate storms 500 <= AE <1000
C
C
C Select peaks of delPch=Pchmax(3d)-Pchmin(-3d) for del > 100 i.u.
C movind 3day window, 
C Select all Del >0: output => 'delpchpos.res'
C for 2011-2018
C
c
	DIMENSION ryears(317),IAE(317),IDST(317)
C	+,pmin(317),pmax(317),del(317)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*94,infile1,infile2,outfile1,outfile2,outfile3,txt
	CHARACTER*2 AMD(317,2)
	CHARACTER*38 txt1(317)
	CHARACTER*23 txt2(317)
	CHARACTER*8 txt3(317)
	CHARACTER*36 txt4(317)
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)
      outfile1='delpchAE1.res' ! Part with AE>=1000
	outfile2='delpchAE2.res' ! Part with 500<=AE<1000
	outfile3='delpchAE3.res' ! Part with AE<500 'quiet'
	OPEN(12,FILE=OUTFILE1)
	OPEN(13,FILE=OUTFILE2)
	OPEN(14,FILE=OUTFILE3)
C
   36	format(1X,F8.3,2(1X,A2),A38,I4,A23,I4,A8)
   37	format(40X,A36)
   38	format(1X,F8.3,2(1X,A2),A38,I4,A23,I4,A8,A36,1X,I3)
   30	format(A94)

C
	infile1='catpchind5d-.txt'
		OPEN(10,FILE=infile1,ACTION='READ')
	read(10,30) txt !~ title line
	write(12,30) txt
	write(13,30) txt
	write(14,30) txt
	infile2='catpcharc4-.txt'
	OPEN(11,FILE=infile2,ACTION='READ')
	read(11,30) txt !~ title line
	write(12,30) txt
	write(13,30) txt
	write(14,30) txt
C
 	icntot=0 ! count of dPch
	i1000=0
	i500=0
	i0=0
	do 100 ii=1,317
	read(10,36,end=102) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),idst(ii),txt3(ii)
      read(11,37,end=103)	txt4(ii)
      icntot=icntot+1
C
	if (IAE(ii).ge.1000) then
	i1000=i1000+1
	write(*,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i1000
	write(12,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i1000
	goto 100
	endif
C
	if ((IAE(ii).ge.500).and.(IAE(ii).lt.1000)) then
	i500=i500+1
	write(*,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i500
	write(13,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i500
	goto 100
	                                             else
	i0=i0+1
	write(*,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i0
	write(14,38) RYEARS(ii),AMD(ii,1),AMD(ii,2),txt1(ii)
	+,IAE(ii),txt2(ii),-idst(ii),txt3(ii),txt4(ii),i0
	endif
C
  100	CONTINUE

  102  CLOSE(unit=10)
  103  CLOSE(unit=11)
       CLOSE(unit=12)
	 CLOSE(unit=13)
	 CLOSE(unit=14)
  900 pause ' '
      STOP
      END

