# Path: alis/almsgs.py
# class Colors:
# class msgs:
#     def disable(self):
#     def __init__(self):
#     def alisheader(self, prognm, verbose=2):
#     def signal_handler(self, signalnum, handler):
#     def error(self, msg, verbose=None):
#     def info(self, msg, verbose=None):
#     def warn(self, msg, verbose=None):
#     def test(self, msg, verbose=None):
#     def bug(self, msg, verbose=None):
#     def work(self, msg, verbose=None):
#     def input(self):
#     def newline(self,verbose=None):
#     def indent(self,verbose=None):
# 
# Path: alis/alutils.py
# def getreason(idnum,verbose=2):
#     if   idnum == 1: return "Both actual and predicted relative reductions in the sum of squares are at most ftol"
#     elif idnum == 2: return "Relative error between two consecutive iterates is at most xtol"
#     elif idnum == 3: return "Both actual and predicted relative reductions in the sum of squares are at most ftol and the relative error between two consecutive iterates is at most xtol"
#     elif idnum == 4: return "The cosine of the angle between fvec and any column of the jacobian is at most gtol in absolute value"
#     elif idnum == 5: return "The maximum number of iterations has been reached"
#     elif idnum == 6: return "ftol is too small. No further reduction in the sum of squares is possible"
#     elif idnum == 7: return "xtol is too small. No further improvement in the approximate solution x is possible"
#     elif idnum == 8: return "gtol is too small. fvec is orthogonal to the columns of the jacobian to machine precision"
#     elif idnum == 9: return "The relative reduction in the sum of squares is less than atol"
#     else:
#         msgs.bug("Convergence reason is unknown (probably failed) --- please contact the author",verbose=verbose)
#         return "Convergence reason is unknown (probably failed) --- please contact the author"


import os
import datetime
from alis import almsgs
from alis.alutils import getreason
        mtyp = mp['mtyp'][i]
        if mp['emab'][i] != lastemab:
            if   mp['emab'][i]=="em": aetag = "emission"
            elif mp['emab'][i]=="ab": aetag = "absorption"
            elif mp['emab'][i]=="cv": aetag = "Convolution"
            elif mp['emab'][i]=="sh": aetag = "Shift"
            elif mp['emab'][i]=="zl": aetag = "zerolevel"
            convstring += "#"+aetag+"\n"
            lastemab = mp['emab'][i]
        funcinst[mtyp]._keywd = mp['mkey'][i]
        outstr, cnvstr, level = funccall[mtyp].parout(funcinst[mtyp], diff, mp, i, level, conv=thresh)
        if outstr in donecv or outstr in donesh or outstr in donezl: continue
        if mp['emab'][i] == "cv": donecv.append(outstr) # Make sure we don't print convolution more than once.
        if mp['emab'][i] == "sh": donesh.append(outstr) # Make sure we don't print shifts more than once.
        if mp['emab'][i] == "zl": donezl.append(outstr) # Make sure we don't print zerolevel more than once.
        convstring += cnvstr
    return convstring

def save_convtest(slf,diff,thresh,info,printout=True,extratxt=["",""]):
    """
    Save the details of what parameters have converged.
    """
    msgs.info("Saving the best-fitting model parameters", verbose=slf._argflag['out']['verbose'])
    filename = extratxt[0]+slf._argflag['out']['modelname']+'.conv'+extratxt[1]
    prestring = "#\n#  Generated by ALIS on {0:s}\n#\n".format(datetime.datetime.now().strftime("%d/%m/%y at %H:%M:%S"))
    prestring += "#   Running Time (hrs)  = {0:f}\n".format(info[0])
    prestring += "#   Initial Chi-Squared = {0:f}\n".format(slf._chisq_init)
    prestring += "#   Bestfit Chi-Squared = {0:f}\n".format(info[1])
    prestring += "#   Degrees-of-Freedom  = {0:d}\n".format(info[2])
    prestring += "#   Num. of Iterations  = {0:d}\n".format(info[3])

