C........................................................Nov 2023
C Produce Em for 2023 (hourly) . 
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 devem2023
C 
C----------------------------------------------------------------
C
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,outfile,txt 
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 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\OMNI\bvswYEAR.txt'  ! OMNI data Vsw
C
C
	outfile='emYEAR.hrs' ! EBy(Vsw,B,By) profiles: t0-12...t0+35 (48 hourly)
C
      AYEAR='2023' ! TEMP
	ndyr=365     !Temp
C Start 
        PI=ATAN(1.0)*4.
        UMR=PI/180.
C
    1	CONTINUE
C
    7	CONTINUE
      infile(18:21)=AYEAR
	outfile(3:6)=AYEAR
      OPEN(12,FILE=OUTFILE)
C
	OPEN(15,file=infile,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!! Vsw
	do k=1,10
	read(15,*) txt  ! extra lines
	enddo
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
C Cycle on days ====================================================
C
       bt_pre=5.6
	 bx_pre=-1.9
	 by_pre=4.6
	 bz_pre=0.4
	 vsw_pre=603.
	 ebz_pre=-0.24
C
c++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       nhrs=283*24
c
	DO 177 ndyr=1,nhrs  ! OMNI data stop on lda=283 (assesed 2023.11.07)
C
C--------------------------------------------------
  222	CONTINUE
C
C------------------------------------------------------
C
   36 format(I4,1X,I3,1X,I2,1X,F4.2)	 ! hourly gec
   37	format(A4,2(1X,A2),7X,F5.1,16X,F5.0)
C
1135    CONTINUE   
  333 read(15,*,end=102) jyear,lda,jUT,bt,bx,by,bz,vsw,ebz
C       
	jyr=jyear-jyear/100*100
	call submmdd(lda,jyr,jmn,jdy)
        call blet2(jmn,AMN)
      call blet2(jdy,ADY)
	call blet2(jut,AUT)
  335	CONTINUE   
C............................................................
  337	CONTINUE   
	if (bt.gt.999.) bt=bt_pre
	if (by.gt.999.) by=by_pre
	if (bz.gt.999.) bz=bz_pre
	if (vsw.gt.9990.) vsw=vsw_pre
	if (ebz.gt.999.) ebz=ebz_pre
C
C Produce Em
C
       if (bz.eq.0.) bz=0.1
	 gteta=atan(abs(by)/bz)
      teta=gteta/umr
      if ((teta.lt.0.).and.(teta.ge.-90.)) teta=teta+180.
	if ((teta.lt.-90.).and.(teta.lt.-180.)) teta=teta+360.
	BTYZ=sqrt(by**2+bz**2)
	eby=vsw*BTYZ*(sin(teta/2.*umr)**2)/1000.
C
   33 FORMAT(1X,3A2,1X,A2,1X,F7.2,1X,I3)
   38 format(48(1X,I4))
   39 format(1X,I4,1X,I3,3(1X,A2),1X,F8.2)
C Output 
 	write(*,39) jyear,lda,AMN,ADY,AUT,eby
	write(12,39) jyear,lda,AMN,ADY,AUT,eby
       bt_pre=bt
	 bx_pre=bx
	 by_pre=by
	 bz_pre=bz
	 vsw_pre=vsw
	 ebz_pre=ebz

  177	CONTINUE
C
  102 close(15)
	close(unit=12)

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

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