#!/usr/bin/ksh
#####################################################################
echo "------------------------------------------------"
echo "        extpc_psurge_datachk.sh.sms "
echo "     TPC Probability Storm Surge Model Data Check Control Script "
echo "------------------------------------------------"
echo "History: 2007.04 - First implementation of this new script."
#####################################################################

cd $DATA

set -x

export kyodate=`echo $PDY$cyc | cut -c3-10`

################################################################
#    Decide how many storms to run.  Write to storm files
#    with the status of running the storm.
################################################################
export prodcyc=$cyc
export TPC_ZSURGE=${TPC_ZSURGE:-/tpc/save/guidance/storm-data/zsurge}

stormdate=`echo $PDY |cut -c1-8`
stormyear=`echo $PDY |cut -c1-4`
stormmonth=`echo $PDY |cut -c5-6`
stormday=`echo $PDY |cut -c7-8`

#unique case exception handing, 
#to be used when storms exist across 2 different years 
#if [ $stormmonth -lt 03 ]
#then
#    stormyear=`expr $stormyear - 1`
#fi

NETNAME=`echo $SMSNAME | sed -e "s+/jtpc_psurge_datachk++g"`

# Check to see if there is any .kill file 

if [ -s $TPC_ZSURGE/queue/*${stormyear}*.kill ]
then
   cp $TPC_ZSURGE/queue/*${stormyear}*.kill $DATA/.
   status="kill"
   kill_status="kill"
   storm_status="yes"
fi   
   
# Check the loadleveler to see how many psurge jobs are currently running
llq -f %jn %id %o | grep nwprod |grep tpc_psurge |grep -v data >>current_storms

rm -rf psurge_joblist

if [ $kill_status = "kill" ]
then
   for fil in `ls *.kill`
   do
      # Check to see if the kill file is too old:
      diff $fil $COM_ZSURGE/queue/$fil
      err1=$?
  
      if [ $err1 -eq 0 ]
      then
          echo "The .kill file has been processed"
      else
         # Check the databases to see if there is any new active storm that's currently running:
         gofile=`echo $fil |sed -e "s/kill/go/g"`
         if [ -s $COM_ZSURGE/queue/$gofile ]
         then
            # llcancel the current psurge run in the system:
            for jid in `cat current_storms |awk '{print $2}'`
            do
               llcancel $jid
            done
        
            # Remove the .go file to allow the new process
            rm -rf $COM_ZSURGE/queue/$gofile 

            # Copy the .kill file to /com/tpc/ area:
            cp $fil $COM_ZSURGE/queue/$fil

            # Find a valid .kill file, exit to process the storm  
            echo "Exit now to prepare for the run for the new storm file" 
            exit
         else
            echo "No active storm is running in the system to be cancelled"
            break
         fi
      fi
   done
fi

if [ -s $TPC_ZSURGE/queue/*${stormyear}*.go ]
then
   cp $TPC_ZSURGE/queue/*${stormyear}*.go $DATA/.
   status="new"
   kill_status="none"
   storm_status="yes"
fi

if [ $status = "new" ]
then
   for fil in `ls *.go`
   do
      # Check the databases to see if there is any new active storm:
      diff $fil $COM_ZSURGE/queue/$fil
      err1=$?

      fil_processed=`echo $fil |sed -e "s/go/processed/g"`
      diff $fil $COM_ZSURGE/queue/${fil_processed}
      err2=$?

      if [ $err1 -eq 0 -o $err2 -eq 0 ]
      then 
         echo "There is no new storm file available"
         echo "No psurge job to run"
      else
         cp $fil $COM_ZSURGE/queue/.
         stormid=`echo $fil |awk -F"_" '{print $1}'`
         stormdate=`echo $fil |awk -F"." '{print $1}' |awk -F"_" '{print $2}'`
         stormnum=01
         echo "$stormid $stormdate" >$COM_ZSURGE/queue/storm${stormnum}.txt
      
         # echo the proper job name to run the psurge
         echo "jtpc_psurge_${stormnum}_nhctrk"  >>psurge_joblist
         echo "jtpc_psurge_${stormnum}_slosh"  >>psurge_joblist
         echo "jtpc_psurge_${stormnum}_post" >>psurge_joblist
         echo "jtpc_psurge_${stormnum}_awips" >>psurge_joblist
         newstorm="yes"
      fi
   done
fi


# Requeue from the first available psurge job on the SMS graph:

if [ $newstorm = "yes" ]
then

   echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds

   for job in `cat psurge_joblist`
   do
      echo "force -r complete $NETNAME/psurge_${stormnum}/$job" >>cdp_cmds
      echo "alter -s $NETNAME/psurge_${stormnum}/$job queued" >>cdp_cmds
      echo "requeue $NETNAME/psurge_${stormnum}/$job" >>cdp_cmds
      echo "alter -s $NETNAME/psurge_${stormnum}/$job complete" >>cdp_cmds
   done

   echo "exit" >> cdp_cmds

   if test "$SENDSMS" = "YES"
   then
      $SMSBIN/cdp < cdp_cmds
   fi
fi

 
if [ $cyc -eq 00 -o $cyc -eq 06 -o $cyc -eq 12 -o $cyc -eq 18 ]
then
  #if [ ! -s $TPC_ZSURGE/queue/*${stormyear}*.kill -a ! -s $TPC_ZSURGE/queue/*${stormyear}*.go ] 
  if [ -f $TPC_ZSURGE/queue/*${stormyear}*.clean ] 
    then
        echo "No active storm"
        echo "Send out blank grib files"
        advcyc=`expr $cyc + 3`
        if [ $advcyc -le 10 ]; then advcyc=0$advcyc; fi
        $USHpsurge/tpc_psurge_blank.sh $USHpsurge $COMOUT $PDY$advcyc $PDY$cyc
   fi
fi                 
   

if [ $storm_status != "yes" ]
then
   echo "There is no new storm file available"
   echo "No psurge job to run"
   exit
fi

#####################################################################
# GOOD RUN
set +x
echo "**************JOB $job COMPLETED NORMALLY "
echo "**************JOB $job COMPLETED NORMALLY "
echo "**************JOB $job COMPLETED NORMALLY "
set -x
#####################################################################
