      program lag1hp0jrot
C..Add Hpo, Apo 1h indices.....................................Aug 2022
C JPL 1h indices based on -15d median geomag. wi indices
C
C rate-of-change daily for SSN=0, Apo >15 nT 
C 
C rWU for day SSN=0
C rWL from -24h:tkp:+24h
C rWE from -24h:tkp:+24h
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)
	+,kp(0:7)
C
c      infile='exaphs0.txt'
	infile='kpssn0.txt'
	infhpo='Hpo19952022.txt'       !rate-of-change Apo,Hpo
	outfile='wule0apojrot.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')
C	OPEN(15,file=infhpo,action='READ')
       OPEN(14,file=outfile,access='APPEND')
C ryear     lda mn dd iut1 Hpo     Apo  nn      
C 1996.033  13  1 13  3.5  5.667   67    1
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,I3))
c   30 FORMAT(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,F6.3,2(1X,I4))
   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
	ndy=0	!norm
C+	ndy=432 !TEMPPPPPPPPPPPPP
C+	ndy=598 !TEMPPPPPPPPPPPPP
C+	ndy=756 !TEMPPPPPPPPPPPPP
C+	ndy=908 !TEMPPPPPPPPPPPPP
C+	ndy=1055 !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
	ndy=1962 !TEMPPPPPPPPPPPPP
C
      lda_out=0
C
C SSN=0 Kp>=3 events
        DO 41 iev=1,3082  ! 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,kp
CTEMPPPPPPPPPPP
C+       if (nn.le.1512) GOTO 41
C+       if (nn.le.1678) GOTO 41
C+       if (nn.le.1836) GOTO 41
C+       if (nn.le.1988) GOTO 41
C+       if (nn.le.2135) 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
       if (nn.le.3042) 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
      kpmax=kp(0)
	itkp3=0
       skp2=0.
	skpav=0.
              DO j=1,7
	if (kpmax.lt.kp(j)) then
	kpmax=kp(j)
	itkp3=j*3
	endif
	dkp=(kp(j)-kp(j-1))/3.0
	skp2=skp2+dkp*dkp
	skpav=skpav+dkp
	           enddo
	rkp=sqrt(skp2/7.-(skpav/7.)**2)
	tkp3=itkp3+1.5
	akpmax=kpmax/10.
C
	if (iyear.lt.1995) then 
C Output Kp-3h results
        write(*,29) ryear,lda,imn,idy,tkp3,akpmax,rkp,nn
      write(14,29) ryear,lda,imn,idy,tkp3,akpmax,rkp,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
       sap2=0.
	sapav=0.
       shp2=0.
	shpav=0.
              DO j=1,23
	IF (apomax.lt.apo(j)) THEN
	apomax=apo(j)
	ithpo=j
	hpomax=hpo(j)
	ENDIF
	dapo=apo(j)-apo(j-1)
	sap2=sap2+dapo*dapo
	sapav=sapav+dapo
	dhpo=hpo(j)-hpo(j-1)
	shp2=shp2+dhpo*dhpo
	shpav=shpav+dhpo
	           ENDDO
	thpomax=ithpo+0.5
	rapo=sqrt(sap2/23.-(sapav/23.)**2)
	rhpo=sqrt(shp2/23.-(shpav/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      nitkp=itkp+24
C WU rate
       swu2=0.
	swuav=0.
              DO j=1,23
	dwu=wu(j)-wu(j-1)
	swu2=swu2+dwu*dwu
	swuav=swuav+dwu
	           enddo
	rwu=sqrt(swu2/23.-(swuav/23.)**2)
C WL rate
       swl2=0.
	swlav=0.
              DO j=1,23
	dwl=wl(j)-wl(j-1)
	swl2=swl2+dwl*dwl
	swlav=swlav+dwl
		           enddo
	rwl=sqrt(swl2/23.-(swlav/23.)**2)
C WE rate
       swe2=0.
	sweav=0.
              DO j=1,23
	dwe=we(j)-we(j-1)
	swe2=swe2+dwe*dwe
	sweav=sweav+dwe
	           enddo
	rwe=sqrt(swe2/23.-(sweav/23.)**2)
C
C Output
      ndy=ndy+1
      write(*,34) ryear,lda,imn,idy,tkp3,akpmax,rkp,thpomax,hpomax
	+,apomax,rhpo,rapo,rwu,rwl,rwe,ndy
	write(14,34) ryear,lda,imn,idy,tkp3,akpmax,rkp,thpomax,hpomax
	+,apomax,rhpo,rapo,rwu,rwl,rwe,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,F5.0,5(1X,F6.2),1X,I4)
   50	close(unit=14)
	pause ' '
	STOP
	END