	program extrsolw1
C........................................................Apr 2023
C .......................................................Aug. 2015
C Re-arrange SW speed in Annual files 
C Sol Wind 
c for 1999-2015/07.......................................................
C
c
	DIMENSION IM(12),ires(0:23)
	CHARACTER*64,infile,txt,outyr
	CHARACTER*2 AYR,AMN,ADY,ADMN,CMN,CDY
C-	+,CUT
	CHARACTER*4 AYEAR
C-	+,CYEAR
C-	CHARACTER*7 CSW
C-		CHARACTER*3 DDD
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
   30 format(7X,17(1X,F5.0))
   31 format(A64,F7.1)
   35 format(A59)
   33 format(1X,3I2,1X,I2,13X,F4.0)
C
C-	iyyyy=1997
	iyyyy=2023  !TEMPPPPPPPPPP
C	infile='d:\Apkp\OMNI\swYEARMN.txt'
	infile='d:\Apkp\OMNI\omniYEAR.txt'
c-	infile='d:\web\graf\2023\MN\aceYYMNDDsw.txt'
C	infile='d:\web\graf\2023\MN\ace230331sw.txt' !TEMPPPPPPPPPPPPPP
	outyr='swyear'
C
C1996   1  0   5.7   4.9   7.0  424.
C start cycle on Year
Cnorm 100	iyyyy=iyyyy+1
C
	swyr=0.
	cntyr=0.
Cpre	if (iyyyy.gt.2015) goto 801 ! total result =>

	year=float(iyyyy)
	if(int(year/4.)*4.eq.iyyyy) THEN 
	ndnr=366
	im(2)=29
	                          ELSE
	ndnr=365
	im(2)=28
	ENDIF
C
	call blet4(iyyyy,AYEAR)
	infile(18:21)=AYEAR
	outyr(3:6)=AYEAR
      OPEN(13,FILE=OUTYR)
		OPEN(11,FILE=INFILE,action='READ')
c     
C  Input datafile 
	do i=1,13
	read(11,31) txt		! extra lines
	enddo
C
	AYR=ayear(3:4)
	iyr=iyyyy-(iyyyy/100)*100
C-	infile(24:25)=AYR
C
C
200   continue	  !
	imnend=12
	if (iyyyy.eq.2023) imnend=3 !(2023/03) TEMPPPPPPPPP
C start  mn-to-mn cycle >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      DO 1234	imn=1,imnend
C		
	cntmn=0.
	cmis=0.
	ctot=0.
	call blet2(imn,AMN)

c-	infile(18:19)=AMN
c-	infile(26:27)=AMN
	ndmn=IM(imn)
	call blet2(ndmn,ADMN)
c-	infile(28:29)=ADMN
C
C
C    1	READ (11,32,END=55,ERR=2) jyear,jmn,jdy,jut,swkm ! SW
C_    1	read(11,35,END=55) txt
    1	read(11,*,END=55) jYEAR,lda,jut,B,BZ,TK,Den,swkm
C-	CYEAR=txt(1:4)
C	CMN=txt(6:7)
C	CDY=txt(9:10)
C-      DDD=txt(6:8)
C-	CUT=txt(10:11)
C-	CSW=txt(40:44)
C-	read(CYEAR,*) ryear
C-	jyear=int(ryear)
C-	read(CMN,*) rmn
C-	jmn=int(rmn)
C-	read(CDY,*) rdy
C-	jdy=int(rdy)
C-       read(DDD,*) rdoy
C-	lda=int(rdoy)
	call submmdd(lda,iyr,imn,idy)
	call blet2(imn,CMN)
      call blet2(idy,CDY)
C-	read(CUT,*) rut
C-	jut=int(rut)
C-		read(CSW,*) swkm
  32	FORMAT(I4,1X,2(1X,I2),2X,38X,F7.1)
      if (swkm.gt.9990.) swkm=0.0  !9999.
      isw=nint(swkm)
	ctot=ctot+1.0
	ires(jut)=isw
C
C
Cpre	call submmdd(jdoy,jyr,jmn,jdy)
	call blet2(jmn,AMN)
	call blet2(jdy,ADY)
C??	cntmn=cntmn+1.0      ! cnt of monthly SW >700 

C
	if (jut.lt.23) GOTO 1
C
	swav=0.
Cpre	cntut=24.
	cntut=0.
	do k=0,23
	if (ires(k).gt.0) then
	cntut=cntut+1.0
	swav=swav+float(ires(k))
	endif 
	enddo
	swyr=swyr+swav
	cntyr=cntyr+cntut
	if (cntut.gt.0.) swav=swav/cntut
	iswav=nint(swav)
C_	DO k=0,23
C_       if (ires(k).eq.0) ires(k)=iswav
C_	ENDDO

	write(*,38) AYR,CMN,CDY,(ires(k),k=0,23),iswav
      write(13,38) AYR,CMN,CDY,(ires(k),k=0,23),iswav
      goto 1
C
1234	CONTINUE ! End of monthly file
  55   CLOSE(unit=11)

   38 format(3A2,24(1X,I4),2X,I4)
   36 format(3A2,5X,F7.1,2X,F6.0)
C 
	swyrav=swyr/cntyr
	write(*,36) AYR,AMN,ADY,swyrav,cntyr
C	write(13,36) AYR,AMN,ADY,swyrav,cntyr
	CLOSE(unit=13)
Cpre	goto 100           !  GO TO NEXT YEAR

C
  801	continue
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

