#!/bin/sh
##############################################################################
#
#  make_batchfile
#
#  This script unloads pertinent information from a current basin layer
#  to make a suitable batchfile.
#
#  Usage:
#  make_batchfile <basin_layer> <output_file>
#
#  It assumes that the environment variables GISRS_DATABASE and
#  GISRS_SCHEMA are set to the corresponding database and schema names
#
#  History:
#  Anders Nilsson, UCAR, 09/13/06, Created
#  Anders Nilsson, UCAR, 10/02/06, Fixed inclusion of CWA
#
#############################################################################

### See if -h ###

    if [ "$1" = -h ]
    then
       echo Usage:
       echo "  make_batchfile <basin_layer> <output_file>"
       exit 1
    fi

### Check correct number of arguments ###

    if [ $# -ne 2 ]
    then
       echo "$# Incorrect number of arguments. Should be 2." 1>&2
       echo
       echo Usage:
       echo "  make_batchfile <basin_layer> <output_file>"
       exit 1
    fi


### Check presence of psql executable ###

    which psql 1>/dev/null

    if [ $? -ne 0 ]
    then
       echo Unable to find psql executable
       exit 1
    fi

### Check inputs ###

    BASINLAYER="$1"
    OUTPUTFILE="$2"

    if [ -z "${BASINLAYER}" ]
    then
       echo Basin layer name not specified
       exit 1
    fi

    if [ -z "${OUTPUTFILE}" ]
    then
       echo Output file not specified
       exit 1
    fi

### Get product group table name ###

    if [ -z ${GISRS_SCHEMA} ]
    then
       PRODUCTTABLE=product
    else
       PRODUCTTABLE=${GISRS_SCHEMA}.product
    fi

    COMMAND="select tabname from ${PRODUCTTABLE} where layer = '${BASINLAYER}'"
    TABNAME=`psql -tc "${COMMAND}" ${GISRS_DATABASE}`
    
    if [ $? -ne 0 ]
    then
       echo Unable to query database/schema ${GISRS_DATABASE} ${GISRS_SCHEMA}
       exit 1
    fi
    if [ -z ${TABNAME} ]
    then
       echo Basin layer $BASINLAYER not found
       exit 1
    fi

### Get attribute table name ###

    if [ -z ${GISRS_SCHEMA} ]
    then
       GROUPTABLE=${TABNAME}
    else
       GROUPTABLE=${GISRS_SCHEMA}.${TABNAME}
    fi

    COMMAND="select obj_attrib_table from ${GROUPTABLE} where gis_layer = '${BASINLAYER}'"
    ATTRIBUTETABLE=`psql -t -c "${COMMAND}" ${GISRS_DATABASE}`

    if [ $? -ne 0 ]
    then
       echo Unable to query database/schema ${GISRS_DATABASE} ${GISRS_SCHEMA}
       exit 1
    fi
    if [ -z ${ATTRIBUTETABLE} ]
    then
       echo Basin layer ${BASINLAYER} not found
       exit 1
    fi

### Unload table contents to file ###

    if [ -z ${GISRS_SCHEMA} ]
    then
       FULLATTRIBUTETABLE=${ATTRIBUTETABLE}
    else
       FULLATTRIBUTETABLE=${GISRS_SCHEMA}.${ATTRIBUTETABLE}
    fi

    psql -to ${OUTPUTFILE} ${GISRS_DATABASE} << NOHRSC
    select trim ( rfc ), trim ( ch5_id ), trim ( name ), trim ( cwa ),
           trim ( forecast_group ), trim ( point_type ),
           trunc ( pnt_lat_user ),
           trunc ( ( pnt_lat_user - trunc ( pnt_lat_user ) ) * 60 ),
           trunc ( ( pnt_lat_user - trunc ( pnt_lat_user ) ) * 3600) -
           ( trunc ( ( pnt_lat_user - trunc ( pnt_lat_user ) ) * 60 ) * 60 ),
           trunc ( pnt_lng_user * ( -1 ) ),
           trunc ( ( trunc ( pnt_lng_user ) - pnt_lng_user ) * 60 ),
           trunc ( ( trunc ( pnt_lng_user ) - pnt_lng_user ) * 3600 ) -
           ( trunc ( ( trunc ( pnt_lng_user ) - pnt_lng_user ) * 60 ) * 60 ),
           trim ( description ), trunc ( total_area ), trim ( user_attribute )
    from ${FULLATTRIBUTETABLE} ;

NOHRSC

    if [ $? -ne 0 ]
    then
       echo Unable to query database/schema $GISRS_DATABASE $GISRS_SCHEMA
       exit 1
    fi

### Done ###

    exit 0

