C........................................................Sep 2025
C By & Vsw for YEAR,MN,DY (for imn & idy)
C Output: Vsw, Em UT=0,...,23 UT
C 
C
      subroutine subbyvsw(iyear,imn,imn_inp,idy,idy_inp)
C 
C----------------------------------------------------------------
C
	DIMENSION vsw(0:47) ! 1h indices for 48h file 
	+,bz(0:47),ivsw(0:47),ieby(0:47),by(0:47),bt(0:47),eby(0:47)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE1,INFILE2,outfile,txt1,txt2
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 AYR,AMN,ADY,ADY_inp,AMN_inp
C-	+,DUT
      CHARACTER*55 title1
	CHARACTER*25 title2
	CHARACTER*8 title3
	CHARACTER*30 title4
C	+,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-	DUT=TT(1:4)
C
	title1='Product: OMNI solar wind speed Vsw and electic field Em'
      title2='Issued: Year,Month,Day = '
	 title3=' Time = '
		 title4='YEAR_DOY_MN_DY_UT___Vsw___Em_ ' 
C
	infile1='c:\web\graf\2025\MN\aceYRMNDDsw.txt'  ! OMNI data Vsw
      infile2='c:\web\graf\2025\MN\aceYRMNDDmb.txt'  ! OMNI data BX,BY,BZ,BT
C
Cnorm	outfile='vswebYRMN.txt' ! Vsw, Em (hourly MN )
C-      outfile='imfvsweb.txt' ! Vsw, Em (hourly MN )
      outfile='imfdata.txt' ! Vsw, Em (hourly MN )
C
C Start 
        PI=ATAN(1.0)*4.
        UMR=PI/180.
C
    1	CONTINUE
C
    7	CONTINUE
C
C
  28  format(A80)	 ! 
   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
C
c-      read(10,28,end=27) xyear,lda1,imn1,idy1,iut1,idst
C++       iyear=2025
	iyr=iyear-iyear/100*100
C++	imn=9
C++	idy_start=1
	call blet2(idy_inp,ADY_inp)
	call blet2(imn_inp,AMN_inp)
	ryear=float(iyear)
	if(int(ryear/4.)*4.eq.iyear) THEN 
	ndyr=366
	                          ELSE
	ndyr=365
	ENDIF
	del=1./ndyr
	lda=ndoy(iyr,imn,idy)
C
        call blet2(iyr,AYR)
	AYEAR='2000'
	AYEAR(3:4)=AYR
         call blet2(imn,AMN)
	call blet2(idy,ADY)
C-	AYEAR='2025'
	infile1(13:16)=AYEAR
c	infile1(18:19)=AMN_inp
	infile1(18:19)=AMN
      infile1(24:25)=AYR 
c	infile1(26:27)=AMN_inp
	infile1(26:27)=AMN
	infile2=infile1
	infile2(30:31)='mb'
CC
c-      outfile(6:7)=AYR 
c-	outfile(8:9)=AMN 
C
Cnorm	OPEN(12,FILE=OUTFILE)  ! exclude previous results for given day
Cnorm  131	read(12,139,end=132) nyear,nlda,nmn,ndy,nut,nvsw,neby
Cnorm      if ((nyear.eq.iyear).and.(nlda.eq.lda)) then
Cnorm	backspace(12)
Cnorm      ENDFILE 12
Cnorm	goto 132
Cnorm	                 else
Cnorm	goto 131
Cnorm	endif
Cnorm  132	close(12)
C
      OPEN(12,FILE=OUTFILE,access='APPEND')
	if (idy.ne.idy_inp) then
	WRITE(*,*) title1
	WRITE(12,*) title1
	WRITE(*,*) title2,DD,title3,TT
	WRITE(12,*) title2,DD,title3,TT
	WRITE(*,*) title4
	WRITE(12,*) title4
	endif

C
        if (ADY_inp.eq.'01') then
	infile1(28:29)=ADY         
	infile2(28:29)=ADY
	                    else
	infile1(28:29)=ADY_inp  
	infile2(28:29)=ADY_inp  
	endif
