#!/bin/ksh
#
#
#       dv_cleanup version 5.0
#       original author Scott Bartholoma, NWIS
#       This is OSW Scripts page version.
#       Refer questions to "gs-w osw scripts"

#    Purpose: Clean up historical DV's while circumventing normal data-aging rules and
#             optionally prevent the storing of future DV's for the provided stat in
#             the specified processor.
#    History:
#      
#       5/2011  version 0.1 - Initial version. Scott Bartholoma
#   10/03/2011  version 1.0 - Updated DV inventory listing after delete to show
#                             dates inventory is covering (not entire period of
#                             record...)-SDB
#   01/24/2012  version 1.1 - Fixed bug causing script to fail when the -p option
#                             was used and more than one processor was in effect for
#                             the applicable start date. Fixed up usage info. a bit
#                             and added version information to it and stdout.-SDB/WJW
#
#   06/12/2012  version 5.0 - Updated for NWIS 5.0, fixed a few typos, and several
#                             other minor changes. Removed rollback accidentally left
#                             in during testing. -SDB/WJW
#
#
version='5.0'
############################## START OF FUNCTIONS ##############################
#
# Functions to perform the various types of retrievals
#
################################################################################

# function to display the usage information
usage()
{
    echo
    echo "dv_cleanup version $version"
    echo
    echo 'Usage: dv_cleanup [-z db_no] [-a agency_cd] -n site_no -d dd_nu'
    echo '      [-b start_dt (yyyymmdd)] [-e end_dt (yyyymmdd)] [-p]'
    echo '      stat_cd stat_cd . . . . .'
    echo
    echo ' where:'
    echo '   -z if omitted defaults to database 01'
    echo '   -a if omitted defaults to "USGS"'
    echo '   -b if omitted defaults to the beginning of period of record'
    echo '   -e if omitted defaults to the end of period of record'
    echo ' '
    echo '   if -p is specified, operates in processor mode:'
    echo '     If the DD specified is an input DD, the processor it is input for'
    echo '        will be used.'
    echo '     The begin date argument will be used to find the processor that is in'
    echo '       effect on the given date.'
    echo '     The end date argument will be ignored, if present.'
    echo '     Daily values for the specified stat codes will be deleted starting at the'
    echo '       begin date of the processor through the end date of the processor.'
    echo '     The processor itself will have the stat code removed, if present.'
    echo
}

# function to get and verify a table name from the database
get_table_name()
{
    # Use tsql to get the table name suffix from the database and construct
    # the actual table name
    #
    # Use like this:
    #
    # get_table_name dd
    # ddtbl=${table_name}
    #
    lcDDLnm=`echo "${1}" | tr '[A-Z]' '[a-z]'`
    ucDDLnm=`echo "${1}" | tr '[a-z]' '[A-Z]'`
    query="SELECT suffix_tx FROM ${NWIS_SCHEMA}.master_table \
           WHERE ddl_nm='${ucDDLnm}_##' AND db_no='${db_no}'"
    ret_table_name=${lcDDLnm}`/usr/local/bin/tsql ${NWISDB} "${query}"`
    # Verify we actually got a table name
    if [ -z "${ret_table_name}" ] ; then
	echo
	echo **** Fatal error - no table name retrieved for ddl_nm ${ucDDLnm}'_##' database ${db_no}
	echo
	exit 7
    fi
    table_name="${NWIS_SCHEMA}.${ret_table_name}"
}

############################### END OF FUNCTIONS ###############################
#
# Process the arguments
while getopts a:n:d:b:e:z:p arg ; do
  case $arg in
      a) agency_cd_in=$OPTARG;;
      n) site_no=$OPTARG;;
      d) dd_nu=$OPTARG;;
      b) start_dt=$OPTARG;;
      e) end_dt=$OPTARG;;
      z) db_no=$OPTARG;;
      p) proc_mode=true;;
      \?) usage
          exit 2;;
  esac
