
# This utility script removes the dump count listing for a
#  particular BUFR type/subtype from ALL status files over the past
#  30 days for a particular run in the
#  /com/arch/prod/obcount_30day directory

# This removes it from the database used to generate the current
#  running 30 day average for the run in question in the
#  /com/arch/prod/avgdata directory.

# Input positional parameters:
#   1 - run (current choices: gfs, dump, nam, gdas, ruc2a, rucs1,
#                             rucs2)
#   2 - BUFR type of the data type to be removed from ALL status files
#       for the past 30 days in the $1 run (must be 3 digits in range
#       000-255)
#   3 - BUFR subtype of the data type to be removed from ALL status
#       files for the past 30 days in the $1 run (must be 3 digits in
#       range 000-255)

# For example, clean_obcount_30day.sh nam 255 030 cleans all Mesonet
#  "other" type data out of the Nam 30-day status file database

if (( $# < 3 )) ; then
  echo "Script $0 requires 3 arguments."
  echo "Usage: $0 run type subtype"
  echo "(1) run is the run"
  echo "    (current choices: gfs, dump, nam, gdas, ruc2a,"
  echo "                      rucs1, rucs2)"
  echo "(2) type is the BUFR type of the data type is clean out"
  echo "(3) subtype is the BUFR subtype of the data type is clean out"
  exit 255
fi

if [ "${#2}" -ne '3' ]; then
  echo
  echo " ===> Positional parameter 2 (BUFR type) must be 3-digits"
  echo
  echo "See comments at top of script $0 "
  echo
  exit 255
fi

if [ "${#3}" -ne '3' ]; then
  echo
  echo " ===> Positional parameter 3 (BUFR subtype) must be 3-digits"
  echo
  echo "See comments at top of script $0 "
  echo
  exit 255
fi

if [ $2 -lt 0 -o $2 -gt 255 ]; then
  echo
  echo " ===> Pos. parameter 2 (BUFR type) must range between 000 and 255"
  echo
  echo "See comments at top of script $0 "
  echo
  exit 255
fi

if [ $3 -lt 0 -o $3 -gt 255 ]; then
  echo
  echo " ===> Pos. parameter 3 (BUFR subtype) must range between 000 and 255"
  echo
  echo "See comments at top of script $0 "
  echo
  exit 255
fi


lsd=`ls -d /com/arch/prod/obcount_30day/$1/*`

set -x

for dir in $lsd; do
   cd $dir
   lsf=`ls`
   for file in $lsf; do
    cp -p $file $file.save
    grep -v "$2.$3" $file.save > $file
    rm $file.save
   done
done

