#####################################################################
echo "-----------------------------------------------------------"
echo "exnam_dump.sh.sms - NAM and NDAS model data dump processing"
echo "-----------------------------------------------------------"
echo "History: Jan 10 2000 - Original script."
#####################################################################

set -xau

# set some variables if they have not already been set

set +u

# JOB_NUMBER = 1 indicates the prepbufr dump job.
# JOB_NUMBER = 2 indicates the non-prepbufr dump job.
# JOB_NUMBER not present indicates dump BOTH prepbufr and non-prepbufr data.
# ------------------------------------------------------------------------
# Dump group #1 (non-pb) = 1bamua 1bhrs3 1bmhs gpsro mtiasi esamua eshrs3
# Dump group #2 (pb) = vadwnd satwnd
# Dump group #3 (pb) = proflr rassda sfcshp adpsfc wndsat ascatt
# Dump group #4 (pb) = msonet gpsipw
# Dump group #5 (pb) = aircft aircar 
# Dump group #6 (non-pb) = nexrad 
# Dump group #7 (non-pb) = goesfv 
# Dump group #8 (non-pb) = 1bamub 1bhrs4 airsev osbuv8 radwnd esamub esmhs
# Dump group #9 (pb) = adpupa
# Dump group #10 STATUS FILE
# ------------------------------------------------------------------------

#VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
# The settings below are based on a future change when the DUMP job will dump
#  only types that go into PREPBUFR and the DUMP2 job will dump only types that
#  do not go into PREPBUFR.  This will speed up the DUMP + PREP processing.
# Although the logic is in place to now do this (see below), for now we will
#  continue to dump only "nexrad" in the NAM and NDAS DUMP2 jobs via the
#  following switch settings ...
# -----------------------------------------------------------------------------

# To alert the nexrad files for ndas_dump2, set to yes in the Job script  
export SENDDBN_NDAS=${SENDDBN_NDAS:-NO}

if [ -n "$JOB_NUMBER" ]; then  
set -u
   if [ $JOB_NUMBER = 2 ]; then
      DUMP_group1="NO"
      DUMP_group7="NO"
      DUMP_group8="NO"
   else
      DUMP_group1="YES"
      DUMP_group7="YES"
      DUMP_group8="YES"
   fi
fi
set +u
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

if [ -n "$JOB_NUMBER" ]; then  
set -u
   if [ $JOB_NUMBER = 2 ]; then
      dump_ind=DUMP2
      DUMP_group1=${DUMP_group1:-"YES"}
      DUMP_group2=${DUMP_group2:-"NO"}
      DUMP_group3=${DUMP_group3:-"NO"}
      DUMP_group4=${DUMP_group4:-"NO"}
      DUMP_group5=${DUMP_group5:-"NO"}
      DUMP_group6=${DUMP_group6:-"YES"}
      DUMP_group7=${DUMP_group7:-"YES"}
      DUMP_group8=${DUMP_group8:-"YES"}
      DUMP_group9=${DUMP_group9:-"NO"}
   else
      dump_ind=DUMP
      DUMP_group1=${DUMP_group1:-"NO"}
      DUMP_group2=${DUMP_group2:-"YES"}
      DUMP_group3=${DUMP_group3:-"YES"}
      DUMP_group4=${DUMP_group4:-"YES"}
      DUMP_group5=${DUMP_group5:-"YES"}
      DUMP_group6=${DUMP_group6:-"NO"}
      DUMP_group7=${DUMP_group7:-"NO"}
      DUMP_group8=${DUMP_group8:-"NO"}
      DUMP_group9=${DUMP_group9:-"YES"}
   fi
else
   dump_ind=DUMP
   DUMP_group1=${DUMP_group1:-"YES"}
   DUMP_group2=${DUMP_group2:-"YES"}
   DUMP_group3=${DUMP_group3:-"YES"}
   DUMP_group4=${DUMP_group4:-"YES"}
   DUMP_group5=${DUMP_group5:-"YES"}
   DUMP_group6=${DUMP_group6:-"YES"}
   DUMP_group7=${DUMP_group7:-"YES"}
   DUMP_group8=${DUMP_group8:-"YES"}
   DUMP_group9=${DUMP_group9:-"YES"}
