#!/usr/bin/env python

import argparse

import soxs

parser = argparse.ArgumentParser(
    description="Create a SIMPUT source of a point from "
    + "a spectrum supplied in a file."
)
parser.add_argument(
    "filename",
    type=str,
    help="The filename of the SIMPUT catalog to write or to append to.",
)
parser.add_argument(
    "name", type=str, help="The name of the source in the SIMPUT catalog."
)
parser.add_argument("ra0", help="The right ascension of the source in degrees.")
parser.add_argument("dec0", help="The declination of the source in degrees.")
parser.add_argument(
    "specfile", type=str, help="The file containing the spectrum to be used."
)
parser.add_argument(
    "--src_filename",
    type=str,
    help="An optional filename to store the source instead of the SIMPUT "
    "catalog file.",
)
parser.add_argument(
    "--append",
    action="store_true",
    help="If set, append a new source an existing SIMPUT catalog. ",
)
parser.add_argument(
    "--overwrite",
    action="store_true",
    help="Overwrite an existing file with the same name.",
)

args = parser.parse_args()

spec = soxs.Spectrum.from_file(args.specfile)

src = soxs.SimputSpectrum.from_spectrum(args.name, spec, args.ra0, args.dec0)

if args.append:
    cat = soxs.SimputCatalog.from_file(args.filename)
    cat.append(src, src_filename=args.src_filename, overwrite=args.overwrite)
else:
    cat = soxs.SimputCatalog.from_source(
        args.filename, src, src_filename=args.src_filename, overwrite=args.overwrite
    )
