def plot_source_spectra(name):
    plot_source_spectrum(source=SourceCatalog3FGL()[name],
                         label="3FGL",
                         color="r")
    plot_source_spectrum(source=SourceCatalog2FHL()[name],
                         label="2FHL",
                         color="g")
    plot_source_spectrum(source=SourceCatalog3FHL()[name],
                         label="3FHL",
                         color="b")

    ax = plt.gca()
    ax.set_ylim(1.0e-12, 7.0e-11)
    ax.set_xlim(1.0e-4, 2.0)
    ax.set_xlabel("Energy (TeV)")
    ax.set_ylabel("E^2 dN/dE (erg cm-2 s-1])")
    plt.legend(loc=0)
Beispiel #2
0
def plot_source_spectra(name):
    plot_source_spectrum(source=SourceCatalog3FGL()[name],
                         label='Fermi 3FGL',
                         color='r')
    plot_source_spectrum(source=SourceCatalog2FHL()[name],
                         label='Fermi 2FHL',
                         color='g')
    plot_source_spectrum(source=SourceCatalog1FHL()[name],
                         label='Fermi 1FHL',
                         color='c')
    plot_source_spectrum(source=SourceCatalog3FHL()[name],
                         label='Fermi 3FHL',
                         color='b')

    ax = plt.gca()
    ax.set_ylim(1.e-12, 7.e-11)
    ax.set_xlim(1.e-4, 2.)
    ax.set_xlabel('Energy (TeV)')
    ax.set_ylabel('E^2 dN/dE (erg cm-2 s-1])')
    plt.legend(loc=0)
Beispiel #3
0
 def setup_class(cls):
     cls.cat = SourceCatalog3FHL()
Beispiel #4
0
 def setup_class(cls):
     cls.cat = SourceCatalog3FHL()
     # Use 3FHL J0534.5+2201 (Crab) as a test source
     cls.source_name = "3FHL J0534.5+2201"
     cls.source = cls.cat[cls.source_name]
Beispiel #5
0
from gammapy.spectrum import FluxPointsDataset, FluxPoints
from gammapy.catalog import (
    SourceCatalog3FGL,
    SourceCatalogGammaCat,
    SourceCatalog3FHL,
)
from gammapy.utils.fitting import Fit

# ## Load spectral points
#
# For this analysis we choose to work with the source 'HESS J1507-622' and the associated Fermi-LAT sources '3FGL J1506.6-6219' and '3FHL J1507.9-6228e'. We load the source catalogs, and then access source of interest by name:

# In[ ]:

fermi_3fgl = SourceCatalog3FGL()
fermi_3fhl = SourceCatalog3FHL()
gammacat = SourceCatalogGammaCat("$GAMMAPY_DATA/gamma-cat/gammacat.fits.gz")

# In[ ]:

source_gammacat = gammacat["HESS J1507-622"]
source_fermi_3fgl = fermi_3fgl["3FGL J1506.6-6219"]
source_fermi_3fhl = fermi_3fhl["3FHL J1507.9-6228e"]

# The corresponding flux points data can be accessed with `.flux_points` attribute:

# In[ ]:

flux_points_gammacat = source_gammacat.flux_points
flux_points_gammacat.table
Beispiel #6
0
    def __init__(self, selection="short", savefig=True):
        self.datadir = "$GAMMAPY_DATA"
        self.resdir = "./res"
        self.savefig = savefig

        # event list
        self.events = EventList.read(
            self.datadir + "/fermi_3fhl/fermi_3fhl_events_selected.fits.gz")
        # psf
        self.psf = EnergyDependentTablePSF.read(
            self.datadir + "/fermi_3fhl/fermi_3fhl_psf_gc.fits.gz")

        # mask margin
        psf_r99max = self.psf.containment_radius(10 * u.GeV, fraction=0.99)
        self.psf_margin = np.ceil(psf_r99max.value[0] * 10) / 10.0

        # energies
        self.dlb = 1 / 8.0
        El_extra = 10**np.arange(3.8, 6.51, 0.1)  # MeV
        self.logEc_extra = (np.log10(El_extra)[1:] +
                            np.log10(El_extra)[:-1]) / 2.0
        self.El_flux = [10.0, 20.0, 50.0, 150.0, 500.0, 2000.0]
        El_fit = 10**np.arange(1, 3.31, 0.1)
        self.energy_axis = MapAxis.from_edges(El_fit,
                                              name="energy",
                                              unit="GeV",
                                              interp="log")

        # background iso
        infile = Path(self.datadir + "/fermi_3fhl/iso_P8R2_SOURCE_V6_v06.txt")
        outfile = Path(self.resdir + "/iso_P8R2_SOURCE_V6_v06_extra.txt")
        self.model_iso = extrapolate_iso(infile, outfile, self.logEc_extra)

        # regions selection
        file3fhl = self.datadir + "/catalogs/fermi/gll_psch_v13.fit.gz"
        self.FHL3 = SourceCatalog3FHL(file3fhl)
        hdulist = fits.open(make_path(file3fhl))
        self.ROIs = hdulist["ROIs"].data
        Scat = hdulist[1].data
        order = np.argsort(Scat.Signif_Avg)[::-1]
        ROIs_ord = Scat.ROI_num[order]

        if selection == "short":
            self.ROIs_sel = [430, 135, 118, 212, 277, 42, 272, 495]
            # Crab, Vela, high-lat, +some fast regions
        elif selection == "long":
            # get small regions with few sources among the most significant
            indexes = np.unique(ROIs_ord, return_index=True)[1]
            ROIs_ord = [ROIs_ord[index] for index in sorted(indexes)]
            self.ROIs_sel = [
                kr for kr in ROIs_ord
                if sum(Scat.ROI_num == kr) <= 4 and self.ROIs.RADIUS[kr] < 6
            ][:100]
        elif selection == "debug":
            self.ROIs_sel = [135]  # Vela region

        # fit options
        self.optimize_opts = {
            "backend": "minuit",
            "tol": 10.0,
            "strategy": 2,
        }

        # calculate flux points only for sources significant above this threshold
        self.sig_cut = 8.0

        # diagnostics stored to produce plots and outputs
        self.diags = {
            "message": [],
            "stat": [],
            "params": {},
            "errel": {},
            "compatibility": {},
            "cat_fp_sel": [],
        }
        self.diags["errel"]["flux_points"] = []
        keys = [
            "PL_tags",
            "PL_index",
            "PL_amplitude",
            "LP_tags",
            "LP_alpha",
            "LP_beta",
            "LP_amplitude",
        ]
        for key in keys:
            self.diags["params"][key] = []
