#!/bin/ksh
######################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exfirewx_fcst.sh.sms
# Script description:  Run nam forecast
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script runs the NMM firewx forecast
#
# Script history log:
# 1999-06-23  Eric Rogers
# 1999-08-02  Brent Gordon  - Modified for production.
# 2001-04-19  Eric Rogers modified Eta script for hiresw runs
# 2002-03-15  Eric Rogers modified script for 8/4 km hires window model
# 2003-03-10  Eric Rogers modified script for firewx runs
#

set -xa

export PS4='$SECONDS + '

cd $DATA
FIXhiresw=/nwprod/fix

#
# Get needed variables from exndas_prelim.sh.sms
#
. $COMIN/${RUN}.t${cyc}z.envir.sh
#
# Determine the size of the model execuitble
#
export SERVER_GROUPS=1
###export SERVER_GROUPS=3

### Add by Julia
rst_hh=00

if [ $rst_hh -lt 12 ]
then
### End by Julia

# Concatenate namlog to see if GDAS or NDAS first guess were used
# in job 240

cat /com/nam/prod/namlog | grep NAM | grep ${PDY}${cyc}
err1=$?

if [ $err1 -eq 0 ] ; then
NAMGUESS=NDAS
else
NAMGUESS=GDAS
fi

if [ $NAMGUESS = NDAS ] 
then
    cp $PARMfirewx/firewx_fcstdata.parm_0012h fcstdata.parm
else
    cp $PARMfirewx/firewx_fcstdata.parm_0012h_cold fcstdata.parm
fi

cp $PARMnam/nam_template.tm00.parm template
cp $FIXnam/nam_micro_lookup.dat micro_lookup.dat

if [ $NAMGUESS = NDAS ]
then
  if [ -s $COMIN/${RUN}.t${cyc}z.restrt00.meso ]
  then
     ln -s -f $COMIN/${RUN}.t${cyc}z.restrt00.meso restrt03
  else
    err_exit "RESTART file not found.  FATAL ERROR"
  fi
else
  if [ -s $COMIN/${RUN}.t${cyc}z.nfcmeso ]
  then
     ln -s -f $COMIN/${RUN}.t${cyc}z.nfcmeso restrt03
  else
    err_exit "RESTART file not found.  FATAL ERROR"
  fi
fi

if [ -s $COMIN/${RUN}.t${cyc}z.nhbmeso ]
then
   ln -s -f $COMIN/${RUN}.t${cyc}z.nhbmeso nhbmeso
else
  err_exit "nhbmeso file not found.  FATAL ERROR"
fi

ic=1
while [ $ic -lt 1000 ] 
do
   if [ -s $DATANAM/post0fw.done13.tm00 ]
   then
      break
   else
      let "ic=ic+1"
      sleep 10
   fi

   if [ $ic -ge 180 ]
   then
      err_exit "COULD NOT LOCATE: $DATANAM/post0fw.done13.tm00"
   fi
done

if [ -s ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f13 ]
then
  cp ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f13 bcnest
else
  err_exit "BOUNDARY conditions file not found.  FATAL ERROR"
fi
#
# Next line is for the threaded version of the nam_namfcst module
#
export XLSMPOPTS="parthds=1:spins=0:yields=0:schedule=guided"
#
#  00-12 h piece
#
export pgm=firewx_fcst
. prep_step
export XLFUNIT_11=$DATA/fcstdata.parm
export XLFUNIT_12=$DATA/nhbmeso
export XLFUNIT_13=$DATA/restrt03
export XLFUNIT_14=$FIXhiresw/hiresw_co2.60_hyb_bot40m
export XLFUNIT_16=bcnest
export XLFUNIT_21=template
export XLFUNIT_22=$FIXfirewx/firewx_z0eff_$NEST
export XLFUNIT_59=$PARMnam/nam_soil_veg_namelist
export XLFUNIT_80=twrite16_360.tm00
export XLFUNIT_92=gsums.tm00
export MP_SHARED_MEMORY=yes

startmsg
$EXECfirewx/firewx_fcst >> $pgmout 2>errfile
export err=$?

if [ $err -ne 1 ]
then
   err_chk
else
   msg="$pgm 00-12 h piece completed normally"
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
   rst_hh=12
fi

### Add by Julia for 00-12 hours
fi
### End by Julia for 00-12 hours

### Add by Julia
if [ $rst_hh -lt 24 ]
then 
### End by Julia

# 12-24 h piece

cp $PARMfirewx/firewx_fcstdata.parm_1224h fcstdata.parm

ic=1
while [ $ic -lt 1000 ] 
do
   if [ -s $DATANAM/post0fw.done25.tm00 ]
   then
      break
   else
      let "ic=ic+1"
      sleep 10
   fi

   if [ $ic -ge 180 ]
   then
      err_exit "COULD NOT LOCATE: $DATANAM/post0fw.done25.tm00"
   fi
done

if [ -s ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f25 ]
then
  rm bcnest
  cp ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f25 bcnest
