      program lag1hp0sint
C..Add Hpo, Apo 1h indices.....................................Sep 2022
C JPL 1h indices based on GTEC Shubin median  wg indices
C
C  Daily integral WUs, WLs, WEs for SSN=0,all Apo 
C 
C WUs for day SSN=0
C WLs for day SSN=0
C WEs for day SSN=0
C
      CHARACTER*64 infile,infhpo,outfile,infileu,infilel,infiler,txt
	CHARACTER*2 AYR
	DIMENSION wu(0:23),wl(0:23),we(0:23),apo(0:23),hpo(0:23)
	+,xkp(0:7)
C
	infile='kpssn0.txt'
	infhpo='Hpo19952022.txt'       !integral Apo,Hpo
	outfile='wules0akpd.txt'
	infileu='d:\web\graf\datm\wupYRjm.txt'
	infilel='d:\web\graf\datm\wlnYRjm.txt'
	infiler='d:\web\graf\datm\werYRjm.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(14,file=outfile,access='APPEND')
C
   29 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,F4.1),1X,F6.2,1X,I4)
   30 format(1X,I4,1X,3I2,8(1X,F3.1))
   31 format(I4,A64)
   32 format(I4,2(1X,I2),1X,I2,32X,F7.3,1X,F4.0)
   33 format(3I2,24(1X,F4.2))
C
C
C+	ndy=0	!norm
C+	ndy=433 !TEMPPPPPPPPPPPPP
C+	ndy=599 !TEMPPPPPPPPPPPPP
C+	ndy=757 !TEMPPPPPPPPPPPPP
C+	ndy=909 !TEMPPPPPPPPPPPPP
C+	ndy=1056 !TEMPPPPPPPPPPPPP
C+	ndy=1168 !TEMPPPPPPPPPPPPP
C+	ndy=1262 !TEMPPPPPPPPPPPPP
C+	ndy=1354 !TEMPPPPPPPPPPPPP
C+	ndy=1444 !TEMPPPPPPPPPPPPP
C+	ndy=1533 !TEMPPPPPPPPPPPPP
C+	ndy=1621 !TEMPPPPPPPPPPPPP
C+	ndy=1708 !TEMPPPPPPPPPPPPP
C+	ndy=1794 !TEMPPPPPPPPPPPPP
C+	ndy=1879 !TEMPPPPPPPPPPPPP
C+	ndy=1962 !TEMPPPPPPPPPPPPP
	ndy=2002 !TEMPPPPPPPPPPPPP
C
      lda_out=0
C
C SSN=0 Kp all events
        DO 41 iev=1,3092  ! Events
C 
	DO k=0,23
	apo(k)=0.
	hpo(k)=0.
	wu(k)=0.
	wl(k)=0.
	we(k)=0.
	ENDDO
C
C_Apo    	read(10,*,end=50) ryear,lda,imn,idy,thpo,hpomax,iapo,nn
  101	read(10,30,end=59) nn,iyr,imn,idy,xkp
CTEMPPPPPPPPPPP
C+       if (nn.le.1513) GOTO 41
C+       if (nn.le.1679) GOTO 41
C+       if (nn.le.1837) GOTO 41
C+       if (nn.le.1989) GOTO 41
C+       if (nn.le.2136) GOTO 41
C+       if (nn.le.2248) GOTO 41
C+       if (nn.le.2342) GOTO 41
C+       if (nn.le.2434) GOTO 41
C+       if (nn.le.2524) GOTO 41
C+       if (nn.le.2613) GOTO 41
C+       if (nn.le.2701) GOTO 41
C+       if (nn.le.2788) GOTO 41
C+       if (nn.le.2874) GOTO 41
C+       if (nn.le.2969) GOTO 41
C+       if (nn.le.3042) GOTO 41
       if (nn.le.3082) GOTO 41
	if (iyr.lt.30) then
	iyear=2000+iyr
	           else
	iyear=1900+iyr
	endif
		z1=iyear/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyear) THEN
	dnr=366.
		  ELSE
                dnr=365.
		       ENDIF
	del=1./dnr
      lda=ndoy(iyr,imn,idy)
        ryear=iyear+(lda-1)*del
