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

#*************************************************************
# Purpose: Display environmental measurements stored in
#          Site Visit.
#*************************************************************


#    History:
#      
#       10/2012  Initial version under NWIS 5.0. Scott Bartholoma
#       01/2013  Added OSW scripts documentation to script comments and added file
#                output option. Wade Walker
#       
version=5.0
############################### START OF FUNCTIONS ##############################
# 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: get_sv_emeas [-a agency_cd] [-z db_no] [-p parm_cd ] [-o outfile] site_no site_no site_no . . .'
    echo
    echo '	if agency code (agency_cd) is missing will default to USGS'
    echo '	if database number (db_no) is missing will default to db01'
    echo '	if output file (outfile) is missing output will be sent to the terminal'
    echo '      -p if present will result in only the specified parameter code being retrieved'
    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]'`
    ret_table_name=${lcDDLnm}`/usr/local/bin/tsql ${NWISDB} "select suffix_tx \
        from ${NWIS_SCHEMA}.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 ###############################
#
# Process the arguments
while getopts a:z:p:o: arg
do
  case $arg in
      a) agency_cd=$OPTARG;;
      z) db_no=$OPTARG;;
      p) parm_cd=$OPTARG;;
	  o) outfile=$OPTARG;;
      \?) usage
      exit 1;;
  esac
done
shift `expr $OPTIND - 1`

if [ -z "${1}" ]
then
    echo 
    echo 'No site numbers supplied on the command line'
    usage
    exit 1
fi

if [ -z "${agency_cd}" ] ; then
    agency_cd='USGS'
fi

if [ -z "${db_no}" ] ; then
    db_no=01
fi

if [ -z "${parm_cd}" ] ; then
    parmWhere=''
else
    #
    # Make database number 2 digits if needed
    if [ `echo ${parm_cd} | wc -c` -lt 6 ] ;     then
	parm_cd=0${parm_cd}
    fi

    parmWhere="and em.parm_cd = '${parm_cd}'"
fi

#send output to controlling terminal if outfile not specified.
#Note we're not checking for existing files, write access, etc...
if [ -z "${outfile}" ] ; then
    outfile='/dev/tty'
else
    echo "Output sent to file ${outfile}"
fi	

#
# source the NWIS environment
. /usr/local/etc/nwis.profile
. $NWISHOME/util/nwo_set_nwis_env

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

get_table_name site_visit
svtbl=${table_name}

get_table_name site_visit_emeas
emtbl=${table_name}

#
# Set query to set dates to  yyyy-mm-dd format
dtFormatQuery="alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"

#
# write rdb table header lines
echo 'agency\tsite\tsvdate\temdate\tparm\tmeth\tvalue' > ${outfile}
echo '6S\t15S\t20D\t20D\t5S\t5S\t16N' >> ${outfile}

#
# Loop through the sites on the argument list
while [ ! -z "${1}" ] ; do
    #
    # get the data
    query="select s.agency_cd,s.site_no,sv.site_visit_start_dt,\
      em.site_visit_emeas_dt,em.parm_cd,em.meth_cd,em.site_visit_emeas_va \
      from ${sitetbl} s, ${svtbl} sv, ${emtbl} em \
      where s.agency_cd='${agency_cd}' and s.site_no='${1}' and sv.site_id=s.site_id and \
      em.site_visit_id=sv.site_visit_id ${parmWhere} \
      order by s.agency_cd,s.site_no,sv.site_visit_start_dt, \
      em.site_visit_emeas_dt,em.parm_cd,em.meth_cd"                                               
    tsql ${NWISDB} "${dtFormatQuery}" "${query}" >> ${outfile}
    
    #
    # shift this site off the argument list
    shift 1
done
