# Path: environ.py
# FERMI_CATALOG_DIR = os.environ.get('FERMI_CATALOG_DIR', '')


from math import *
from array import *
from Plot.PlotLibrary import *
from Catalog.ReadFermiCatalog import *
from environ import FERMI_CATALOG_DIR
    from astropy.coordinates import ICRS, Galactic, FK4, FK5 
import string
import numpy,scipy.optimize
import  ctoolsAnalysis.xml_generator as xml
import matplotlib.pyplot as plt
            flux2FHL = numpy.delete(flux2FHL,i)
            dflux2FHL = numpy.delete(dflux2FHL,i)
            
    ener2FHL = numpy.sqrt(em2FHL*ep2FHL) 
    dem2FHL = ener2FHL-em2FHL
    dep2FHL = ep2FHL-ener2FHL
    c2FHL=Cat.ReadPL2('2FHL')[3]
    e2dnde2FHL = (-c2FHL+1)*flux2FHL*numpy.power(ener2FHL*1e6,-c2FHL+2)/(numpy.power((ep2FHL*1e6),-c2FHL+1)-numpy.power((em2FHL*1e6),-c2FHL+1))*1.6e-6
    de2dnde2FHL = e2dnde2FHL*dflux2FHL/flux2FHL


    for i in xrange(len(ener2FHL)):
        xdata.append(numpy.log10(ener2FHL[i]))
        ydata.append(numpy.log10(e2dnde2FHL[i]))
        dydata.append(numpy.log10(de2dnde2FHL[i]))

    return enerbut2FHL,but2FHL,enerphi2FHL,phi2FHL,ener2FHL, e2dnde2FHL, dem2FHL, dep2FHL, de2dnde2FHL

# table for the data
xdata = []
ydata = []
dydata = []

#Read Data : either you use the FGL name or another name (should be recognize by the astropy module
#source = "3FGL J1517.6-2422"
#Cat = FermiCatalogReader(source,FERMI_CATALOG_DIR,"e2dnde","TeV")
source = "AP Librae"
Cat = FermiCatalogReader.fromName(source,FK5,FERMI_CATALOG_DIR,"e2dnde","TeV")

#print some information

# Path: environ.py
# FERMI_CATALOG_DIR = os.environ.get('FERMI_CATALOG_DIR', '')


# Path: Script/FitLPmodelAndMakeXmlCtools.py
from math import *
from array import *
from Plot.PlotLibrary import *
from Catalog.ReadFermiCatalog import *
from environ import FERMI_CATALOG_DIR
    from astropy.coordinates import ICRS, Galactic, FK4, FK5 
import string
import numpy,scipy.optimize
import  ctoolsAnalysis.xml_generator as xml
import matplotlib.pyplot as plt

            flux2FHL = numpy.delete(flux2FHL,i)
            dflux2FHL = numpy.delete(dflux2FHL,i)
            
    ener2FHL = numpy.sqrt(em2FHL*ep2FHL) 
    dem2FHL = ener2FHL-em2FHL
    dep2FHL = ep2FHL-ener2FHL
    c2FHL=Cat.ReadPL2('2FHL')[3]
    e2dnde2FHL = (-c2FHL+1)*flux2FHL*numpy.power(ener2FHL*1e6,-c2FHL+2)/(numpy.power((ep2FHL*1e6),-c2FHL+1)-numpy.power((em2FHL*1e6),-c2FHL+1))*1.6e-6
    de2dnde2FHL = e2dnde2FHL*dflux2FHL/flux2FHL


    for i in xrange(len(ener2FHL)):
        xdata.append(numpy.log10(ener2FHL[i]))
        ydata.append(numpy.log10(e2dnde2FHL[i]))
        dydata.append(numpy.log10(de2dnde2FHL[i]))

    return enerbut2FHL,but2FHL,enerphi2FHL,phi2FHL,ener2FHL, e2dnde2FHL, dem2FHL, dep2FHL, de2dnde2FHL

# table for the data
xdata = []
ydata = []
dydata = []

#Read Data : either you use the FGL name or another name (should be recognize by the astropy module
#source = "3FGL J1517.6-2422"
#Cat = FermiCatalogReader(source,FERMI_CATALOG_DIR,"e2dnde","TeV")
source = "AP Librae"
Cat = FermiCatalogReader.fromName(source,FK5,FERMI_CATALOG_DIR,"e2dnde","TeV")

#print some information

print "2FGL association ",Cat.Association('3FGL')