#!/bin/ksh

##################################################
# This script runs the Tropical Cyclone Graphics  
# (4-charts) by hand for the storm id provided.   
#                                                 
#                                                 
# M.Mainelli 5/02     Intially Created               
# M.Mainelli 6/05     Updated for 2005 season       
# M.Mainelli 5/06     Updated to remove the probability swath and intensity    
#                     graphics from being sent to the web.                      
# M.Klein    5/06     Modify to work in HPC environment.                  
# M.Klein    6/08     Comment out sending of wind table graphic and call new    
#                     script to make wind table graphic.                      
# M.Klein    7/08     Call new wind speed probability script at end     
# A.Robson  1/11/11   Changed address of webserver  
# A.Robson  09/28/11  Added test variables and RH5 tested.
# M.Klein   10/06/11  Comment out copy of gptpc.
# A.Robson  11/09/11  Changed Xvfb display number.
# A.Robson  12/14/11  Uncomment stopXvfb's
# A.Robson  12/15/11  changed xvfb number to 61
# M.Klein   05/14/12  Make changes to support NCWCP and comment out machine check.
# A.Robson  06/12/13  Added $NHCBKUPWEB
# A.Robson  07/12/13  Cleaned up a bit. No other changes. intensitytable.pl calls data from
#                     NCOSRV instead of CCS. fourpanel_ccs used if WCOSS down.


# SET UP ENVIRONMENTAL VARIABLES
#if [ ${NOSSH:-null} = "null" ]; then
    ssh="ssh"
    scp="scp"
#else
#    echo "No ssh or scp allowed."
#    ssh=echo
#    scp=echo
#fi

exedir="/export-5/$NETAPPIP/hpcops/exe"
ADVS=$HOME/wgraph
export ADVS
cd $ADVS
rm -f send3_file

# ASK FOR USER INPUT
print "Please enter the four-character storm id (e.g., al01):" 
read stormid

basin=`echo $stormid | cut -c1-2`
bas=`echo $basin | tr "a-z" "A-Z"`
stmnum=`echo $stormid | cut -c3-4`
year=`date -u "+%Y"`
date=`date -u "+%Y%m%d"`
hour=`date -u "+%H"`
day=`date -u "+%d"`
month=`date -u "+%b"`
ls -al $ADVS/mar/${stormid}${year}.* | tr -s " " | cut -f9 -d" " > listing
stmadv=`cat listing | tail -1 | cut -f3 -d"."`
chmod 666 listing
echo $stmadv

sleep 5

# START THE VIRTUAL SERVER.
dsp=`startXvfb_tpc 61`

if [ $? = "0" ]; then
    echo $dsp
    DISPLAY=$dsp
    export DISPLAY
fi

#------------------------------------------------------------------------------
# As of NAWIPS release 5.11.3, there is a problem with the operational version
# of gptpc.  Need to copy files from NCOSRV and run a revised version

#cp ${exedir}/gptpc* .

gptpc  << EOF
     STRMID = ${basin}${stmnum}${year}/${stmadv}/gif

     list 

     run

     exit

EOF

# CONVERT GIF FILES TO PS, JPG, AND PDF
eval=y

if [ $eval = "y" ]; then
    convert ${bas}${stmnum}${year}S.${stmadv}.GIF ${bas}${stmnum}${year}S.${stmadv}.PS
    convert ${bas}${stmnum}${year}S.${stmadv}.GIF ${bas}${stmnum}${year}S.${stmadv}.PDF
    convert ${bas}${stmnum}${year}S.${stmadv}.GIF ${bas}${stmnum}${year}S.${stmadv}.JPG
    echo "${bas}${stmnum}${year}S.${stmadv}" >> send3_file
else
    echo "Please run the script again..."
    echo "If problem occurs again call the on-call person."
fi
chmod 666 send3_file

if [ ! -s send3_file ];
then
   rm -f listing
   rm -f out2
   rm -f send3_file
   exit
else
   for send in `cat send3_file`
   do
      snumber=`echo $send | cut -c3-4`
      syear=`echo $send | cut -c7-8`
      year=`echo $send | cut -c5-8`
      sbasin=`echo $send | cut -c1-2`
      stype=`echo $send | cut -c9`

      if [ $sbasin = "AL" ]; then
        dirname=AT${snumber}
        filename="AL"
        sname="${filename}${snumber}${syear}.NAM"
      else
        dirname=EP${snumber}
        filename="EP"
        sname="${filename}${snumber}${syear}.NAM"
      fi
      # Looks format of the NAM file changed... gets created by wwgraph... will rename before scp to webserver...
      cp ${filename}${snumber}${year}.NAM ${filename}${snumber}${syear}.NAM
      chmod 664 *.NAM
  
      webfile="${filename}${snumber}${syear}${stype}"
      namfile="${filename}${snumber}${syear}.NAM"
      arcfile="${filename}${snumber}${syear}${stype}_${stmadv}"

      # SEAHORSE is TPC's primary webserver
      # RATFISH is our backup webserver
      # LNX131 is HPC's backup webserver for TPC
      direct=/home/httpadm/docs/ftp/graphics/$dirname
      machine="ratfish.nhc.noaa.gov"
      machine2="seahorse.nhc.noaa.gov"
      #machine3="vp-tpcbkup.ncep.noaa.gov"
      machine3="$NHCBKUPWEB"
      user="graphics"
      $scp ${send}.GIF graphics@${machine3}:$direct/${webfile}.GIF
      $scp $sname graphics@${machine3}:$direct/${namfile}
      $scp $namfile graphics@${machine3}:$direct/${namfile}

   done

   rm -f listing
   rm -f out2
   #rm -f send3_file
   rm -f *.JPG
   rm -f *.PDF
   mv -f *.PS ${ADVS}/psfiles/
   mv -f *.GIF ${ADVS}/giffiles/
fi

echo "\nNow creating the wind table graphic....\n"

# WILL JUST RUN THE INTENSITYTABLE.PL SCRIPT OUTRIGHT...
   intensitytable.pl

# KICK OFF THE SCRIPT TO GENERATE THE WIND PROB GRAPHICS
# FIRST DETERMINE THE CYCLE TIME FROM THE TCM ADVISORY
tcmadv="${stormid}${year}.fstadv.${stmadv}"
repeatline=`cat ${ADVS}/mar/${tcmadv} | grep -i -n "repeat...center located" | awk -F: '{print $1}'`
cycleline=`expr ${repeatline} + 1`
cyc=`cat ${ADVS}/mar/${tcmadv} | head -${cycleline} | tail -1 | awk -F" " '{print $2}' | cut -c4-5`
echo "${cyc}"
datecycle="${date}${cyc}"
echo $datecycle

echo "Now creating Wind Prob Graphics. This may take a while."
echo "******************************************************"
echo "If it appears that this script has been waiting a LONG time, then"
echo "type CTRL + C to exit the script and then re-run it manually"
echo "by typing 'windprob.pl YYYYMMDDCC'."

windprob.pl $datecycle

stopXvfb 61

exit
