#####################################################################
echo "----------------------------------------------------------"
echo "exrtma_dump.sh.sms - RTMA model data dump processing"
echo "----------------------------------------------------------"
echo "History: May 19 2006 - Original script."
#####################################################################

set -x

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

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

export dumptime=`cut -c7-16 ncepdate`

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

msg="RTMA ANALYSIS TIME IS $PDY$cyc"
postmsg "$jlogfile" "$msg"

set +x
echo
echo "CENTER DATA DUMP DATE-TIME FOR $tmmark_uc RTMA IS $dumptime"
echo
set -x
 
export COMSP=$COMOUT/$RUN.${cycle}.
 
err1=0
err2=0
err3=0
if [ "$PROCESS_DUMP" = 'YES' ]; then

###################################
###################################
#  The data "dump" script for tm00
###################################
###################################

msg="START THE $tmmark_uc RTMA DATA DUMP CENTERED ON $dumptime"
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 : ASCATT, WNDSAT, SATWND -- TOTAL NUMBER OF SUBTYPES = 13
#              (1)     (1)    (11)
#            time window radius is -6.00 to 0.00 hours for ASCATT
#            time window radius is -6.00 to 0.00 hours for WNDSAT
#            for SATWND subtypes 005/064, 005/065 and 005/066 at all times:
#              time window radius is -1.50 to +1.49 hours
#            for all other SATWND subtypes:
#              time window radius is +/- 2.5 hours
#              (note: window bumped up to 2.5-hrs to allow 01, 07, 13, 19Z
#                     dumps to get some data)
#            (note: SATWND needs at least 1.25-hr window radius
#                   because some 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

DTIM_earliest_005064=-1.50
DTIM_latest_005064=+1.49
DTIM_earliest_005065=-1.50
DTIM_latest_005065=+1.49
DTIM_earliest_005066=-1.50
DTIM_latest_005066=+1.49

DTIM_earliest_ascatt=-6.00
DTIM_latest_ascatt=0.00
DTIM_earliest_wndsat=-6.00
DTIM_latest_wndsat=0.00

$ushscript_dump/bufr_dump_obs.sh $dumptime 2.5 1 ascatt wndsat satwnd
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
#              (5)     (3)
#            -- TOTAL NUMBER OF SUBTYPES = 8
#            time window radius is +/- 0.50 hours for SFCSHP and ADPSFC
#=======================================================================

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

export SKIP_000002=YES

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.5 1 sfcshp adpsfc
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 : MSONET -- TOTAL NUMBER OF SUBTYPES = 30
#            time window radius is 0.50 hours
#===========================================================================

$ushscript_dump/bufr_dump_obs.sh $dumptime 0.5 1 msonet
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

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

thread_1 &
thread_2 &
thread_3 &

wait

cat $DATA/1.out $DATA/2.out $DATA/3.out
err1=`cat $DATA/error1`
err2=`cat $DATA/error2`
err3=`cat $DATA/error3`

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


#  endif loop $PROCESS_DUMP
fi

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


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

   if [ "$err1" -gt '5' -o "$err2" -gt '5' -o "$err3" -gt '5' ]; then
      for n in $err1 $err2 $err3
      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 "
echo " --> @@ F A T A L   E R R O R @@   --  ABNORMAL EXIT    "
echo " ###################################################### "
echo
               set -x
               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 "
      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.'
postmsg "$jlogfile" "$msg"

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