done
shift `expr $OPTIND - 1`

# check for site number
if [ -z "${site_no}" ] ; then
    echo
    echo Site number not specified.
    usage
    exit 3;
fi

# check for dd number
if [ -z "${dd_nu}" ] ; then
    echo
    echo DD number not specified.
    usage
    exit 3;
fi

# check for at least one stat code argument
if [ -z "${1}" ] ; then
    echo
    echo No stat codes specified on the command line,
    usage
    exit 3;
fi

# Determine if user has access to do this
if [ ! `whoami` = 'nwis' ] ; then
     echo ""
     echo "You are running as `whoami`. "
     echo "This script needs to be run as user nwis."
     echo "Therefore you can not run this script."
     echo
     echo "EXITING....."
     echo
     exit 1
fi

if [ -z "${agency_cd_in}" ] ; then
    # default agency code to USGS if not specified
    agency_cd="USGS"
else
    # convert agency code to upper case
    agency_cd=`echo ${agency_cd_in} | tr '[a-z]' '[A-Z]'`
fi

# default database number to 01 if not specified
if [ -z "${db_no}" ] ; then
    db_no=01
fi

#
# Make sure db_no is 2 digits
if [ `echo ${db_no} | wc -c` -lt 3 ] ; then
    db_no=0${db_no}
fi

echo
echo "dv_cleanup version $version"
echo

#
# Source the nwis environment
. /usr/opt/etc/nwis.profile

#
# Source the wrapper
. $NWISHOME/util/nwo_set_nwis_env

#
# Create query to set date format to get yyyy-mm-dd style dates
dtFormatQuery="alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"
dtOnlyFormatQuery="alter session set NLS_DATE_FORMAT='YYYY-MM-DD'"
tsFormatQuery="alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS'"

# get table names
get_table_name sitefile
sitetbl=${table_name}

get_table_name loc
loctbl=${table_name}

get_table_name dd
ddtbl=${table_name}

get_table_name dv
dvtbl=${table_name}

get_table_name dv_data
dvdatatbl=${table_name}

get_table_name dv_diff
dvdifftbl=${table_name}

get_table_name ts_comp_inst
tscomptbl=${table_name}

get_table_name dd_ts_comp_inst
ddtscomptbl=${table_name}

get_table_name dv_comp_inst
dvcomptbl=${table_name}

#
# Get the tz_cd values from the sitefile
str1='SELECT trim(tz_cd) || local_time_fg FROM '${sitetbl}
str2=" WHERE agency_cd='${agency_cd}' AND site_no='${site_no}'"
query=$str1$str2
tz=$(tsql ${NWISDB} "${query}")
if [ -z "${tz}" ] ; then
    echo "***  Station ${agency} ${site_no} not found, check arguments and try again  ***"
    exit 6
