from matplotlib import use
use('agg')
import sys
from os.path import join, expanduser
CThome = expanduser(join('~', 'CoronaTemps'))
sys.path.append(CThome)
from temperature import TemperatureMap as TMap
from matplotlib import pyplot as plt
from sunpy.time import parse_time as parse

dates = ['2014-12-15 12:00', '2014-12-15 12:30', '2014-12-15 14:00']

completed = False
while completed == False:
    try:
        for date in dates:
            thismap = TMap(date, data_dir=join(CThome, 'data'), maps_dir=CThome, verbose=True)
            thismap.save()
            #thismap = thismap.submap([-1200, -200], [-500, 500], units='data')
            fig = plt.figure(figsize=(32, 24))
            thismap.plot()
            plt.colorbar()
            plt.savefig('t_{:%Y-%m-%d_%H-%M-%S}'.format(parse(thismap.date)))
            plt.close()
        completed = True
    except:
        print 'Failed - restarting'
from matplotlib import use
use('agg')
import sys
from os.path import join, expanduser
CThome = expanduser(join('~', 'CoronaTemps'))
sys.path.append(CThome)
from temperature import TemperatureMap as TMap
from matplotlib import pyplot as plt
from sunpy.time import parse_time as parse

dates = ['2014-12-15 12:00', '2014-12-15 12:30', '2014-12-15 14:00']

completed = False
while completed == False:
    try:
        for date in dates:
            thismap = TMap(date,
                           data_dir=join(CThome, 'data'),
                           maps_dir=CThome,
                           verbose=True)
            thismap.save()
            #thismap = thismap.submap([-1200, -200], [-500, 500], units='data')
            fig = plt.figure(figsize=(32, 24))
            thismap.plot()
            plt.colorbar()
            plt.savefig('t_{:%Y-%m-%d_%H-%M-%S}'.format(parse(thismap.date)))
            plt.close()
        completed = True
    except:
        print 'Failed - restarting'
    #plt.savefig(path.join(outdir, 'model_emission_h={}'.format(np.log10(wid)).replace('.', '_')))
    plt.savefig(path.join(outdir, 'model_emission_w={}'.format(wid).replace('.', '_')))
    plt.close()
    
    #images = [Map(emission[i, :, :, w], mapmeta) for i in range(6)]
    images = [Map(emission[i, :, w, :], mapmeta) for i in range(6)]
    if n_pars == 3:
        cmdargs = "mpiexec -n 10 python {} model {} {} {} {} {} {}".format(
            tmap_script, n_pars, path.join(CThome, 'data'),
            None, None, True, True).split()
    else:
        cmdargs = "python {} model {} {} {} {} {} {}".format(
            tmap_script, n_pars, path.join(CThome, 'data'),
            None, None, True, True).split()
    status = subp.call(cmdargs)
    newmap = TemperatureMap(fname=path.join(CThome, 'temporary.fits'))
    subp.call(["rm", path.join(CThome, 'temporary.fits')])
    data, meta = newmap.data, newmap.meta
    fitsmap = Map(np.log10(newmap.goodness_of_fit), newmap.meta.copy())
    #print fitsmap.max()
    newmap.data = data
    print '-------------MINMAX:-------------'#, newmap.min(), newmap.max(), newmap.shape, 
    #print newmap.data[newmap.data == 0].shape, '----------\n'
    print 'GoF', fitsmap.min(), fitsmap.mean(), fitsmap.max()
    print 'T_out', newmap.min(), newmap.mean(), newmap.max()

    #truetemp = np.array(list(temps)*n_widths).reshape((n_widths, n_temps)).T
    truetemp = np.array(list(temps)*n_heights).reshape((n_heights, n_temps)).T
    #print truetemp.shape, data.shape
    diff = Map((abs(truetemp - data) / truetemp) * 100, newmap.meta.copy())
    print 'T_diff', diff.min(), diff.mean(), diff.max()