##########################################################################
echo "--------------------------------------------------------------------"
echo "exdump_monitor.sh.sms - Data dump monitor processing"
echo "--------------------------------------------------------------------"
echo "History: Jan 03 2001 - Original script."
###########################################################################

set -x

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

msg="HAS BEGUN on `hostname`"
$DATA/postmsg "$jlogfile" "$msg"

cat break > $pgmout

export dumptime=`cut -c7-16 ncepdate`
export cycp=`echo $dumptime|cut -c9-10`

# Determine if there is a lag between the system clock on the IBM-CCS and sms
#  (assume that the sms clock is accurate)
# Calculate the lag time in minutes by comparing the time job PROD_SETUP begins
#  vs. the sms clock time which is 31 minutes past the hour.
#  S K I P  THIS  -- IBM-CCS clocks are accurate right now!

sp_min=31
lines=1
while [ $lines -lt 10 ]; do
   sp_hr=`grep -h LLprod_setup_ /com/logs/jlogfile.old.14 \
    /com/logs/jlogfile.old.15 /com/logs/jlogfile.old.16 /com/logs/jlogfile | \
    grep -h -e "HAS BEGUN" | tail -n$lines | head -n1 | cut -d":" -f1 | \
    cut -d" " -f2`
   if [ $sp_hr = 11 -o $sp_hr = 23 ]; then
      sp_min=`grep -h LLprod_setup_ /com/logs/jlogfile.old.14 \
       /com/logs/jlogfile.old.15 /com/logs/jlogfile.old.16 /com/logs/jlogfile \
       | grep -h -e "HAS BEGUN" | tail -n$lines | head -n1 | cut -d":" -f2`
      break
   fi
   lines=`expr $lines + 1`
done

if [ -n "$sp_min" ]; then
   lag_time=`expr 31 - $sp_min`
else
   lag_time=0
fi

#  S K I P  THIS  -- IBM-CCS clocks are accurate right now!
lag_time=0

echo $lag_time

# Min and max times for this job are now created, based on the lag time
#  and a plus or minus 3-minute window

min_min=`expr -3 - $lag_time`
min_max=`expr  3 - $lag_time`

dumptime_min=`/nwprod/util/exec/mdate $min_min ${dumptime}30`   
dumptime_max=`/nwprod/util/exec/mdate $min_max ${dumptime}30`


currdate=`date -u +%Y%m%d%H%M`

set +x
echo
echo "CENTER DATA DUMP DATE-TIME FOR MONITORING IS $dumptime"
echo
echo "CURRENT DATE IS $currdate"
echo
set -x

TIME_CHECK=${TIME_CHECK:-YES}
if [ $TIME_CHECK = YES ];then
   if [ $currdate -gt $dumptime_max -o $currdate -lt $dumptime_min ];then
msg="CENTER DATA DUMP TIME OUTSIDE ALLOWABLE TIME WINDOW, NO DUMPS PRODUCED"
      set +x
      echo
      echo $msg
      echo
      set -x
      $DATA/postmsg "$jlogfile" "$msg"
      set +x
      echo " "
      echo " ****** PROCESSING COMPLETED NORMALLY BUT NO DUMPS PRODUCED"
      echo " ****** PROCESSING COMPLETED NORMALLY BUT NO DUMPS PRODUCED"
      echo " ****** PROCESSING COMPLETED NORMALLY BUT NO DUMPS PRODUCED"
      echo " ****** PROCESSING COMPLETED NORMALLY BUT NO DUMPS PRODUCED"
      echo " "
      set -x
# save standard output
      cat  break $pgmout break > allout
      cat allout
      # rm allout

      sleep 10

      msg='ENDED NORMALLY, BUT NO DUMPS PRODUCED.'
      $DATA/postmsg "$jlogfile" "$msg"
      exit
   fi
fi

export COMSP=${DATA}${COMOUT}/$RUN.${cycle}.
mkdir -p ${DATA}${COMOUT}

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

###########################
###########################
#  The data "dump" script
###########################
###########################

msg="START THE DATA DUMP CENTERED ON $dumptime"
$DATA/postmsg "$jlogfile" "$msg"

