#!/usr/bin/env python

import argparse
import os
import numpy as np
from soxs import instrument_simulator, \
    add_instrument_to_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, or '
                                                  '"None" if you only want to simulate '
                                                  'backgrounds.')
parser.add_argument("out_file", type=str, help='The name of the event file to be written.')
parser.add_argument("exp_time", 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("--overwrite", action='store_true', 
                    help='Overwrite an existing file with the same name.')
parser.add_argument("--roll_angle", default=0.0,
                    help='The roll angle in degrees. Default: 0.0')
parser.add_argument("--bkgnd_file", type=str,
                    help='Use background stored in a file instead of generating one.')
parser.add_argument("--subpixel_res", action='store_true',
                    help="Don't uniformly distribute event positions within pixels.")
parser.add_argument("--no_dither", action="store_true", help="Turn dithering off entirely.")
parser.add_argument("--dither_params", help="The parameters controlling the size and period of "
                                            "dither. Four floats joined by commas, in the form "
                                            "of x_amp,y_amp,x_period,y_period. The first two "
                                            "numbers are in arcseconds and the second are in "
                                            "seconds. Default: 8.0,8.0,1000.0,707.0")
parser.add_argument("--aimpt_shift", type=str, 
                    help="The shift of the aimpoint on the detector in both directions from "
                         "the nominal aimpoint in arcseconds. Default: [0.0, 0.0]")
parser.add_argument("--random_seed", type=int,
                    help="A constant integer random seed to produce a consistent set of random numbers.")
ptsrc_parser = parser.add_mutually_exclusive_group(required=False)
ptsrc_parser.add_argument("--ptsrc_bkgnd", dest='ptsrc_bkgnd', action='store_true',
                            help='Turn the point-source background on.')
ptsrc_parser.add_argument("--no_ptsrc_bkgnd", dest='ptsrc_bkgnd', action='store_false',
                          help='Turn the point-source background off.')
instr_parser = parser.add_mutually_exclusive_group(required=False)
instr_parser.add_argument("--instr_bkgnd", dest='instr_bkgnd', action='store_true',
                            help='Turn the instrumental background on.')
instr_parser.add_argument("--no_instr_bkgnd", dest='instr_bkgnd', action='store_false',
                            help='Turn the instrumental background off.')
frgnd_parser = parser.add_mutually_exclusive_group(required=False)
frgnd_parser.add_argument("--foreground", dest='foreground', action='store_true',
                            help='Turn the galactic foreground on.')
frgnd_parser.add_argument("--no_foreground", dest='foreground', action='store_false',
                            help='Turn the galactic foreground off.')
parser.set_defaults(foreground=True, ptsrc_bkgnd=True, instr_bkgnd=True)

args = parser.parse_args()

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

if args.aimpt_shift is None:
    aimpt_shift = None
else:
    aimpt_shift = np.array(args.aimpt_shift.split(',')).astype("float64")

if args.dither_params is None:
    dither_params = None
else:
    dither_params = np.array(args.dither_params.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

instrument_simulator(args.simput_file, args.out_file, args.exp_time,
                     instrument, sky_center, overwrite=args.overwrite,
                     no_dither=args.no_dither, dither_params=dither_params, 
                     roll_angle=args.roll_angle, instr_bkgnd=args.instr_bkgnd, 
                     ptsrc_bkgnd=args.ptsrc_bkgnd, foreground=args.foreground, 
                     bkgnd_file=args.bkgnd_file, subpixel_res=args.subpixel_res, 
                     aimpt_shift=aimpt_shift, prng=args.random_seed)
