#!/bin/ksh
######################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exhiresw_wrfbufr.sh
# Script description:  Trigger hiresw sounding post job
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script triggers the hiresw sounding post job, which
#           creates a piece of the model sounding profile whose
#           time interval is determined by the input forecast hours.
#
# Script history log:
# 2000-05-16  Eric Rogers
# 2006-01-20  Eric Rogers -- extended to 84-h and modified for WRF-NMM NAM
# 2009-12-18  Matthew Pyle -- shortened to 48-h and generalized for multiple domains
#                             and diferent dynamical cores
#
set -x

mkdir -p $DATA/bufrpost
cd $DATA/bufrpost

export tmmark=tm00
. $COMIN/${RUN}.t${cyc}z.envir.sh

cp $FIXhiresw/hiresw_${RUN}_profdat hiresw_profdat

OUTTYP=binary

if [ $MODEL == "nmm" ]
then
model=NCEP
else
model=NCAR
fi

echo here with model $model

INCR=01

let NFILE=1

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

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

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

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

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`

echo $STARTDATE
echo $FINALDATE

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 [ $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`

let fhrold="$fhr - 1"
dateold=`/nwprod/util/exec/ndate $fhrold $CYCLE`

oyr=`echo $dateold | cut -c1-4`
omn=`echo $dateold | cut -c5-6`
ody=`echo $dateold | cut -c7-8`
ohr=`echo $dateold | cut -c9-10`

timeform=${wyr}"-"${wmn}"-"${wdy}"_"${whr}":00:00"
timeformold=${oyr}"-"${omn}"-"${ody}"_"${ohr}":00:00"

OUTFIL=$DATA/wrfout_d01_${timeform}
OLDOUTFIL=$DATA/wrfout_d01_${timeformold}

icnt=1

# wait for model restart file
while [ $icnt -lt 1000 ]
do
   if [ -s $DATA/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 ${RUN} FCST F${fhr} to end."
    err_exit $msg
fi
done


cat > itag <<EOF
$OUTFIL
$model
$OUTTYP
$STARTDATE
$NFILE
$INCR
$fhr
$OLDOUTFIL
EOF
 
export pgm=hiresw_wrfbufr
. prep_step
export XLFUNIT_19=$DATA/bufrpost/hiresw_profdat
export XLFUNIT_79=$DATA/bufrpost/profilm.c1.${tmmark}

startmsg

hpmcount $EXEChiresw/hiresw_wrfbufr < itag >> $pgmout 2>errfile 
export err=$?;err_chk

mv $DATA/bufrpost/profilm.c1.${tmmark} $DATA/profilm.c1.${tmmark}.f${fhr}
echo done > $DATA/sndpostdone${fhr}.${tmmark}

cat $DATA/profilm.c1.${tmmark}  $DATA/profilm.c1.${tmmark}.f${fhr} > $DATA/profilm_int
mv $DATA/profilm_int $DATA/profilm.c1.${tmmark}

fhr=`expr $fhr + $INCR`


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

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

done

cd $DATA

########################################################
############### SNDP code
########################################################

export pgm=hiresw_sndp_${RUN}
. prep_step


cp $PARMhiresw/hiresw_sndp.parm.mono $DATA/hiresw_sndp.parm.mono
cp $PARMhiresw/hiresw_bufr.tbl $DATA/hiresw_bufr.tbl

export XLFUNIT_11=$DATA/hiresw_sndp.parm.mono
export XLFUNIT_32=$DATA/hiresw_bufr.tbl
export XLFUNIT_66=$DATA/profilm.c1.${tmmark}
export XLFUNIT_78=$DATA/class1.bufr

startmsg
hpmcount $EXEChiresw/hiresw_sndp_${MODEL} < $PARMhiresw/hiresw_modtop.parm >> $pgmout 2>errfile
export err=$?;err_chk

############### put into gempak (eliminate when RFC)
############### end put into gempak (eliminate when RFC)

if [ $SENDCOM == "YES" ]
then
cp $DATA/class1.bufr $COMOUT/${RUN}.t${cyc}z.class1.bufr.${tmmark}
cp $DATA/profilm.c1.${tmmark} ${COMOUT}/${RUN}.t${cyc}z.profilm.c1.${tmmark}
fi

# Make an upper case version of the ${RUN} variable for the alert
export DBN_NEST=`echo ${RUN} | tr '[a-z]' '[A-Z]'`

if [ $SENDDBN == "YES" ]
then
  $DBNROOT/bin/dbn_alert MODEL NAM_${DBN_NEST}_BUFR $job $COMOUT/${RUN}.t${cyc}z.class1.bufr.${tmmark}
fi

echo EXITING $0 with return code $err
exit $err
