""" Code to select entries
    
Context : SRP
Module  : SRPTNGPAOLOSelectRow
Author  : Stefano Covino
Date    : 18/02/2012
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/utenti/covino
Purpose : Derive instrumental Q and U Stokes parameters

Usage   : SRPTNGPAOLOSelectRow [-h] -f file -o file [-r [rows [rows ...]]] [-v] [--version]
            -f Input FITS photometry file
            -o Output FITS file
            -r Rows to select
    
History : (18/02/2012) First version.

"""

__version__ = '0.1.0'


import argparse, math, sys
import atpy, numpy
#import SRP.SRPTNG.PAOLO as STP
#from SRP.SRPFits.GetHeaderValue import GetHeaderValue
#from SRP.SRPFits.IsFits import IsFits
#from SRP.SRPMath.PointMatch import PointMatch
#from SRP.SRPPhotometry.Counts2Mag import Counts2Mag
#from SRP.SRPPhotometry.Mag2Counts import Mag2Counts



parser = argparse.ArgumentParser()
parser.add_argument("-f", "--fitsphotfile", action="store", help="Input FITS photometry file", required=True, metavar='file')
parser.add_argument("-o", "--outfile", action="store", help="Output FITS file", required=True, metavar='file')
parser.add_argument("-r", "--rows", action="store", type=int, nargs='*', help="Rows to select", metavar='rows')
parser.add_argument("-v", "--verbose", action="store_true", help="Fully describe operations")
parser.add_argument("--version", action="version", version=__version__)
options = parser.parse_args()


#
try:
    tphot = atpy.Table(options.fitsphotfile, type='fits')
except IOError:
    parser.error("Invalid input FITS file.")
if options.verbose:
    print "Input FITS photometry file: %s" % options.fitsphotfile
#
if options.verbose:
    print "Rows to select: %s" % options.rows
rows = list(numpy.array(options.rows)-1)
#
try:
    tnew = tphot.rows(rows)
except IndexError:
    parser.error("Invalid raws to select.")    
#
tnew.write(options.outfile,type='fits',overwrite=True)
if options.verbose:
    print "Results saved in file %s with %d entries" % (options.outfile, len(tnew))
else:
    print "%d %s" % (len(tnew), options.outfile)
#
