#!/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_temp$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 temp_list.dat.???? pilot_list.dat.????
cat temp_list.dat.???? > temp_list.dat
cat pilot_list.dat.???? > pilot_list.dat

# TEMP data; uses land/sea mask for grid definition from first boundary

$HOME/PlotsOfTheDay/use-temp `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` temp_list.dat
mv TEMP_COUNT.txt $todir

# PILOT data; uses land/sea mask for grid definition from first boundary

$HOME/PlotsOfTheDay/use-pilot `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` pilot_list.dat
mv PILOT_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 TEMP$houroff.txt -o -s PILOT$houroff ]
   then

      [ -s TEMP$houroff.txt ]  && plotcom="$plotcom 'TEMP$houroff.txt' title 'temp ${h}H' with points palette pointtype 5,"
      [ -s PILOT$houroff.txt ] && plotcom="$plotcom 'PILOT$houroff.txt' title 'pilot ${h}H' with points palette pointtype 7,"
                                  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_temp$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 TEMP/PILOT rond $dtgoff UTC (groen: gebruikt; rood: verworpen).'
         $plotcom
EOF

      mv use_temp$houroff.png $todir

# Construct the plot in Postscript format

      gnuplot << EOF
         set terminal postscript landscape color
         set output 'use_temp$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 TEMP/PILOT rond $dtgoff UTC (groen: gebruikt; oranje: redundant; rood: verworpen).'
         $plotcom
EOF

      mv use_temp$houroff.ps $todir

# Remove the intermediate text files

      rm TEMP$houroff.txt PILOT$houroff.txt

   fi

done

cd

rm -rf $wd

exit 0
