#!/bin/sh
set -x

. $HOME/hl_home/OPR/progressPP.log
prevdtg=`$HOME/bin/mandtg $DTGPP + -6`
todir=$HOME/public_html/$prevdtg

[ -d $todir ] || mkdir $todir       # Create directory if not there yet.
[ -r $todir/J0.png ] && exit        # Already processed.

wd=/data/hirlam/WD$$
mkdir $wd || exit
cd $wd || exit

dtgs=""
for h in `seq -720 6 0`
do
   dtgs="$dtgs "`$HOME/bin/mandtg $prevdtg + $h`
done

for dtg in $dtgs
do
   yyyy=`$HOME/bin/mandtg -year $dtg`
   mm=`$HOME/bin/mandtg -month $dtg`
   dd=`$HOME/bin/mandtg -day $dtg`
   hh=`$HOME/bin/mandtg -hour $dtg`
   cost=`grep " cost = " /data/hirlam/hl_arc/OPR/$yyyy/$mm/$dd/$hh/HL_Cycle_$yyyy$mm$dd$hh.html | head -1 | awk '{print $5}'`
   echo $dtg $cost >> J0-$hh.txt
done

gnuplot << EOF
   set terminal pngcairo mono size $XMAP,$YMAP
   set output 'J0.png'
   set title 'Beginwaarde van de afstand van het model tot de waarnemingen.'
   set key outside
   set grid linestyle 2
   set xdata time
   set timefmt '%Y%m%d%H'
   set format x '%d/%m %H h'
   set yrange [0:]
   set xlabel 'Data assimilatie tijdstip (UTC)'
   set ylabel 'Afstand (dimensieloos)'
   plot 'J0-00.txt' using 1:2 title 'Afstand 00 UTC' with linespoints linetype 4 pointtype 4, \
        'J0-06.txt' using 1:2 title 'Afstand 06 UTC' with linespoints linetype 4 pointtype 6, \
        'J0-12.txt' using 1:2 title 'Afstand 12 UTC' with linespoints linetype 2 pointtype 5, \
        'J0-18.txt' using 1:2 title 'Afstand 18 UTC' with linespoints linetype 2 pointtype 7
EOF

rm J0-??.txt
mv J0.png $todir

cd

rm -rf $wd

exit 0
