#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exfirewx_prelim.sh.sms
# Script description:  Gets all input fields for NDAS run
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: The scripts gets all the input files needed for the NMM firewx run
#           and runs the code which interpolates the operational Nam initial condition
#           to the nested NMM domain
#
# Script description:  Runs interpolation code to create boundary conditions for the
#                      NMM hiresw run

#
# Script history log:
# 1999-06-23  Eric Rogers
# 1999-07-30  Brent Gordon  - Modified for production.
# 2000-03-03  Eric Rogers modified scripts for 60-h Nam forecast
# 2001-04-19  Eric Rogers modified NDAS scripts for hiresw runs
# 2001-11-19  Eric Rogers modified for interface w/Nam-12
# 2002-03-15  Eric Rogers modified 8-km and 4-km Meso hires window runs
# 2003-03-11  Eric Rogers modified for NMM FIREWX runs
#

set -x

msg="JOB $job FOR NEST=$NEST HAS BEGUN"
postmsg "$jlogfile" "$msg"

cd $DATA

if [ `cat ${SDMINPUT}/run_date` -ne $PDY  ] ; then
   if test "$SENDSMS" = "YES"
   then
      SMSPASS=FREE
      SMSNAME=/${envir}${cyc}/firewx${cyc}/jfirewx_gempak_${cyc}
      $SMSBIN/smsinit
      $SMSBIN/endt
      SMSPASS=FREE
      SMSNAME=/${envir}${cyc}/firewx${cyc}/jfirewx_postsnd_${cyc}
      $SMSBIN/smsinit
      $SMSBIN/endt
      for fhr in 48 45 42 39 36 33 30 27 24 21 18 15 12 09 06 03 00
         do
         SMSNAME=/${envir}${cyc}/firewx${cyc}/post/jfirewx_post_f${fhr}_${cyc}
         $SMSBIN/smsinit
         $SMSBIN/endt
      done
      SMSNAME=/${envir}${cyc}/firewx${cyc}/post/jfirewx_post_manager_${cyc}
      $SMSBIN/smsinit
      $SMSBIN/endt
      SMSNAME=/${envir}${cyc}/firewx${cyc}/jfirewx_forecast_${cyc}
      $SMSBIN/smsinit
      $SMSBIN/endt
      exit
   fi
fi

echo $NEST > nestname
echo "export NEST=$NEST" > $COMOUT/${RUN}.t${cyc}z.envir.sh

export CYCLE=$PDY$cyc
echo "export CYCLE=$CYCLE" >> $COMOUT/${RUN}.t${cyc}z.envir.sh

#
# Get the Meso Nam initial condition restart file
#
if [ -s ${GESDIR}/nam.t${cyc}z.restrt00.init ]
then
  cp ${GESDIR}/nam.t${cyc}z.restrt00.init restrt00.init
else
  err_exit "INITIAL condition file not found.  FATAL ERROR"
fi

#
# Get the Meso Nam constants file
#
if [ -s ${COMNAM}/nam.t${cyc}z.nhb1260.tm00 ]
then
  cp ${COMNAM}/nam.t${cyc}z.nhb1260.tm00 nhb1260
else
  err_exit "CONSTANTS file not found.  FATAL ERROR"
fi

$utilscript/getges.sh -t sstgrb -v $CYCLE sstgrb

export pgm=grbindex
$utilexec/grbindex sstgrb sstgrb.index
export err=$?
if [ $err -ne 0 ]
then
  msg="Problem with $pgm on file sstgrb"
  postmsg "$jlogfile" "$msg"
  err_chk
fi

$utilscript/getsnow_nam.sh $CYCLE 
export err=$?
if [ $err -ne 0 ]
then
  err_exit "SNOW grib files not found.  FATAL ERROR"