else
    # Get the Ingres timezone that matches
    case "${tz}" in
	AFTY)
	    tzcd="+04:00"
	    ;;
	AKSTY)
	    tzcd="America/Anchorage"
	    ;;
	ASTY)
	    tzcd="Canada-Atlantic"
	    ;;
	AWSTY)
	    tzcd="Australia/West"
	    ;;
	BTY)
	    tzcd="+03:00"
	    ;;
	CSTY)
	    tzcd="America/Chicago"
	    ;;
	DSTY)
	    tzcd="+01:00"
	    ;;
	EETY)
	    tzcd="EET"
	    ;;
	ESTY)
	    tzcd="America/New_York"
	    ;;
	GMTY)
	    tzcd="+00:00"
	    ;;
	GSTY)
	    tzcd="+10:00"
	    ;;
	HSTY)
	    tzcd="US/HAWAII"
	    ;;
	IDLEY)
	    tzcd="+12:00"
	    ;;
	JSTY)
	    tzcd="JAPAN"
	    ;;
	MSTY)
	    tzcd="America/Denver"
	    ;;
	NZTY)
	    tzcd="NEW-ZEALAND"
	    ;;
	PSTY)
	    tzcd="America/Los_Angeles"
	    ;;
	SATY)
	    tzcd="Australia/South"
	    ;;
	UTCY)
	    tzcd="+00:00"
	    ;;
	WASTY)
	    tzcd="Australia/West"
	    ;;
	ZP-11Y)
	    tzcd="-11:00"
	    ;;
	ZP11Y)
	    tzcd="+11:00"
	    ;;
	ZP4Y)
	    tzcd="+04:00"
	    ;;
	ZP5Y)
	    tzcd="+05:00"
	    ;;
	ZP6Y)
	    tzcd="+06:00"
	    ;;
	AFTN)
	    tzcd="+04:00"
	    ;;
	AKSTN)
	    tzcd="-09:00"
	    ;;
	ASTN)
	    tzcd="-04:00"
	    ;;
	AWSTN)
	    tzcd="+08:00"
	    ;;
	BTN)
	    tzcd="+03:00"
	    ;;
	CSTN)
	    tzcd="-06:00"
	    ;;
	DSTN)
	    tzcd="+01:00"
	    ;;
	EETN)
	    tzcd="+02:00"
	    ;;
	ESTN)
	    tzcd="-05:00"
	    ;;
	GMTN)
	    tzcd="+00:00"
	    ;;
	GSTN)
	    tzcd="+10:00"
	    ;;
	HSTN)
	    tzcd="-10:00"
	    ;;
	IDLEN)
	    tzcd="+12:00"
	    ;;
	JSTN)
	    tzcd="+09:00"
	    ;;
	MSTN)
	    tzcd="-07:00"
	    ;;
	NZTN)
	    tzcd="+12:00"
	    ;;
	PSTN)
	    tzcd="-08:00"
	    ;;
	SATN)
	    tzcd="+09:00"
	    ;;
	UTCN)
	    tzcd="+00:00"
	    ;;
	WASTN)
	    tzcd="+07:00"
	    ;;
	ZP-11N)
	    tzcd="-11:00"
	    ;;
	ZP11N)
	    tzcd="+11:00"
	    ;;
	ZP4N)
	    tzcd="+04:00"
	    ;;
	ZP5N)
	    tzcd="+05:00"
	    ;;
	ZP6N)
	    tzcd="+06:00"
	    ;;
	*)
	    echo
	    echo "Unhandled tz_cd \"${tz}\" using UTC"
	    tzcd='+00:00'
	    ;;
    esac
fi


#
# Create query to set time zone for INCOMING datefimes (-b argument)
tzQuery="alter session set TIME_ZONE='${tzcd}'"

# construct start date part of where clause
if [ -z "${start_dt}" ] ; then
    tswhere="";
else
    startlen=`print -n ${start_dt} | wc -c`
    if [ ${startlen} -ne 8 ] ; then
	echo
	echo Start date is not 8 digits long.
	exit 4
    fi
    year=`echo ${start_dt} | cut -c1-4`
    month=`echo ${start_dt} | cut -c5-6`
    day=`echo ${start_dt} | cut -c7-8`
    tswhere=" AND ts.ts_comp_start_dt <= '${year}-${month}-${day} 12:00:00' AND ts.ts_comp_end_dt >=  '${year}-${month}-${day} 12:00:00' "
fi

query="SELECT d.dd_nu, 'Parm',d.parm_cd, 'Official',d.dd_official_fg \
       FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d \
       WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
       d.loc_id=l.loc_id AND d.dd_nu=${dd_nu}"
echo
echo '***** 'For ${agency_cd} ${site_no}, DD `tsql ${NWISDB} "${query}"`:

