#!/usr/bin/env python
"""
Cuts stamp images from a larger image, with individual objects defined by a DS9
region file or sextractor segmentation map.
Additional keyword arguments can be supplied in -<keyword> <value> format. See
the docstring for fitsstamp.cut_stamps for additional information.
Understood keyword arguments:
{0}

Examples:
fitsstamp_cut uds_full_f160w_sci.fits my_favorite_galaxies.reg
fitsstamp_cut img.fits regions.reg -extension 1 -mask_inf_nan True
fitsstamp_cut img.fits segs.fits -selected_labels [1234,382,660361]
"""

import fitsstamp
import sys
import textwrap
from inspect import getargspec
from ast import literal_eval
from astropy.io import fits
from astropy.utils.exceptions import AstropyUserWarning
from warnings import catch_warnings, filterwarnings

def print_doc():
    arg_names, _args, _kw, arg_vals = getargspec(fitsstamp.cut_stamps)
    arg_names = arg_names[-len(arg_vals):]
    arg_list = ', '.join(['-'+nm for nm in arg_names])
    arg_list = '\n'.join(textwrap.wrap(arg_list, 80))
    print(__doc__.format(arg_list))

if __name__ == '__main__':
    # Process command line arguments to be passed to cut_stamps function
    cl_args = dict()
    arg_names = getargspec(fitsstamp.cut_stamps)[0]
    for arg_name in arg_names:
        try:
            arg_ind = sys.argv.index('-'+arg_name)
        except ValueError:
            continue
        try:
            cl_args[arg_name] = sys.argv[arg_ind+1]
            del sys.argv[arg_ind:arg_ind+2]
        except IndexError:
            print_doc()
            exit(1)
        # convert ints, floats, bool etc. to python literals. Bare string
        # (without additional quotes) will raise ValueError and remain unchanged
        try:
            cl_args[arg_name] = literal_eval(cl_args[arg_name])
        except (ValueError, SyntaxError):
            pass

    try:
        img_file = sys.argv[1]
        reg_file = sys.argv[2]
    except IndexError:
        print_doc()
        exit(1)

    # Check if the format of reg_file has been specified. If not, infer it.
    if not 'region_format' in cl_args:
        try:
            with catch_warnings():
                filterwarnings('ignore', category=AstropyUserWarning)
                fits.info(reg_file, output=False)
            cl_args['region_format'] = 'label'
        except IOError:
            cl_args['region_format'] = 'region'

    cut = fitsstamp.cut_stamps(img_file, reg_file, **cl_args)
    print('Cutting complete for {0:d} stamps'.format(len(cut)))