C........................................................June 2023
C Detrended dGEC for Apo storms. 
C Base level GECq=GEC(tau_24) [gec(tau) for t=-36,-35,...-13h) from t(Apomax)]
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 detreapost12
C Input HSSUT.txt 1996-2019...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION inpn(0:47) ! 1h indices for 48h file 
	+,gec(0:47)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS
	+,outfile
	 CHARACTER*20 start
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 AYR,AMN0,ADY0,AUT0,CYR,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='gectau.txt'
C
	outfile='dgecapost12.txt' !detrended dGEC,%, total file for 1995-2021 Apo-storms =nev lines x 48 cols superposed epoch	! 3
  	infhss='Apostorm12.txt'  ! Apo storms Apo>=100 nT
C
C Start 
C
    1	CONTINUE
C
    7	OPEN(11,FILE=OUTFILE)
C
C Kp_1_2 nev storms from 1994 to 2021
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
   29 format(A20,1X,I4,3(1X,I2),1X,F5.0)
  197 format(3X,2A2,1X,A2,10X,24(1X,I4))
C
	open(10,file=infhss,action='READ')
C
C
C Cycle on events ====================================================
C
Cnorm	nev=945 1995- 2023.02
C-       	nev=932	 !TEmp
C-       	nev=70 ! intense Spo smoothed storms
       	nev=231 ! total intense Apo storms	1995-2023.04
Crem	nev=228 ! total intense Apo storms	1995-2022

C
	DO 177 iev=1,nev
      read(10,29,end=27) start,iyear,imn1,idy1,iut1,aep
C--	if (iev.lt.229) GOTO 177  ! test output
C
	iutcnt=0
	iyr=iyear-iyear/100*100
	iut0=iut1-12
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
	lda0=lda1
C
       nhr=0
	khr=0
C
	iyear0=iyear
	if (iut0.lt.0) then
	iut0=iut0+24
	lda0=lda1-1
	endif
C
      call submmdd(lda0,iyr,imn0,idy0)
C--------------------------------------------------
  222	lda2=lda1+2
	ryear=float(iyear)
		if(int(ryear/4.)*4.eq.iyear) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
	if (lda2.gt.ndnyr) lda2=lda2-ndnyr
	
C------------------------------------------------------
C
   36 format(3A2,1X,A2,3(1X,F4.2))	 ! hourly GEC, GECav(prec 24h),GEC(tau_24)
   37	format(A4,2(1X,A2),44X,I3)
C
C
C read cur. year:
C
C
1135       call blet4(iyear,AYEAR)
        AYR=AYEAR(3:4)
	call blet2(imn0,AMN0)
      call blet2(idy0,ADY0)
	call blet2(iut0,AUT0)
C
	open(15,file=infile,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!!
C
C============================================
C
  333 read(15,36,end=102) CYR,CMN,CDY,CUT,geccur,gecav,gtau
      IF ((CYR.eq.AYR).and.(CMN.eq.AMN0).and.(CDY.eq.ADY0)
	+.and.(CUT.eq.AUT0)) THEN
      backspace(15)
	GOTO 444
	                  ELSE
       GOTO 333
	                 ENDIF
C
C 
  444	do kk=0,47
      read(15,36,end=102) CYR,CMN,CDY,CUT,gec(kk),gecavkk,gtaukk
	inpn(kk)=nint((gec(kk)-gtau)/gtau*100.) ! dGEC, %
	enddo
  102	close(unit=15)
C
C
   38 format(48(1X,I3))
C Output matrices:
 	write(*,38) (inpn(k),k=0,47)
	write(11,38) (inpn(k),k=0,47)
C
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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