figure()
sttl = 'Background - R1017'
cts = msc349bkg
ttl = 'IP=1000ms, 20 scan summation, H2counts={:.0f}, H2/H20={:.2f}%'
ttl = ttl.format(cts[1], (cts[1]/cts[14])*100)
plot_MS_bar(massBkg, cts, sttl, ttl)

#==========Generating cross talk plots from data====================#

#specific case for 2015-349 containing data for both H2 and water
#the script did not terminate D003 correctly 
#raw D003 contains H2 OSNB CS-off, H2O bkgc, H2O bkgo, H2O OSNB CS-On  
ds349 = get_datasets(sorted(msd349.keys()))
ds349['D003']['srange'] = (101, 140)
ds349['D000bkgc'] = ds349.pop('D000')
_ = ds349.pop('D004')
_ = ds349.pop('D005')

ds349h2o = {'D000bkgc': {'srange': (151, 170)},
            'D000bkgo': {'srange': (171, 190)},
            'D003': {'srange': (196, 235)},
            'D004': {'srange': (236, 275)},
            'D005': {'srange': (276, 315)}}

#ds349 uses the same OSNB background dataset as ds349h2o
ds349['D000bkgo']['sum'] = ds349h2o['D000bkgo'].copy()

#include 'H2O' in the ds349h2o sttl 
for k in ds349h2o.keys():
    ds349h2o[k]['sttl'] = ds349h2o[k]['sttl']+' H2O'
    print ds349h2o[k]['sttl']

#the background scans in ds349 are masses 1-50 -> truncate to 1-23
dst = ds349
for k in dst.keys():
    if len(dst[k]['masses']) > 19:
        dst[k]['masses'] = masses
        dst[k]['sum'] = dst[k]['sum'][:19]

#the background scans in ds349h2o are masses 1-50 -> truncate to 1-23
dst = ds349h2o
for k in dst.keys():
    if len(dst[k]['masses']) > 19:
        dst[k]['masses'] = masses
        dst[k]['sum'] = dst[k]['sum'][:19]


#specific case for ds356
ds356['D004']['srange'] = (121, 145)
ds356['D001bkgc'] = ds356.pop('D001')
ds356['D002bkgo'] = ds356.pop('D002')

#specific case for ds015
ds015['D003']['sttl'] = 'OSNB - OS Off - R1023'
ds015['D001bkgc'] = ds015.pop('D001')
ds015['D002bkgo'] = ds015.pop('D002')

#specific case for ds018
ds018['D001bkgc'] = ds018.pop('D001')
ds018['D002bkgo'] = ds018.pop('D002')

#specific case for ds027
ds027['D001bkgc'] = ds027.pop('D001')
ds027['D002bkgo'] = ds027.pop('D002')

#specific case for ds028
_ = ds028.pop('D000')
ds028['D001bkgc'] = ds028.pop('D001')
ds028['D002bkgo'] = ds028.pop('D002')

#special case for ds046 
#some of the scans in D000 occur between D002 and D003. 
#Therefore need to sort on integer value of scan number
msc046.sort(key=lambda ms: int(ms[2]))
_ = ds046.pop('D000')
ds046['D001bkgc'] = ds046.pop('D001')
ds046['D002bkgo'] = ds046.pop('D002')

#specific case for ds047 
#Observed evolution of outgassing products as filament warms up
#R1031-D003was moved to 2016-047 directory from 2016-048
#plot evolution of mass species

#specific case for ds048
#must be sorted similar to ds046
msc048.sort(key=lambda ms: int(ms[2]))
_ = ds048.pop('D000')
ds048['D004bkgc'] = ds048.pop('D004')
ds048['D005bkgo'] = ds048.pop('D005')


#specific case for 2015-352
dsets = get_datasets(msf)
msc352 = get_INMS_scan_data(msd352, id0='mass', sfields=sfld, mfields=mfld)
ds352 = {k:{'srange':(int(dsets[k][0][1:]),int(dsets[k][-1][1:]))} 
			for k in dsets.keys()[1:4]}
