#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exndas_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 NDAS run
#           and runs preliminary codes for GSI first guess 
#
# Script description:  Runs WRF-NMM surface update codes for SST
#                      snow, sea-ice, invoke partial cycling processing if needed.
#
# 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
# 2004-10-14  Julia Zhu	 - NAM/EDAS name changes to NAM/NDAS
# 2006-01-12  Eric Rogers - Extensive changes for WRF-NMM
# 2008-08-14  Eric Rogers - Changed so that coldstart (partial cycling) is 
#                           always invoked

set -x

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

export USHGETGFSBC=${USHGETGFSBC:-/nwprod/util/ush}

cd $DATA

export DOMAIN=nam

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

TM12=`/nwprod/util/exec/ndate -12 $CYCLE`
TM09=`/nwprod/util/exec/ndate -09 $CYCLE`
TM06=`/nwprod/util/exec/ndate -06 $CYCLE`
TM03=`/nwprod/util/exec/ndate -03 $CYCLE`

echo $CYCLE > date
yyyymmdd=`cut -c 1-8 date`
hh=`cut -c 9-10 date`

echo $TM12 > datetm12
yyyymmddm12=`cut -c 1-8 datetm12`
hh12=`cut -c 9-10 datetm12`
SDATE=`cut -c 1-10 datetm12`
yyyymmddm09=`echo $TM09 | cut -c 1-8`
yyyymmddm06=`echo $TM06 | cut -c 1-8`
yyyymmddm03=`echo $TM03 | cut -c 1-8`

cp /com/date/t${hh12}z nmcdate.tm12

#
# Find the best ndas restart file from a previous run, if none available
# look for a partial restart file in /nwges/prod/ndas.hold.  Otherwise do a
# cold start with the Gdas.
#
# 8/2008 : For Fall 2008 NAM implementation: Leave script in place for full cycling 
# if we ever want to reinstate it, but disable copy of wrfrst file so partial
# cycling coldstart is always invoked
#
case $cyc in
 00) ndas1=$GESDIR/ndas.${yyyymmddm06}/ndas.t18z.input_nemsio_d01.tm09
     ndas2=$GESDIR/ndas.${yyyymmddm12}/ndas.t12z.input_nemsio_d01.tm03;;
 06) ndas1=$GESDIR/ndas.${yyyymmddm06}/ndas.t00z.input_nemsio_d01.tm09
     ndas2=$GESDIR/ndas.${yyyymmddm12}/ndas.t18z.input_nemsio_d01.tm03;;
 12) ndas1=$GESDIR/ndas.${yyyymmddm06}/ndas.t06z.input_nemsio_d01.tm09
     ndas2=$GESDIR/ndas.${yyyymmddm12}/ndas.t00z.input_nemsio_d01.tm03;;
 18) ndas1=$GESDIR/ndas.${yyyymmddm06}/ndas.t12z.input_nemsio_d01.tm09
     ndas2=$GESDIR/ndas.${yyyymmddm12}/ndas.t06z.input_nemsio_d01.tm03;;
esac

##if [ -s $ndas1 ]
##then
##  cp $ndas1 wrfrst_d01
##else
##  if [ -s $ndas2 ]
##  then
##    cp $ndas2 wrfrst_d01
##  fi
##fi

if [ -s wrfrst_d01 ]
then
  export GUESS=NAM
  echo "export GUESS=NAM" >> $GESDIR/ndas.t${cyc}z.envir.sh
else
#
#  No valid NDAS files found on system.  Try to find the last resort
#  restart file in /nwges/prod/ndas.hold.  This will allow cycling of
#  soil/cloud water/TKE.  Use GDAS files for basic state first guess.
#
#  8/2008 : No need to copy wrfrst file here, it is read in the exndas_coldstart job
#
  if [ -s $GESDIR/ndas.hold/nmm_b_restart_nemsio_hold.${hh12}z ]
  then
     export GUESS=GDAS
     echo "export GUESS=GDAS" >> $GESDIR/ndas.t${cyc}z.envir.sh
  else
#
#   No NDAS restart files found.  Do a cold start from GDAS first 
#   guess files.
#
    export GUESS=GDAS
    echo "export GUESS=GDAS" >> $GESDIR/ndas.t${cyc}z.envir.sh
  fi
fi

# Send message to ndaslog file w/date and origin of first guess

msg="$CYCLE $GUESS"
postmsg "$jlogfile" "$msg"

# GET GDAS sigma coeff files for boundary conditions
export envir_getges=prod

$USHGETGFSBC/getgfsbc_nam.sh $TM12 06 $envir_getges tm12
$USHGETGFSBC/getgfsbc_nam.sh $TM09 06 $envir_getges tm09
$USHGETGFSBC/getgfsbc_nam.sh $TM06 06 $envir_getges tm06
$USHGETGFSBC/getgfsbc_nam.sh $TM03 06 $envir_getges tm03
####$utilscript/getgfsbc_nam.sh $CYCLE 87 $envir_getges tm00

#
# Release the 4 NDAS mkbnd jobs on SMS
#

mkdir -p $DATA/mkbnd.tm12
mkdir -p $DATA/mkbnd.tm09
mkdir -p $DATA/mkbnd.tm06
mkdir -p $DATA/mkbnd.tm03
###mkdir -p $DATA/mkbnd.tm00

$SMSBIN/setev release_mkbnd.tm12
$SMSBIN/setev release_mkbnd.tm09
$SMSBIN/setev release_mkbnd.tm06
$SMSBIN/setev release_mkbnd.tm03
###$SMSBIN/setev release_mkbnd.tm00

if [ $GUESS = GDAS ] ; then

# release coldstart scripts and exit this script

# 1) Run GFS post to get GDAS 1st guess GRIB output to 1 mb
# 2) Run WPS on GDAS GRIB files from step 1
# 3) Run parallel real to get GDAS-based wrfinput file; run partial cycling code to get
#    best available land states from cycled NDAS wrfrst file
# 4) Run sfcupdate script to get latest sst,snow,sea-ice analyses
# 5) Run GSI analysis
# 6) Run WRF_NMM forecast

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

[[ $cyc = 00 ]] && prevcyc=18
[[ $cyc = 06 ]] && prevcyc=00
[[ $cyc = 12 ]] && prevcyc=06
[[ $cyc = 18 ]] && prevcyc=12

NETNAME="/$envir$prevcyc/ndas$cyc"
echo "force -r queued $NETNAME/main/coldstart" >>cdp_cmds
echo "exit" >>cdp_cmds

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

$SMSBIN/setev release_ndas_coldstart

else

# Echo coldstart.done file to indicate no coldstart
  echo "DONE" >$DATA/coldstart.done

#end GUESS=GDAS test
fi

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