Skip to content

Commit

Permalink
Introducing gammapy.utils.serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
joleroi committed Apr 27, 2018
1 parent bd020db commit a0c231c
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 475 deletions.
1 change: 0 additions & 1 deletion gammapy/catalog/gammacat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from ..extern import six
from astropy import units as u
from astropy.table import Table
from ..utils.modeling import SourceModel, SourceLibrary, UnknownModelError
from ..utils.scripts import make_path
from ..spectrum import FluxPoints
from ..spectrum.models import PowerLaw, PowerLaw2, ExponentialCutoffPowerLaw
Expand Down
25 changes: 24 additions & 1 deletion gammapy/cube/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@
import astropy.units as u
from astropy.utils import lazyproperty
from ..utils.modeling import ParameterList
from ..utils.scripts import make_path

__all__ = [
'SourceLibrary',
'SkyModel',
'SkyModelMapEvaluator',
]

class SourceLibrary(object):
"""Collection of `~gammapy.cube.models.SkyModel`
Parameters
----------
skymodels : list of `~gammapy.cube.models.SkyModel`
Sky models
"""
def __init__(self, skymodels):
self.skymodels = skymodels

@classmethod
def from_xml(cls, filename):
from ..utils.serialization import xml_to_source_library
path = make_path(filename)
xml = path.read_text()
return xml_to_source_library(xml)


class SkyModel(object):
"""Sky model component.
Expand All @@ -27,11 +47,14 @@ class SkyModel(object):
Spatial model (must be normalised to integrate to 1)
spectral_model : `~gammapy.spectrum.models.SpectralModel`
Spectral model
name : str
Model identifier
"""

def __init__(self, spatial_model, spectral_model):
def __init__(self, spatial_model, spectral_model, name='SkyModel'):
self._spatial_model = spatial_model
self._spectral_model = spectral_model
self.name = name
self._init_parameters()

def _init_parameters(self):
Expand Down
30 changes: 26 additions & 4 deletions gammapy/spectrum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,12 @@ def __init__(self, energy, values, scale=1, scale_logy=True):

@classmethod
def read_xspec_model(cls, filename, param):
"""A Table containing absorbed values from a XSPEC model
as a function of energy.
Todo:
Format of the file should be described and discussed in
"""Read XSPEC table model
The input is a table containing absorbed values from a XSPEC model as a
function of energy.
TODO: Format of the file should be described and discussed in
https://gamma-astro-data-formats.readthedocs.io/en/latest/index.html
Parameters
Expand Down Expand Up @@ -1230,6 +1232,26 @@ def read_xspec_model(cls, filename, param):

return cls(energy=energy, values=values, scale_logy=False)

@classmethod
def read_fermi_isotropic_model(cls, filename):
"""Read Fermi isotropic diffuse model
see `LAT Background models <https://fermi.gsfc.nasa.gov/ssc/data/access/lat/BackgroundModels.html>`_
Parameters
----------
filename : `str`
filename
param : float
Model parameter value
"""
filename = str(make_path(filename))
vals = np.loadtxt(filename)
energy = vals[:,0] * u.MeV
values = vals[:,1] * u.Unit('MeV-1 s-1 cm-2')

return cls(energy=energy, values=values, scale_logy=False)

def evaluate(self, energy, scale):
"""Evaluate the model (static function)."""
# What's with all this checking?
Expand Down

0 comments on commit a0c231c

Please sign in to comment.