# Path: alis/almsgs.py
# class Colors:
# class msgs:
#     def disable(self):
#     def __init__(self):
#     def alisheader(self, prognm, verbose=2):
#     def signal_handler(self, signalnum, handler):
#     def error(self, msg, verbose=None):
#     def info(self, msg, verbose=None):
#     def warn(self, msg, verbose=None):
#     def test(self, msg, verbose=None):
#     def bug(self, msg, verbose=None):
#     def work(self, msg, verbose=None):
#     def input(self):
#     def newline(self,verbose=None):
#     def indent(self,verbose=None):
# 
# Path: alis/alutils.py
# def getreason(idnum,verbose=2):
#     if   idnum == 1: return "Both actual and predicted relative reductions in the sum of squares are at most ftol"
#     elif idnum == 2: return "Relative error between two consecutive iterates is at most xtol"
#     elif idnum == 3: return "Both actual and predicted relative reductions in the sum of squares are at most ftol and the relative error between two consecutive iterates is at most xtol"
#     elif idnum == 4: return "The cosine of the angle between fvec and any column of the jacobian is at most gtol in absolute value"
#     elif idnum == 5: return "The maximum number of iterations has been reached"
#     elif idnum == 6: return "ftol is too small. No further reduction in the sum of squares is possible"
#     elif idnum == 7: return "xtol is too small. No further improvement in the approximate solution x is possible"
#     elif idnum == 8: return "gtol is too small. fvec is orthogonal to the columns of the jacobian to machine precision"
#     elif idnum == 9: return "The relative reduction in the sum of squares is less than atol"
#     else:
#         msgs.bug("Convergence reason is unknown (probably failed) --- please contact the author",verbose=verbose)
#         return "Convergence reason is unknown (probably failed) --- please contact the author"


# Path: alis/alconv.py
import os
import datetime
from alis import almsgs
from alis.alutils import getreason

        mtyp = mp['mtyp'][i]
        if mp['emab'][i] != lastemab:
            if   mp['emab'][i]=="em": aetag = "emission"
            elif mp['emab'][i]=="ab": aetag = "absorption"
            elif mp['emab'][i]=="cv": aetag = "Convolution"
            elif mp['emab'][i]=="sh": aetag = "Shift"
            elif mp['emab'][i]=="zl": aetag = "zerolevel"
            convstring += "#"+aetag+"\n"
            lastemab = mp['emab'][i]
        funcinst[mtyp]._keywd = mp['mkey'][i]
        outstr, cnvstr, level = funccall[mtyp].parout(funcinst[mtyp], diff, mp, i, level, conv=thresh)
        if outstr in donecv or outstr in donesh or outstr in donezl: continue
        if mp['emab'][i] == "cv": donecv.append(outstr) # Make sure we don't print convolution more than once.
        if mp['emab'][i] == "sh": donesh.append(outstr) # Make sure we don't print shifts more than once.
        if mp['emab'][i] == "zl": donezl.append(outstr) # Make sure we don't print zerolevel more than once.
        convstring += cnvstr
    return convstring

def save_convtest(slf,diff,thresh,info,printout=True,extratxt=["",""]):
    """
    Save the details of what parameters have converged.
    """
    msgs.info("Saving the best-fitting model parameters", verbose=slf._argflag['out']['verbose'])
    filename = extratxt[0]+slf._argflag['out']['modelname']+'.conv'+extratxt[1]
    prestring = "#\n#  Generated by ALIS on {0:s}\n#\n".format(datetime.datetime.now().strftime("%d/%m/%y at %H:%M:%S"))
    prestring += "#   Running Time (hrs)  = {0:f}\n".format(info[0])
    prestring += "#   Initial Chi-Squared = {0:f}\n".format(slf._chisq_init)
    prestring += "#   Bestfit Chi-Squared = {0:f}\n".format(info[1])
    prestring += "#   Degrees-of-Freedom  = {0:d}\n".format(info[2])
    prestring += "#   Num. of Iterations  = {0:d}\n".format(info[3])

    prestring += "#   Convergence Reason  = {0:s}\n".format(getreason(info[4],verbose=slf._argflag['out']['verbose']))