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

	infile1='cat1skp0int.txt' !1954-1994 SSN=0, modeling set
	infile2='cat2skp0int.txt' !1995-2021 SSN=0, training set
C
        ktest=1994  !TEMP
       CYEAR='1994' !TEMP
       OPEN(11,file=infile1,action='READ')
       OPEN(14,file=outfile,access='APPEND')
C
   26 format(A64)
   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,1X,F6.2,8(1X,F3.1)
     +,1X,I4,1X,I3)
   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,1X,F6.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)
C1994	if (iyear.lt.ktest) goto 101
C1994	if (iyear.gt.ktest) goto 59    ! stop
C 1986
	if ((nn.ge.929).and.(nn.le.931)) then
	 goto 100
	else
	      goto 101
	endif
C+	if ((nn.ge.977).and.(nn.le.979)) then
C+	 goto 100
C+	else
C+	      goto 101
C+	endif
	if (iyear.gt.1986) goto 59
C 1986
C
  100      lda_dev=400.
       ndy=ndy+1
	rmsmin=100.0
      write(*,29) ryear,lda,imn,idy,tkp3,kpmax,skp,(xkp(k),k=0,7),nn,ndy
      write(14,29) ryear,lda,imn,idy,tkp3,kpmax,skp,(xkp(k),k=0,7),nn
	+,ndy
C
	OPEN(12,file=infile2,action='READ')
  102	read(12,27,end=60) yyear,klda,kmn,kdy,ytkp3,kpmaxy,yskp
     +,(kp(k),k=0,7),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 ((yskp.ge.skp-20.).and.(yskp.le.skp+20.0).and.
	+(lda.ge.klda-30).and.(lda.le.klda+30)) THEN
C RMS
C Kpmax, Rkp
      sum=0.
      DO j=0,7
	ykp(j)=kp(j)/10.
	del=(xkp(j)-ykp(j))**2
	sum=sum+del
	           enddo
	rms=sqrt(sum/8.)
	if (rms.lt.rmsmin) then
	rmsmin=rms
	zyear=yyear
	ldaz=klda
	zskp=yskp
	mmz=mm
	txt1=txt
	endif 
C
C
       kdev=abs(klda-lda)
C+	if (kdev.lt.lda_dev) then
	if ((kdev.lt.lda_dev).and.(sum.gt.0.)) then
	lda_dev=kdev
	rmsl=rms
	zyearl=yyear
	ldazl=klda
	zskpl=yskp
	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
   60	close(12)
   41 continue
      write(*,26) txt1
      write(14,26) txt1
      write(*,31) zyear,ldaz,zskp,rmsmin,mmz
	write(14,31) zyear,ldaz,zskp,rmsmin,mmz
      write(*,26) txt2
      write(14,26) txt2
      write(*,31) zyearl,ldazl,zskpl,rmsl,mmzl
	write(14,31) zyearl,ldazl,zskpl,rmsl,mmzl
      GOTO 101
   59	close(11)
C
   50	close(unit=14)
	pause ' '
	STOP
	END