fi

if [ $RUN = nam ]; then
   ADPUPA_wait=${ADPUPA_wait:-"YES"}
########ADPUPA_wait=${ADPUPA_wait:-"NO"} # saves ~15 sec if ADPUPA_wait=NO
   CHECK_STATUS=${CHECK_STATUS:-"NO"}
else
   ADPUPA_wait=${ADPUPA_wait:-"NO"}
   CHECK_STATUS=${CHECK_STATUS:-"YES"}
fi


# send extra output of DUMP2 for monitoring purposes.
set +u
if [ -n "$JOB_NUMBER" ]; then  
   [ $JOB_NUMBER = 2 ]  && export PS4='$SECONDS + '
fi
set -u

# Make sure we are in the $DATA directory
cd $DATA

msg="HAS BEGUN on `hostname`"
$DATA/postmsg "$jlogfile" "$msg"
 
cat break > $pgmout

tmhr=`echo $tmmark|cut -c3-4`
export dumptime=`/nwprod/util/exec/ndate -$tmhr $PDY$cyc`
export cycp=`echo $dumptime|cut -c9-10`

RUN_uc=$(echo $RUN | tr [a-z] [A-Z])
tmmark_uc=$(echo $tmmark | tr [a-z] [A-Z])

msg="$RUN_uc ANALYSIS TIME IS $PDY$cyc"
$DATA/postmsg "$jlogfile" "$msg"

set +x
echo
echo "CENTER DATA DUMP DATE-TIME FOR $tmmark_uc $RUN_uc IS $dumptime"
echo
set -x

export COMSP=$COMOUT/$RUN.${cycle}.
 
if [ "$PROCESS_GRIBFLDS" = 'YES' ]; then

#################################################
#################################################
#  copy imssnow and snowdepth from $TANK_GRIBFLDS
#################################################
#################################################

   imssnow=$TANK_GRIBFLDS/$PDY/wgrbbul/imssnow.grb
   imssold=$TANK_GRIBFLDS/$PDYm1/wgrbbul/imssnow.grb

   if [ -s $imssnow ]; then
      cp $imssnow ${COMSP}imssnow.grb
      if [ $RUN = nam ]; then
         $EXGRBIX/cnvgrib -g12 -p40 ${COMSP}imssnow.grb \
          ${COMSP}imssnow.grb.grib2
         $EXGRBIX/wgrib2 ${COMSP}imssnow.grb.grib2 -s > \
          ${COMSP}imssnow.grb.grib2.idx
      fi
      msg="todays IMS snow grib file located and copied to /com"
      $DATA/postmsg "$jlogfile" "$msg"
      if [ "$SENDDBN" = 'YES' ]; then
         $DBNROOT/bin/dbn_alert MODEL NAMIMSSNOW $job ${COMSP}imssnow.grb
      fi
      if [ "$SENDDBN_GB2" = 'YES' ]; then
         $DBNROOT/bin/dbn_alert MODEL NAMIMSSNOW_GB2 $job \
          ${COMSP}imssnow.grb.grib2
         $DBNROOT/bin/dbn_alert MODEL NAMIMSSNOW_GB2_WIDX $job \
          ${COMSP}imssnow.grb.grib2.idx
      fi
   elif [ -s $imssold ]; then
      cp $imssold ${COMSP}imssnow.grb
      msg="**todays IMS snow grib file not located - copy 1-day old file"
      $DATA/postmsg "$jlogfile" "$msg"
      if [ "$SENDDBN" = 'YES' ]; then
         $DBNROOT/bin/dbn_alert MODEL NAMIMSSNOW $job ${COMSP}imssnow.grb
      fi
   else
      set +x
      echo " "
      echo " ##########################################"
      echo " cannot locate IMS snow grib file "
      echo " non-fatal error (but will cause failure in"
      echo " downstream analysis)"
      echo " ##########################################"
      echo " "
      set -x
      msg="**CANNOT LOCATE IMS SNOW GRIB FILE --> non-fatal"
      $DATA/postmsg "$jlogfile" "$msg"
   fi

   snowdepth=$TANK_GRIBFLDS/$PDY/wgrbbul/snowdepth.grb
   snowdeold=$TANK_GRIBFLDS/$PDYm1/wgrbbul/snowdepth.grb

   if [ -s $snowdepth ]; then
      cp $snowdepth ${COMSP}snowdepth.grb
      msg="todays snow depth grib file located and copied to /com"
      $DATA/postmsg "$jlogfile" "$msg"
   elif [ -s $snowdeold ]; then
      cp $snowdeold ${COMSP}snowdepth.grb
      msg="**todays snow depth grib file not located - copy 1-day old file"
      $DATA/postmsg "$jlogfile" "$msg"
   else
      set +x
      echo " "
      echo " ##########################################"
      echo " cannot locate snow depth grib file "
      echo " non-fatal error (but will cause failure in"
      echo " downstream analysis)"
      echo " ##########################################"
      echo " "
      set -x
      msg="**CANNOT LOCATE SNOW DEPTH GRIB FILE --> non-fatal"
      $DATA/postmsg "$jlogfile" "$msg"
   fi

