
# This script has two functions:
#  1) Generates synthetic cyclone bogus near tropical storms and appends them
#     to a PREPBUFR file (based on script variable DO_BOGUS).  If may also,
#     based on user-requested switch, flag mass pressure reports "near"
#     tropical storms.
#  2) Flag dropwinsonde wind reports "near" tropical storms (based on user-
#     requested switch).
#
#  Note: It can do both 1 and 2 above or just one of them without the other.
#   
#  (NOTE: SYNDATA is currently restricted to run with T126 gaussian
#         land-sea mask)
#
# It is normally executed by the script prepobs_makeprepbufr.sh
#  but can also be executed from a checkout parent script
# -------------------------------------------------------------

set -aux

# Positional parameters passed in:
#   1 - path to COPY OF input prepbufr file --> becomes output prepbufr
#       file upon successful completion of this script (note that input
#       prepbufr file is NOT saved by this script)
#   2 - path to COPY OF input tcvitals file
#   3 - expected center date in PREPBUFR file (YYYYMMDDHH)


# Imported variables that must be passed in:
#   DATA  - path to working directory
#   SGES  - path to COPY OF global simga first guess file 1 (valid at
#            either center date of PREPBUFR file or nearest cycle time prior
#            to center date of PREPBUFR file which is a multiple of 3)
#   SGESA - path to COPY OF global simga first guess file 2 (either
#            null if SGES is valid at center date of PREPBUFR file or valid
#            at nearest cycle time after center date of PREPBUFR file which
#            is a multiple of 3 if SGES is valid at nearest cycle time
#            prior to center date of PREPBUFR file which is a multiple of 3)
#   PRVT  - path to observation error table file
#   FIXSYND - path to synthethic data fixed field files
#   SYNDX   - path to SYNDAT_SYNDATA program executable
#   SYNDC   - path to SYNDAT_SYNDATA program parm cards

# Imported variables that can be passed in:
#   DO_BOGUS - Generate synthetic cyclone bogus near tropical storms and
#              append them to a PREPBUFR file (and also, based on user-
#              requested switch, flag mass pressure reports "near" tropical
#              storms)?  (choices are "YES" or "NO", anything else defaults to
#              "YES", including if this is not passed in)
#   jlogfile - string indicating path to joblog file (skipped over by this
#              script if not passed in)
#   pgmout   - string indicating path to for standard output file (skipped
#              over by this script if not passed in)