CC
C
	OPEN(15,file=infile1,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!! Vsw
	OPEN(16,file=infile2,action='READ')	 !+++++++++++++++++++++++++!!!!!!!!!! B,By,Bz,EBz
C
	do k=1,20
	read(16,28,end=103) txt2  ! extra lines
      if (k.lt.19) then
	read(15,28,end=102) txt1  ! extra lines
	endif
	enddo
CC
CC  one day =idy
CC
C---        DO 222 idy=idy_start,idy_inp
C-	idy=1 !TEMPPPPPPPPPPP
C
	xyear=ryear+(lda-1)*del
C
C------------------------------------------------------
C
C
	DO nn=0,23
	vsw(nn)=0.
	by(nn)=0.
	bz(nn)=0.
	bt(nn)=0.
	eby(nn)=0.
	ENDDO
CC
CC Cycle on UT ----------------------------------------
CC
	 DO 335 iut=0,23
	nut=iut
  333 read(15,*,end=336) jyear,jmn,jdy,jutmm,jddd,jmmm,j1,pd,vsw(iut)
     +,tp
	if (jdy.ne.idy) goto 333
	 jut=jutmm/100
  334    read(16,*,end=336) kyear,kmn,kdy,kutmm,kddd,kmmm,k1,bx,by(iut)
	+,bz(iut),bt(iut),xlat,xlon
	 if (kdy.ne.idy) goto 334
	kut=kutmm/100
C
  335	CONTINUE   
C
C
C Produce Em
C
  336    do 337 kk=0,nut
       if (bz(kk).eq.0.) bz(kk)=0.1
	if ((kk.gt.0).and.(kk.lt.23)) then
C	if (vsw(kk).lt.-9998.) vsw(kk)=(vsw(kk-1)+vsw(kk+1))/2.
	if ((vsw(kk).lt.-9998.).OR.(bz(kk).lt.-999.)) THEN
	vsw(kk)=-9999.0
	EBY(KK)=99.9
	GOTO 337
	                ENDIF
	if (bt(kk).lt.-998.) bt(kk)=(bt(kk-1)+bt(kk+1))/2
	if (by(kk).lt.-998.) by(kk)=(by(kk-1)+by(kk+1))/2
	if (bz(kk).lt.-998.) bz(kk)=(bz(kk-1)+bz(kk+1))/2
	endif
c
         if ((vsw(kk).lt.0.).or.(bt(kk).lt.0.)) then
c-	eby(kk)=-0.1
      	vsw(kk)=-9999.0
 	EBY(KK)=99.9
	   goto 337
	endif
	 gteta=atan(abs(by(kk))/bz(kk))
      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(kk)**2+bz(kk)**2)
	eby(kk)=vsw(kk)*BTYZ*(sin(teta/2.*umr)**2)/1000.
  337	continue
C
C Output 
C
      nn=nut
	if (idy.eq.idy_inp) nn=nut-1
      do kk=0,nn
	if ((kk.gt.0).and.(kk.lt.nn)) then
	 if (eby(kk).gt.99.) then
	  if ((eby(kk-1).lt.99.).and.(eby(kk+1).lt.99.)) then
        eby(kk)=(eby(kk-1)+eby(kk+1))/2.
	  vsw(kk)=(vsw(kk-1)+vsw(kk+1))/2.
	                      endif
	 endif
	endif
	ivsw(kk)=nint(vsw(kk))
      if (eby(kk).lt.99.)	ieby(kk)=nint(eby(kk)*10.) 
	write(*,139) ayear,lda,imn,idy,kk,ivsw(kk),eby(kk)
	write(12,139) ayear,lda,imn,idy,kk,ivsw(kk),eby(kk) 
      enddo
C
C
   33 FORMAT(1X,3A2,1X,A2,1X,F7.2,1X,I3)
   38 format(72(1X,I4))
   39 format(72(1X,I5))
  139 format(1X,A4,1X,I3,3(1X,I2),1X,I5,1X,F4.1)
c
  222	CONTINUE
  102 close(15)
  103 close(16)
   27 close(unit=12)
C
C
   35      write(*,*) infile1,infile2
c-      pause ' ' 
        RETURN
C--      STOP
       END
C***********************************************************