#  endif loop $PROCESS_GRIBFLDS
fi


echo "=======> Dump group 1 (thread_1) not executed." > $DATA/1.out
echo "=======> Dump group 2 (thread_2) not executed." > $DATA/2.out
echo "=======> Dump group 3 (thread_3) not executed." > $DATA/3.out
echo "=======> Dump group 4 (thread_4) not executed." > $DATA/4.out
echo "=======> Dump group 5 (thread_5) not executed." > $DATA/5.out
echo "=======> Dump group 6 (thread_6) not executed." > $DATA/6.out
echo "=======> Dump group 7 (thread_7) not executed." > $DATA/7.out
echo "=======> Dump group 8 (thread_8) not executed." > $DATA/8.out
echo "=======> Dump group 9 (thread_9) not executed." > $DATA/9.out

err1=0
err2=0
err3=0
err4=0
err5=0
err6=0
err7=0
err8=0
err9=0
if [ "$PROCESS_DUMP" = 'YES' ]; then

##################################################################
##################################################################
#  The data "dump" script for either tm12, tm09, tm06 tm03 or tm00
##################################################################
##################################################################

msg="START THE $tmmark_uc $RUN_uc DATA $dump_ind CENTERED ON $dumptime"
$DATA/postmsg "$jlogfile" "$msg"

if [ $CHECK_STATUS = YES -a -s ${COMSP}status${JOB_NUMBER}.${tmmark}.bufr_d ]
then

msg="**WARNING: status${JOB_NUMBER} file already exists for ${cycp}Z - \
no data dumps produced"
$DATA/postmsg "$jlogfile" "$msg"

else

#----------------------------------------------------------------
cat<<\EOF>thread_1; chmod +x thread_1
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_1
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=1

#============================================================================
# Dump # 1 : 1BAMUA, 1BHRS3, 1BMHS, GPSRO, MTIASI, ESAMUA, ESHRS3
#              (1)     (1)    (1)    (1)     (1)     (1)     (1)
#            -- TOTAL NUMBER OF SUBTYPES = 7
#            time window radius is +/- 1.50 hours for all types
#============================================================================

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 1bamua 1bhrs3 1bmhs gpsro \
 mtiasi esamua eshrs3
error1=$?
echo "$error1" > $DATA/error1

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_1bamua $job ${COMSP}1bamua.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_1bhrs3 $job ${COMSP}1bhrs3.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_1bmhs $job ${COMSP}1bmhs.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_gpsro  $job ${COMSP}gpsro.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_mtiasi $job ${COMSP}mtiasi.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_esamua $job ${COMSP}esamua.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_eshrs3 $job ${COMSP}eshrs3.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_1
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/1.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_2; chmod +x thread_2
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_2
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=2

