import numpy as np
import pylab as py
import ReadMapMakeXml as rxml
import sys
import os

'''
 This script reads the tod data that are generated by LB_SYSPL_beta*.*

 Run this script as
    > python script_todio.py yourxmlfile.xml your_daynum your_todnum
 example)
    > python script_todio.py xmlfile.xml day1 tod1
 or otherwise strip this script to read the tod in your favorite way.

 Written by T. Matsumura, 2013-12-28
'''

xml_filename = sys.argv[1]
xml_input = rxml.Get_Mapmake_Inputs(xml_filename)

name_day = sys.argv[2]
name_tod = sys.argv[3]

out = np.load(xml_input["dir_simedmap"]+'/'+name_day+'/tod/'+name_tod+'.npz')

print out['help']
top_tod = out['top_tod']
bot_tod = out['bot_tod']
i_pix = out['i_pix']
pix_list = out['pix_list']
top_ptg_out = out['top_ptg_out']
bot_ptg_out = out['bot_ptg_out']

print '++++++++++++++++++++++++++++++++++++++++++++++'
print ' read ', pix_list[i_pix], ' pixel'

py.subplot(211)
py.plot(top_tod)
py.plot(bot_tod)
py.title('pixel # (top,bot,pix): '+ str(pix_list[i_pix]) ) 
py.xlabel('sample')
py.ylabel('top, bot [Kcmb (or $\mu$K)]')

py.subplot(212)
py.plot(top_tod-bot_tod)
py.xlabel('sample')
py.ylabel('top - bot [Kcmb (or $\mu$K)]')

py.savefig('tmp.png')
os.system('display tmp.png &')

