#!/usr/bin/env python

import argparse
import os
import numpy as np
from soxs import instrument_simulator, \
    add_instrument_to_registry, \
    add_background_to_registry
from soxs.background import background_registry
from soxs.instrument import instrument_registry
parser = argparse.ArgumentParser(description='Run the instrument simulator and produce a simulated event file.')
parser.add_argument("simput_file", type=str, help='The SIMPUT file to be used as input.')
parser.add_argument("out_file", type=str, help='The name of the event file to be written.')
parser.add_argument("exp_time", type=float, help='The exposure time to use, in seconds.')
parser.add_argument("instrument", type=str, help='The name of the instrument to use, '+
                    'or alternatively the name of a JSON file which contains an instrument '+
                    'specification.')
parser.add_argument("sky_center", type=str, help='The center RA, Dec coordinates of the '+
                    'observation, in degrees, comma-separated')
parser.add_argument("--clobber", action='store_true', 
                    help='Whether or not to clobber an existing file with the same name.')
parser.add_argument("--dither_shape", type=str, default='square',
                    help='The shape of the dither pattern: square, circle, or None. '+
                    'Default: square')
parser.add_argument("--dither_size", type=float, default=16.0,
                    help='The size of the dither pattern in arcseconds. For a circle, the'+
                         'size is the radius; for a square, the size is the width. Default: 16.0')
parser.add_argument("--roll_angle", type=float, default=0.0,
                    help='The roll angle in degrees. Default: 0.0')
parser.add_argument("--astro_bkgnd", type=str, default="hm_cxb",
                    help='The astrophysical background to use. Default: hm_cxb')
parser.add_argument("--instr_bkgnd_scale", type=float, default=1.0, 
                    help='The scale of the instrumental background. Default: 1.0')

args = parser.parse_args()

sky_center = np.array(args.sky_center.split(',')).astype("float64")

if args.instrument not in instrument_registry and os.path.exists(args.instrument):
    instrument = add_instrument_to_registry(args.instrument)
else:
    instrument = args.instrument

if args.dither_shape == "None":
    dither_shape = None
else:
    dither_shape = args.dither_shape

if args.astro_bkgnd == "None":
    astro_bkgnd = None
elif args.astro_bkgnd not in background_registry and os.path.exists(args.astro_bkgnd):
    astro_bkgnd = add_background_to_registry(args.astro_bkgnd)
else:
    astro_bkgnd = args.astro_bkgnd

instrument_simulator(args.simput_file, args.out_file, args.exp_time,
                     instrument, sky_center, dither_size=args.dither_size,
                     dither_shape=dither_shape, clobber=args.clobber,
                     roll_angle=args.roll_angle, instr_bkgnd_scale=args.instr_bkgnd_scale,
                     astro_bkgnd=astro_bkgnd)
