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'
 #print truetemp.shape, data.shape
 diff = Map((abs(truetemp - data) / truetemp) * 100, newmap.meta.copy())
 print 'T_diff', diff.min(), diff.mean(), diff.max()
 if n_pars == 3:
     wdata = Map(newmap.dem_width, newmap.meta.copy())
     truew = np.ones(shape=(n_temps, n_heights)) * wid
     #print 'truew', truew.min(), truew.mean(), truew.max()
     diffw = Map((abs(truew - wdata.data) / truew) * 100, newmap.meta.copy())
     print 'w_out', wdata.min(), wdata.mean(), wdata.max()
     print 'w_diff', diffw.min(), diffw.mean(), diffw.max()
 
 #print wid, newmap.xrange, newmap.yrange, newmap.scale
 #print wid, diff.xrange, diff.yrange, diff.scale
 fig = plt.figure(figsize=(24, 12))
 fig.add_subplot(1, 3, 1)
 newmap.plot(cmap='coolwarm', vmin=5.6, vmax=7.0, aspect='auto')
 plt.colorbar()
 plt.title('Solution log(T)', fontsize=28)
 plt.ylabel('Input log(T)', fontsize=24)
 plt.xlabel('Input EM', fontsize=24)#width', fontsize=24)
 #fig.gca().add_artist(rect)
 plt.axvline(20.0, color='white')
 plt.axvline(35.0, color='white')
 plt.axhline(5.6, color='white')
 plt.axhline(7.0, color='white')
 
 ax = fig.add_subplot(1, 3, 2)
 #print 'diff', diff.min(), diff.max()
 #print np.nanmin(diff.data), np.nanmax(diff.data)
 diff.plot(cmap='RdYlGn_r', aspect='auto')#, vmin=diff.min(), vmax=diff.max())
 plt.colorbar()