def test_background_2d_read_gadf(): filename = "$GAMMAPY_DATA/tests/irf/bkg_2d_full_example.fits" bkg = Background2D.read(filename) data = bkg.quantity assert data.shape == (20, 5) assert bkg.axes.names == ["energy", "offset"] assert data.unit == "s-1 MeV-1 sr-1"
def test_background_2d_read_write(tmp_path, bkg_2d): bkg_2d.to_fits().writeto(tmp_path / "tmp.fits") bkg_2d_2 = Background2D.read(tmp_path / "tmp.fits") axis = bkg_2d_2.data.axis("energy") assert axis.nbin == 2 assert axis.unit == "TeV" axis = bkg_2d_2.data.axis("offset") assert axis.nbin == 3 assert axis.unit == "deg" data = bkg_2d_2.data.data assert data.shape == (2, 3) assert data.unit == "s-1 MeV-1 sr-1"
def test_background_2d_gammapy(bg_hdu): '''Test our background hdu is readable by gammapy''' from gammapy.irf import Background2D background, hdu = bg_hdu with tempfile.NamedTemporaryFile(suffix='.fits') as f: fits.HDUList([fits.PrimaryHDU(), hdu]).writeto(f.name) # test reading with gammapy works bg2d = Background2D.read(f.name, 'BACKGROUND') assert u.allclose(background, bg2d.data.data, atol=1e-16 * u.Unit('TeV-1 s-1 sr-1'))
def load(self): """Load HDU as appropriate class. TODO: this should probably go via an extensible registry. """ hdu_class = self.hdu_class filename = self.path() hdu = self.hdu_name if hdu_class == "events": from gammapy.data import EventList return EventList.read(filename, hdu=hdu) elif hdu_class == "gti": from gammapy.data import GTI return GTI.read(filename, hdu=hdu) elif hdu_class == "aeff_2d": from gammapy.irf import EffectiveAreaTable2D return EffectiveAreaTable2D.read(filename, hdu=hdu) elif hdu_class == "edisp_2d": from gammapy.irf import EnergyDispersion2D return EnergyDispersion2D.read(filename, hdu=hdu) elif hdu_class == "psf_table": from gammapy.irf import PSF3D return PSF3D.read(filename, hdu=hdu) elif hdu_class == "psf_3gauss": from gammapy.irf import EnergyDependentMultiGaussPSF return EnergyDependentMultiGaussPSF.read(filename, hdu=hdu) elif hdu_class == "psf_king": from gammapy.irf import PSFKing return PSFKing.read(filename, hdu=hdu) elif hdu_class == "bkg_2d": from gammapy.irf import Background2D return Background2D.read(filename, hdu=hdu) elif hdu_class == "bkg_3d": from gammapy.irf import Background3D return Background3D.read(filename, hdu=hdu) else: raise ValueError(f"Invalid hdu_class: {hdu_class}")
def test_background_2d_read_write(tmpdir, bkg_2d): filename = str(tmpdir / "bkg2d.fits") bkg_2d.to_fits().writeto(filename) bkg_2d_2 = Background2D.read(filename) axis = bkg_2d_2.data.axis("energy") assert axis.nbin == 2 assert axis.unit == "TeV" axis = bkg_2d_2.data.axis("offset") assert axis.nbin == 3 assert axis.unit == "deg" data = bkg_2d_2.data.data assert data.shape == (2, 3) assert data.unit == "s-1 MeV-1 sr-1"