program T_smooth

use std_type
use DeBoor_smooth
use VstupVystup



integer :: i,bodu
real(kind=DP),dimension(:),allocatable :: x,y,dy
real(kind=DP),dimension(:,:),allocatable :: a,v
real(kind=DP) :: s, sfq,pp
character(len=30) :: Vstup = "smooth.dat", Vystup="vystup.dat"
character(len=1) :: znaky

! Cteni souboru vstupnich dat x(i) y(i)
open(unit=10,file=Vstup,status="old",action="read",position="rewind")
read(unit=10,fmt=*)znaky,bodu
print *,"Pocet bodu",bodu
allocate(x(bodu))
allocate(y(bodu))
do i=1,bodu
    read(unit=10,fmt=*) x(i),y(i)
end do
close(unit=10)

allocate(a(bodu,4))
allocate(v(bodu,7))
allocate(dy(bodu))

call cti_cislo("Zadej dy = ",s)
dy = s
call cti_cislo("Zadej s  = ",s)

call smooth(x,y,dy,bodu,s,v,a,sfq,pp)

print *
print "(a5,f8.5)","dy =",dy(1),"s  =",s,"sfq =",sfq
print "(a,f5.3)","p   = ",pp

print *,char(10)//" ENTER = graf"
read *

!do i=1,bodu
!	print *,x(i),y(i)-a(i,1)
!end do

!zapis do souboru
open(unit=11,file=Vystup,status="replace",action="readwrite")
do i=1,bodu
	write(unit=11,fmt="(5es15.6)")x(i),a(i,1),a(i,2),a(i,3),a(i,4)
end do
close(unit=11)

! Vystup do souboru pro gnuplot
open(unit=12,file="Graf.gp",status="replace",action="readwrite")
write(unit=12,fmt=*)"reset"
write(unit=12,fmt=*)"plot 'smooth.dat','vystup.dat' using 1:2 with lines title 'splajn'"
write(unit=12,fmt=*)"pause -1"
write(unit=12,fmt=*)"plot 'vystup.dat' using 1:3 with lines title '1.derivace'"
write(unit=12,fmt=*)"pause -1"
write(unit=12,fmt=*)"plot 'vystup.dat' using 1:4 with lines title '2.derivace'"
write(unit=12,fmt=*)"pause -1"
close(unit=12)

call system("wgnuplot graf.gp")

print *,"ENTER = konec"
read *


end program T_smooth
