import matplotlib
matplotlib.use('Agg')
import datetime
from irfpy.swim import *
from irfpy.cy1orb.Cy1OrbitNr import Cy1OrbitNr
from irfpy.util.utc import *
from pylab import *
import matplotlib.dates
import sys
if __name__ == '__main__':
if len(sys.argv)<2:
print('USAGE: %s orbitnr'%sys.argv[0])
sys.exit(-1)
orb=int(sys.argv[1])
print(orb)
figure(figsize=[12,12])
orbnr=Cy1OrbitNr()
orbnr.setFromDefaultUri()
td0=convert(orbnr.getStartTime(orb), datetime.datetime)
td1=convert(orbnr.getStopTime(orb), datetime.datetime)
t0 = matplotlib.dates.date2num(td0)
t1 = matplotlib.dates.date2num(td1)
obstime=swim_start.getobstime(orbit=orb) # Obtain observation time for specified orbit
t=[]
bg=[]
bgth=[]
et=[]
et1=[]
et2=[]
etr=[]
for otime in obstime:
print(otime)
cntdata = swim_start.getdata(otime)
jd =cntdata.getJd()
cntarr = cntdata.getData()
print(jd)
print(cntarr.sum(), end=' ')
cntarr2 = swim_start.filter.remove_background_5numsum(cntarr, factor=2)
print(swim_start.filter.info)
print(cntarr2.sum())
# cntarr1 = swim_start.filter.remove_one_count(cntarr2)
cntarr1 = swim_start.filter.remove_isolated_one_count(cntarr2)
info=swim_start.filter.info.split()
_bg=float(info[0][3:])
_bgth=float(info[1][5:])
t.append(matplotlib.dates.date2num(convert(otime, datetime.datetime)))
bg.append(_bg)
bgth.append(_bgth)
enraw = cntarr.sum(axis=1)
en1rm = cntarr1.sum(axis=1)
enpro = cntarr2.sum(axis=1)
enres = enraw-en1rm
et.append(enraw)
et1.append(en1rm)
et2.append(enpro)
etr.append(enres)
ax=subplot(5,1,5)
plot(t, bg, '*', label='background')
plot(t, bgth, '+', label='threshold')
x,y=meshgrid(t, list(range(17)))
subplot(5,1,1, sharex=ax)
gca().set_title('Raw data ([%d] %s-%s)'%(orb, td0.strftime('%FT%T'), td1.strftime('%FT%T')))
aet=log10(array(et)+0.1)
pcolor(x,y,aet.T, vmax=3, vmin=0.5)
#colorbar()
subplot(5,1,3, sharex=ax)
gca().set_title('One count removal')
aet1=log10(array(et1)+0.1)
pcolor(x,y,aet1.T, vmax=3, vmin=0.5)
#colorbar()
subplot(5,1,2, sharex=ax)
gca().set_title('5 num summary removal')
aet2=log10(array(et2)+0.1)
pcolor(x,y,aet2.T, vmax=3, vmin=0.5)
#colorbar()
subplot(5,1,4, sharex=ax)
gca().set_title('Background')
aetr=log10(array(etr)+0.1)
pcolor(x,y,aetr.T, vmax=3, vmin=0.5)
#colorbar()
print(t0, t1)
ax.set_xlim(t0,t1)
ax.xaxis.set_major_formatter(DateFormatter('%T'))
gcf().autofmt_xdate()
savefig('SWIM_BG_%04d'%orb)