#!/bin/bash
echo divacvrand:///////////////////////////////////////////
echo divacvrand:          Cross Validation
echo divacvrand:///////////////////////////////////////////
echo ' '

divacvrandlog='./output/divacvrand.log'
echo divacvrand://///////////////////////////////////////// > $divacvrandlog
echo divacvrand:          Cross Validation >> $divacvrandlog
echo divacvrand://///////////////////////////////////////// >> $divacvrandlog
echo ' ' >> $divacvrandlog

nddd=$(cat ./input/data.dat | wc -l)
if [ $# -eq "0" ] 
then
echo ============================== >> $divacvrandlog
echo Need at least two paramteres for divacv: >> $divacvrandlog
echo number of points for cross references >> $divacvrandlog
echo number of repeated data removal >> $divacvrandlog
echo =============================== >> $divacvrandlog
else
if [ "$1" == "-r" ]
then
echo Please put option -r after the two numbers >> $divacvrandlog
echo Please put option -r after the two numbers
else
let nppp=$1*$2
if [ "$nppp" -gt "$nddd" ]
then
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! >> $divacvrandlog
echo Your statistics will not be independant >> $divacvrandlog
echo $1 times $2 = $nppp is larger than $nddd >> $divacvrandlog
echo Stopping >> $divacvrandlog
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! >> $divacvrandlog
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo Your statistics will not be independant
echo $1 times $2 = $nppp is larger than $nddd
echo Stopping
echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
else
cp ./input/param.par  ./input/param.par.ori
cp ./input/data.dat  ./input/data.dat.ori
rm -f ./input/valatxy.coord.ori
mv -f ./input/valatxy.coord ./input/valatxy.coord.ori
rm -f ./output/gcv.dat
if [ -f ./input/gcvsampling.dat ]
then
#Create mesh once
if [ -f ./meshgenwork/fort.22 ]
then 
echo divacvrand:=========================================================================== >> $divacvrandlog
echo divacvrand:Mesh files aready exist, hope they are correct: otherwise exectute divamesh >> $divacvrandlog
echo divacvrand:=========================================================================== >> $divacvrandlog
else
divamesh
 cat  $divacvrandlog ./output/divamesh.log > bidon
 mv bidon $divacvrandlog

fi
else
echo divacvrand:Need to provide gcvsampling.dat in ./input ! >> $divacvrandlog
echo divacvrand:Need to provide gcvsampling.dat in ./input !
fi
for gcv in `cat ./input/gcvsampling.dat`
do
echo divacvrand:working with $gcv >> $divacvrandlog
#For a given subsample size
isize=$1
ijm=0
N=$2
rm -f ./divawork/cverror.dat
rm -f ./divawork/cvdatavar.dat
#Repeat N times the estimates
while [ "$ijm" -lt "$N" ]
do
# Extract a random subsample 
dvsample $isize $ijm
let ijm=$ijm+1
head -17 ./input/param.par.ori > bidon
echo 1 >> bidon
echo   >> bidon
echo 1 >> bidon
echo   >> bidon
echo -99 >> bidon
echo >> bidon
echo $gcv >> bidon
#tail -2 ./input/param.par.ori >> bidon
# no error field
echo >> bidon
echo 0 >> bidon
mv bidon ./input/param.par
divacalc
 cat  $divacvrandlog ./output/divacalc.log > bidon
 mv bidon $divacvrandlog

#now calculate errors from differences in valatxy.coord and fieldatxyascii.anl
# only in real points
# with valex=-99
# and cat them into a file
cd divawork
cp ../input/valatxy.coord fort.20
cp ../output/valatxyascii.anl fort.21

echo -99 | ../../bin/cverror.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' ' >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo A problem was encountered during execution ! >> ../$divacvrandlog
echo         divacvrand       cverror.a >> ../$divacvrandlog
echo Check execution track >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo         divacvrand       cverror.a
echo Check execution track
echo --------------------------------------------
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo         divacvrand       cverror.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
fi
cat fort.22 >> cverror.dat
cat fort.33 >> cvdatavar.dat
cd ..
# End loops
# 
done
cd divawork
cp cverror.dat fort.20
cp cvdatavar.dat fort.21
if [ -f ../output/diva.log ] ; then
 cp -f ../output/diva.log .
fi
echo $gcv | ../../bin/cvtotalerror.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' ' >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo A problem was encountered during execution ! >> ../$divacvrandlog
echo          divacvrand      cvtotalerror.a >> ../$divacvrandlog
echo Check execution track >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo          divacvrand      cvtotalerror.a
echo Check execution track
echo --------------------------------------------
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo          divacvrand      cvtotalerror.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
fi
cat fort.22 >> ../output/gcv.dat
cd ..
#Next cv test
done
mv -f ./input/param.par.ori ./input/param.par
mv -f ./input/data.dat.ori ./input/data.dat
mv -f ./input/valatxy.coord.ori ./input/valatxy.coord
cp ./output/gcv.dat ./divawork/fort.11
cd divawork
echo divacvrand:Looking for the minimum of GCV >> ../$divacvrandlog
echo ' ' >> ../$divacvrandlog
if [ -f ../input/gcvsampling.dat ] 
then 
../../bin/findmin.a >> ../output/diva.log
if [ $? -ne 0 ];then
echo ' ' >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo A problem was encountered during execution ! >> ../$divacvrandlog
echo          divacvrand      findmin.a >> ../$divacvrandlog
echo Check execution track >> ../$divacvrandlog
echo -------------------------------------------- >> ../$divacvrandlog
echo ' '
echo --------------------------------------------
echo A problem was encountered during execution !
echo          divacvrand      findmin.a
echo Check execution track
echo --------------------------------------------
echo ' ' >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
echo A problem was encountered during execution ! >> ../output/diva.log
echo          divacvrand      findmin.a  >> ../output/diva.log
echo Check execution track >> ../output/diva.log
echo -------------------------------------------- >> ../output/diva.log
fi
echo ' ' >> ../$divacvrandlog
echo divacvrand:---------------------------------------- >> ../$divacvrandlog
echo divacvrand:CV finished >> ../$divacvrandlog
echo divacvrand:Results written in ./output/gcvsnvar.dat >> ../$divacvrandlog
echo divacvrand:---------------------------------------- >> ../$divacvrandlog
else
echo divacvrand:' ' >> ../$divacvrandlog
echo divacvrand:-------------------------------------------- >> ../$divacvrandlog
echo divacvrand:A problem was encountered during execution ! >> ../$divacvrandlog
echo divacvrand:Check execution track >> ../$divacvrandlog
echo divacvrand:-------------------------------------------- >> ../$divacvrandlog
fi
mv fort.12 ../output/gcvsnvar.dat
echo divacvrand:----------------------------------------------------- >> ../$divacvrandlog
echo divacvrand:'Creating adapted param.par in ./output/param.par.gcv' >> ../$divacvrandlog
echo divacvrand:----------------------------------------------------- >> ../$divacvrandlog
head -23 ../input/param.par  > ../output/param.par.gcv
head -2 ../output/gcvsnvar.dat | tail -1 >> ../output/param.par.gcv
head -25 ../input/param.par | tail -1 >> ../output/param.par.gcv
head -4 ../output/gcvsnvar.dat  | tail -1 >> ../output/param.par.gcv
if [ "$3" == "-r" ]
then
echo divacvrand:=======================================================
echo divacvrand:Replacing  param file with fit
echo divacvrand:======================================================= >> ../$divacvrandlog
echo divacvrand:Replacing  param file with fit >> ../$divacvrandlog
cd ..
cp -v ./input/param.par ./input/param.par.old
cp -v ./output/param.par.gcv ./input/param.par
echo =======================================================
else
cd ..
fi
fi
fi
fi