#============================================================================
# Dump # 2 : VADWND, SATWND
#              (1)   (11/8) 
#            -- TOTAL NUMBER OF SUBTYPES = 12 at tm12, tm06 and tm00
#                                           9 at tm09 and tm03
#
#            time window radius is +/- 1.50 hours for all types except
#            SATWND subtypes 005/064, 005/065 and 005/066 where it is 
#            -1.50 to +1.49 hours
#            (note: SATWND needs at least 1.25-hr window radius
#                   because some NESDIS SATWND report times can
#                   be cycle time minus 1:15)
#============================================================================

# Skip all Indian satellite winds in SATWND (not in domain)

export SKIP_005021=YES
export SKIP_005022=YES
export SKIP_005023=YES

if [ "$tmmark" = 'tm09' -o  "$tmmark" = 'tm03' ]; then

# At off-off NDAS times skip Japanese satellite winds in SATWND (never any data)

   export SKIP_005044=YES
   export SKIP_005045=YES
   export SKIP_005046=YES
fi

DTIM_latest_005064=${DTIM_latest_005064:-"+1.49"}
DTIM_latest_005065=${DTIM_latest_005065:-"+1.49"}
DTIM_latest_005066=${DTIM_latest_005066:-"+1.49"}

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 vadwnd satwnd
error2=$?
echo "$error2" > $DATA/error2

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_vadwnd $job ${COMSP}vadwnd.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_satwnd $job ${COMSP}satwnd.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_2
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/2.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_3; chmod +x thread_3
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_3
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=3

#========================================================================
# Dump # 3 : PROFLR, RASSDA, SFCSHP, ADPSFC, WNDSAT, ASCATT
#              (3)     (1)     (5)     (3)     (1)     (1)
#            -- TOTAL NUMBER OF SUBTYPES = 14
#            time window radius is +/- 1.50 hours for all types except
#            PROFLR where it is -2.50 to +2.50 hours
#=======================================================================

# Dump PROFLR with wide time window to improve PREPOBS_PROFCQC performance
#  (time window will be winnowed down in output from PREPOBS_PROFCQC, see
#   parm cards for output time window)

DTIM_earliest_proflr=${DTIM_earliest_proflr:-"-2.50"}
DTIM_latest_proflr=${DTIM_latest_proflr:-"+2.50"}

# Skip Japanese profiler reports in PROFLR (not in domain)

export SKIP_002013=YES

# Skip mobile synoptic reports in ADPSFC (not in domain)

export SKIP_000002=YES

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 proflr rassda sfcshp adpsfc \
 wndsat ascatt
error3=$?
echo "$error3" > $DATA/error3

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_proflr $job ${COMSP}proflr.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_rassda $job ${COMSP}rassda.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_sfcshp $job ${COMSP}sfcshp.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_adpsfc $job ${COMSP}adpsfc.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_wndsat $job ${COMSP}wndsat.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_wdsatr $job ${COMSP}wdsatr.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_ascatt $job ${COMSP}ascatt.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_ascatw $job ${COMSP}ascatw.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_3
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/3.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_4; chmod +x thread_4
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_4
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=4

#===========================================================================
# Dump # 4 : MSONET, GPSIPW -- TOTAL NUMBER OF SUBTYPES = 31
#             (30)     (1)
#            time window radius is +/- 1.50 hours for all reports except
#            GPSIPW where it is -1.0 to -0.50 hours
#            (note: GPSIPW uses only xx15 UTC data from 45-min ago; xx45 UTC
#                   data are not as good)
#===========================================================================

DTIM_earliest_gpsipw=${DTIM_earliest_gpsipw:-"-1.00"}
DTIM_latest_gpsipw=${DTIM_latest_gpsipw:-"-0.50"}

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 msonet gpsipw
error4=$?
echo "$error4" > $DATA/error4

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_gpsipw $job ${COMSP}gpsipw.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_msonet $job ${COMSP}msonet.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_4
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/4.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_5; chmod +x thread_5
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_5
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=5

