#!/bin/ksh

#
#       meas4meter
#       original author Scott Bartholoma, NWIS
#       This is the OSW Scripts page version.
#       Refer questions to "gs-w osw scripts"

#*************************************************************
# Purpose: List all discharge measurements entered in a database
#          with specified serial number(s) or partial serial 
#          number(s).
#*************************************************************


#    History:
#      
#       04/2011  Initial version under NWIS 4.11. Scott Bartholoma
#       01/2013  Added OSW scripts documentation to script comments and updated
#                for NWIS 5.0/Oracle. Wade Walker
#       
version=5.0




############################## START OF FUNCTIONS ##############################
#
# Functions to perform the various types of retrievals
#
################################################################################

# function to display the usage information
usage()
{
    echo "Version $version"
    echo "Please address questions or comments to gs-w_osw_scripts@usgs.gov"
    echo
    echo Usage meas4meter [-z db_no] \'serial_no\' \'serial_no\' \'serial_no\' .......
    echo
}

#
# source the nwis wrapper scripts
. /usr/opt/etc/nwis.profile
. $NWISHOME/util/nwo_set_nwis_env

# 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]'`
  ret_table_name=${lcDDLnm}`/usr/local/bin/tsql ${NWISDB} "select suffix_tx \
        from master_table where ddl_nm='${ucDDLnm}_##' AND db_no='${db_no}'"`
  # 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  ##############################


#export II_DATE_FORMAT=SWEDEN

#
# Process the arguments
while getopts z: arg
do
  case $arg in
      z) db_no=$OPTARG;;
      \?) usage
      exit 2;;
  esac
done
shift `expr $OPTIND - 1`

# check for remaining sensor serial number arguments
if [ -z "${1}" ] ; then
    usage
    exit 1
fi

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

#
# Make database number 2 digits if needed
if [ `echo ${db_no} | wc -c` -lt 3 ]
then
    db_no=0${db_no}
fi

#
# get table names
get_table_name sensor
sensortbl=${table_name}
get_table_name sitefile
sitetbl=${table_name}
get_table_name site_visit
svtbl=${table_name}
get_table_name q_meas
qmtbl=${table_name}
get_table_name q_meas_chan
qmctbl=${table_name}
get_table_name q_meas_mdsc
qmmtbl=${table_name}
get_table_name q_meas_adcp
qmatbl=${table_name}


# Verify the meter exists
sensor_id=`tsql $NWISDB "select sensor_id from ${sensortbl} where sensor_ser_no like '${1}%'"`
if [ -z "${sensor_id}" ] ; then
    echo
    echo No meter found with serial number starting with ${1}
    echo
    exit 2
fi

# Display the measurements made with these meters
echo 'agency\tsite\tsvdtm\tmeasno\tchannu\tqmeth\tvmeth\tmeter' >| /tmp/$$.meas4meter.rdb
echo '6S\t15S\t20S\t6S\t6N\t6S\t6S\t32S' >> /tmp/$$.meas4meter.rdb

# loop through the serial number arguments
while [ ! -z "${1}" ] ; do
  # get Midsection measurements
  query="select s.agency_cd,s.site_no,sv.site_visit_start_dt,qm.q_meas_no,c.q_meas_chan_nu, \
c.q_meth_cd,st.meth_cd,n.sensor_ser_no from ${sitetbl} s, ${svtbl} sv, ${qmtbl} qm, \
${qmctbl} c, ${qmmtbl} md, ${sensortbl} n, sensor_type st where sv.site_id=s.site_id and \
qm.site_visit_id=sv.site_visit_id and c.site_visit_id=qm.site_visit_id and \
c.q_meas_no=qm.q_meas_no and md.site_visit_id=c.site_visit_id and \
md.q_meas_no=c.q_meas_no and md.q_meas_chan_nu=c.q_meas_chan_nu and \
n.sensor_id=md.sensor_id and sensor_ser_no like '${1}%' and st.sensor_type_id=n.sensor_type_id order by n.sensor_ser_no, \
s.agency_cd,s.site_no,sv.site_visit_start_dt,qm.q_meas_no,c.q_meas_chan_nu"

  # get ADCP measurements
  tsql $NWISDB -Gnwis_select "${query}" >> /tmp/$$.meas4meter.rdb
  query="select s.agency_cd,s.site_no,sv.site_visit_start_dt,qm.q_meas_no,c.q_meas_chan_nu, \
c.q_meth_cd,st.meth_cd,n.sensor_ser_no from ${sitetbl} s, ${svtbl} sv, ${qmtbl} qm, \
${qmctbl} c, ${qmatbl} md, ${sensortbl} n, sensor_type st where sv.site_id=s.site_id and \
qm.site_visit_id=sv.site_visit_id and c.site_visit_id=qm.site_visit_id and \
c.q_meas_no=qm.q_meas_no and md.site_visit_id=c.site_visit_id and \
md.q_meas_no=c.q_meas_no and md.q_meas_chan_nu=c.q_meas_chan_nu and \
n.sensor_id=md.sensor_id and sensor_ser_no like '${1}%' and st.sensor_type_id=n.sensor_type_id order by n.sensor_ser_no, \
s.agency_cd,s.site_no,sv.site_visit_start_dt,qm.q_meas_no,c.q_meas_chan_nu"
  tsql $NWISDB -Gnwis_select "${query}" >> /tmp/$$.meas4meter.rdb

  # shift this meter off the argument list
  shift 1
done

sorttbl meter agency site svdtm measno channu < /tmp/$$.meas4meter.rdb | ptbl -l132 -p0
rm -f /tmp/$$.meas4meter.rdb
