#!/usr/bin/env python
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Apply the a trous wavelet transform on a 2D image.

This book contains an overview of methods:
http://www.amazon.de/dp/3540330240

Specifically I'd like to have an a trous transform.
Here's a few references that look useful:
* https://code.google.com/p/image-funcut/

I'm also interested in these transforms:
* http://scikit-image.org/docs/dev/api/skimage.transform.html#pyramid-expand
* http://scikit-image.org/docs/dev/api/skimage.transform.html#pyramid-gaussian
* http://scikit-image.org/docs/dev/api/skimage.transform.html#pyramid-laplacian
* http://scikit-image.org/docs/dev/api/skimage.transform.html#pyramid-reduce
"""

# TODO: add option to copy over input file
# TODO: add option to select input HDU name or number

# Parse command line arguments

from gammapy.utils.scripts import argparse, GammapyFormatter
parser = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=GammapyFormatter)
parser.add_argument('infile', type=str,
                    help='Input FITS file name')
parser.add_argument('outfile', type=str,
                    help='Output FITS file name')
parser.add_argument('n_levels', type=int,
                    help='Number of levels (wavelet scales)')
parser.add_argument('--clobber', action='store_true',
                    help='Clobber output files?')
args = parser.parse_args()
args = vars(args)

# Execute script

import logging
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
from astropy.io import fits
from gammapy.image.utils import atrous_hdu

logging.info('Reading {0}'.format(args['infile']))
hdu = fits.open(args['infile'])[0]
atrous_hdus = atrous_hdu(hdu, n_levels=args['n_levels'])

logging.info('Writing {0}'.format(args['outfile']))
atrous_hdus.writeto(args['outfile'], clobber=args['clobber'])
