#!/usr/bin/env python
import argparse
import os

import numpy as np
from astropy.io import fits

import qusp

def main():
    # parse command-line arguments
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--verbose", action="store_true",
        help="print verbose output")
    parser.add_argument("-o","--output", type=str, default=None,
        help="optional output FITS file to write")
    parser.add_argument("-i","--input", type=str, default=None,
        help="required input FITS file to read")
    parser.add_argument("-s","--select", type=str, default=None,
        help="tbdata selection string, ex: \"(tbdata['OBJTYPE'] == 'QSO') & (tbdata['Z'] > 2.1)\"")
    parser.add_argument("--save", type=str, default=None,
        help="target list text file to save")
    parser.add_argument("--annotate", type=str, default=None,
        help="colon separated list of columns to annotate target list with, ex: \"ra:dec:z\"")
    args = parser.parse_args()

    assert args.input is not None, 'No input file specified'
    # read input FITS file
    hdulist = fits.open(args.input)
    # filter table data
    tbdata = hdulist[1].data
    if args.verbose:
        print 'Read %d entries from %s' % (len(tbdata), args.input)
    if args.select:
        mask = eval(args.select)
        filtered = tbdata[mask]
        if args.verbose:
            print 'Selecting %d rows matching: %s' % (len(filtered), args.select)
    else:
        filtered = tbdata
        if args.verbose:
            print 'No selection specified (selecting all entries).'

    # construct list of fields to annotate target list
    targets = [ ]
    try:
        fields = args.annotate.split(':')
    except AttributeError:
        fields = [ ]
    # extract targets and fields from table data
    for i,row in enumerate(filtered):
        if args.verbose and (0 == ((i+1) % 1000)):
            print '(processing input entry %d)' % (i+1)
        targetstr = '-'.join([str(row[key]) for key in ['PLATE','MJD','FIBERID']])
        target = qusp.target.Target(target=targetstr)
        for field in fields:
            target[field] = row[field]
        targets.append(target)
    # save target list
    if args.save:
        # save target list as text file
        if args.verbose:
            print "Saving %d final targets to %s" % (len(targets), args.save)
        qusp.target.saveTargetData(args.save,targets,fields)
    # save filtered FITS file
    if args.output:
        if args.verbose:
            print "Saving filtered FITS table to %s" % args.output
        hdulist[1].data = filtered
        hdulist.writeto(args.output, clobber=True)

if __name__ == '__main__':
    main()
@@ -19,53 +19,84 @@
 hon%0A
-import argparse%0Aimport os%0A%0Aimport numpy as np
+%22%22%22%0AA program to filter FITS catalogs into target lists%0A%22%22%22%0Aimport argparse%0A
 %0Afro
@@ -218,16 +218,25 @@
 tParser(
+%0A        
 formatte
@@ -307,16 +307,25 @@
 rgument(
+%0A        
 %22--verbo
@@ -415,13 +415,23 @@
 ent(
+%0A        
 %22-o%22,
+ 
 %22--o
@@ -541,13 +541,23 @@
 ent(
+%0A        
 %22-i%22,
+ 
 %22--i
@@ -664,13 +664,23 @@
 ent(
+%0A        
 %22-s%22,
+ 
 %22--s
@@ -724,16 +724,17 @@
    help=
+(
 %22tbdata 
@@ -755,16 +755,33 @@
 ng, ex: 
+%22%0A              %22
 %5C%22(tbdat
@@ -824,24 +824,25 @@
 %5D %3E 2.1)%5C%22%22)
+)
 %0A    parser.
@@ -854,16 +854,25 @@
 rgument(
+%0A        
 %22--save%22
@@ -967,16 +967,25 @@
 rgument(
+%0A        
 %22--annot
@@ -1027,16 +1027,17 @@
    help=
+(
 %22colon s
@@ -1089,16 +1089,33 @@
 ist with
+%22%0A              %22
 , ex: %5C%22
@@ -1126,16 +1126,17 @@
 ec:z%5C%22%22)
+)
 %0A    arg
@@ -1586,24 +1586,41 @@
 ing: %25s' %25 (
+%0A                
 len(filtered
@@ -1831,25 +1831,24 @@
  targets = %5B
- 
 %5D%0A    try:%0A 
@@ -1933,17 +1933,16 @@
 elds = %5B
- 
 %5D%0A    # 
@@ -1993,17 +1993,22 @@
    for i
-,
+ndex, 
 row in e
@@ -2064,16 +2064,20 @@
 0 == ((i
+ndex
 +1) %25 10
@@ -2134,16 +2134,20 @@
 d)' %25 (i
+ndex
 +1)%0A    
@@ -2171,16 +2171,29 @@
 -'.join(
+%0A            
 %5Bstr(row
@@ -2223,14 +2223,16 @@
 TE',
+ 
 'MJD',
+ 
 'FIB
@@ -2605,18 +2605,20 @@
 save
-T
+_t
 arget
-Data
+_list
 (arg
@@ -2628,16 +2628,17 @@
 ave,
+ 
 targets,
 fiel
@@ -2633,16 +2633,17 @@
 targets,
+ 
 fields)%0A
@@ -2904,12 +2904,13 @@
 :%0A    main()
+%0A

update filter program to use new names