# get the dd_id
cleanup_dd_id=`tsql $NWISDB -Gnwis_select "select d.dd_id from ${sitetbl} s, ${loctbl} l, ${ddtbl} d where s.agency_cd='${agency_cd}' and s.site_no='${site_no}' and l.site_id=s.site_id and d.loc_id=l.loc_id and d.dd_nu=${dd_nu}"`
if [ -z "${cleanup_dd_id}" ] ; then
    echo "***  DD ${dd_nu} for station ${agency_cd} ${site_no} not found, check arguments and try again  ***"
    exit 5
fi

FS='\t'
# are we in processor mode?
if [ ! -z "${proc_mode}" ] ; then
    # Get list of processors for this DD active on the supplied begin data (if any) 
    query="SELECT DISTINCT ts.ts_comp_inst_id, ts.ts_comp_inst_cd, \
             FROM_TZ(CAST(ts.ts_comp_start_dt AS TIMESTAMP),'+00:00') AT TIME ZONE '${tzcd}' AS start_dt, \
             FROM_TZ(CAST(ts.ts_comp_end_dt AS TIMESTAMP),'+00:00') AT TIME ZONE '${tzcd}' \
             FROM ${ddtscomptbl} dts, ${tscomptbl} ts, ${ddtbl} d, ${loctbl} l, ${sitetbl} s \
             WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
             d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dts.dd_id=d.dd_id AND \
             ts.ts_comp_inst_id=dts.ts_comp_inst_id ${tswhere} \
             ORDER BY ts.ts_comp_inst_cd, start_dt"
    tsql ${NWISDB} "${dtFormatQuery}" "${tsFormatQuery}" "${tzQuery}" "${query}" >| /tmp/$$.ts_comp_inst_list
    nproc=`wc -l < /tmp/$$.ts_comp_inst_list`

    if [ ${nproc} -eq 0 ] ; then
	echo
	echo No active processors found.
	echo
	exit 4
    elif [ ${nproc} -eq 1 ] ; then
	read ts_comp_inst_id ts_comp_inst_cd ts_comp_start_dt ts_comp_start_tm ts_comp_end_dt ts_comp_end_tm < /tmp/$$.ts_comp_inst_list
    else
	answer=0
	i=0
	echo 
	echo 'Available processors: '
	echo
	while read inst_id inst_cd start_dt start_tm end_dt end_tm ; do
	    i=`expr ${i} + 1`
	    echo ${i} ${inst_cd} from ${start_dt} @ ${start_tm} to ${end_dt} @ ${end_tm}
	done < /tmp/$$.ts_comp_inst_list
	while [ ${answer} -lt 1 ] || [ ${answer} -gt ${i} ] ; do
	    echo
	    print -n 'Enter number of desired processor: '
	    read answer
	done
	i=0;
	while read inst_id inst_cd start_dt start_tm end_dt end_tm; do
	    i=`expr ${i} + 1`
	    if [ ${i} -eq ${answer} ] ; then
		ts_comp_inst_id=${inst_id}
		ts_comp_inst_cd=${inst_cd}
		ts_comp_start_dt=${start_dt}
		ts_comp_start_tm=${start_tm}
		ts_comp_end_dt=${end_dt}
		ts_comp_end_tm=${end_tm}
	    fi
	done < /tmp/$$.ts_comp_inst_list
    fi

    echo
    echo '***** 'Using ${ts_comp_inst_cd} processor in effect from ${ts_comp_start_dt} @ ${ts_comp_start_tm} to ${ts_comp_end_dt} @ ${ts_comp_end_tm}

    # Set start date to beginning of processor
    year=`echo ${ts_comp_start_dt} | cut -c1-4`
    month=`echo ${ts_comp_start_dt} | cut -c6-7`
    day=`echo ${ts_comp_start_dt} | cut -c9-10`
    start_dt=${year}${month}${day}
    echo "Start date set to ${start_dt}"

    # Set end date to end of processor
    year=`echo ${ts_comp_end_dt} | cut -c1-4`
    month=`echo ${ts_comp_end_dt} | cut -c6-7`
    day=`echo ${ts_comp_end_dt} | cut -c9-10`
    end_dt=${year}${month}${day}
    echo "  End date set to ${end_dt}"
