def test_fermi_isotropic(): filename = "$GAMMAPY_DATA/fermi_3fhl/iso_P8R2_SOURCE_V6_v06.txt" model = create_fermi_isotropic_diffuse_model(filename) coords = {"lon": 0 * u.deg, "lat": 0 * u.deg, "energy": 50 * u.GeV} flux = model(**coords) assert_allclose(flux.value, 1.463e-13, rtol=1e-3) assert flux.unit == "MeV-1 cm-2 s-1 sr-1"
def extrapolate_iso(infile, outfile, logEc_extra): tmp = np.loadtxt(make_path(infile), delimiter=" ") Ecbd = tmp[:, 0] qiso = tmp[:, 1] finterp = interp1d(np.log10(Ecbd), np.log10(qiso), kind="linear", fill_value="extrapolate") qiso_extra = 10**finterp(logEc_extra) np.savetxt(outfile, np.c_[10**logEc_extra, qiso_extra], delimiter=" ") return create_fermi_isotropic_diffuse_model( filename=outfile, norm=0.92, interp_kwargs={"fill_value": None}) # norm=0.92 see paper appendix A
# In[ ]: # TODO: show how one can fix the extrapolate to high energy # by computing and padding an extra plane e.g. at 1e3 TeV # that corresponds to a linear extrapolation # ## Isotropic diffuse background # # To load the isotropic diffuse model with Gammapy, use the `~gammapy.modeling.models.TemplateSpectralModel`. We are using `'fill_value': 'extrapolate'` to extrapolate the model above 500 GeV: # In[ ]: filename = "$GAMMAPY_DATA/fermi_3fhl/iso_P8R2_SOURCE_V6_v06.txt" diffuse_iso = create_fermi_isotropic_diffuse_model( filename=filename, interp_kwargs={"fill_value": None}) # We can plot the model in the energy range between 50 GeV and 2000 GeV: # In[ ]: erange = [50, 2000] * u.GeV diffuse_iso.spectral_model.plot(erange, flux_unit="1 / (cm2 MeV s)") # ## PSF # # Next we will tke a look at the PSF. It was computed using ``gtpsf``, in this case for the Galactic center position. Note that generally for Fermi-LAT, the PSF only varies little within a given regions of the sky, especially at high energies like what we have here. We use the `~gammapy.irf.EnergyDependentTablePSF` class to load the PSF and use some of it's methods to get some information about it. # In[ ]: psf = EnergyDependentTablePSF.read(
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] = []