#----------------------------------------------------------------
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 : ADPUPA, GOESND, 1BAMUB, RADWND, 1BMHS,
#              (6)     (1)     (1)     (2)    (1)
#            1BHRS4, OSBUV8, TESAC, TRKOB
#              (1)     (1)     (1)   (1)
#            -- TOTAL NUMBER OF SUBTYPES = 15
#  time window radius is -0.50 to +0.49 hours on ADPUPA, TESAC
#  time window radius is -1.00 to -0.01 hours on 1x1 GOESND (cloud)
#  time window radius is -1.50 to -0.51 hours on RADWND, TRKOB
#  time window radius is -2.00 to -1.01 hours on OSBUV8, 1BAMUB, 1BMHS,
#                                                1BHRS4
#=========================================================================

DTIM_latest_adpupa=+0.49
DTIM_latest_tesac=+0.49

DTIM_earliest_003002=-1.00
DTIM_latest_003002=-0.01

DTIM_earliest_1bamub=-2.00
DTIM_latest_1bamub=-1.01

DTIM_earliest_1bmhs=-2.00
DTIM_latest_1bmhs=-1.01

DTIM_earliest_1bhrs4=-2.00
DTIM_latest_1bhrs4=-1.01

DTIM_earliest_radwnd=-1.50
DTIM_latest_radwnd=-0.51

DTIM_earliest_trkob=-1.50
DTIM_latest_trkob=-0.51

DTIM_earliest_osbuv8=-2.00
DTIM_latest_osbuv8=-1.01

# Skip GOES 1x1 soundings/radiances (these are already in "goesfv" dump)

export SKIP_003003=YES

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 adpupa goesnd osbuv8 \
 1bamub 1bmhs 1bhrs4 radwnd tesac trkob
error1=$?
echo "$error1" > $DATA/error1

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 : SFCSHP, ADPSFC, RASSDA, WNDSAT, AMSRE, AIRSEV, GOESFV, MTIASI,
#              (5)     (4)     (1)     (1)    (1)    (1)     (1)      (1)
#            GOME, AVCSAM   -- TOTAL NUMBER OF SUBTYPES = 17
#             (1)    (1)
#  time window radius is -0.50 to +0.49 hours on SFCSHP (all types except
#                                                tide gauge from CREX),
#                                                ADPSFC, RASSDA, GOESFV
#  time window radius is -1.00 to -0.01 hours on SFCSHP (tide gauge from
#                                                        CREX only)
#  time window radius is -1.50 to -0.51 hours on GOME
#  time window radius is -2.00 to -1.01 hours on MTIASI
#  time window radius is -2.50 to -1.51 hours on AMSRE, AIRSEV, AVCSAM
#  time window radius is -3.00 to -2.01 hours on WNDSAT
#==========================================================================

DTIM_latest_001001=+0.49
DTIM_latest_001002=+0.49
DTIM_latest_001003=+0.49
DTIM_latest_001004=+0.49
DTIM_latest_adpsfc=+0.49
DTIM_latest_rassda=+0.49
DTIM_latest_goesfv=+0.49

DTIM_earliest_001005=-1.00
DTIM_latest_001005=-0.01

DTIM_earliest_amsre=-2.50
DTIM_latest_amsre=-1.51

DTIM_earliest_wndsat=-3.00
DTIM_latest_wndsat=-2.01

DTIM_earliest_airsev=-2.50
DTIM_latest_airsev=-1.51

DTIM_earliest_mtiasi=-2.00
DTIM_latest_mtiasi=-1.01

DTIM_earliest_gome=-1.50
DTIM_latest_gome=-0.51

DTIM_earliest_avcsam=-2.50
DTIM_latest_avcsam=-1.51

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 sfcshp adpsfc rassda \
 amsre wndsat airsev goesfv mtiasi gome avcsam
error2=$?
echo "$error2" > $DATA/error2

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 : SATWND, 1BAMUA, GPSRO, AVCSPM
#             (13)     (1)    (1)     (1)
#            -- TOTAL NUMBER OF SUBTYPES = 16
#  time window radius is -1.50 to -0.51 hours on all EUMETSAT SATWND types
#  time window radius is -2.00 to -1.01 hours on 1BAMUA, and all
#                                                    GOES     SATWND types
#  time window radius is -2.50 to -1.51 hours on all other    SATWND types,
#                                                    GPSRO, AVCSPM
#  time window radius is -4.00 to -3.01 hours on all MODIS    SATWND types
#===========================================================================