fi

# construct start date part of where clause
if [ -z "${start_dt}" ] ; then
    startdisp="BOP"
    startwhere="";
    startwywhere="";
else
    startlen=`print -n ${start_dt} | wc -c`
    if [ ${startlen} -ne 8 ] ; then
	echo
	echo Start date is not 8 digits long.
	exit 4
    fi
    startdisp="${start_dt}"
    year=`echo ${start_dt} | cut -c1-4`
    month=`echo ${start_dt} | cut -c5-6`
    day=`echo ${start_dt} | cut -c7-8`
    startwhere="AND dvd.dv_dt >= '${year}-${month}-${day}' "
    if [ ${month} -ge 10 ] ; then
	startwy=`expr ${year} + 1`
    else
	startwy=${year}
    fi
    startwywhere="AND dv.dv_water_yr >= ${startwy} "
fi

# construct end date part of where clause
if [ -z "${end_dt}" ] ; then
    enddisp="EOP"
    endwhere="";
    endwywhere="";
else
    endlen=`print -n ${end_dt} | wc -c`
    if [ ${endlen} -ne 8 ] ; then
	echo
	echo End date is not 8 digits long.
	exit 4
    fi
    enddisp="${end_dt}"
    year=`echo ${end_dt} | cut -c1-4`
    month=`echo ${end_dt} | cut -c5-6`
    day=`echo ${end_dt} | cut -c7-8`
    endwhere="AND dvd.dv_dt <= '${year}-${month}-${day}' "
    if [ ${month} -ge 10 ] ; then
	endwy=`expr ${year} + 1`
    else
	endwy=${year}
    fi
    endwywhere="AND dv.dv_water_yr <= ${endwy} "
fi

#
# Write rdb file headers
echo "Type\tAgency\tStation\tDD\tPrimary\tParm\tStat\tNdays\tFrom\tTo\tBegins\tEnds" >| /tmp/$$.dv_cleanup.rdb
echo "6S\t6S\t15S\t4N\t7S\t5S\t5S\t8N\t12N\t12N\t12S\t12S" >> /tmp/$$.dv_cleanup.rdb

#
# Start a file of sql commands
echo "set echo on;" >|  /tmp/$$.dv_cleanup.sql
echo "${dtFormatQuery};" >> /tmp/$$.dv_cleanup.sql
echo "${tzQuery};" >> /tmp/$$.dv_cleanup.sql
delete=false

statlist=""