Beispiel #7
0
    def __init__(self, selection="short", savefig=True):
        log.info("Executing __init__()")
        self.resdir = BASE_PATH / "results"
        self.savefig = savefig

        # event list
        self.events = EventList.read(
            "$GAMMAPY_DATA/fermi_3fhl/fermi_3fhl_events_selected.fits.gz"
        )
        # psf
        self.psf = EnergyDependentTablePSF.read(
            "$GAMMAPY_DATA/fermi_3fhl/fermi_3fhl_psf_gc.fits.gz"
        )

        # mask margin
        psf_r99max = self.psf.containment_radius(10 * u.GeV, fraction=0.99)
        self.psf_margin = np.ceil(psf_r99max.value[0] * 10) / 10.0

        # energies
        self.El_flux = [10.0, 20.0, 50.0, 150.0, 500.0, 2000.0]
        El_fit = 10 ** np.arange(1, 3.31, 0.1)
        self.energy_axis = MapAxis.from_edges(
            El_fit, name="energy", unit="GeV", interp="log"
        )

        # iso norm=0.92 see paper appendix A
        self.model_iso = create_fermi_isotropic_diffuse_model(
            filename="data/iso_P8R2_SOURCE_V6_v06_extrapolated.txt",
            norm=0.92,
            interp_kwargs={"fill_value": None},
        )
        # regions selection
        file3fhl = "$GAMMAPY_DATA/catalogs/fermi/gll_psch_v13.fit.gz"
        self.FHL3 = SourceCatalog3FHL(file3fhl)
        hdulist = fits.open(make_path(file3fhl))
        self.ROIs = hdulist["ROIs"].data
        Scat = hdulist[1].data
        order = np.argsort(Scat.Signif_Avg)[::-1]
        ROIs_ord = Scat.ROI_num[order]

        if selection == "short":
            self.ROIs_sel = [430, 135, 118, 212, 277, 42, 272, 495]
            # Crab, Vela, high-lat, +some fast regions
        elif selection == "long":
            # get small regions with few sources among the most significant
            indexes = np.unique(ROIs_ord, return_index=True)[1]
            ROIs_ord = [ROIs_ord[index] for index in sorted(indexes)]
            self.ROIs_sel = [
                kr
                for kr in ROIs_ord
                if sum(Scat.ROI_num == kr) <= 4 and self.ROIs.RADIUS[kr] < 6
            ][:100]
        elif selection == "debug":
            self.ROIs_sel = [135]  # Vela region
        else:
            raise ValueError(f"Invalid selection: {selection!r}")

        # fit options
        self.optimize_opts = {
            "backend": "minuit",
            "optimize_opts": {"tol": 10.0, "strategy": 2},
        }

        # calculate flux points only for sources significant above this threshold
        self.sig_cut = 8.0

        # diagnostics stored to produce plots and outputs
        self.diags = {
            "message": [],
            "stat": [],
            "params": {},
            "errel": {},
            "compatibility": {},
            "cat_fp_sel": [],
        }
        self.diags["errel"]["flux_points"] = []
        keys = [
            "PL_tags",
            "PL_index",
            "PL_amplitude",
            "LP_tags",
            "LP_alpha",
            "LP_beta",
            "LP_amplitude",
        ]
        for key in keys:
            self.diags["params"][key] = []