# Skip Indian water vapor satellite winds in SATWND (never any data)

SKIP_005023=YES

DTIM_earliest_1bamua=-2.00
DTIM_latest_1bamua=-1.01

DTIM_earliest_005064=-1.50
DTIM_latest_005064=-0.51

DTIM_earliest_005065=-1.50
DTIM_latest_005065=-0.51

DTIM_earliest_005066=-1.50
DTIM_latest_005066=-0.51

DTIM_earliest_005010=-2.00
DTIM_latest_005010=-1.01

DTIM_earliest_005011=-2.00
DTIM_latest_005011=-1.01

DTIM_earliest_005012=-2.00
DTIM_latest_005012=-1.01

DTIM_earliest_005021=-2.50
DTIM_latest_005021=-1.51

DTIM_earliest_005022=-2.50
DTIM_latest_005022=-1.51

DTIM_earliest_005044=-2.50
DTIM_latest_005044=-1.51

DTIM_earliest_005045=-2.50
DTIM_latest_005045=-1.51

DTIM_earliest_005046=-2.50
DTIM_latest_005046=-1.51

DTIM_earliest_gpsro=-2.50
DTIM_latest_gpsro=-1.51

DTIM_earliest_005070=-4.00
DTIM_latest_005070=-3.01

DTIM_earliest_005071=-4.00
DTIM_latest_005071=-3.01

DTIM_earliest_avcspm=-2.50
DTIM_latest_avcspm=-1.51

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 satwnd gpsro 1bamua avcspm
error3=$?
echo "$error3" > $DATA/error3

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 : AIRCAR, AIRCFT, PROFLR, VADWND, GEOIMR, TRMM, 1BHRS3, ASCATT
#              (2)     (6)     (4)     (1)     (1)    (1)    (1)     (1)
#            -- TOTAL NUMBER OF SUBTYPES = 17
#  time window radius is -0.50 to +0.49 hours on AIRCAR, AIRCFT,
#                                                PROFLR, VADWND
#  time window radius is -1.00 to -0.01 hours on GEOIMR
#  time window radius is -1.50 to -0.51 hours on ASCATT
#  time window radius is -2.00 to -1.01 hours on 1BHRS3
#  time window radius is -2.50 to -1.51 hours on TRMM
#=========================================================================

DTIM_latest_aircar=+0.49
DTIM_latest_aircft=+0.49
DTIM_latest_proflr=+0.49
DTIM_latest_vadwnd=+0.49

DTIM_earliest_geoimr=-1.00
DTIM_latest_geoimr=-0.01

DTIM_earliest_trmm=-2.50
DTIM_latest_trmm=-1.51

DTIM_earliest_1bhrs3=-2.00
DTIM_latest_1bhrs3=-1.01

DTIM_earliest_ascatt=-1.50
DTIM_latest_ascatt=-0.51

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 aircar aircft proflr vadwnd \
 geoimr trmm 1bhrs3 ascatt
error4=$?
echo "$error4" > $DATA/error4

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 : MSONET, GPSIPW -- TOTAL NUMBER OF SUBTYPES = 31
#             (30)     (1)
#  time window radius is -0.50 to +0.49 hours on MSONET
#  time window radius is -1.00 to -0.01 hours on GPSIPW
#===================================================================

DTIM_latest_msonet=+0.49

DTIM_earliest_gpsipw=-1.00
DTIM_latest_gpsipw=-0.01

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

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 = 4
#             (4)
#  time window radius is -0.50 to +0.49 hours
#===================================================================

DTIM_latest_nexrad=+0.49

# 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   # (23.50 - 00.49 Z)
   unset SKIP_006033 # radial wind  23Z
   unset SKIP_006010 # radial wind  00Z
   unset SKIP_006063 # reflectivity 23Z
   unset SKIP_006040 # reflectivity 00Z
