#!/usr/bin/env python

import argparse
import numpy as np
from soxs.cosmology import make_cosmological_sources_file

parser = argparse.ArgumentParser(description='Create a SIMPUT photon list catalog of a '
                                             'cosmological background.')
parser.add_argument("filename", type=str,
                    help='The filename of the SIMPUT catalog to be used as the root of the ' +
                         'catalog. If it does not exist, it will be created.')
parser.add_argument("name", type=str,
                    help='The name of the source in the SIMPUT catalog.')
parser.add_argument("exp_time", help='The exposure time to use, in seconds.')
parser.add_argument("fov", help="The field of view on a side in arcminutes.")
parser.add_argument("sky_center", type=str, help='The center RA, Dec coordinates of the ' +
                                                 'observation, in degrees, comma-separated')
parser.add_argument("--cat_center", type=str, help='The center of the field in the coordinates '
                                                   'of the halo catalog, which range from -5.0 '
                                                   'to 5.0 degrees in both directions. If not '
                                                   'set, a center will be randomly chosen.')
parser.add_argument("--absorb_model", type=str, default="wabs",
                    help="The absorption model to use for foreground galactic absorption. Default: 'wabs'")
parser.add_argument("--nh", default=0.05,
                    help='The hydrogen column in units of 10**22 atoms/cm**2. Default: 0.05')
parser.add_argument("--area", default=30000.0,
                    help='The collecting area to use, in cm^2. Default: 30000.0')
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.')
parser.add_argument("--output_sources", type=str, 
                    help="Output the source properties to the specified file.")
parser.add_argument("--write_regions", type=str,
                    help="Write ds9 circle region files corresponding to the positions "
                         "and r500 of the halos.")
parser.add_argument("--random_seed", type=int,
                    help="A constant integer random seed to produce a consistent set of random numbers.")

args = parser.parse_args()

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

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

make_cosmological_sources_file(args.filename, args.name, args.exp_time, args.fov, 
                               sky_center, cat_center=cat_center, 
                               absorb_model=args.absorb_model, nH=args.nh, 
                               area=args.area, append=args.append, 
                               overwrite=args.overwrite, 
                               src_filename=args.src_filename,
                               output_sources=args.output_sources, 
                               write_regions=args.write_regions, prng=args.random_seed)