#===========================================================================
# Dump # 5 : AIRCFT, AIRCAR -- TOTAL NUMBER OF SUBTYPES = 8
#              (6)     (2)
#            time window radius is +/- 3.25 hours for all types
#            dump is global for all types
#===========================================================================

# Dump AIRCFT and AIRCAR with wide time window and globally to improve
#  PREPOBS_PREPACQC track-check performance
#  (time window will be winnowed down to +/- 1.50 hours in output from
#   PREPOBS_PREPACQC, and limited to north of 20S latitude)

export LALO=0  # GLOBAL dumps here

$ushscript_dump/bufr_dump_obs.sh $dumptime 3.25 1 aircft aircar
error5=$?
echo "$error5" > $DATA/error5

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_aircft $job ${COMSP}aircft.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_aircar $job ${COMSP}aircar.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_5
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/5.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_6; chmod +x thread_6
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_6
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=6

#===========================================================================
# Dump # 6 : NEXRAD -- TOTAL NUMBER OF SUBTYPES = 8
#              (8)
#            time window radius is 1.50 hours
#===========================================================================

# Dump globally since all reports over CONUS (and geographical filtering is
#  computationally expensive)

export LALO=0

# NEXRAD tanks are hourly
# Process only those hourly tanks w/i requested dump center cycle time window

SKIP_006010=YES # radial wind  00Z
SKIP_006011=YES # radial wind  01Z
SKIP_006012=YES # radial wind  02Z
SKIP_006013=YES # radial wind  03Z
SKIP_006014=YES # radial wind  04Z
SKIP_006015=YES # radial wind  05Z
SKIP_006016=YES # radial wind  06Z
SKIP_006017=YES # radial wind  07Z
SKIP_006018=YES # radial wind  08Z
SKIP_006019=YES # radial wind  09Z
SKIP_006020=YES # radial wind  10Z
SKIP_006021=YES # radial wind  11Z
SKIP_006022=YES # radial wind  12Z
SKIP_006023=YES # radial wind  13Z
SKIP_006024=YES # radial wind  14Z
SKIP_006025=YES # radial wind  15Z
SKIP_006026=YES # radial wind  16Z
SKIP_006027=YES # radial wind  17Z
SKIP_006028=YES # radial wind  18Z
SKIP_006029=YES # radial wind  19Z
SKIP_006030=YES # radial wind  20Z
SKIP_006031=YES # radial wind  21Z
SKIP_006032=YES # radial wind  22Z
SKIP_006033=YES # radial wind  23Z

SKIP_006040=YES # reflectivity 00Z
SKIP_006041=YES # reflectivity 01Z
SKIP_006042=YES # reflectivity 02Z
SKIP_006043=YES # reflectivity 03Z
SKIP_006044=YES # reflectivity 04Z
SKIP_006045=YES # reflectivity 05Z
SKIP_006046=YES # reflectivity 06Z
SKIP_006047=YES # reflectivity 07Z
SKIP_006048=YES # reflectivity 08Z
SKIP_006049=YES # reflectivity 09Z
SKIP_006050=YES # reflectivity 10Z
SKIP_006051=YES # reflectivity 11Z
SKIP_006052=YES # reflectivity 12Z
SKIP_006053=YES # reflectivity 13Z
SKIP_006054=YES # reflectivity 14Z
SKIP_006055=YES # reflectivity 15Z
SKIP_006056=YES # reflectivity 16Z
SKIP_006057=YES # reflectivity 17Z
SKIP_006058=YES # reflectivity 18Z
SKIP_006059=YES # reflectivity 19Z
SKIP_006060=YES # reflectivity 20Z
SKIP_006061=YES # reflectivity 21Z
SKIP_006062=YES # reflectivity 22Z
SKIP_006063=YES # reflectivity 23Z