cd $DATA
PRPI=$1
if [ ! -s $PRPI ] ; then exit 1 ;fi
VITL=$2
CDATE10=$3
if [ ! -s $VITL ] ; then
   msg="TCVITALS EMPTY - NO PROCESSING PERFORMED BY SYNDAT_SYNDATA for \
$CDATE10  --> non-fatal"
   set +x
   echo
   echo "$msg"
   echo
   set -x
   set +u
   [ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
   set -u

   exit
fi

if [ $DO_BOGUS = 'YES' ]; then
   suffix_char=""
else
   suffix_char="_nobog"
fi

rm $PRPI.syndata bogdomn.wrk${suffix_char} alldat${suffix_char}
rm stmtrk.wrk${suffix_char} rawdat.wrk${suffix_char} dumcoef${suffix_char}
rm matcoef${suffix_char} dthistry${suffix_char} bogrept${suffix_char}
rm bogdata${suffix_char} fenvdta.wrk${suffix_char} stkdatb.wrk${suffix_char}
rm gesvit${suffix_char} bghistry.diag${suffix_char}
rm prevents.filtering.syndata${suffix_char}

pgm=`basename  $SYNDX`
if [ -s $DATA/prep_step ]; then
   . $DATA/prep_step
else
   [ -f errfile ] && rm errfile
   export XLFUNITS=0
   unset `env | grep XLFUNIT | awk -F= '{print $1}'`

   set +u
   if [ -z "$XLFRTEOPTS" ]; then
     export XLFRTEOPTS="unit_vars=yes"
   else
     export XLFRTEOPTS="${XLFRTEOPTS}:unit_vars=yes"
   fi
   set -u

fi

export XLFUNIT_11=$VITL
echo "      $CDATE10" > cdate10.dat
export XLFUNIT_13=cdate10.dat
export XLFUNIT_14=$FIXSYND/syndat_slmask.t126.gaussian
export XLFUNIT_15=bogdomn.wrk${suffix_char}
export XLFUNIT_16=stmtrk.wrk${suffix_char}
export XLFUNIT_17=rawdat.wrk${suffix_char}
export XLFUNIT_19=bghistry.diag${suffix_char}
export XLFUNIT_21=gesvit${suffix_char}
export XLFUNIT_22=stkdatb.wrk${suffix_char}
export XLFUNIT_23=fenvdta.wrk${suffix_char}
export XLFUNIT_24=bogdata${suffix_char}
export XLFUNIT_25=$PRPI
#####export XLFUNIT_30=$SGES
#####export XLFUNIT_31=$SGESA

# The SYNDAT_SYNDATA code will soon, or may now, open GFS spectral coefficient
# guess files using sigio routines (via W3LIB rouitne GBLEVENTS) via explicit
# open(unit=number,file=filename) statements.  This conflicts with the XLFUNIT
# statements above.  One can either remove the explicit open  statements in the
# code or replace the above XLFUNIT lines with soft links.  The soft link
# approach is taken below.

ln -sf $SGES              fort.30
ln -sf $SGESA             fort.31
export XLFUNIT_32=$PRVT
export XLFUNIT_40=$FIXSYND/syndat_weight
export XLFUNIT_58=bogrept${suffix_char}
export XLFUNIT_59=dthistry${suffix_char}
export XLFUNIT_61=$PRPI.syndata
export XLFUNIT_70=matcoef${suffix_char}
export XLFUNIT_71=dumcoef${suffix_char}
export XLFUNIT_72=rawdat.wrk${suffix_char}
export XLFUNIT_73=stmtrk.wrk${suffix_char}
export XLFUNIT_74=alldat${suffix_char}
export XLFUNIT_80=prevents.filtering.syndata${suffix_char}
export XLFUNIT_89=bogdomn.wrk${suffix_char}

set +u
if [ -z "$XLFRTEOPTS" ]; then
   export XLFRTEOPTS="nlwidth=132"
else
   export XLFRTEOPTS="${XLFRTEOPTS}:nlwidth=132"
fi
set -u

#This causes a failure
####XLSMPOPTS=parthds=2:stack=64000000
#This works!
XLSMPOPTS=parthds=2:stack=20000000

TIMEIT=""
[ -s $DATA/timex ] && TIMEIT=$DATA/timex
$TIMEIT $SYNDX < $SYNDC > outout  2> errfile
err=$?
###cat errfile
cat errfile >> outout
[ $DO_BOGUS = 'YES' ]  &&  cat prevents.filtering.syndata >> outout
cat outout >> syndata.out
set +u
[ -n "$pgmout" ]  &&  cat outout >> $pgmout
set -u
rm outout
set +x
echo
echo 'The foreground exit status for SYNDAT_SYNDATA is ' $err
echo
set -x
if [ $err -eq 0 ]; then

   set +x
   echo " --------------------------------------------- "
   echo " ********** COMPLETED PROGRAM $pgm  **********"
   echo " --------------------------------------------- "
   set -x
   msg="$pgm completed normally for $CDATE10 - DO_BOGUS= $DO_BOGUS"
   set +u
   [ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
   set -u
   mv $PRPI.syndata $PRPI

else

msg="SYNDAT_SYNDATA TERMINATED ABNORMALLY WITH CONDITION CODE $err \
--> non-fatal"
   set +x
   echo
   echo "$msg"
   echo
   set -x
   set +u
   [ -n "$jlogfile" ] && $DATA/postmsg "$jlogfile" "$msg"
   set -u

fi

exit 0
