Skip to content

Instantly share code, notes, and snippets.

@giacomov
Created March 28, 2016 23:53
Show Gist options
  • Save giacomov/6e0ea510d1b20a0e1988 to your computer and use it in GitHub Desktop.
Save giacomov/6e0ea510d1b20a0e1988 to your computer and use it in GitHub Desktop.
FileSpectrum in pyLikelihood
import FuncFactory
# Create the text file with the energies and the fluxes (which can be whatever at this stage)
source_name = 'a'
tempName = "__%s.txt" % source_name
with open(tempName, "w+") as f:
for e, v in zip(energies, fluxes):
# Gtlike needs energies in MeV and fluxes in ph/MeV/cm2)
f.write("%s %s\n" % (e, v))
# Now generate the XML source wrapper
# This is convoluted, but that's the ST way of doing things...
# The final product is a class with a writeXml method
src = '\n'.join((('<source name= "%s" ' % source_name) + 'type="PointSource">',
' <spectrum type="PowerLaw2"/>',
' <!-- point source units are cm^-2 s^-1 MeV^-1 -->',
' <spatialModel type="SkyDirFunction"/>',
'</source>\n'))
src = FuncFactory.minidom.parseString(src).getElementsByTagName('source')[0]
src = FuncFactory.Source(src)
src.spectrum = FuncFactory.FileFunction()
src.spectrum.file = tempName
src.spectrum.parameters['Normalization'].value = 1.0
src.spectrum.parameters['Normalization'].fix()
src.spectrum.setAttributes()
src.deleteChildElements('spectrum')
src.node.appendChild(src.spectrum.node)
src.spatialModel = FuncFactory.SkyDirFunction()
src.deleteChildElements('spatialModel')
src.node.appendChild(src.spatialModel.node)
ra, dec = 120.6, -12.3 # whatever you need
src.spatialModel.RA.value = ra
src.spatialModel.DEC.value = dec
src.spatialModel.setAttributes()
src.setAttributes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment