#!/usr/bin/env python
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Create sky maps from VHE event lists.

TODO: document
"""


# 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 file. Either an individual FITS file or a batch file.')
parser.add_argument('-s', '--skymap_size', type=float, default=6.,
                    help='Diameter of the sky map in degrees')
parser.add_argument('-b', '--bin_size', type=float, default=0.03,
                    help='Bin size in degrees')
parser.add_argument('-p', '--analysis_position', type=str, default=None,
                    help='Center of the skymap in RA and Dec (J2000) in degrees. '
                    'Format: \'(RA, Dec)\', including the quotation marks. '
                    'If no center is given, the source position from the first input file is used.')
parser.add_argument('-r', '--oversampling_radius', type=float, default=0.125,
                    help='Radius used to correlated the sky maps in degrees.')
parser.add_argument('--ring_bg_radii', type=str, default='(.3, .7)',
                    help='Inner and outer radius of the ring used for the ring background. '
                    'Format \'(r_in, r_out)\', including the quotation marks.')
parser.add_argument('-w', '--write_output', action='store_true', default=False,
                    help='Write results to FITS files in current directory.')
parser.add_argument('--no_acceptance_correction', action='store_false', default=True,
                    help='Do not correct skymaps for FoV acceptance.')
parser.add_argument('-t', '--template_background', type=str, default=None,
                    help='Bankfile with template background eventlists.')
parser.add_argument('--no_graphical_output', action='store_false', default=True,
                    help='Switch off graphical output.')
parser.add_argument('-l', '--loglevel', type=str, default='INFO',
                    help='Amount of logging e.g. DEBUG, INFO, WARNING, ERROR.')

args = parser.parse_args()
args = vars(args)

# Execute script

import logging
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
from gammapy.utils.pyfact import create_sky_map

create_sky_map(input_file_name=args['infile'],
               skymap_size=args['skymap_size'],
               skymap_bin_size=args['bin_size'],
               r_overs=args['oversampling_radius'],
               ring_bg_radii=args['ring_bg_radii'],
               template_background=args['template_background'],
               skymap_center=args['skymap_center'],
               write_output=args['write_output'],
               fov_acceptance=args['fov_acceptance'],
               do_graphical_output=args['graphical_output'],
               loglevel=args['loglevel'])
