# -*- python -*-
import tabulate
bs=u'\\ '
bs=bs[0:1]
tabulate.LATEX_ESCAPE_RULES[bs]=bs
tabulate.LATEX_ESCAPE_RULES[u'{']=u'{'
tabulate.LATEX_ESCAPE_RULES[u'}']=u'}'
tabulate.LATEX_ESCAPE_RULES[u'^']=u'^'
tabulate.LATEX_ESCAPE_RULES[u'_']=u'_'
tabulate.LATEX_ESCAPE_RULES[u'$']=u'$'

from os.path import join
add_plugin('f85ut')


try:
    fittest
except NameError:
    fittest = 'scf'
#endtry

try:
    material
except NameError:
    material = 'Copper'
#endtry

a=ut(material)
a.verbose=False
a.fittest=fittest

try:
    doplot
except NameError:
    doplot=True
#endtry
a.doplot = doplot


shells = ''
try:
    firstshell
except NameError:
    firstshell=False
#endtry
a.firstshell = firstshell
if a.firstshell:
    shells='_1st'
#endif

##
## output can be any of "plain", "simple", "grid", "fancy_grid", "pipe", "orgtbl", "rst", "mediawiki", "html", "latex", or "latex_booktabs"
## see: https://pypi.python.org/pypi/tabulate#table-format
##
suffix = '.tab'
try:
    output
except NameError:
    output='simple'
#endtry
if output.startswith('latex'): 
    suffix = '.tex'
#endif

#show a







a.fitcompare()







f = open(join(material, fittest, material+shells+suffix), 'w')

f.write('\\subsection{Best fit values}\n')

variables = getattr(getattr(getattr(a, a.models[0]), 'params'), 'covar_vars')

table = []
for d in a.models:
    dd=d
    if "_" in dd : dd=d.replace("_", "(") + ")"
    inner = [dd,]
    for var in variables:
        if var in ('amp', 'enot', 'nu'):
            val = "\\num{%.2f(%d)}" % (getattr(getattr(getattr(getattr(a, d), 'params'), var), 'value'),
                                       int(100*getattr(getattr(getattr(getattr(a, d), 'params'), var), 'stderr')))
        elif var in ('thetad'):
            val = "\\num{%.0f(%d)}" % (getattr(getattr(getattr(getattr(a, d), 'params'), var), 'value'),
                                       int(getattr(getattr(getattr(getattr(a, d), 'params'), var), 'stderr')))
        elif var.startswith('dr'):
            val = "\\num{%.3f(%d)}" % (getattr(getattr(getattr(getattr(a, d), 'params'), var), 'value'),
                                       int(1000*getattr(getattr(getattr(getattr(a, d), 'params'), var), 'stderr')))
        elif var.startswith('e'):
            val = "\\num{%.3f(%d)}" % (getattr(getattr(getattr(getattr(a, d), 'params'), var), 'value'),
                                       int(1000*getattr(getattr(getattr(getattr(a, d), 'params'), var), 'stderr')))
        else:
            val = "\\num{%.5f(%d)}" % (getattr(getattr(getattr(getattr(a, d), 'params'), var), 'value'),
                                       int(100000*getattr(getattr(getattr(getattr(a, d), 'params'), var), 'stderr')))
        #endif
        inner.append( val )
    #endfor
    table.append(inner)
#endfor

f.write(tabulate.tabulate(table, headers=['model',]+variables, tablefmt=output))


f.write('\n\n\subsection{Statistics}\n')

table = []
for d in a.models:
    dd=d
    if "_" in dd : dd=d.replace("_", "(") + ")"
    inner = [dd,]
    for stat in ('chi_square', 'chi_reduced', 'rfactor'):
        inner.append( getattr(getattr(getattr(a, d), 'params'), stat) )
    #endfor
    table.append(inner)
#endfor

f.write(tabulate.tabulate(table, headers=['model','$\chi^2$', '$\chi^2_\\nu$', '$\mathcal{R}$'], floatfmt=".4f", tablefmt=output)) 
f.write('\n')

f.close

