#!/usr/bin/env python
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from __future__ import (absolute_import, unicode_literals, division,
                        print_function)

from maltpynt.mp_save_as_xspec import mp_save_as_xspec
import argparse
import logging

description = ('Save a frequency spectrum in a qdp file that can be '
               'read by flx2xsp and produce a XSpec-compatible spectrum file')
parser = argparse.ArgumentParser(description=description)
parser.add_argument("files", help="List of files", nargs='+')
parser.add_argument("--loglevel",
                    help=("use given logging level (one between INFO, "
                          "WARNING, ERROR, CRITICAL, DEBUG; default:WARNING)"),
                    default='WARNING',
                    type=str)
parser.add_argument("--debug", help="use DEBUG logging level",
                    default=False, action='store_true')
parser.add_argument("--flx2xsp", help="Also call flx2xsp at the end",
                    default=False, action='store_true')

if __name__ == '__main__':
    args = parser.parse_args()
    files = args.files
    if args.debug:
        args.loglevel = 'DEBUG'

    numeric_level = getattr(logging, args.loglevel.upper(), None)
    logging.basicConfig(filename='MP2xpec.log', level=numeric_level,
                        filemode='w')

    for f in files:
        mp_save_as_xspec(f, direct_save=args.flx2xsp)