else
  err_exit "BOUNDARY conditions file not found.  FATAL ERROR"
fi

export pgm=firewx_fcst
. prep_step
export XLFUNIT_11=$DATA/fcstdata.parm
export XLFUNIT_12=$DATA/nhbmeso
export XLFUNIT_13=$DATA/restrt12.tm00
export XLFUNIT_14=$FIXhiresw/hiresw_co2.60_hyb_bot40m
export XLFUNIT_16=bcnest
export XLFUNIT_21=template
export XLFUNIT_22=$FIXfirewx/firewx_z0eff_$NEST
export XLFUNIT_59=$PARMnam/nam_soil_veg_namelist
export XLFUNIT_80=twrite16_360.tm00
export XLFUNIT_92=gsums.tm00
export MP_SHARED_MEMORY=yes

startmsg
$EXECfirewx/firewx_fcst >> $pgmout 2>errfile
export err=$?

if [ $err -ne 1 ]
then
   err_chk
else
   msg="$pgm 12-24 h piece completed normally"
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
   rst_hh=24
fi

### Add by Julia for 12-24 hours
fi
### End by Julia for 12-24 hours

### Add by Julia
if [ $rst_hh -lt 36 ]
then
### End by Julia

# 24-36 h piece

cp $PARMfirewx/firewx_fcstdata.parm_2436h fcstdata.parm

ic=1
while [ $ic -lt 1000 ] 
do
   if [ -s $DATANAM/post0fw.done37.tm00 ]
   then
      break
   else
      let "ic=ic+1"
      sleep 10
   fi

   if [ $ic -ge 180 ]
   then
      err_exit "COULD NOT LOCATE: $DATANAM/post0fw.done37.tm00"
   fi
done

if [ -s ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f37 ]
then
  rm bcnest
  cp ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f37 bcnest
else
  err_exit "BOUNDARY conditions file not found.  FATAL ERROR"
fi

export pgm=firewx_fcst
. prep_step
export XLFUNIT_11=$DATA/fcstdata.parm
export XLFUNIT_12=$DATA/nhbmeso
export XLFUNIT_13=$DATA/restrt24.tm00
export XLFUNIT_14=$FIXhiresw/hiresw_co2.60_hyb_bot40m
export XLFUNIT_16=bcnest
export XLFUNIT_21=template
export XLFUNIT_22=$FIXfirewx/firewx_z0eff_$NEST
export XLFUNIT_59=$PARMnam/nam_soil_veg_namelist
export XLFUNIT_80=twrite16_360.tm00
export XLFUNIT_92=gsums.tm00
export MP_SHARED_MEMORY=yes

startmsg
$EXECfirewx/firewx_fcst >> $pgmout 2>errfile
export err=$?

if [ $err -ne 1 ]
then
   err_chk
else
   msg="$pgm 24-36 h piece completed normally"
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
   rst_hh=36
fi

### Add by Julia for 24-36 hours
fi
### End by Julia for 24-36 hours

### Add by Julia
if [ $rst_hh -lt 48 ]
then
### End by Julia
# 36-48 h piece

cp $PARMfirewx/firewx_fcstdata.parm_3648h fcstdata.parm

ic=1
while [ $ic -lt 1000 ] 
do
   if [ -s $DATANAM/post0fw.done48.tm00 ]
   then
      break
   else
      let "ic=ic+1"
      sleep 10
   fi

   if [ $ic -ge 180 ]
   then
      err_exit "COULD NOT LOCATE: $DATANAM/post0fw.done48.tm00"
   fi
done

if [ -s ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f48 ]
then
  rm bcnest
  cp ${COMNAM}/nam.t${cyc}z.bcnest.${NEST}.f48 bcnest
else
  err_exit "BOUNDARY conditions file not found.  FATAL ERROR"
fi

export pgm=firewx_fcst
. prep_step
export XLFUNIT_11=$DATA/fcstdata.parm
export XLFUNIT_12=$DATA/nhbmeso
export XLFUNIT_13=$DATA/restrt36.tm00
export XLFUNIT_14=$FIXhiresw/hiresw_co2.60_hyb_bot40m
export XLFUNIT_16=bcnest
export XLFUNIT_21=template
export XLFUNIT_22=$FIXfirewx/firewx_z0eff_$NEST
export XLFUNIT_59=$PARMnam/nam_soil_veg_namelist
export XLFUNIT_80=twrite16_360.tm00
export XLFUNIT_92=gsums.tm00
export MP_SHARED_MEMORY=yes

startmsg
$EXECfirewx/firewx_fcst >> $pgmout 2>errfile
export err=$?

if [ $err -ne 1 ]
then
   err_chk
else
   msg="$pgm 36-48 h piece completed normally"
   sh $utilscript/postmsg.sh "$jlogfile" "$msg"
fi

### Add by Julia for 36-48 hours
fi
### End by Julia for 36-48 hours
date
echo EXITING $0
