from __future__ import print_function
import os
import sys
import warnings
from optparse import OptionParser
from emcee.utils import MPIPool
from gsf_core import *

#Parse the commands#
#-------------------#
parser = OptionParser()
parser.add_option("-w", "--warning", dest="warning",
                  action="store_true", default=False,
                  help="Stop ignoring the warnings.")
parser.add_option("-o", "--overwrite", dest="overwrite",
                  action="store_true", default=False,
                  help="Overwrite the object information with the command-line inputs.")
parser.add_option("-r", "--refit", dest="refit", action="store_true", default=False,
                  help="Refit the SED though there is a result found.")
(options, args) = parser.parse_args()
if len(args) == 0:
    raise AssertionError("The config file is not specified!")
else:
    configName = args[0] #Get the input configure file information.
#Some times the warning may stop the code, so we ignore the warnings by default.
if options.warning:
    pass
else:
    warnings.simplefilter("ignore")
if options.refit:
    refit = True
else:
    refit = False

pool = MPIPool()
if not pool.is_master():
    pool.wait()
    sys.exit(0)

#-> The starter of this module
print("\n")
print("############################")
print("# Galaxy SED Fitter starts #")
print("############################")
print("\n")
#--> The object can be provided by the configure file or be overwrite with the
# command-line inputs
len_args = len(args)
if not options.overwrite:
    if len_args > 1:
        print("**Warning[UniFit]: there are more arguments may not be used...")
    gsf_fitter(configName, mpi_pool=pool, refit=refit)
else:
    if len_args < 4:
        pool.close()
        raise AssertionError("The object information is lacking!")
    if len_args == 4:
        targname = args[1]
        redshift = eval(args[2])
        distance = None
        sedFile  = args[3]
    elif len_args == 5:
        targname = args[1]
        redshift = eval(args[2])
        distance = eval(args[3]) #The distance should be in Mpc.
        sedFile  = args[4]
    else:
        print("**Warning[UniFit]: there are more arguments may not be used...")
    gsf_fitter(configName, targname, redshift, distance, sedFile, pool, refit)
pool.close()
