      program calwuleps
C..Select WU,WL,WE,Wp from sample days SSN=0..................................Sep 2022
C
C 
C
      CHARACTER*99 infpred,infhpo,infileu,infilel,infilee,infilep
	+,outfile,txt
	CHARACTER*4 CYEAR
	CHARACTER*2 AYR,AMN,ADY,CYR,CMN,CDY
	DIMENSION hpo(0:23),xkp(0:7),wu(0:71),wl(0:71),we(0:71),wp(0:71)
C
	outfile='calwuleps.txt'
       OPEN(16,file=outfile,access='APPEND')
C
	infpred='test94.txt' !modeling prediction 1994.04.02-04,06-07
	infhpo='d:\Apkp\Hpo\HpoYEAR.txt'  !mod-day (produce from Kp for yr<1995)
	infileu='d:\web\graf\dats\wupYRs.txt' !1995-2021 SSN=0, training set
	infilel='d:\web\graf\dats\wlnYRs.txt' !1995-2021 SSN=0, training set
	infilee='d:\web\graf\dats\werYRs.txt' !1995-2021 SSN=0, training set
	infilep='d:\web\graf\dats\wptecYRs.txt' !1995-2021 SSN=0, training set
C Data for modeling
C
       OPEN(10,file=infpred,action='READ')  
C
   24 format(3A2,24(1X,F4.1))
   25 format(3A2,24(1X,F4.2))
   26 format(2(1X,I4,1X,I3,2(1X,I2)),1X,F4.1,1X,I3)
   27 format(1X,I3,5(1X,F6.2))
   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)
   30 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,1X,F7.2,3(1X,F7.2),1X,I4)
c   30 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)
C
        DO 123 iev=1,5
        do k=0,71
      wu(k)=0.
      wl(k)=0.
      we(k)=0.
      wp(k)=0.
	  enddo
C
  	READ(10,29,end=120) ryear,lda,imn,idy,tkp3,kpmax,skp,shpo
	+,(xkp(k),k=0,7),nn,ndy 
	 write(*,29)  ryear,lda,imn,idy,tkp3,kpmax,skp,shpo
	+,(xkp(k),k=0,7),nn,ndy 
	 write(16,29)  ryear,lda,imn,idy,tkp3,kpmax,skp,shpo
	+,(xkp(k),k=0,7),nn,ndy 
C	 READ(10,30,end=120) xyear,klda,kmn,kdy,tkpx,xkpm,skpm,thpo
C	+,hpo,shpo,apo,sapo,wum,wlm,wem,inx
	 READ(10,30,end=120) xyear,klda,kmn,kdy,tkpx,xkpm,skpm,thpo
	+,xhpo,shpo,apo,sapo,wum,wlm,wem,inx
	 write(*,30) xyear,klda,kmn,kdy,tkpx,xkpm,skpm,thpo
	+,xhpo,shpo,apo,sapo,wum,wlm,wem,inx
	 write(16,30) xyear,klda,kmn,kdy,tkpx,xkpm,skpm,thpo
	+,xhpo,shpo,apo,sapo,wum,wlm,wem,inx
 	iyear=int(ryear)
	iyr=iyear-iyear/100*100
	call blet4(iyear,CYEAR)
	infhpo(16:19)=CYEAR
	delh=tkp3-thpo	   !time shift = UTmod-UTsample
	idelh=int(delh)	   	   !time shift = UTmod-UTsample
	ini_del=24+idelh  !
C
	if (iyear.lt.1995) then
	n=0
	do j=0,7
	 hpo(n)=xkp(j)
	hpo(n+1)=xkp(j)
	hpo(n+2)=xkp(j)
	n=n+3
	enddo
	GOTO 130
      endif
C
       OPEN(11,file=infhpo,action='READ')
C	nend=lda*24
	do jj=1,lda
	DO j=0,23
	read(11,*,end=121) myear,mmn,mdy,uth,ut05,yyh,yy05,hpo(j),mapo,mm
	ENDDO
	enddo
  121	close(11)
C
 130      kyear=int(xyear)
	kyr=kyear-kyear/100*100
	call blet2(kyr,AYR)
	call blet2(kmn,amn)
	call blet2(kdy,ady)
      infileu(21:22)=AYR
	infilel(21:22)=AYR
	infilee(21:22)=AYR
	infilep(23:24)=AYR
	OPEN(12,file=infileu,action='READ')
	read(12,*) txt
	OPEN(13,file=infilel,action='READ')
	read(13,*) txt
	OPEN(14,file=infilee,action='READ')
	read(14,*) txt
	OPEN(15,file=infilep,action='READ')
	read(15,*) txt
C
        DO 140 ldy=1,klda+1
C move data up for 24h
      do k=0,23
      wu(k)=wu(k+24)
      wl(k)=wl(k+24)
      we(k)=we(k+24)
      wp(k)=wp(k+24)
	 wu(k+24)=wu(k+48)
	 wl(k+24)=wl(k+48)
	 we(k+24)=we(k+48)
	 wp(k+24)=wp(k+48)
      enddo
	read(12,25,end=122) cyr,cmn,cdy,(wu(k+48),k=0,23)
	read(13,25,end=126) cyr,cmn,cdy,(wl(k+48),k=0,23)
	read(14,25,end=124) cyr,cmn,cdy,(we(k+48),k=0,23)
	read(15,24,end=125) cyr,cmn,cdy,(wp(k+48),k=0,23)
  140 CONTINUE	 
  122 close(12)   
  126 close(13)   
  124 close(14)   
  125 close(15)   
C
C 
C
C
C Output forecast for given day
      write(*,26) IYEAR,lda,imn,idy,kyear,klda,kmn,kdy,thpo,idelh
	write(16,26) IYEAR,lda,imn,idy,kyear,klda,kmn,kdy,thpo,idelh
	DO iut=0,23
        ni=ini_del+iut
	write(*,27) iut,hpo(iut),wu(ni),wl(ni),we(ni),wp(ni)
	write(16,27) iut,hpo(iut),wu(ni),wl(ni),we(ni),wp(ni)
	ENDDO
  123 continue
C
   58	close(16)
C
  120	close(10)
C
   	close(unit=14)
	pause ' '
	STOP
	END