#!/bin/ksh

set -x

#------------------------------------------------------------------
# Automated retrieval of NESDIS fire file and HYSPLIT submit
#------------------------------------------------------------------
# Revised: 10 Jul 2002 (RRD) - Initial version: Roland Draxler 
#          15 Aug 2002 (RRD) - only resubmit 10 times
#          23 Aug 2002 (RRD) - multiple file access
#          05 Feb 2003 (RRD) - single conus simulation
#          17 Mar 2003 (RRD) - fire location with no points
#          28 Apr 2003 (RRD) - conversion for frost/snow
#          10 Mar 2004 (RRD) - options for prelim or final fires
#          06 May 2004 (RRD) - capture prelim first then final  
#          26 Aug 2004 (RRD) - changed name of fire server
#          02 Sep 2004 (RRD) - simplified procedures
#          19 Nov 2004 (RRD) - additonal mail notification options
#          02 Dec 2004 (RRD) - date corrections
#          21 Dec 2004 (RRD) - missing fire file use old data
#          24 Jan 2005 (RRD) - added SAB email notification
#          14 Mar 2005 (RRD) - modified submit if no fires file
#------------------------------------------------------------------
#Overview
#The dispersion model is preconfigured to run from selected regions using 
#a daily NAM meteorological forecast. Hourly average output maps of soot air
#concentration are produced. The fire locations for each day are tabulated
#and two dispersion simulations are produced.  A 24 h analysis simulation
#is run for the previous day using that day's fire locations.  The smoke
#particle positions at the end of the day are used to intitialze the 48 h
#dispersion forecast simulation.  The next day, the same initialization file
#is used to start the analysis simulation, prior to running the forecast.
# run submission date should be after meteorology is available    

  RUNDATE=${PDY}
  echo " rundate= ",$RUNDATE

# fire points date is valid for the previous day
  FIREDATE=${PDYm1}
  echo " firedate= ",$FIREDATE

# working directory for model results
  export OLD=${COMINm1}
  export WRK=$DATA

# message log file
#  log=$COMOUT/"log.fire"
  echo "**Starting fire pm25 smoke simulation: `date`"


#------------------------------------------------------------------
# FINAL FILE FIRST ...
# Construct NESDIS file name from current local date.
# FTP the data file if not already in the local directory.
# Note NESDIS computer times are GMT!
#------------------------------------------------------------------

FILE="hmshysplit${FIREDATE}.txt"


#ping -c3 gp12.ssd.nesdis.noaa.gov
#pingerr=$?

#if [ $pingerr -eq 0 ] ; then
    ftp -nv gp12.ssd.nesdis.noaa.gov <<EOD
    user anonymous david.michaud@noaa.gov
    cd pub/FIRE/HMS
    pwd
    ascii
    prompt
    get ${FILE}
    bye
EOD
#else
#    echo "**NESDIS HMS server unreachable"
#fi

#------------------------------------------------------------------
# PRELIM FILE NEXT ...
# Construct NESDIS file name from current local date.
# FTP the data file if not already in the local directory.
# Note NESDIS computer times are GMT!
#------------------------------------------------------------------

if [ ! -f ${FILE} ];then

   FILE="hmshysplit${FIREDATE}.prelim.txt"

#   ping -c3 gp12.ssd.nesdis.noaa.gov
#   pingerr=$?

#   if [ $pingerr -eq 0 ] ; then
     ftp -nv gp12.ssd.nesdis.noaa.gov <<EOD
     user anonymous david.michaud@noaa.gov
     cd pub/FIRE/HMS
     pwd
     ascii
     prompt
     get ${FILE}
     bye
EOD
#   else
#     echo "**NESDIS HMS server unreachable"
#   fi

fi

#------------------------------------------------------------------
# if there is no current fire location file, was there a valid
# run yesterday as shown by FIRES.ARC       
#------------------------------------------------------------------

if [ ! -f ${FILE} ];then
   if [ -f ${OLD}/FIRES.NEW ];then
#     set dummy file name to one that exists to submit std job
      FILE="${OLD}/FIRES.NEW"
      echo "** Wildfire locations today are from yesterday"
   else
      echo "** No wildfire locations today or yesterday"
      err_exit "** No wildfire locations today or yesterday"
   fi 

#------------------------------------------------------------------
# check file sizes to determine Email notification:
# 17 bytes = header record only, no fire locations
# 34 bytes = header record plus one data record
#------------------------------------------------------------------

else
   let flen=0
   echo "`wc -c ${FILE}`" | read flen tmp

   if [ $flen -gt 10 ];then
      echo "**${FILE} found and is of length: ${flen}"
#GSM not sure if we need the mail commands here in this section
      if [ $flen -gt 30 ];then
         echo "** Wildfire locations today"
         cp ${FILE} FIRES.TXT
      else
         echo "** HMShysplit file <=30 bytes!"
         echo "~s HMShysplit file <=30 bytes! \n No fires" | mail SABSupervisor@noaa.gov
         exit
      fi
   else
      echo "** HMShysplit file <=10 bytes!"
      echo "~s HMShysplit file <=10 bytes! \n Empty file" | mail SABSupervisor@noaa.gov
   fi
fi

#------------------------------------------------------------------
# Submit standard simulation if fires available or submit test
#------------------------------------------------------------------

if [ -f ${FILE} ];then 

   ${USHsmoke}/smoke_hysplit.sh ${RUNDATE} ${FIREDATE}

else
   #GSM another mail command
   echo "** HMShysplit file not found! No Smoke Run Today!"
   exit
fi
