!$ID$
! This will preprocess the data, involving, sorting and then uses every other
! Path in the sort 
program prepro
implicit none
integer,dimension(:),allocatable :: index
real*8,dimension(:,:),allocatable:: path
real*8,dimension(:),allocatable :: payoff,sortpayoff
real*8                          :: price,oldprice
integer                         :: i,j,idum,npaths,ntime,compress

write(6,*) 'input the number of original paths'
read(5,*) npaths
write(6,*) 'input the compression factor'
read(5,*) compress
write(6,*) 'input the number of time slices'
read(5,*) ntime
allocate(payoff(npaths),sortpayoff(npaths),index(npaths))
allocate(path(ntime,npaths))

open(22,file='paths.stock') 
do i =1 , npaths
	read(22,*) idum
	do j =1 , ntime
		read(22,*) price
		path(j,i) = price
		if ((j.eq.1).or.(price.lt.oldprice)) then
			oldprice = price
		end if
	end do !j
! now I know the payoff
	payoff(i) = oldprice 
  index(i) = i
end do !i

call batcher(payoff,index,npaths)
close(22)

open(23,file='path.out')
do i = 1, npaths, compress 
	write(23,*) i
	do j = 1, ntime
		write(23,*)  path(j,index(i))
	end do
end do
close(23)
end program