#
# Loop through the stat codes on the argument list
echo
while [ ! -z "${1}" ] ; do
  stat_cd=${1}

  # make stat code 5 digits long by prepending zeros
  while [ `echo ${stat_cd} | wc -c` -lt 6 ] ; do 
      stat_cd=0${stat_cd}
  done

  echo
  echo '***** '"Inventorying stat code ${stat_cd} from ${startdisp} to ${enddisp}"

  statlist="${statlist} ${stat_cd}"

  delforstat=false

  #
  # Get the DV inventory information
  rm -f  /tmp/$$.dv_data_cleanup.rdb
  query="SELECT 'dvdata',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd, \
         COUNT(dvd.dv_dt),MIN(dvd.dv_va),MAX(dvd.dv_va),MIN(dvd.dv_dt),MAX(dvd.dv_dt) \
         FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvtbl} dv, ${dvdatatbl} dvd \
         WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
         d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id AND dv.stat_cd='${stat_cd}' AND \
         dvd.dv_id=dv.dv_id ${startwhere} ${endwhere} \
         GROUP BY s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd \
         ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dv.stat_cd"
  tsql ${NWISDB} "${dtOnlyFormatQuery}" "${tzQuery}" "${query}" >> /tmp/$$.dv_data_cleanup.rdb
  if [ -s "/tmp/$$.dv_data_cleanup.rdb" ] ; then
      delforstat=true
      cat /tmp/$$.dv_data_cleanup.rdb >> /tmp/$$.dv_cleanup.rdb
      rm -f /tmp/$$.dv_data_cleanup.rdb

      #
      # Write the SQL to do the DV_DATA_## delete
      echo "" >> /tmp/$$.dv_cleanup.sql
      echo "/* Delete the DV_DATA_## table entries */" >> /tmp/$$.dv_cleanup.sql
      echo "DELETE FROM ${dvdatatbl} dvd " >> /tmp/$$.dv_cleanup.sql
      echo "WHERE dvd.dv_id IN (" >> /tmp/$$.dv_cleanup.sql
      echo "    SELECT distinct dv.dv_id FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, " >> /tmp/$$.dv_cleanup.sql
      echo "        ${dvtbl} dv, ${dvdatatbl} dvd " >> /tmp/$$.dv_cleanup.sql
      echo "    WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id" >> /tmp/$$.dv_cleanup.sql
      echo "        AND d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id" >> /tmp/$$.dv_cleanup.sql
      echo "        AND dv.stat_cd='${stat_cd}' AND dvd.dv_id=dv.dv_id" >> /tmp/$$.dv_cleanup.sql
      echo "        ${startwhere} ${endwhere})" >> /tmp/$$.dv_cleanup.sql
      echo "    ${startwhere} ${endwhere};" >> /tmp/$$.dv_cleanup.sql
  fi

  rm -f  /tmp/$$.dv_diff_cleanup.rdb
  query="SELECT 'dvdiff',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd, \
         COUNT(dvd.dv_dt),MIN(dvd.dv_va),MAX(dvd.dv_va),MIN(dvd.dv_dt),MAX(dvd.dv_dt) \
         FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvtbl} dv, ${dvdifftbl} dvd \
         WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
         d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id AND dv.stat_cd='${stat_cd}' AND \
         dvd.dv_id=dv.dv_id ${startwhere} ${endwhere} \
         GROUP BY s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd \
         ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dv.stat_cd"
  tsql ${NWISDB} "${dtOnlyFormatQuery}" "${query}" >> /tmp/$$.dv_diff_cleanup.rdb
  if [ -s "/tmp/$$.dv_diff_cleanup.rdb" ] ; then
      delforstat=true
      cat /tmp/$$.dv_diff_cleanup.rdb >> /tmp/$$.dv_cleanup.rdb
      rm -f /tmp/$$.dv_diff_cleanup.rdb

      #
      # Write the SQL to do the DV_DIFF_## delete
      echo "" >> /tmp/$$.dv_cleanup.sql
      echo "/* Delete the DV_DIFF_## table entries */" >> /tmp/$$.dv_cleanup.sql
      echo "DELETE FROM ${dvdifftbl} dvd " >> /tmp/$$.dv_cleanup.sql
      echo "WHERE dvd.dv_id IN (" >> /tmp/$$.dv_cleanup.sql
      echo "    SELECT distinct dv.dv_id FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, " >> /tmp/$$.dv_cleanup.sql
      echo "        ${dvtbl} dv, ${dvdifftbl} dvd " >> /tmp/$$.dv_cleanup.sql
      echo "    WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id" >> /tmp/$$.dv_cleanup.sql
      echo "        AND d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id" >> /tmp/$$.dv_cleanup.sql
      echo "        AND dv.stat_cd='${stat_cd}' AND dvd.dv_id=dv.dv_id" >> /tmp/$$.dv_cleanup.sql
      echo "        ${startwhere} ${endwhere})" >> /tmp/$$.dv_cleanup.sql
      echo "    ${startwhere} ${endwhere};" >> /tmp/$$.dv_cleanup.sql
  fi
      
  if [ "${delforstat}" = "true" ] ; then
      delete=true

      #
      # Write SQL to cleanup any DV_## entries
      echo "" >> /tmp/$$.dv_cleanup.sql
      echo "/* Delete any DV_## table entries with no child entries in DV_DATA_## or DV_DIFF_## */" >> /tmp/$$.dv_cleanup.sql
      echo "DELETE FROM ${dvtbl} WHERE dv_ID in (SELECT dv.dv_id" >> /tmp/$$.dv_cleanup.sql
      echo "FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, (${dvtbl} dv " >> /tmp/$$.dv_cleanup.sql
      echo "    LEFT JOIN ${dvdatatbl} dvd ON dvd.dv_id=dv.dv_id" >> /tmp/$$.dv_cleanup.sql
      echo "    LEFT JOIN ${dvdifftbl} dvf ON dvf.dv_id=dv.dv_id)" >> /tmp/$$.dv_cleanup.sql
      echo "WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND " >> /tmp/$$.dv_cleanup.sql
      echo "    l.site_id=s.site_id AND d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND " >> /tmp/$$.dv_cleanup.sql
      echo "    dv.dd_id=d.dd_id AND dv.stat_cd='${stat_cd}' ${startwywhere} ${endwywhere}" >> /tmp/$$.dv_cleanup.sql
      echo "GROUP BY dv.dv_id" >> /tmp/$$.dv_cleanup.sql
      echo "HAVING COUNT(dvd.dv_dt)=0 AND COUNT(dvf.dv_dt)=0);" >> /tmp/$$.dv_cleanup.sql
  fi

  if [ ! -z "${proc_mode}" ] ; then
      rm -f  /tmp/$$.dv_comp_cleanup.rdb
      query="SELECT 'proc',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd, \
            dci.stat_cd,'--','--','--','--','--' \
            FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvcomptbl} dci 
            WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
            d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dci.dd_id=d.dd_id AND \
            dci.ts_comp_inst_id=${ts_comp_inst_id} AND dci.stat_cd=${stat_cd} \
            ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dci.stat_cd"
      tsql ${NWISDB} "${dtOnlyFormatQuery}" "${query}" >> /tmp/$$.dv_comp_cleanup.rdb
      cat /tmp/$$.dv_comp_cleanup.rdb
      if [ -s "/tmp/$$.dv_comp_cleanup.rdb" ] ; then
	  delete=true
	  cat /tmp/$$.dv_comp_cleanup.rdb >> /tmp/$$.dv_cleanup.rdb
	  rm -f /tmp/$$.dv_comp_cleanup.rdb
	  echo "" >> /tmp/$$.dv_cleanup.sql
	  echo "/* Delete stat code FROM the processor DV_COMP_INST_## table */" >> /tmp/$$.dv_cleanup.sql
	  echo "DELETE FROM ${dvcomptbl} " >> /tmp/$$.dv_cleanup.sql
          echo "WHERE ts_comp_inst_id=${ts_comp_inst_id} AND dd_id=${cleanup_dd_id} AND " >> /tmp/$$.dv_cleanup.sql
	  echo "     stat_cd=${stat_cd};">> /tmp/$$.dv_cleanup.sql
      fi
  fi
  
   #
   # shift this stat code off the argument list
  shift 1