elif [ $cycp -eq 01 ]; then   # (00.50 - 01.49 Z)
   unset SKIP_006010 # radial wind  00Z
   unset SKIP_006011 # radial wind  01Z
   unset SKIP_006040 # reflectivity 00Z
   unset SKIP_006041 # reflectivity 01Z
elif [ $cycp -eq 02 ]; then   # (01.50 - 02.49 Z)
   unset SKIP_006011 # radial wind  01Z
   unset SKIP_006012 # radial wind  02Z
   unset SKIP_006041 # reflectivity 01Z
   unset SKIP_006042 # reflectivity 02Z
elif [ $cycp -eq 03 ]; then   # (02.50 - 03.49 Z)
   unset SKIP_006012 # radial wind  02Z
   unset SKIP_006013 # radial wind  03Z
   unset SKIP_006042 # reflectivity 02Z
   unset SKIP_006043 # reflectivity 03Z
elif [ $cycp -eq 04 ]; then   # (03.50 - 04.49 Z)
   unset SKIP_006013 # radial wind  03Z
   unset SKIP_006014 # radial wind  04Z
   unset SKIP_006043 # reflectivity 03Z
   unset SKIP_006044 # reflectivity 04Z
elif [ $cycp -eq 05 ]; then   # (04.50 - 05.49 Z)
   unset SKIP_006014 # radial wind  04Z
   unset SKIP_006015 # radial wind  05Z
   unset SKIP_006044 # reflectivity 04Z
   unset SKIP_006045 # reflectivity 05Z
elif [ $cycp -eq 06 ]; then   # (05.50 - 06.49 Z)
   unset SKIP_006015 # radial wind  05Z
   unset SKIP_006016 # radial wind  06Z
   unset SKIP_006045 # reflectivity 05Z
   unset SKIP_006046 # reflectivity 06Z
elif [ $cycp -eq 07 ]; then   # (06.50 - 07.49 Z)
   unset SKIP_006016 # radial wind  06Z
   unset SKIP_006017 # radial wind  07Z
   unset SKIP_006046 # reflectivity 06Z
   unset SKIP_006047 # reflectivity 07Z
elif [ $cycp -eq 08 ]; then   # (07.50 - 08.49 Z)
   unset SKIP_006017 # radial wind  07Z
   unset SKIP_006018 # radial wind  08Z
   unset SKIP_006047 # reflectivity 07Z
   unset SKIP_006048 # reflectivity 08Z
elif [ $cycp -eq 09 ]; then   # (08.50 - 09.49 Z)
   unset SKIP_006018 # radial wind  08Z
   unset SKIP_006019 # radial wind  09Z
   unset SKIP_006048 # reflectivity 08Z
   unset SKIP_006049 # reflectivity 09Z
elif [ $cycp -eq 10 ]; then   # (09.50 - 10.49 Z)
   unset SKIP_006019 # radial wind  09Z
   unset SKIP_006020 # radial wind  10Z
   unset SKIP_006049 # reflectivity 09Z
   unset SKIP_006050 # reflectivity 10Z
elif [ $cycp -eq 11 ]; then   # (10.50 - 11.49 Z)
   unset SKIP_006020 # radial wind  10Z
   unset SKIP_006021 # radial wind  11Z
   unset SKIP_006050 # reflectivity 10Z
   unset SKIP_006051 # reflectivity 11Z
elif [ $cycp -eq 12 ]; then   # (11.50 - 12.49 Z)
   unset SKIP_006021 # radial wind  11Z
   unset SKIP_006022 # radial wind  12Z
   unset SKIP_006051 # reflectivity 11Z
   unset SKIP_006052 # reflectivity 12Z
elif [ $cycp -eq 13 ]; then   # (12.50 - 13.49 Z)
   unset SKIP_006022 # radial wind  12Z
   unset SKIP_006023 # radial wind  13Z
   unset SKIP_006052 # reflectivity 12Z
   unset SKIP_006053 # reflectivity 13Z
elif [ $cycp -eq 14 ]; then   # (13.50 - 14.49 Z)
   unset SKIP_006023 # radial wind  13Z
   unset SKIP_006024 # radial wind  14Z
   unset SKIP_006053 # reflectivity 13Z
   unset SKIP_006054 # reflectivity 14Z