ds352['D000bkgo'] = {'srange':(57,96)}
ds352['D000bkgc'] = {'srange':(17,56)}
_ = ds352.pop('D000')

plot_crosstalk(ds352, msc352)
dkeys = sorted(ds352.keys())
dsum = [ds352[k]['sum'] for k in dkeys]
dsum[2] = dsum[2]-dsum[1]
dsum[3] = dsum[3]-dsum[0]
dsum[4] = dsum[4]-dsum[1]

for i in range(2,5):
    sttl = ds352[dkeys[i]]['sttl']+' Bkg Subtract'
    cts = dsum[i]
    ttl = ds352[dkeys[i]]['ttl']
    plot_MS_bar(masses, cts, sttl, ttl)

cstoff = dsum[4]/dsum[3]*100
sttl = 'Crosstalk Ratio, 8.112kms, CS Off - R1020'
cts = cstoff
ttl = 'IP=1000ms, 40 scan summation, KE=0.675eV, H2 ratio={:.3f}%'.format(cts[1])
plot_MS_bar(masses, cts, sttl, ttl, yl='Percent')

cston = dsum[2]/dsum[3]*100
sttl = 'Crosstalk Ratio, 8.112kms, CS On - R1020'
cts = cston
ttl = 'IP=1000ms, 40 scan summation, KE=0.675eV, H2 ratio={:.3f}%'.format(cts[1])
plot_MS_bar(masses, cts, sttl, ttl)

#all data sets
dsAll = [ds352, ds349, ds356, ds349h2o]
dsp = [[ds[s]['sttl'], ds[s]['h2cts'].mean(), ds[s]['wcts'].mean() ] 
		for ds in dsAll for s in sorted(ds.keys())]


#=================Consolidating H2/H2O leakag data============================#
# gather all datasets into one list
# iterate the list and 

#H2 Thermal leakage
thermalH2Data = [ds349, ds352, ds356, ds018, ds028] 

#=====================plotting SID Data for R1003============================#
dpath = inmsDataDir+'2015-272'
msSID = get_INMS_data(dpath, '*_MS_*')
mscSID = get_INMS_scan_data(msSID, id0='mass')

BKG = [0]
SID1 = range(1,8,2)
THM1 = range(2,9,2)
SID2 = range(9,16,2)
THM2 = range(10,17,2)
SID3 = range(17,25,2)
THM3 = range(18,25,2)

sid272 = {'SID1':SID1, 'THM1':THM1, 
            'SID2':SID2, 'THM2':THM2, 
            'SID3':SID3, 'THM3':THM3,
            'BKG':BKG}

ds272 = get_datasets(sid272)
sum_sets(ds272, mscSID)
dkeys = sorted(ds272.keys())

for k in sid272.keys():
    sid272[k] = sum(array([ds272[dkeys[i]]['sum'] for i in sid272[k]]),0)

M = mscSID[0][-1]
for k, s in sid272.iteritems():
    plot_MS_bar(M, s, k, 'R1003')

SID_1 = sid272['SID1']-sid272['THM1']
SID_2 = sid272['SID2']-sid272['THM2']
SID_3 = sid272['SID3']-sid272['THM3']

#=====================Plotting Simulted Energy Scan Data from GM=====================#
os.chdir(inmsDataDir)
simfile = glob.glob('*.dat')[0]
with open(simfile) as df:
        dlist = [item.strip('\r\n').split(',') 
        			for item in df.readlines()]
simQL = array([d[0] for d in dlist], float)
simCnts = array([d[1] for d in dlist], float)
figure()
plot(simQL, simCnts/simCnts.max())
ylim(0, 1.1)
ylabel('Counts')
xlabel('QL3 Voltage')    
title('Simulated Energy Scan 11.6eV CO2', fontsize=16)
draw()
savefig('Simulated Energy Scan 11.6eV CO2.png')
close()