#! /bin/ksh

################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exhiresw_post.sh.sms
# Script description:  Runs WRF-NMM or WRF-EM post-processor
#
# Author:        Eric Rogers       Org: NP22         Date: 2004-07-07
#
# Script history log:
#
# 2003-11-01  Matt Pyle - Original script for parallel
#
# 2004-07-07  Eric Rogers - Preliminary modifications for production.
#
# 2007-04-30  Matthew Pyle - Changed to a "one size fits all" version of the WRF post.
#                             Eliminated inline call of prdgen job.
#
# 2009-09-24  Pyle/Cokley  - Reduces potential for conflict with other jobs by:
#
#                            1) doing work in a "post" subdirectory
#                            2) eliminating write of "lower" file
#                            3) Extracting date information from PDY rather than
#                               copying date file
#
#                            Also, cleaned out some unused items related to prdgen to
#                            streamline and prevent confusion.

set -x
export MEMBER=ctl

msg="JOB $job FOR WRF NEST=${NEST}${MODEL}_${MEMBER} HAS BEGUN"
postmsg "$jlogfile" "$msg"


######
export INCR=01
######

mkdir -p $DATA/post
cd $DATA/post

cp $PARMhiresw/hiresw_micro_lookup.dat .
cp hiresw_micro_lookup.dat eta_micro_lookup.dat

rm ${DATA}/postdone*

#
# Get needed variables from exhiresw_prelim.sh.sms
#

. $COMIN/${RUN}.t${cyc}z.envir.sh

OUTTYP=binarympiio

# echo $MODEL > lower
# model=`cat lower | tr '[a-z]' '[A-Z]'`

model=`echo $MODEL | tr '[a-z]' '[A-Z]'`

filedir=$DATA

YYYY=`echo $PDY | cut -c1-4`
MM=`echo $PDY | cut -c5-6`
DD=`echo $PDY | cut -c7-8`
CYCLE=$PDY$cyc

startd=$YYYY$MM$DD
startdate=$CYCLE

STARTDATE=${YYYY}-${MM}-${DD}_${cyc}:00:00

endtime=`/nwprod/util/exec/ndate 48 $CYCLE`

YYYY=`echo $endtime | cut -c1-4`
MM=`echo $endtime | cut -c5-6`
DD=`echo $endtime | cut -c7-8`

FINALDATE=${YYYY}-${MM}-${DD}_${cyc}:00:00

export tmmark=tm00

wyr=`echo $STARTDATE | cut -c1-4`
wmn=`echo $STARTDATE | cut -c6-7`
wdy=`echo $STARTDATE | cut -c9-10`
whr=`echo $STARTDATE | cut -c12-13`

eyr=`echo $FINALDATE | cut -c1-4`
emn=`echo $FINALDATE | cut -c6-7`
edy=`echo $FINALDATE | cut -c9-10`
ehr=`echo $FINALDATE | cut -c12-13`

edate=$eyr$emn$edy$ehr

# If job needs to be restarted due to production failure, reset
# wdate=valid date of first available WRF restart file

wdate=$wyr$wmn$wdy$whr

timeform=$STARTDATE

if [ $MODEL = nmm ] ; then
   cp $PARMhiresw/hiresw_nmm_cntrl.parm wrf_cntrl.parm
else
   cp $PARMhiresw/hiresw_arw_cntrl.parm wrf_cntrl.parm
fi

if [ $wdate -eq $startdate ] ; then
  fhr=00
else
  fhr=`/nwprod/util/exec/nhour $wdate $startdate`
fi

export fhr

while [ $wdate -le $edate ]
do

date=`/nwprod/util/exec/ndate $fhr $CYCLE`

wyr=`echo $date | cut -c1-4`
wmn=`echo $date | cut -c5-6`
wdy=`echo $date | cut -c7-8`
whr=`echo $date | cut -c9-10`

timeform=${wyr}"-"${wmn}"-"${wdy}"_"${whr}":00:00"

icnt=1
                                                                                                                   
# wait for model restart file
while [ $icnt -lt 1000 ]
do
   if [ -s $filedir/fcstdone${fhr}.${tmmark} ]
   then
      break
   else
      icnt=$((icnt + 1))
      sleep 9
   fi
if [ $icnt -ge 200 ]
then
    msg="ABORTING after 30 minutes of waiting for HIRESW FCST F${fhr} to end."
    err_exit $msg
fi
done

if [ $MODEL = nmm ] ; then 

cat > itag <<EOF
$filedir/wrfout_d01_${timeform}
$OUTTYP
$timeform
$model
EOF

else

export POSTMOD=NCAR

cat > itag <<EOF
$filedir/wrfout_d01_${timeform}
$OUTTYP
$timeform
$POSTMOD
EOF

fi
#-----------------------------------------------------------------------
#   Run wrf post.


# sync the wrfout file before executing post code

/nwprod/util/exec/fsync_file $filedir/wrfout_d01_${timeform}

export pgm=hiresw_${MODEL}_post
. prep_step

export XLFUNIT_14=wrf_cntrl.parm 

$EXEChiresw/hiresw_post < itag > $pgmout 2>errfile
# $EXEChiresw/hiresw_post_ops < itag > $pgmout 2>errfile

export err=$?;err_chk

cp $pgmout ${pgmout}_${fhr}
cp errfile errfile_${fhr}

echo "done executing the post" > $DATA/postdone${fhr}
postmsg "$jlogfile" "HIRESW ${NEST}${MODEL}_${MEMBER} POST done for F${fhr}"

fhr=`expr $fhr + $INCR`

if [ $fhr -lt 10 ]
then
fhr=0$fhr
fi

wdate=`/nwprod/util/exec/ndate ${fhr} $CYCLE`

done

echo EXITING $0
exit