elif [ $cycp -eq 15 ]; then   # (14.50 - 15.49 Z)
   unset SKIP_006024 # radial wind  14Z
   unset SKIP_006025 # radial wind  15Z
   unset SKIP_006054 # reflectivity 14Z
   unset SKIP_006055 # reflectivity 15Z
elif [ $cycp -eq 16 ]; then   # (15.50 - 16.49 Z)
   unset SKIP_006025 # radial wind  15Z
   unset SKIP_006026 # radial wind  16Z
   unset SKIP_006055 # reflectivity 15Z
   unset SKIP_006056 # reflectivity 16Z
elif [ $cycp -eq 17 ]; then   # (16.50 - 17.49 Z)
   unset SKIP_006026 # radial wind  16Z
   unset SKIP_006027 # radial wind  17Z
   unset SKIP_006056 # reflectivity 16Z
   unset SKIP_006057 # reflectivity 17Z
elif [ $cycp -eq 18 ]; then   # (17.50 - 18.49 Z)
   unset SKIP_006027 # radial wind  17Z
   unset SKIP_006028 # radial wind  18Z
   unset SKIP_006057 # reflectivity 17Z
   unset SKIP_006058 # reflectivity 18Z
elif [ $cycp -eq 19 ]; then   # (18.50 - 19.49 Z)
   unset SKIP_006028 # radial wind  18Z
   unset SKIP_006029 # radial wind  19Z
   unset SKIP_006058 # reflectivity 18Z
   unset SKIP_006059 # reflectivity 19Z
elif [ $cycp -eq 20 ]; then   # (19.50 - 20.49 Z)
   unset SKIP_006029 # radial wind  19Z
   unset SKIP_006030 # radial wind  20Z
   unset SKIP_006059 # reflectivity 19Z
   unset SKIP_006060 # reflectivity 20Z
elif [ $cycp -eq 21 ]; then   # (20.50 - 21.49 Z)
   unset SKIP_006030 # radial wind  20Z
   unset SKIP_006031 # radial wind  21Z
   unset SKIP_006060 # reflectivity 20Z
   unset SKIP_006061 # reflectivity 21Z
elif [ $cycp -eq 22 ]; then   # (21.50 - 22.49 Z)
   unset SKIP_006031 # radial wind  21Z
   unset SKIP_006032 # radial wind  22Z
   unset SKIP_006061 # reflectivity 21Z
   unset SKIP_006062 # reflectivity 22Z
elif [ $cycp -eq 23 ]; then   # (22.50 - 23.49 Z)
   unset SKIP_006032 # radial wind  22Z
   unset SKIP_006033 # radial wind  23Z
   unset SKIP_006062 # reflectivity 22Z
   unset SKIP_006063 # reflectivity 23Z
fi

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

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 : ATOVS, OMI, ESAMUA, ESAMUB, ESHRS3, ESMHS, SSMISU, SEVCSR,
#             (1)   (1)    (1)     (1)     (1)    (1)     (1)     (1)
#            LGHTNG, LGYCLD, ATMS, BATHY, MLS  --
#              (2)     (1)    (1)   (1)   (1)
#            -- TOTAL NUMBER OF SUBTYPES = 14
#  time window radius is -0.50 to +0.49 hours on ESAMUA, ESAMUB, ESHRS3,
#                                                ESMHS,  SEVCSR, LGHTNG,
#                                                LGYCLD
#  time window radius is -1.00 to -0.01 hours on ATMS
#  time window radius is -1.50 to -0.51 hours on SSMISU, MLS
#  time window radius is -2.00 to -1.01 hours on ATOVS
#  time window radius is -2.50 to -1.51 hours on OMI, BATHY
#======================================================================

DTIM_latest_esamua=+0.49
DTIM_latest_esamub=+0.49
DTIM_latest_eshrs3=+0.49
DTIM_latest_esmhs=+0.49
DTIM_latest_sevcsr=+0.49
DTIM_latest_lghtng=+0.49
DTIM_latest_lgycld=+0.49

DTIM_earliest_atovs=-2.00
DTIM_latest_atovs=-1.01

DTIM_earliest_omi=-2.50
DTIM_latest_omi=-1.51

