C........................................................June 2023
C GEC and dGEC,%, for Catalogue of montly peak Apo(tau6) . 
C dGEC% profile:: (GEC-GECav)/GECav*100%
C  GECav: from GEC(to-36H) to GEC(to-13h) ~ 1 day prior to event
C
C  Search new t0 = t(Apo_tau6) peak [after peak t(Apomax)]
C retrive new t0-12h ... t0(Apo_tau6) peak ... t0+35 h ===total 48 hrs
C
C Extract Apo index and GEC  for -12h<t0< +36h for 223 Apo > 100 nT events 1995-2021
C or test storms 224-230 events 2022-2023
C
C........................................................Mar. 2023
C 
C t0 at Apo smoothed>=100nT storm peak
C
C Extract Apo smoothed-index and GEC  for -24h<t0< +48h for Apo events 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ AEmax_hour
C 
      PROGRAM degecmn12
C 
C----------------------------------------------------------------
C
	DIMENSION gec(0:48) ! 1h indices for 48h file 
	+,dgec(0:47),igec(0:47),idgec(0:47)
	+,jDYR(0:48),jDMN(0:48),jDDY(0:48),jDUT(0:48)
C
	CHARACTER*4 AYEAR,CYEAR
      CHARACTER*128 INFILE,INFHSS,outgec,outdev 
	+,outfile  !<= add GECave to infhss
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 AYR,AMN,ADY,AUT,CMN,CDY,CUT
C
      ICALLS=0
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
      infile='gechr.txt'  ! obtained in /Projects/GEC/
C  	infhss='catapo6mn.txt'  ! Apo(tau6) monthly peak
  	infhss='catapo6mn90.txt'  ! Apo(tau6) monthly peak
C
	outfile='catgecavmn90.txt'  !Catalogue for 1995-2023 + GECav at pre-event 24hrs 
	outgec='gecapomn90.txt' ! GEC profiles: t0-12...t0+35 (48 hourly )
	outdev='dgecapomn90.txt' ! dGEC profiles: t0-12...t0+35 (48 hourly)
C
C Start 
C
    1	CONTINUE
C
C    7	OPEN(11,FILE=OUTFILE,access='APPEND')
C      OPEN(12,FILE=OUTGEC,access='APPEND')
C	OPEN(13,FILE=OUTDEV,access='APPEND')
    7	OPEN(11,FILE=OUTFILE)
      OPEN(12,FILE=OUTGEC)
	OPEN(13,FILE=OUTDEV)
C
C Kp_1_2 nev storms from 1994 to 2021
C
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0)
   29 format(1X,F8.3,1X,I3,3(1X,A2),1X,F5.0,1X,F5.2,1X,I4)
  197 format(3X,2A2,1X,A2,10X,24(1X,I4))
C
	open(10,file=infhss,action='READ')
C
	open(15,file=infile,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!!
C
C Cycle on events ====================================================
C
Cnorm	nev=945 1995- 2023.02
C       	nev=342 ! total Apo monthly peak	1995-2023.06
              	nev=90 ! total Apo(tau6)>=90 nT monthly peak	1995-2023.06
C
	DO 177 iev=1,nev
C
      read(10,28,end=27) xyear,lda1,imn1,idy1,iut1,aep
C	if (iev.lt.290) GOTO 177  ! test outputTEMPPPPPPPPP
C
        call blet2(imn1,AMN)
      call blet2(idy1,ADY)
	call blet2(iut1,AUT)

	iutcnt=0
	iyear=int(xyear)
	iyr=iyear-iyear/100*100
	iut0=iut1-12
	lda0=lda1-1 ! pre-event day
C
C
       nhr=0
	khr=0
C
C--------------------------------------------------
  222	CONTINUE
C
		if(int(xyear/4.)*4.eq.iyear) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
C
C------------------------------------------------------
C
   36 format(1X,I4,3(1X,I2),1X,F4.2)	 ! hourly gec
   37	format(A4,2(1X,A2),44X,I3)
C
C
C read all years:
C
C
1135       call blet4(iyear,AYEAR)
        AYR=AYEAR(3:4)
C
C	open(15,file=infile,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!!
C
C============================================
       iflag=0
	napo=48
C
C
  333 read(15,36,end=102) jyear,jDMN(48),jDDY(48),jDUT(48),gec(48)
C        jyear=int(ryear)
	call blet4(jyear,CYEAR)
	jyr=jyear-jyear/100*100
	jDYR(48)=jyr
C--	jlda=ndoy(jyr,imn,jdy)
C
C Move data up 1 line
        do kk=0,47
	gec(kk)=gec(kk+1)
	jDYR(kk)=jDYR(kk+1)
	jDMN(kk)=jDMN(kk+1)
	jDDY(kk)=jDDY(kk+1)
	jDUT(kk)=jDUT(kk+1)
	  enddo
C
	     IF (iflag.eq.1) GOTO 334 ! continue read event data
C
C Produce GECav for pre-event day
C
      IF ((jDYR(47).eq.iYR).and.(jDMN(47).eq.iMN1).and.
	+(jDDY(47).eq.iDY1).and.(jDUT(47).eq.iut1)) THEN		 !++++++++++
	sum=0. ! to produce gecav for pre-event day
	do k=12,35
	sum=sum+gec(k)
	enddo
	gecav=sum/24.
	call blet2(imn1,CMN)
	call blet2(idy1,CDY)
	call blet2(iut1,CUT)
	write(*,29) xyear,lda1,CMN,CDY,CUT,aep,gecav,iev
      write(11,29) xyear,lda1,CMN,CDY,CUT,aep,gecav,iev
	iflag=1
	GOTO 333
	                 ELSE				  !++++++++++

	GOTO 333 
	                 ENDIF				  !++++++++++
C
C continue read GEC event profile:
C
  334	IF ((jDYR(12).eq.iYR).and.(jDMN(12).eq.iMN1).and.
     +(jDDY(12).eq.iDY1).and.(jDUT(12).eq.iut1)) THEN		 !==
         GOTO 335
	                else				 !==
              	GOTO 333
	               endif				 !==
C
C produce dGEC
C
  335	CONTINUE                     !
      do kk=0,47
	dgec(kk)=(gec(kk)-gecav)/gecav*100.
	idgec(kk)=nint(dgec(kk)*10.)
	igec(kk)=nint(gec(kk)*100.)
	enddo
C
   33 FORMAT(1X,3A2,1X,A2,1X,F7.2,1X,I3)
   38 format(48(1X,I4))
C Output 
 	write(*,38) (igec(k),k=0,47)
	write(12,38) (igec(k),k=0,47)
C
 	write(*,38) (idgec(k),k=0,47)
	write(13,38) (idgec(k),k=0,47)
  177	CONTINUE
  102 close(15)
   27 close(unit=10)
      close(unit=11)
	close(unit=12)
	close(unit=13)

C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

C***********************************************************