C
C Kpmax, Rkp
      akpmax=xkp(0)
	itkp3=0
       skp2=0.
	skp=xkp(0)/2.
              DO j=1,7
	skp=skp+xkp(j)
	if (akpmax.lt.xkp(j)) then
	akpmax=xkp(j)
	itkp3=j*3
	endif
	           enddo
	skp=(skp-xkp(7)/2.)*3.0
	tkp3=itkp3+1.5
C
	if (iyear.lt.1995) then 
C Output Kp-3h results
        write(*,29) ryear,lda,imn,idy,tkp3,akpmax,skp,nn
      write(14,29) ryear,lda,imn,idy,tkp3,akpmax,skp,nn
	goto 101
	              endif
	OPEN(15,file=infhpo,action='READ')
C
C+	if (iev.lt.266) GOTO 41 !TEMPPPPPPPPPPPPPPPPPP
c+	if (iev.lt.384) GOTO 41 !TEMPPPPPPPPPPPPPPPPPP
C+	if (iev.lt.474) GOTO 41 !TEMPPPPPPPPPPPPPPPPPP
C
C?   21 tkp=thpo
C?      itkp=int(tkp-0.005)
c-	iyear=int(ryear)
c-	iyr=iyear-iyear/100*100
C
      ihlda=(lda-1)*24
	nda=0
   1   read(15,31,end=58) kyear,txt
       IF (kyear.lt.iyear) then   !1
	 	goto 1
			ELSE				!1
       backspace(15)
	      ENDIF                    !1
   2   read(15,31,end=58) kyear,txt
	if (kyear.eq.iyear) then		!2
	nda=nda+1
	 if (nda.lt.ihlda) then			!3
	 GOTO 2
	                    else		!3
C Read Hpo Apo for given day SSN=0
          	DO j=0,23					
 	read(15,32,end=58) kyear,kmn,kdy,kut,hpo(j),apo(j)
	 ENDDO						
C produce Rate-of-change
       apomax=apo(0)
	hpomax=hpo(0)
	jthpo=0
	sap=apo(0)/2.
	shp=hpo(0)/2.
              DO j=1,23
	IF (apomax.lt.apo(j)) THEN
	apomax=apo(j)
	ithpo=j
	hpomax=hpo(j)
	ENDIF
	sap=sap+apo(j)
	shp=shp+hpo(j)
	           ENDDO
	thpomax=ithpo+0.5
	sap=sap-apo(23)/2.
	shp=shp-hpo(23)/2.
              	endif			  !3
	                else       !2
	               goto 2
	endif                       !2
C
   58   close(unit=15)
C
C++ READ 1h WU,WL,WE
C
	call blet2(iyr,AYR)
	infileu(21:22)=AYR
	infilel(21:22)=AYR
	infiler(21:22)=AYR
	OPEN(11,file=infileu,action='READ')
	read(11,*) txt
      OPEN(12,file=infilel,action='READ')
	read(12,*) txt
      OPEN(13,file=infiler,action='READ')
	read(13,*) txt
C
	DO kk=1,lda
	read(11,33,end=55) kyr,kmn,kdy,(wu(k),k=0,23)
	read(12,33,end=56) kyr,kmn,kdy,(wl(k),k=0,23)
	read(13,33,end=57) kyr,kmn,kdy,(we(k),k=0,23)
	 ENDDO
   55	close(11)
   56	close(12)
   57	close(13)
c     
C WU rate
	swu=wu(0)/2.
              DO j=1,23
	swu=swu+wu(j)
	           enddo
	swu=swu-wu(23)/2.
C WL rate
	swl=wl(0)/2.
              DO j=1,23
	swl=swl+wl(j)
		           enddo
	swl=swl-wl(23)/2.
C WE rate
	swe=we(0)/2.
              DO j=1,23
	swe=swe+we(j)
	           enddo
	swe=swe-we(23)/2.
C
C Output
      ndy=ndy+1
      write(*,34) ryear,lda,imn,idy,tkp3,akpmax,skp,thpomax,hpomax,shp
	+,apomax,sap,swu,swl,swe,ndy
	write(14,34) ryear,lda,imn,idy,tkp3,akpmax,skp,thpomax,hpomax,shp
	+,apomax,sap,swu,swl,swe,ndy
C
C
   41 continue
   59	close(10)
C
   34 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,F4.1),1X,F6.2,1X,F4.1,1X,F6.3
     +,1X,F7.3,1X,F5.0,4(1X,F7.2),1X,I4)
   50	close(unit=14)
	pause ' '
	STOP
	END