DTIM_earliest_bathy=-2.50
DTIM_latest_bathy=-1.51

DTIM_earliest_ssmisu=-1.50
DTIM_latest_ssmisu=-0.51

DTIM_earliest_mls=-1.50
DTIM_latest_mls=-0.51

DTIM_earliest_atms=-1.00
DTIM_latest_atms=-0.01

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 atovs omi esamua \
 esamub eshrs3 esmhs ssmisu sevcsr lghtng lgycld atms bathy mls
error7=$?
echo "$error7" > $DATA/error7

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

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

thread_1 &
thread_2 &
thread_3 &
thread_4 &
thread_5 &
thread_6 &
thread_7 &

wait

cat $DATA/1.out $DATA/2.out $DATA/3.out $DATA/4.out $DATA/5.out $DATA/6.out \
 $DATA/7.out

set +x
echo " "
echo " "
set -x

err1=`cat $DATA/error1`
err2=`cat $DATA/error2`
err3=`cat $DATA/error3`
err4=`cat $DATA/error4`
err5=`cat $DATA/error5`
err6=`cat $DATA/error6`
err7=`cat $DATA/error7`


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

export STATUS=YES
export DUMP_NUMBER=8
$ushscript_dump/bufr_dump_obs.sh $dumptime 0.50 1 null


cp -p ${DATA}${COMOUT}/$RUN.${cycle}.status.tm00.bufr_d $COMOUT

#  endif loop $PROCESS_DUMP
fi

echo " " >> $pgmout
echo "##################################################################\
####################"  >> $pgmout
echo " " >> $pgmout

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

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

#  Prepare the data counts for the SDM if requested
#  ------------------------------------------------

   $ushscript_datacount/bufr_datacount.sh
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' ]; then
      for n in $err1 $err2 $err3 $err4 $err5 $err6 $err7
      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 "
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 "
      echo " --> NOT ALL DATA DUMP FILES ARE COMPLETE - CONTINUE    "
      echo " ###################################################### "
      echo
      set -x
   fi

#  endif loop $PROCESS_DUMP
fi


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

########################################################################
#    Update Data Count Average Tables for All Cycles in Dump Monitor   #
#               (Normally done only in 23Z run of this job)            #
########################################################################

   msg="Attempt to update data count average table dump monitoring"
   $DATA/postmsg "$jlogfile" "$msg"

   $ushscript_avgdata/bufr_avgdata.sh $NET
   errsc=$?
   if [ "$errsc" -eq '0' ]; then
      rm $AVGDarch_OUT/obcount_30davg.${NET}.current
      cp obcount_30davg.${NET}.current \
       $AVGDarch_OUT/obcount_30davg.${NET}.current
      chmod 775 $AVGDarch_OUT/obcount_30davg.${NET}.current
      msg="Data count average table SUCCESSFULLY updated for dump monitoring"
      $DATA/postmsg "$jlogfile" "$msg"
      typeset -Z2 this_month last_month
      this_month=`echo $PDY | cut -c5-6`
      year=`echo $PDY | cut -c1-4`
      last_month=`expr $this_month - 1`
      if [ "$last_month" -eq '0' ]; then
         last_month=12
         year=`expr $year - 1`
      fi
###   if [ ! -s $AVGDarch_OUT/obcount_30davg.${NET}.${year}${last_month} ]
###   then

#  If no data count average table found for previous month, save this one
#   (Currently not done in data monitoring)

###      rm $AVGDarch_OUT/obcount_30davg.${NET}.*${last_month}
###      cp obcount_30davg.${NET}.current \
###       $AVGDarch_OUT/obcount_30davg.${NET}.${year}${last_month}
###      chmod 775 $AVGDarch_OUT/obcount_30davg.${NET}.${year}${last_month}
###      msg="DATA COUNT AVERAGE table for ${year}${last_month} saved \
###for dump monitoring"
###      $DATA/postmsg "$jlogfile" "$msg"
###   fi
   else
      msg="Data count average table NOT updated for dump monitoring"
      $DATA/postmsg "$jlogfile" "$msg"
   fi

#  endif loop $PROCESS_AVGTABLES
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 #######################
