#!/bin/bash

DRIVER=png
#DRIVER=ps


#===============================================
# ADAPT the following to the gnuplot executable
gplot=c:/JMB/gnuplot/bin/wgnuplot.exe
gplot=gnuplot
export gplot
#===============================================


echo Starting plotting with gnuplot
echo smoothing parameter $smooth

mkdir -p plots
rm -f ./plots/*


# TAKE error scale from varkab
head -26 ../input/param.par | tail -1 > bb 
{
read VARBAK
} < bb
rm bb

STDDEV=$(echo $VARBAK | awk '{print sqrt($1)}')

echo set cbrange"["0:$STDDEV"]" > dverrorrange

# if VARBAK=0 and error file is there: message and VARBAK=1

# TAKE projection from param.par
head -4 ../input/param.par | tail -1 > bb 
{
read ICOORD
} < bb
rm bb

# TAKE ispec from param.par
head -6 ../input/param.par | tail -1 > bb 
{
read ISPEC
} < bb
rm bb

# TAKE field scale from input



if [ "$#" -le "1" ]
then
echo ==================================================================
echo ==================================================================
echo Usage: divaplotall fmin fmax
echo Will use automatic scale for analysis plot
echo Automatic scale includes sometimes 0 and might be inappropriate
echo
echo if threshold.dat is present, masking will apply to
echo regions with errors above treshold*varbak
echo
echo if longref.dat is present, its content will be used for projections
echo ===================================================================
echo ===================================================================
else
echo set cbrange"["$1:$2"]" >> dvanalysisrange
fi

echo Variance of the background field: $VARBAK
THRESH=0.5
# TAKE treshold for mask from file
if [ -f threshold.dat ]
then
{
read THRESH
} < threshold.dat
fi

LONGREF=0
# TAKE longref from file
if [ -f longref.dat ]
then
{
read LONGREF
} < longref.dat
fi

for i in `ls diva_*`
#for i in `ls diva_RL`

do
echo ===========================================
echo 
echo ===========================================
echo Execution of gnuplot $i
echo --------------------------------------


if [ "$DRIVER" == "png" ]
then
echo set size 1,1 > bidon
#echo set terminal png transparent size 1280,1024 crop xffffff >> bidon
#echo set terminal png transparent giant size 2560,2048 crop xffffff >> bidon
echo set terminal png transparent giant size 1920,1540 crop \#ffffff >> bidon
echo set output '"./plots/'$i'.png"' >> bidon
else
if [ "$DRIVER" == "ps" ]
then
# For postscript comment previous lines and use the following instead
echo set terminal postscript eps color > bidon
echo set output '"./plots/'$i'.eps"' >> bidon
fi
fi

if [ "$smooth" == "1" ]
then
echo ii=14 >> bidon
else
echo ii=1>> bidon
fi

jj=$(echo $i | grep data | wc -l)
if [ "$jj" == 1 ]
then
echo Data related field: using range
cat dvanalysisrange
cat dvanalysisrange >> bidon

if [ -f plotboundingbox.dat ]
then

{
read xmingp xmaxgp
read ymingp ymaxgp
} < plotboundingbox.dat
echo set xrange"[" $xmingp ":" $xmaxgp "]" > boundingbox.dat
echo set yrange"[" $ymingp ":" $ymaxgp "]" >> boundingbox.dat
cat boundingbox.dat >> bidon
cat boundingbox.dat
fi

if [ -f analysis.pal ]
then
echo using user defined palette
cat analysis.pal >> bidon
else
echo using default palette
cat analysis.pal.default >> bidon
fi

fi

jj=$(echo $i | grep -i error | wc -l)
if [ "$jj" == 1 ]
then
echo Error related field: using range
cat dverrorrange
cat dverrorrange >> bidon
if [ -f plotboundingbox.dat ]
then

{
read xmingp xmaxgp
read ymingp ymaxgp
} < plotboundingbox.dat
echo set xrange"[" $xmingp ":" $xmaxgp "]" > boundingbox.dat
echo set yrange"[" $ymingp ":" $ymaxgp "]" >> boundingbox.dat
cat boundingbox.dat >> bidon
cat boundingbox.dat

fi
if [ -f error.pal ]
then
echo using user defined palette
cat error.pal >> bidon
else
echo using default palette
cat error.pal.default >> bidon
fi


fi

jj=$(echo $i | grep -i analysis | wc -l)
if [ "$jj" == 1 ]
then
echo Analysis related field: using range
cat dvanalysisrange
cat dvanalysisrange >> bidon

if [ -f plotboundingbox.dat ]
then
{
read xmingp xmaxgp
read ymingp ymaxgp
} < plotboundingbox.dat
echo set xrange"[" $xmingp ":" $xmaxgp "]" > boundingbox.dat
echo set yrange"[" $ymingp ":" $ymaxgp "]" >> boundingbox.dat
cat boundingbox.dat >> bidon
cat boundingbox.dat
fi
if [ -f analysis.pal ]
then
echo using user defined palette
cat analysis.pal >> bidon
else
echo using default palette
cat analysis.pal.default >> bidon
fi


fi

jj=$(echo $i | grep -i masked | wc -l)
if [ "$jj" == 1 ]
then
echo Masking option
echo thresh=$THRESH*$STDDEV >> bidon 
fi

jj=$(echo $i | grep -i projection | wc -l)
if [ "$jj" == 1 ]
then

if [ -f longref.dat ] 
then
echo Projection option
echo longref=$LONGREF
echo longref=$LONGREF >> bidon 
else
echo Using default longitude reference from analysis grid
cat longref.dat.default
cat longref.dat.default >> bidon
fi

fi



jj=$(echo $i | grep -i cova | wc -l)
if [ "$jj" == 1 ]
then
echo covariance option
cat xrangecova  >> bidon 
fi




#cat $i >> bidon
$i
#$gplot bidon
#cp ./plots/$i.png ./plots/$i.ori.png


#nl=$(cat bidon |wc -l)
#let nl=$nl-1
#head -$nl bidon > bidon2
#echo  " ">> bidon2
#echo unset colorbox >> bidon2
#echo set title >> bidon2
#tail -1 bidon >> bidon2
#$gplot bidon2


done

# Erase plot that should not exist (when files are not there)

if [ "$VARBAK" == "0" ]
then
echo Erasing error related plots since VARBAK=0
rm -f ./plots/*error*
fi
if [ "$ISPEC" == "0" ]
then
echo Erasing error related plots since ISPEC=0
rm -f ./plots/*error*
fi

if [ -f fort.55 ]
then
echo Keeping velocity plots
else
echo Removing velocity plots : UVvel.dat not there
rm -f ./plots/*UV*
fi

if [ -f fort.72 ]
then
echo Keeping variable length scale related plots
else
echo Removing variable length scale related plots :RL.dat not there
rm -f ./plots/*RL*
fi


if [ "$ICOORD" == "2" ]
then
echo Keeping projections
else
  if [ "$ICOORD" == "1" ]
   then
   echo Keeping projections
  else
   echo Removing projections: icoordchange not 2
   rm -f ./plots/*projection*
  fi
fi


if [ -f ../output/covariance.dat ]
then
echo Keeping covariance fitting plots
else
echo Removing covariance fitting plots, no covariance files
rm -f ./plots/*cova*
fi
for jjj in 1 2 3 4 5 
do
if [ -f ../output/trends.all.$jjj.dat ] 
then
echo Keeping trends of class $jjj
else
rm -f ./plots/*trends_$jjj.*
fi
done

if [ -f ../output/DATABINS.dat ]
then
echo Keeping data bins plots
else
rm -f ./plots/*DATABINS*
fi

if [ -f ../output/varfieldgher.anl ]
then
echo Keeping data standard deviation plots
else
rm -f ./plots/diva_std*
fi


if [ -f plotboundingbox.dat ]
then
echo need to go from plotboundinbox.dat to fort.47 of kml

{
read xmingp xmaxgp
read ymingp ymaxgp
} < plotboundingbox.dat

echo    "<north>"  $ymaxgp "</north>" > googlebox.dat
echo    "<south>"  $ymingp "</south>" >> googlebox.dat
echo    "<east>"  $xmaxgp "</east>" >> googlebox.dat
echo    "<west>" $xmingp "</west>" >> googlebox.dat


else
cp -v fort.47 googlebox.dat
fi