C........................................................Nov 2023
C Dst for Catalogue of montly peak Apo(tau6) . 
C 
C 
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 devbymn12
C 
C----------------------------------------------------------------
C
	DIMENSION idst(0:72) ! 1h indices for 48h file 
	+,inpdst(0:23),ioutdst(0:47)
	+,jDYR(0:72),jDMN(0:72),jDDY(0:72),jDUT(0:72)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS,outfile 
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 AYR,AMN,ADY,AUT
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='d:\Apkp\dst\dstYEAR'  ! 
C
  	infhss='catapo6mn90.txt'  ! Apo(tau6) monthly peak
C
	outfile='dstapomn90.txt' ! Dst profiles: t0-12...t0+35 (48 hourly )
C
C Start 
C
    1	CONTINUE
C
    7	CONTINUE
      OPEN(12,FILE=OUTFILE)
C
C
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0)		 ! infhss
   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
C
C Cycle on events ====================================================
C
              	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
        call blet2(imn1,AMN)
      call blet2(idy1,ADY)
	call blet2(iut1,AUT)

	iutcnt=0
	iyear=int(xyear)
	call blet4(iyear,AYEAR)
	infile(16:19)=AYEAR
C
	OPEN(15,file=infile,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!! Vsw
C
	iyr=iyear-iyear/100*100
	iut0=iut1-12
	lda0=lda1-1 ! pre-event day
	niut1=iut1+24	! data for day lda1 [24:47]
	niut0=niut1-12  !Start output results

C
C
       nhr=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(3X,2(I2),1X,I2,10X,24(I4))	 ! hourly dst
   37	format(A4,2(1X,A2),7X,F5.1,16X,F5.0)
C
C
C read all years:
C
C	   call blet4(iyear,AYEAR)
1135    CONTINUE   
        AYR=AYEAR(3:4)
C
C============================================
	napo=0
C
  333 read(15,36,end=102) jyr,jmn,jdy,(inpdst(k),k=0,23)
      lda=ndoy(jyr,jmn,jdy)
      
C       
  	IF (lda.lt.lda0) GOTO 333
C
      DO kut=0,23			!1
      jDYR(kut+napo)=jyr
		 jDMN(kut+napo)=jmn
	jDDY(kut+napo)=jdy
	jDUT(kut+napo)=kut
	idst(kut+napo)=inpdst(kut)
	                ENDDO	   !1
	napo=napo+24
	if (napo.lt.71) GOTO 333
C++++++++++++++++++++++++++++
C
C extract output event profile:
         DO kk=0,47
       ioutdst(kk)=idst(niut0+kk)
	ENDDO
C
C
   33 FORMAT(1X,3A2,1X,A2,1X,F7.2,1X,I3)
   38 format(48(1X,I4))
C Output 
C
 	write(*,38) (ioutdst(k),k=0,47)
	write(12,38) (ioutdst(k),k=0,47)
  102 close(15)
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
	close(unit=12)

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

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