import itertools

pl.rcParams["font.size"] = 20

ratio = fits.getdata("W51_H2CO11_to_22_tau_ratio_supersampled_neighbors.fits")

colors = itertools.cycle(["#" + x for x in "348ABD, 7A68A6, A60628, 467821, CF4457, 188487, E24A33".split(", ")])

abunds = (-8.5, -10)

dcs = {}

for abund in abunds:
    dcs[abund] = {}
    for sigma in (0.001, 1.0):
        tau1, tau2, dens, col = select_data(abund)
        tau, vtau, vtau_ratio = generate_tau_functions(abundance=abund)

        tauratio = vtau_ratio(dens, line1=tau1, line2=tau2, sigma=sigma)

        ok = np.arange(tauratio.size) > np.argmax(tauratio)

        def ratio_to_dens(ratio):
            inds = np.argsort(tauratio[ok])
            return np.interp(ratio, tauratio[ok][inds], dens[ok][inds], np.nan, np.nan)

        if sigma == 0.001:
            sigma = 0

        r = ratio_to_dens(ratio)
        dcs[abund][sigma] = r[r == r]
import pylab as pl
import numpy as np
from smoothtau_models import generate_simpletools,select_data
from measure_tau import savefig,savepath,tau11,etau11,tau22,etau22,ratio,eratio
import itertools

for abundance in [-9,-8.5,-8.25,-8.0]:
    opr = 1
    temperature = 20
    tau1x,tau2x,dens,col = select_data(abundance=abundance, opr=opr, temperature=temperature)
    tauratio,tauratio_hopkins,tau,tau_hopkins = generate_simpletools(abundance=abundance, opr=opr, temperature=temperature)
    print len(tau1x)

    pl.figure(30)
    pl.clf()
    ax = pl.gca()
    ax.plot(dens,tau1x/tau2x,'k',linewidth=4.0,label=r'Dirac $\delta$',alpha=0.75)

    logmeandens = np.linspace(-2,7,300)
    meandens = 10**logmeandens

    stylecycle = itertools.cycle(('-','-.','--',':'))
    dashcycle = itertools.cycle(((None,None),(6,2),(10,4),(2,2),(5,5)))

    for sigma in np.arange(0.5,4.0,1):
        ax.plot(logmeandens,tauratio(meandens,sigma=sigma),color='k',linewidth=2, alpha=0.5,  label='$\\sigma_s=%0.1f$' % sigma, dashes=dashcycle.next())

    dashcycle = itertools.cycle(((None,None),(6,2),(10,4),(2,2),(5,5)))
    for sigma in np.arange(0.5,4.0,1):
        ax.plot(logmeandens,tauratio_hopkins(meandens,sigma=sigma),color='orange', label='$\\sigma_s=%0.1f$ Hopkins' % sigma, linewidth=3, alpha=0.8, dashes=dashcycle.next())
# load defaults by default
abundance = -8.5
#abundance = -9.0
opr = 1 # default
opr = 0 # Faure suggestion

# the observed values.  f=tau ratio = 6.65 (6.99?).  tau might be too high, but the "best fits" were tau=9 before, which is just not possible
tau11 = 0.1072     # 0.0846168    "total tau" version.  WRONG.
etau11 = 0.0012    # 0.00102343
tau22 = 0.0165    # 0.0130407
etau22 = 0.0009   # 0.000723822
ratio = 6.5171576487145408
eratio = 0.38657654404132163

tauratio,tauratio_hopkins,tau,tau_hopkins = generate_simpletools(abundance=abundance,opr=opr)
tau1x,tau2x,dens,col = select_data(abundance=abundance)

def tauoneone(meandens, sigma):
    return tau(meandens, sigma, line=tau1x)
def tautwotwo(meandens, sigma):
    return tau(meandens, sigma, line=tau2x)
def tauoneone_hopkins(meandens, sigma):
    return tau_hopkins(meandens, sigma, line=tau1x)
def tautwotwo_hopkins(meandens, sigma):
    return tau_hopkins(meandens, sigma, line=tau2x)

def mcmc_sampler_dict(tauoneone=tauoneone,tautwotwo=tautwotwo,truncate_at_50sigma=False):
    """
    Generator for the MCMC parameters

    truncate_at_50sigma will reject all "solutions" that are 5-sigma deviant