done
echo "" >> /tmp/$$.dv_cleanup.sql
#echo "rollback;" >> /tmp/$$.dv_cleanup.sql
#echo "" >> /tmp/$$.dv_cleanup.sql
echo 'quit' >> /tmp/$$.dv_cleanup.sql

if [ "${delete}" != "true" ] ; then
    echo
    echo "Nothing to be deleted."
else
    # Report
    echo 
    echo '***** 'Data to be deleted:
    echo
    sorttbl Agency Station DD Stat Type < /tmp/$$.dv_cleanup.rdb | ptbl -l132 -p0
    echo
    print -n 'Ok to proceed with delete? (Y/N): '
    read answer
    if [ "${answer}" = 'y' ] || [ "${answer}" = 'Y' ] ; then
	echo
	echo '***** Processing sql to delete requested data'
	sqlplus /@${NWIS_HOST} @/tmp/$$.dv_cleanup.sql

	# Do an after inventory
	echo "Type\tAgency\tStation\tDD\tPrimary\tParm\tStat\tNdays\tFrom\tTo\tBegins\tEnds" >| /tmp/$$.dv_cleanup.after.rdb
	echo "6S\t6S\t15S\t4N\t7S\t5S\t5S\t8N\t12N\t12N\t12S\t12S" >> /tmp/$$.dv_cleanup.after.rdb

	for stat_cd in ${statlist} ; do
	    query="SELECT 'dvdata',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd,\
                  COUNT(dvd.dv_dt),MIN(dvd.dv_va),MAX(dvd.dv_va),MIN(dvd.dv_dt),MAX(dvd.dv_dt) \
                  FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvtbl} dv, ${dvdatatbl} dvd \
                  WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
                  d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id AND \
                  dv.stat_cd='${stat_cd}' AND dvd.dv_id=dv.dv_id ${startwhere} ${endwhere} \
                  GROUP BY s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd \
                  ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dv.stat_cd"
	    tsql ${NWISDB} "${dtOnlyFormatQuery}" "${query}" >> /tmp/$$.dv_cleanup.after.rdb
	    
	    query="SELECT 'dvdiff',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd,\
                  COUNT(dvd.dv_dt),MIN(dvd.dv_va),MAX(dvd.dv_va),MIN(dvd.dv_dt),MAX(dvd.dv_dt) \
                  FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvtbl} dv, ${dvdifftbl} dvd \
                  WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
                  d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dv.dd_id=d.dd_id AND \
                  dv.stat_cd='${stat_cd}' AND dvd.dv_id=dv.dv_id ${startwhere} ${endwhere} \
                  GROUP BY s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,dv.stat_cd \
                  ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dv.stat_cd"
	    tsql ${NWISDB} "${dtOnlyFormatQuery}" "${query}" >> /tmp/$$.dv_cleanup.after.rdb

	    query="SELECT  'proc',s.agency_cd,s.site_no,d.dd_nu,d.dd_official_fg,d.parm_cd,\
                  dci.stat_cd,'--','--','--','--','--' \
                  FROM ${sitetbl} s, ${loctbl} l, ${ddtbl} d, ${dvcomptbl} dci \
                  WHERE s.agency_cd='${agency_cd}' AND s.site_no='${site_no}' AND l.site_id=s.site_id AND \
                  d.loc_id=l.loc_id AND d.dd_nu=${dd_nu} AND dci.dd_id=d.dd_id AND \
                  dci.ts_comp_inst_id=${ts_comp_inst_id} AND dci.stat_cd=${stat_cd} \
                  ORDER BY s.agency_cd,s.site_no,d.dd_nu,d.parm_cd,dci.stat_cd"
	    if [ ! -z "${proc_mode}" ] ; then
		tsql ${NWISDB} "${dtOnlyFormatQuery}"  "${query}" >> /tmp/$$.dv_cleanup.after.rdb
	    fi
	done
	echo 
	echo '***** '"After data delete inventory from ${startdisp} to ${enddisp}"
:
	echo
	sorttbl Agency Station DD Stat Type < /tmp/$$.dv_cleanup.after.rdb | ptbl -l132 -p0
    else
	echo
	echo "Exiting at user request."
    fi
fi

# finished, cleanup
rm -f /tmp/$$.dv_comp_cleanup.rdb
rm -f /tmp/$$.dv_data_cleanup.rdb
rm -f /tmp/$$.dv_diff_cleanup.rdb
rm -f /tmp/$$.dv_cleanup.rdb
rm -f /tmp/$$.dv_cleanup.after.rdb
rm -f /tmp/$$.dv_cleanup.sql
echo
