Exemple #1
0
def plot_result(results):
    import matplotlib.pyplot as plt

    # Plot model
    energy = Energy.equal_log_spacing(1e-3, 1e2, nbins=100, unit='TeV')
    flux = results['model'](energy.to('TeV').value)
    flux = Quantity(flux, 'erg cm-2 s-1')
    plt.plot(energy, flux, label='model')

    # Plot data points
    data = FermiData()
    plt.errorbar(x=data.x,
                 y=data.y,
                 yerr=data.staterror,
                 fmt='ro',
                 label='data')

    data = IACTData()
    plt.errorbar(x=data.x,
                 y=data.y,
                 yerr=data.staterror,
                 fmt='ro',
                 label='data')

    # Make it pretty
    plt.loglog()
    plt.xlabel('Energy (TeV)')
    plt.ylabel('E^2 * dN / dE (TeV cm^-2 s^-1)')
    plt.legend()
    filename = 'example_sherpa_mwl.png'
    print('Writing {}'.format(filename))
    plt.savefig(filename)
    def plot_spectral_models(self):

        for component in self.get_components(tags=['pwn', 'composite']):
            fig, ax = plt.subplots()
            table = component['table']
            tag = component['tag']
            vals = []
            idx = 0

            energies = Energy.equal_log_spacing(emin=0.02,
                                                emax=100,
                                                unit='TeV',
                                                nbins=40)
            for row in table:
                idx += 1
                if (idx < 100):
                    spec = LogParabola(
                        amplitude=row['spec_norm'] * u.Unit('MeV-1 s-1 cm-2'),
                        alpha=row['spec_alpha'],
                        reference=1 * u.TeV,
                        beta=row['spec_beta'],
                    )
                    fluxes = []
                    e2dnde = []
                    for energy in energies:
                        dnde = spec.evaluate(
                            energy=energy,
                            amplitude=row['spec_norm'] *
                            u.Unit('MeV-1 s-1 cm-2'),
                            alpha=row['spec_alpha'],
                            reference=1 * u.TeV,
                            beta=row['spec_beta'],
                        )
                        fluxes.append(dnde.value)
                        e2dnde.append(
                            ((energy**2 * dnde).to('erg cm-2 s-1')).value)
                    ax.plot(energies.value,
                            e2dnde,
                            color='black',
                            alpha=0.2,
                            lw=2)
                else:
                    break

            ax.set_title('{} spectra'.format(component['tag']))
            ax.loglog()
            ax.set_xlabel('Energy (TeV)')
            ax.set_ylabel('e2dnde (erg cm-2 s-1)')
            ax.set_ylim(2e-18, 5e-10)

            fig.tight_layout()
            filename = 'ctadc_skymodel_gps_sources_spectra_{}.png'.format(
                component['tag'])
            log.info('Writing {}'.format(filename))
            fig.savefig(filename)
            plt.close(fig)
    def _get_ref_cube(self, enumbins=11):
        p = self.parameters

        wcs = self.reference.wcs.deepcopy()
        shape = (enumbins, ) + self.reference.data.shape
        data = np.zeros(shape)

        energy = Energy.equal_log_spacing(p['emin'], p['emax'], enumbins,
                                          'TeV')
        energy_axis = LogEnergyAxis(energy, mode='center')
        return SkyCube(data=data, wcs=wcs, energy_axis=energy_axis)
Exemple #4
0
def plot_spectral_model(model, ax, plot_kwargs):
    energies = Energy.equal_log_spacing(emin=0.02,
                                        emax=100,
                                        unit='TeV',
                                        nbins=40)
    fluxes = []
    for energy in energies:
        energy_gammalib = gammalib.GEnergy(energy.value, 'TeV')
        fluxes.append(model.eval(energy_gammalib))

    dnde = u.Quantity(fluxes, 'cm-2 s-1 MeV-1')
    e2dnde = (energies**2 * dnde).to('erg cm-2 s-1')

    ax.plot(energies.value, e2dnde.value, **plot_kwargs)
def plot_result(results):
    import matplotlib.pyplot as plt

    # Plot model
    energy = Energy.equal_log_spacing(1e-3, 1e2, nbins=100, unit='TeV')
    flux = results['model'](energy.to('TeV').value)
    flux = Quantity(flux, 'erg cm-2 s-1')
    plt.plot(energy, flux, label='model')

    # Plot data points
    data = FermiData()
    plt.errorbar(x=data.x, y=data.y, yerr=data.staterror, fmt='ro', label='data')

    data = IACTData()
    plt.errorbar(x=data.x, y=data.y, yerr=data.staterror, fmt='ro', label='data')

    # Make it pretty
    plt.loglog()
    plt.xlabel('Energy (TeV)')
    plt.ylabel('E^2 * dN / dE (TeV cm^-2 s^-1)')
    plt.legend()
    filename = 'example_sherpa_mwl.png'
    print('Writing {}'.format(filename))
    plt.savefig(filename)
Exemple #6
0

from gammapy.utils.nddata import NDDataArray, DataAxis, BinnedDataAxis
from gammapy.utils.energy import Energy, EnergyBounds
import numpy as np
import astropy.units as u


# ## 1D example
# 
# Let's start with a simple example. A one dimensional array storing an exposure in ``cm-2 s-1`` as a function of energy. The energy axis is log spaced and thus also the interpolation shall take place in log.

# In[ ]:


energies = Energy.equal_log_spacing(10, 100, 10, unit=u.TeV)
x_axis = DataAxis(energies, name="energy", interpolation_mode="log")
data = np.arange(20, 0, -2) / u.cm ** 2 / u.s
nddata = NDDataArray(axes=[x_axis], data=data)
print(nddata)
print(nddata.axis("energy"))


# In[ ]:


eval_energies = np.linspace(2, 6, 20) * 1e4 * u.GeV
eval_exposure = nddata.evaluate(energy=eval_energies, method="linear")

plt.plot(
    nddata.axis("energy").nodes.value,