# Path: Script/Utils.py
# def LiMa(non, noff, alpha):
# 	excess = non - noff*alpha;
# 
# 	if (alpha == 0):
# 			return sqrt(non)
# 
# 	if excess>0 :
# 		n1 = non;
# 		n2 = noff;
# 		sign = 1.0;
# 		a = alpha;
# 	else :
# 		n2 = non;
# 		n1 = noff;
# 		sign = -1.0;
# 		a = 1.0/alpha;
# 
# 	if(n2 == 0): 
# 		return sign * numpy.sqrt(2 * n1 * numpy.log((1+a)/a));
# 	if(n1 == 0):
# 		return sign * numpy.sqrt(2 * n2 * numpy.log(1+a));
# 
# 	# the standard Li & Ma formula:
# 	nt = n1+n2;
# 	pa = 1 + a;
# 
# 	t1 = n1*numpy.log((pa/a)*(n1/nt));
# 	t2 = n2*numpy.log(pa*(n2/nt));
# 	sig = numpy.sqrt(2)*numpy.sqrt(numpy.abs(t1+t2));
# 
# 	return sign*sig;


import os,sys,numpy
import matplotlib.pyplot as plt
import pyfits
from Script.Utils import LiMa
from ctoolsAnalysis.config import get_config,get_default_config
# ------ Imports --------------- #
# ------------------------------ #
try:
    get_ipython().magic(u'pylab')
except :
    pass

try:  #conf file provided
    config = get_config(sys.argv[-1])
except :
    print "usage : python "+sys.argv[0]+" config_file"
    exit()
prefix = config["target"]["name"]+"_onoff"
Onfile = config['out']+"/"+prefix+"_stacked_pha_on.fits"
Offfile = config['out']+"/"+prefix+"_stacked_pha_off.fits"

Ondata = pyfits.open(Onfile)[1].data
Oncount = Ondata['COUNTS']

# Path: Script/Utils.py
# def LiMa(non, noff, alpha):
# 	excess = non - noff*alpha;
# 
# 	if (alpha == 0):
# 			return sqrt(non)
# 
# 	if excess>0 :
# 		n1 = non;
# 		n2 = noff;
# 		sign = 1.0;
# 		a = alpha;
# 	else :
# 		n2 = non;
# 		n1 = noff;
# 		sign = -1.0;
# 		a = 1.0/alpha;
# 
# 	if(n2 == 0): 
# 		return sign * numpy.sqrt(2 * n1 * numpy.log((1+a)/a));
# 	if(n1 == 0):
# 		return sign * numpy.sqrt(2 * n2 * numpy.log(1+a));
# 
# 	# the standard Li & Ma formula:
# 	nt = n1+n2;
# 	pa = 1 + a;
# 
# 	t1 = n1*numpy.log((pa/a)*(n1/nt));
# 	t2 = n2*numpy.log(pa*(n2/nt));
# 	sig = numpy.sqrt(2)*numpy.sqrt(numpy.abs(t1+t2));
# 
# 	return sign*sig;


# Path: Script/plotOnOff_Ctools.py
import os,sys,numpy
import matplotlib.pyplot as plt
import pyfits
from Script.Utils import LiMa
from ctoolsAnalysis.config import get_config,get_default_config

# ------ Imports --------------- #
# ------------------------------ #
try:
    get_ipython().magic(u'pylab')
except :
    pass

try:  #conf file provided
    config = get_config(sys.argv[-1])
except :
    print "usage : python "+sys.argv[0]+" config_file"
    exit()
prefix = config["target"]["name"]+"_onoff"
Onfile = config['out']+"/"+prefix+"_stacked_pha_on.fits"
Offfile = config['out']+"/"+prefix+"_stacked_pha_off.fits"

Ondata = pyfits.open(Onfile)[1].data
Oncount = Ondata['COUNTS']

dOncount = Ondata['STAT_ERR']