if [ $cycp -eq 00 ]; then   # (22.5 - 01.5 Z)
   unset SKIP_006032 # radial wind  22Z
   unset SKIP_006033 # radial wind  23Z
   unset SKIP_006010 # radial wind  00Z
   unset SKIP_006011 # radial wind  01Z
   unset SKIP_006062 # reflectivity 22Z
   unset SKIP_006063 # reflectivity 23Z
   unset SKIP_006040 # reflectivity 00Z
   unset SKIP_006041 # reflectivity 01Z
elif [ $cycp -eq 03 ]; then # (01.5 - 04.5 Z)
   unset SKIP_006011 # radial wind  01Z
   unset SKIP_006012 # radial wind  02Z
   unset SKIP_006013 # radial wind  03Z
   unset SKIP_006014 # radial wind  04Z
   unset SKIP_006041 # reflectivity 01Z
   unset SKIP_006042 # reflectivity 02Z
   unset SKIP_006043 # reflectivity 03Z
   unset SKIP_006044 # reflectivity 04Z
elif [ $cycp -eq 06 ]; then # (04.5 - 07.5 Z)
   unset SKIP_006014 # radial wind  04Z
   unset SKIP_006015 # radial wind  05Z
   unset SKIP_006016 # radial wind  06Z
   unset SKIP_006017 # radial wind  07Z
   unset SKIP_006044 # reflectivity 04Z
   unset SKIP_006045 # reflectivity 05Z
   unset SKIP_006046 # reflectivity 06Z
   unset SKIP_006047 # reflectivity 07Z
elif [ $cycp -eq 09 ]; then # (07.5 - 10.5 Z)
   unset SKIP_006017 # radial wind  07Z
   unset SKIP_006018 # radial wind  08Z
   unset SKIP_006019 # radial wind  09Z
   unset SKIP_006020 # radial wind  10Z
   unset SKIP_006047 # reflectivity 07Z
   unset SKIP_006048 # reflectivity 08Z
   unset SKIP_006049 # reflectivity 09Z
   unset SKIP_006050 # reflectivity 10Z
elif [ $cycp -eq 12 ]; then # (10.5 - 13.5 Z)
   unset SKIP_006020 # radial wind  10Z
   unset SKIP_006021 # radial wind  11Z
   unset SKIP_006022 # radial wind  12Z
   unset SKIP_006023 # radial wind  13Z
   unset SKIP_006050 # reflectivity 10Z
   unset SKIP_006051 # reflectivity 11Z
   unset SKIP_006052 # reflectivity 12Z
   unset SKIP_006053 # reflectivity 13Z
elif [ $cycp -eq 15 ]; then # (13.5 - 16.5 Z)
   unset SKIP_006023 # radial wind  13Z
   unset SKIP_006024 # radial wind  14Z
   unset SKIP_006025 # radial wind  15Z
   unset SKIP_006026 # radial wind  16Z
   unset SKIP_006053 # reflectivity 13Z
   unset SKIP_006054 # reflectivity 14Z
   unset SKIP_006055 # reflectivity 15Z
   unset SKIP_006056 # reflectivity 16Z
elif [ $cycp -eq 18 ]; then # (16.5 - 19.5 Z)
   unset SKIP_006026 # radial wind  16Z
   unset SKIP_006027 # radial wind  17Z
   unset SKIP_006028 # radial wind  18Z
   unset SKIP_006029 # radial wind  19Z
   unset SKIP_006056 # reflectivity 16Z
   unset SKIP_006057 # reflectivity 17Z
   unset SKIP_006058 # reflectivity 18Z
   unset SKIP_006059 # reflectivity 19Z
elif [ $cycp -eq 21 ]; then # (19.5 - 22.5 Z)
   unset SKIP_006029 # radial wind  19Z
   unset SKIP_006030 # radial wind  20Z
   unset SKIP_006031 # radial wind  21Z
   unset SKIP_006032 # radial wind  22Z
   unset SKIP_006059 # reflectivity 19Z
   unset SKIP_006060 # reflectivity 20Z
   unset SKIP_006061 # reflectivity 21Z
   unset SKIP_006062 # reflectivity 22Z