fi
for SSTPDY in $PDY $PDYm1 $PDYm2 $PDYm3 $PDYm4
do
  if [ -s /com/gfs/prod/sst.${SSTPDY}/sst2dvar.t12z.nam_grid ]
  then
    cp /com/gfs/prod/sst.${SSTPDY}/sst2dvar.t12z.nam_grid sst14.t12z.nam_grid
    break
  fi
done
if [ $err -ne 0 ]
then
  msg="No NAM SST file was found."
  postmsg "$jlogfile" "$msg"
fi

cp $FIXnam/nam_deta_ldt1.60_ieee32 nam_deta_ldt1.60_ieee32_1

export pgm=firewx_intrst_${NEST}
. prep_step
export XLFUNIT_11=$DATA/restrt00.init            
export XLFUNIT_15=$PARMfirewx/firewx_econdata.${NEST}.parm
export XLFUNIT_16=$FIXnam/nam_deta_ldt1.60_ieee32
export XLFUNIT_17=$FIXnam/nam_deta_ldt1.60.25mb_bot40m
export XLFUNIT_18=$FIXnam/nam_hgt_stdh_sm_${NEST}
export XLFUNIT_19=$FIXnam/nam_hgtsmref.r1260.tmasks 
export XLFUNIT_20=$FIXnam/nam_maxsnoalb     
export XLFUNIT_21=$FIXnam/nam_alb100     
export XLFUNIT_22=$FIXnam/nam_alb200     
export XLFUNIT_23=$FIXnam/nam_alb300     
export XLFUNIT_24=$FIXnam/nam_alb400     
export XLFUNIT_30=$FIXnam/nam_ivgtyp_1d  
export XLFUNIT_31=$FIXnam/nam_isltyp_1d  
export XLFUNIT_32=$FIXnam/nam_islope_1d  
export XLFUNIT_34=$DATA/nhb1260             
export XLFUNIT_38=$FIXnam/nam_veg.nam.grb
export XLFUNIT_39=$DATA/sstgrb           
export XLFUNIT_40=$DATA/sstgrb.index     
export XLFUNIT_41=$DATA/snowdepth.grb    
export XLFUNIT_42=$FIXnam/nam_rfusaflw 
export XLFUNIT_43=$FIXnam/nam_imsmask  
export XLFUNIT_44=$DATA/sst14.t12z.nam_grid   
export XLFUNIT_45=$DATA/imssnow.grb 
export XLFUNIT_51=$DATA/restrt00.meso
export XLFUNIT_53=$DATA/nfcmeso
export XLFUNIT_54=$DATA/nhbmeso
export XLFUNIT_55=$DATA/nbcmeso
export XLFUNIT_99=$DATA/stcice
rm fort.*
ln -s -f $FIXnam/nam_veg.nam.grb   fort.38
ln -s -f sstgrb                    fort.39
ln -s -f sstgrb.index              fort.40
ln -s -f snowdepth.grb             fort.41
ln -s -f imssnow.grb               fort.45

startmsg
$EXECfirewx/firewx_intrst_${NEST} < $PARMnam/nam_modtop.parm >> $pgmout 2>errfile
export err=$?;err_chk

mv restrt00.meso $COMOUT/${RUN}.t${cyc}z.restrt00.meso
mv nhbmeso $COMOUT/${RUN}.t${cyc}z.nhbmeso
mv nfcmeso $COMOUT/${RUN}.t${cyc}z.nfcmeso
###mv bcnest $COMOUT/${RUN}.t${cyc}z.bcnest

cp $FIXfirewx/firewx_${NEST}.gif $COMOUT/firewx_${NEST}.gif.t${cyc}z
#Send gif containing a graphic of the chosen nest 

if test $SENDDBN = 'YES'
then
    $DBNROOT/bin/dbn_alert MODEL NAM_FIREWX_GIF $job $COMOUT/firewx_${NEST}.gif.t${cyc}z
fi


msg="JOB $job FOR NEST=$NEST HAS COMPLETED NORMALLY."
postmsg "$jlogfile" "$msg"
