""" Code to select entries in a table

Context : SRP
Module  : SRPGetTabEntry.py
Author  : Stefano Covino
Date    : 20/12/2012
E-mail  : stefano.covino@brera.inaf.astro.it
URL:    : http://www.merate.mi.astro.it/utenti/covino

Usage   : SRPGetTabEntry [-a] -c arg1 arg2 -C arg3 arg4 [-h] -i arg5 [-j arg6] [-o arg7] -t arg8 [-v]
            -a Angular distance if set, else Cartesian distance
            -c Column coordinate positions for input table (col1 col2)
            -C Object coordinates (coord1 coord2)
            -i Input table
            -j Number of lines to be skipped
            -o Output table without identified entries
            -t Maximum tolerance for object association (same units as for the coordinates)

History : (09/11/2010) First version.
        : (01/09/2011) Better cosmetics.
        : (20/12/2012) Possibility to skip lines.
"""



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



parser = OptionParser(usage="usage: %prog [-a] -c arg1 arg2 -C arg3 arg4 [-h] -i arg5 [-j arg6] [-o arg7] -t arg8 [-v]", version="%prog 1.2.0")
parser.add_option("-a", "--angular", action="store_true", help="Angular distance if set, else Cartesian distance")
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("-i", "--inptable", action="store", nargs=1, type="string", help="Input table")
parser.add_option("-j", "--jump", action="store", nargs=1, type="int", default=0, help="Number of lines tio be skipped.")
parser.add_option("-o", "--outtable", action="store", nargs=1, type="string", help="Output table without identified entries.")
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")
(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 greater than 1.")
    # tolerance
    if options.tolerance <= 0.0:
        parser.error("Tolerance must be positive.")
    # Ssipped lines
    if options.jump < 0:
        parser.error("Number of lines to be skiped must be positive.")
    # Find objects
    # read lines
    dti = inpi.readlines()
    inpi.close()
    #
    assdata,nassdata = FindObjects(dti[options.jump:],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)
    #
    if options.outtable:
        of = open(options.outtable,'w')
        msg = ''
        for o in nassdata:
            msg = msg + str(o).strip() + os.linesep
        of.write(msg)
        of.close()
        if options.verbose:
            print "%d object(s) not selected." % len(nassdata)
    #
    sys.exit(1)

else:
    parser.print_help()
