bint_thresh = 8e9



i_fig = 0
plt.close('all')
beam_col = ['b','r']
for beam in [1,2]:
    fbct = FBCT.FBCT(fill_dict, beam=beam)
    bct = BCT.BCT(fill_dict, beam=beam)
    
    fig1 = plt.figure(i_fig, figsize=(14, 8), tight_layout=False)
    fig1.patch.set_facecolor('w')
    ax0 = plt.subplot(211)
    ax1 = plt.subplot(212)
    ms.mystyle()

    ax0.plot((bct.t_stamps-t_ref)/3600., bct.values, color=beam_col[beam-1], lw=2)
    for i in xrange(0, n_traces):
        t_cut_h = traces_times[i]
        t_curr = t_ref+t_cut_h*3600.	
        fbct_curr, t_fbct_curr = fbct.nearest_older_sample(t_curr, flag_return_time=True)
            
        ax1.plot(fbct_curr, color=ms.colorprog(i, n_traces),
                     label='%.2f h'%((t_fbct_curr-t_ref)/3600.))
        ax0.axvline((t_fbct_curr-t_ref)/3600., lw=1.5, color=ms.colorprog(i, n_traces))

    ax1.set_xlabel('25 ns slot')
    ax1.set_xlim(0, 3500)
    ax1.set_ylabel('Bunch intensity [p$^+$]')
    ax1.grid('on')
    heatloads = SetOfHomogeneousNumericVariables(variable_list=arc_keys_list +
                                                 quad_keys_list,
                                                 timber_variables=fill_dict)
energy = Energy.energy(fill_dict, beam=1)
bct_bx = {}
for beam_n in colstr:
    bct_bx[beam_n] = BCT(fill_dict, beam=beam_n)

hli_calculator = ihl.HeatLoadCalculatorImpedanceLHCArc()
hlsr_calculator = srhl.HeatLoadCalculatorSynchrotronRadiationLHCArc()

hl_imped_fill = fc.HeatLoad_calculated_fill(fill_dict, hli_calculator)
hl_sr_fill = fc.HeatLoad_calculated_fill(fill_dict, hlsr_calculator)

figs = []
ms.mystyle(12)
title = 'Recalculated arc heat loads %i' % filln
fig = ms.figure(title, figs)

# Arc half cell histograms
lhc_hist_dict = qf.lhc_histograms(qbs_ob, avg_time_hrs, 0.1)
arc_hist_dict = lhc_hist_dict['arcs']
arc_hist_total = lhc_hist_dict['total']

# Intensity and Energy
sptotint = plt.subplot(2, 2, 1)
sptotint.set_ylabel('Total intensity [p+]')
sptotint.grid('on')
for beam_n in colstr:
    sptotint.plot((bct_bx[beam_n].t_stamps - t_ref) / 3600.,
                  bct_bx[beam_n].values,
    tm.parse_timber_file(
        'fill_bunchbybunch_data_csvs/bunchbybunch_data_fill_%d.csv' % filln,
        verbose=False))

pl.close('all')
for beam in [1, 2]:
    energy = Energy.energy(fill_dict, beam=beam)
    bct = BCT.BCT(fill_dict, beam=beam)
    bsrt = BSRT.BSRT(fill_dict, beam=beam)
    if plot_emittance:
        bsrt.calculate_emittances(energy)

    # START PLOT
    fig_h = pl.figure(beam, figsize=(17, 10))
    fig_h.patch.set_facecolor('w')
    ms.mystyle()

    # Intensity and energy
    sp_int = pl.subplot2grid((2, 3), (0, 0), rowspan=1)
    sp_energy = sp_int.twinx()

    mask_bct = bct.values > 1e12
    #~ t_start_bct = bct.t_stamps[np.min(np.where(mask_bct))]
    sp_int.plot((bct.t_stamps[mask_bct] - t_ref) / 3600., bct.values[mask_bct],
                'b')
    mask_ene = energy.t_stamps > t_ref
    sp_energy.plot((energy.t_stamps[mask_ene] - t_ref) / 3600.,
                   energy.energy[mask_ene] / 1e3, 'k')

    sp_int.set_ylabel('Intensity [p$^+$]')
    sp_energy.set_ylabel('Energy [TeV]')
import matplotlib.pyplot as plt

from LHCMeasurementTools import myfilemanager as mfm
from LHCMeasurementTools import mystyle as ms

ob = mfm.myloadmat_to_obj('./time_build.mat')

plt.close('all')
ms.mystyle(fontsz=14)
fig1 = plt.figure(1)
#ax11 = fig1.add_subplot(2,1,1)
ax12 = fig1.add_subplot(111)

#ax11.semilogx(ob.n_variables, ob.Dt, 'o')
ax12.loglog(ob.n_variables, ob.Dt / ob.n_variables, 'o')
ax12.set_xlabel('Number of variables')
ax12.set_ylabel('Extraction time [s/variable]')
ax12.grid(True, linestyle='--')
ax12.set_ylim(bottom=0.1)
fig1.subplots_adjust(bottom=.15, left=.16)

plt.show()