      program cal2hp0rms
C..Select best fit Kp-rms for SHpo(SKp) 1h indices..................................Sep 2022
C
C  Daily integral Skp for SSN=0,
C 
C
      CHARACTER*99 infile1,infile2,outfile,txt1,txt2,txt
	CHARACTER*4 CYEAR
	DIMENSION xkp(0:7)
C-	+,kp(0:7),ykp(0:7)
C
	outfile='calkp0rms.txt'

	infile1='cat0skp0int.txt' !1954-2021 SSN=0, modeling set
	infile2='wules0akpd.txt' !1995-2021 SSN=0, training set
C Date for modeling
	write(*,*) ' Enter ncat1,ncat2 for modeling:'
	read(*,*) ncat1,ncat2
C
        ktest=1994  !TEMP
       CYEAR='1994' !TEMP
       OPEN(11,file=infile1,action='READ')
       OPEN(14,file=outfile,access='APPEND')
C
   26 format(A99)
   27 format(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,I3,1X,F6.2,8(1X,I2)
     +,1X,I4)
   28 format(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,I3,1X,F6.2,8(F3.1)
     +,1X,I4)
   29 format(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,I3,2(1X,F6.2),8(1X,F3.1)
     +,1X,I4,1X,I3)
C??   29 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,F4.1),1X,F6.2,1X,F4.1,1X,F6.3
C??     +,1X,F7.3,1X,F5.0,4(1X,F7.2),2(1X,I4))
   30	format(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,I3,1X,F6.2,8(1X,F3.1)
     +,1X,F7.2,1X,I4)
   31	format(1X,F8.3,1X,I3,1X,F7.2,2(1X,F6.2),1X,I4)
   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)
C
C
	ndy=0	!norm
C
C
C SSN=0 Kp events
C 
C
C
  101	read(11,28,end=59) ryear,lda,imn,idy,tkp3,kpmax,skp,xkp,nn
	 iyear=int(ryear)
	if (nn.lt.ncat1) goto 101
	if (nn.gt.ncat2) goto 59  !????????????????
C1994	if (iyear.gt.ktest) goto 59    ! stop
C 1986
C-	if ((nn.ge.ncat1).and.(nn.le.ncat2)) then
C-	 goto 100
C-	else
C-	      goto 101
C-	endif
C+	if ((nn.ge.977).and.(nn.le.979)) then
C+	 goto 100
C+	else
C+	      goto 101
C+	endif

C 1986
C
  100      lda_dev=400.
       ndy=ndy+1
	rmsmin=100.0
	shpo=1.036*skp+3.158
      write(*,29) ryear,lda,imn,idy,tkp3,kpmax,skp,shpo,(xkp(k),k=0,7)
	+,nn,ndy
      write(14,29) ryear,lda,imn,idy,tkp3,kpmax,skp,shpo,(xkp(k),k=0,7)
	+,nn,ndy
C?      write(*,29) ryear,lda,imn,idy,tkp3,akpmax,skp,thpomax
C?     +,hpomax,shp,apomax,sap,swu,swl,swe,nn,ndy
C?      write(14,29) ryear,lda,imn,idy,tkp3,akpmax,skp,thpomax
C?     +,hpomax,shp,apomax,sap,swu,swl,swe,nn,ndy

C
	OPEN(12,file=infile2,action='READ')
C+  102	read(12,27,end=60) yyear,klda,kmn,kdy,ytkp3,kpmaxy,yskp
C+     +,(kp(k),k=0,7),mm
  102	read(12,34,end=41) yyear,klda,kmn,kdy,ytkp3,ykpmax,yskp,thpomax
     +,hpomax,shpy,apomax,sap,swu,swl,swe,mm
	 backspace(12)
	read(12,26) txt
C
C lda	IF ((lda.ge.klda-2).and.(lda.le.klda+2)) THEN
C	IF ((yskp.ge.skp-10.).and.(yskp.le.skp+10.)) THEN
Cnorm	IF ((yskp.ge.skp-10.).and.(yskp.le.skp+10.0).and.
	IF ((shpy.ge.shpo-20.).and.(shpy.le.shpo+20.0).and.
	+(lda.ge.klda-30).and.(lda.le.klda+30)) THEN
C RMS	= min(abs(shpy-shpo))
C Kpmax, Rkp
C?      sum=0.
C?      DO j=0,7
C?	ykp(j)=kp(j)/10.
C?	del=(xkp(j)-ykp(j))**2
C?	sum=sum+del
C?	           enddo
C?	rms=sqrt(sum/8.)
      rms=abs(shpy-shpo)
	if (rms.lt.rmsmin) then
	rmsmin=rms
	zyear=yyear
	ldaz=klda
	zskp=yskp
	zshpo=shpy
	mmz=mm
	txt1=txt
	endif 
C
C
       kdev=abs(klda-lda)
C+	if (kdev.lt.lda_dev) then
	if ((kdev.lt.lda_dev).and.(rms.lt.400.)) then
	lda_dev=kdev
	rmsl=rms
	zyearl=yyear
	ldazl=klda
	zskpl=yskp
	zshpol=shpy
	mmzl=mm
	txt2=txt
	endif
C Output Kp-3h results
C+      write(*,30) yyear,klda,kmn,kdy,ytkp3,kpmaxy,yskp,(ykp(k),k=0,7)
C+	+,rms,mm
C+      write(14,30) yyear,klda,kmn,kdy,ytkp3,kpmaxy,yskp,
C+	+(ykp(k),k=0,7),rms,mm
C
          jn=jn+1
      GOTO 102
	                ELSE
	GOTO 102
	                ENDIF
   41 continue
      write(*,26) txt1
      write(14,26) txt1
      write(*,31) zyear,ldaz,zskp,zshpo,rmsmin,mmz
	write(14,31) zyear,ldaz,zskp,zshpo,rmsmin,mmz
      write(*,26) txt2
      write(14,26) txt2
      write(*,31) zyearl,ldazl,zskpl,zshpol,rmsl,mmzl
	write(14,31) zyearl,ldazl,zskpl,zshpol,rmsl,mmzl
   58	close(12)
      goto 101
C
   59	close(11)
C
   50	close(unit=14)
	pause ' '
	STOP
	END