#!/bin/sh
set -x

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

date=`$HOME/bin/mandtg -date $prevdtg`
hour=`$HOME/bin/mandtg -hour $prevdtg`

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

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

# Get the OBST file with CMA information in readable form

tar --wildcards -zxvf $ARCHDIR/OBST${date}_$hour.tar.gz synop_list.dat.???? ship_list.dat.???? dribu_list.dat.????
cat synop_list.dat.???? > synop_list.dat
cat ship_list.dat.????  > ship_list.dat
cat dribu_list.dat.???? > dribu_list.dat

# SYNOP/SHIP/DRIBU data; uses land/sea mask for grid definition from first boundary

$HOME/PlotsOfTheDay/use-synop `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` synop_list.dat
mv SYNOP_COUNT.txt $todir
$HOME/PlotsOfTheDay/use-ship  `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` ship_list.dat
mv SHIP_COUNT.txt $todir
$HOME/PlotsOfTheDay/use-dribu `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` dribu_list.dat
mv DRIBU_COUNT.txt $todir

. $HOME/PlotsOfTheDay/LONLAT.def

ln -s $HOME/PlotsOfTheDay/coastline.txt .

for h in -3 -2 -1 +0 +1 +2
do

# Process the resulting text files and move result to $HOME/public_html/$todir

   dtgoff=`$HOME/bin/mandtg $prevdtg + $h`
   houroff=`$HOME/bin/mandtg -hour $dtgoff`
   plotcom="splot "

   if [ -s SYNOP$houroff.txt -o -s SHIP$houroff.txt -o -s DRIBU$houroff.txt ]
   then

      [ -r SYNOP$houroff.txt ] && plotcom="$plotcom 'SYNOP$houroff.txt' title 'synop ${h}H' with points palette pointtype 1,"
      [ -r SHIP$houroff.txt ]  && plotcom="$plotcom 'SHIP$houroff.txt'  title 'ship ${h}H'  with points palette pointtype 2,"
      [ -r DRIBU$houroff.txt ] && plotcom="$plotcom 'DRIBU$houroff.txt' title 'dribu ${h}H' with points palette pointtype 3,"
                               plotcom="$plotcom 'coastline.txt' notitle with points pointtype 7 pointsize 0.05 linecolor 'medium-blue'"

# Construct the plot in PNG format

      gnuplot << EOF
         set terminal png truecolor size $XMAP,$YMAP
         set output 'use_other$houroff.png'
         set view map
         $XRANGE
         $YRANGE
         set pointsize 0.5
         set palette defined (0.0 'dark-gray', 0.5 'dark-gray', 0.5 'green', 1.5 'green', 1.5 'yellow', 2.5 'yellow', 2.5 'orange', 3.5 'orange', 3.5 'red', 4.0 'red')
         unset colorbox
         set zrange [0:4]
         set cbrange [0:4]
         set title 'Gebruik SYNOP/SHIP/DRIBU rond $dtgoff UTC (groen: gebruikt; oranje: redundant; rood: verworpen).'
         $plotcom
EOF

      mv use_other$houroff.png $todir

# Construct the plot in Postscript format

      gnuplot << EOF
         set terminal postscript landscape color
         set output 'use_other$houroff.ps'
         set view map
         $XRANGE
         $YRANGE
         set pointsize 0.25
         set palette defined (0.0 'dark-gray', 0.5 'dark-gray', 0.5 'green', 1.5 'green', 1.5 'yellow', 2.5 'yellow', 2.5 'orange', 3.5 'orange', 3.5 'red', 4.0 'red')
         unset colorbox
         set zrange [0:4]
         set cbrange [0:4]
         set title 'Gebruik SYNOP/SHIP/DRIBU rond $dtgoff UTC (groen: gebruikt; oranje: redundant; rood: verworpen).'
         $plotcom
EOF

      mv use_other$houroff.ps $todir

   fi

# Remove the intermediate text files

   rm SYNOP$houroff.txt SHIP$houroff.txt DRIBU$houroff.txt

done

cd

rm -rf $wd

exit 0