fi

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 nexrad
error6=$?
echo "$error6" > $DATA/error6

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_nexrad $job ${COMSP}nexrad.$tmmark.bufr_d
fi

if [ "$SENDDBN_NDAS" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NDAS_BUFR_nexrad $job ${COMSP}nexrad.$tmmark.bufr_d
fi
 
set +x
echo "********************************************************************"
echo Script thread_6
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/6.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_7; chmod +x thread_7
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_7
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=7

#===========================================================================
# Dump # 7 : GOESFV -- TOTAL NUMBER OF SUBTYPES = 1
#              (1)
#            time window radius is +/- 1.50 hours
#            dump is global
#              (dump is much quicker w/o geographical filtering, all GOES
#               reports are in NAM domain anyway)
#===========================================================================

export LALO=0  # GLOBAL dumps here

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 goesfv
error7=$?
echo "$error7" > $DATA/error7

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_goesfv $job ${COMSP}goesfv.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_7
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/7.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_8; chmod +x thread_8
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_8
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=8

#============================================================================
# Dump # 8 : 1BAMUB, 1BHRS4, AIRSEV, OSBUV8, RADWND, ESAMUB, ESMHS
#              (1)     (1)     (1)     (1)     (2)     (1)    (1)
#            -- TOTAL NUMBER OF SUBTYPES = 8
#            time window radius is +/- 1.50 hours for all types except
#            RADWND where it is -1.75 to +1.75 hours
#            (note: RADWND needs the slightly larger time window
#                   because BUFR subsets can be split into 500
#                   "level" pieces each 1-min later to avoid
#                   being tossed by duplicate check DUPRAD)
#============================================================================

DTIM_earliest_radwnd=${DTIM_earliest_radwnd:-"-1.75"}
DTIM_latest_radwnd=${DTIM_latest_radwnd:-"+1.75"}

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 1bamub 1bhrs4 airsev osbuv8 \
 radwnd esamub esmhs
error8=$?
echo "$error8" > $DATA/error8

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_1bamub $job ${COMSP}1bamub.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_1bhrs4 $job ${COMSP}1bhrs4.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_airsev $job ${COMSP}airsev.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_osbuv8 $job ${COMSP}osbuv8.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_radwnd $job ${COMSP}radwnd.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_esamub $job ${COMSP}esamub.$tmmark.bufr_d
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_esmhs  $job ${COMSP}esmhs.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_8
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/8.out 2>&1
EOF

#----------------------------------------------------------------
cat<<\EOF>thread_9; chmod +x thread_9
set -uax

cd $DATA

{ echo
set +x
echo "********************************************************************"
echo Script thread_9
echo Executing on node  `hostname`
echo Starting time: `date`
echo "********************************************************************"
echo
set -x

export STATUS=NO
export DUMP_NUMBER=9

#===========================================================================
# Dump # 9 : ADPUPA -- TOTAL NUMBER OF SUBTYPES = 6
#              (6)
#            time window radius is +/- 1.50 hours
#            (note: ADPUPA is dumped last and after all other dumping has
#                   completed in the NAM (tm00) dump - ONLY- in order to
#                   maximize availability of ADPUPA data for NAM network)
#===========================================================================

$ushscript_dump/bufr_dump_obs.sh $dumptime 1.5 1 adpupa
error9=$?
echo "$error9" > $DATA/error9

if [ "$SENDDBN" = 'YES' ]; then
 $DBNROOT/bin/dbn_alert MODEL NAM_BUFR_adpupa $job ${COMSP}adpupa.$tmmark.bufr_d
fi

set +x
echo "********************************************************************"
echo Script thread_9
echo Finished executing on node  `hostname`
echo Ending time  : `date`
echo "********************************************************************"
set -x
} > $DATA/9.out 2>&1
EOF

#----------------------------------------------------------------

[ $DUMP_group1 = YES ]  &&  thread_1 &
[ $DUMP_group2 = YES ]  &&  thread_2 &
[ $DUMP_group3 = YES ]  &&  thread_3 &
[ $DUMP_group4 = YES ]  &&  thread_4 &
[ $DUMP_group5 = YES ]  &&  thread_5 &
[ $DUMP_group6 = YES ]  &&  thread_6 &
[ $DUMP_group7 = YES ]  &&  thread_7 &
[ $DUMP_group8 = YES ]  &&  thread_8 &
[ $DUMP_group9 = YES -a $ADPUPA_wait != YES ]  &&  thread_9 &

wait

#  if ADPUPA_wait is YES, adpupa is dumped AFTER all other dump threads have
#   run (normally done in real-time tm00 NAM runs to dump as late as possible
#   in order to maximize data availability in NAM network)
#  --------------------------------------------------------------------------

[ $DUMP_group9 = YES -a $ADPUPA_wait  = YES ]  &&  thread_9

cat $DATA/1.out $DATA/2.out $DATA/3.out $DATA/4.out $DATA/5.out $DATA/6.out \
 $DATA/7.out $DATA/8.out $DATA/9.out
[ -s $DATA/error1 ] && err1=`cat $DATA/error1`
[ -s $DATA/error2 ] && err2=`cat $DATA/error2`
[ -s $DATA/error3 ] && err3=`cat $DATA/error3`
[ -s $DATA/error4 ] && err4=`cat $DATA/error4`
[ -s $DATA/error5 ] && err5=`cat $DATA/error5`
[ -s $DATA/error6 ] && err6=`cat $DATA/error6`
[ -s $DATA/error7 ] && err7=`cat $DATA/error7`
[ -s $DATA/error8 ] && err8=`cat $DATA/error8`
[ -s $DATA/error9 ] && err9=`cat $DATA/error9`


#===============================================================================

export STATUS=YES
export DUMP_NUMBER=10
$ushscript_dump/bufr_dump_obs.sh $dumptime 3.00 1 null


#  endif test for existence of status file
fi

#  endif loop $PROCESS_DUMP
fi

#================================================================
#================================================================


if [ "$PROCESS_DUMP" = 'YES' ]; then

  if [ "$err1" -gt '5' -o "$err2" -gt '5' -o "$err3" -gt '5' -o \
       "$err4" -gt '5' -o "$err5" -gt '5' -o "$err6" -gt '5' -o \
       "$err7" -gt '5' -o "$err8" -gt '5' -o "$err9" -gt '5' ]; then
      for n in $err1 $err2 $err3 $err4 $err5 $err6 $err7 $err8 $err9
      do
         if [ "$n" -gt '5' ]; then
            if [ "$n" -ne '11' -a "$n" -ne '22' ]; then

## fatal error in dumping of BUFR obs. files

               set +x
echo
echo " ###################################################### "
echo " --> > 22 RETURN CODE FROM DATA DUMP, $err1, $err2, $err3, $err4, \
$err5, $err6, $err7, $err8, $err9 "
echo " --> @@ F A T A L   E R R O R @@   --  ABNORMAL EXIT    "
echo " ###################################################### "
echo
               set -x
               $DATA/err_exit
               exit 9
            fi
         fi
      done

## a status code of 11 or 22 from dumping of BUFR obs. files
## is non-fatal but still worth noting

      set +x
      echo
      echo " ###################################################### "
      echo " --> > 5 RETURN CODE FROM DATA DUMP, $err1, $err2, $err3, $err4, \
$err5, $err6, $err7, $err8, $err9 "
      echo " --> NOT ALL DATA DUMP FILES ARE COMPLETE - CONTINUE    "
      echo " ###################################################### "
      echo
      set -x
   fi

#  endif loop $PROCESS_DUMP
fi


# GOOD RUN
set +x
echo " "
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " ****** PROCESSING COMPLETED NORMALLY"
echo " "
set -x


# save standard output
cat break $pgmout break > allout
cat allout
# rm allout
 
sleep 10

msg='ENDED NORMALLY.'
$DATA/postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################
