""" Code to select entries in a table

Context : SRP
Module  : SRPGetTabEntry.py
Version : 1.0.1
Author  : Stefano Covino
Date    : 01/09/2011
E-mail  : stefano.covino@brera.inaf.astro.it
URL:    : http://www.merate.mi.astro.it/utenti/covino

Usage   : SRPGetTabEntry [-a arg1] -c arg2 -C arg3 arg4 arg5 arg6 arg7 [-h] -i arg8 -I arg9 arg10 arg11 arg12 arg13 arg14 [-t arg15] [-v] [-z arg16]
            -i Input table
            -c Column coordinate positions for input table (col1 col2)
            -C Object coordinates (coord1 coord2)
            -t Maximum tolerance for object association (same units as for the coordinates)
            -a Angular distance if set, else cartesian distance
 

History : (09/11/2010) First version.
        : (01/09/2011) Better cosmetics.
"""



from optparse import OptionParser
import os, sys
from SRP.SRPTables.FindObjects import FindObjects



parser = OptionParser(usage="usage: %prog [-a arg1] -c arg2 -C arg3 arg4 arg5 arg6 arg7 [-h] -i arg8 -I arg9 arg10 arg11 arg12 arg13 arg14 [-t arg15] [-v] [-z arg16]", version="%prog 1.0.1")
parser.add_option("-i", "--inptable", action="store", nargs=1, type="string", help="Input table")
parser.add_option("-c", "--inpcols", action="store", nargs=2, type="int", help="Column coordinate positions for input table (col1 col2)")
parser.add_option("-C", "--coords", action="store", nargs=2, type="float", help="Object coordinates (coord1 coord2)")
parser.add_option("-t", "--tolerance", action="store", nargs=1, type="float", help="Maximum tolerance for object association (same units as for the coordinates)")
parser.add_option("-v", "--verbose", action="store_true", help="Fully describe operations")
parser.add_option("-a", "--angular", action="store_true", help="Angular distance if set, else Cartesian distance")
(options, args) = parser.parse_args()


if options.inptable and options.inpcols and options.coords and options.tolerance:
    # files
    try:
        inpi = file(options.inptable)
    except IOError:
        if options.verbose:
            print "File %s can not be accessed." % options.inptable
        else:
            print -1
        sys.exit(1)
    # columns
    for i in options.inpcols:
        if i < 1:
            parser.error("Column numbers must be grater than 1.")
    # tolerance
    if options.tolerance <= 0.0:
        parser.error("Tolerance must be positive.")
    # Find objects
    # read lines
    dti = inpi.readlines()
    inpi.close()
    #
    assdata = FindObjects(dti,options.inpcols,options.coords,options.tolerance,options.angular)
    #
    if assdata != None:
        if options.verbose:
            print "%d object(s) selected." % len(assdata)
        else:
            print str(len(assdata))
        msg = ''
        for o in assdata:
            msg = msg + str(o).strip() + os.linesep
        print msg
    else:
        if options.verbose:
            print "Error in browsing table %s." % options.inptable
        else:
            print -1
        sys.exit(1)
else:
    parser.print_help()
