#!/bin/sh
set -x

now=`date -u +"%Y%m%d%H"`
DTG=${1:-`$HOME/bin/mandtg $now + -2`}

[ `expr $DTG % 100 % 3` -eq 0 ] || exit
date=`$HOME/bin/mandtg -date $DTG`
hour=`$HOME/bin/mandtg -hour $DTG`

if [ `expr $DTG % 100 % 6` -ne 0 ]
then
   todir=$HOME/public_html/`$HOME/bin/mandtg $DTG + -3`
   [ -d $todir ] || mkdir $todir                # Create directory if not there yet.
   [ -r $todir/SYNOP$hour.txt ]        && exit  # Already processed.
else
   todir=$HOME/public_html/$DTG
   [ -d $todir ] || mkdir $todir                # Create directory if not there yet.
   [ -r $todir/observations$hour.png ] && exit  # Already processed.
fi

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

export ECCODES_DIR=/home/toon/eccodes-install
export LD_LIBRARY_PATH=$ECCODES_DIR/lib:$LD_LIBRARY_PATH

if [ `expr $DTG % 100 % 6` -ne 0 ]
then

# Conventional observations; needs orography and grid definition from interpolated boundary file

   $HOME/PlotsOfTheDay/decode-eccodes `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` /data/hirlam/hl_arc/OPR/BUFR0001$DTG.DAT
   for h in -3 -2 -1 +0 +1 +2
   do
      dtgoff=`$HOME/bin/mandtg $DTG + $h`
      houroff=`$HOME/bin/mandtg -hour $dtgoff`
      mv SYNOP$houroff.txt $todir
   done

else

# Conventional observations; needs orography and grid definition from interpolated boundary file

   ln -s /data/hirlam/hl_arc/OPR/ob$DTG .
   $HOME/PlotsOfTheDay/decode-eccodes `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` ob$DTG
   rm ob$DTG
   mv spanobs.txt /data/hirlam/hl_arc/OPR/spanobs$DTG.txt

# Scatterometer data; needs land/sea mask and grid definition from first boundary

   ln -s /data/hirlam/hl_home/OPR/ob${DTG}AS .
   rm -f fort.4
   export BUFRTAB_DIR=/data/hirlam/hl_home/OPR/lib/data/bufrtables/
   $HOME/PlotsOfTheDay/expbufr -f ob${DTG}AS | awk -f $HOME/PlotsOfTheDay/ascat.awk
   rm ob${DTG}AS
   $HOME/PlotsOfTheDay/process-ascat `ls -1t /data/hirlam/hl_home/OPR/mf2*f | head -1` ascat.hour ascat.minute ascat.lat ascat.lon
   rm ascat.hour ascat.minute ascat.lat ascat.lon

   . $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/$DTG

      dtgoff=`$HOME/bin/mandtg $DTG + $h`
      houroff=`$HOME/bin/mandtg -hour $dtgoff`
      plotcom="splot "
      [ -r ASCAT$houroff.txt ]       && plotcom="$plotcom 'ASCAT$houroff.txt' title 'ascat ${h}H' with points palette pointtype 7,"
      [ -r SHIP$houroff.txt ]        && plotcom="$plotcom 'SHIP$houroff.txt' title 'ship ${h}H' with points palette pointtype 2,"
      [ -r SYNOP$houroff.txt ]       && plotcom="$plotcom 'SYNOP$houroff.txt' using 1:2:3 title 'synop ${h}H' with points palette pointtype 1,"
      [ -r TEMP$houroff.txt ]        && plotcom="$plotcom 'TEMP$houroff.txt' title 'temp/pilot ${h}H' with points palette pointtype 5,"
      [ -r AIREP-AMDAR$houroff.txt ] && plotcom="$plotcom 'AIREP-AMDAR$houroff.txt' title 'airep/amdar ${h}H' with points palette pointtype 6,"
                                     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 'observations$houroff.png'
         set view map
         $XRANGE
         $YRANGE
         set pointsize 0.5
         set palette cubehelix
         set title 'Waarnemingen rond $dtgoff UTC.'
         $plotcom
EOF

      mv observations$houroff.png $todir

# Construct the plot in Postscript format

      gnuplot << EOF
         set terminal postscript landscape color
         set output 'observations$houroff.ps'
         set view map
         $XRANGE
         $YRANGE
         set pointsize 0.25
         set palette cubehelix
         set title 'Waarnemingen rond $dtgoff UTC.'
         $plotcom
EOF

      mv observations$houroff.ps SYNOP$houroff.txt $todir

# Remove the intermediate text files

      rm SHIP$houroff.txt TEMP$houroff.txt AIREP-AMDAR$houroff.txt ASCAT$houroff.txt

   done

fi

cd

rm -rf $wd

exit 0
