def from_ogip_files(cls, filename): """Read `~gammapy.spectrum.SpectrumDatasetOnOff` from OGIP files. BKG file, ARF, and RMF must be set in the PHA header and be present in the same folder. The naming scheme is fixed to the following scheme: * PHA file is named ``pha_obs{name}.fits`` * BKG file is named ``bkg_obs{name}.fits`` * ARF file is named ``arf_obs{name}.fits`` * RMF file is named ``rmf_obs{name}.fits`` with ``{name}`` the dataset name. Parameters ---------- filename : str OGIP PHA file to read """ filename = make_path(filename) dirname = filename.parent with fits.open(filename, memmap=False) as hdulist: data = _read_ogip_hdulist(hdulist) counts = CountsSpectrum(energy_hi=data["energy_hi"], energy_lo=data["energy_lo"], data=data["data"]) phafile = filename.name try: rmffile = phafile.replace("pha", "rmf") energy_dispersion = EDispKernel.read(dirname / rmffile) except OSError: # TODO : Add logger and echo warning energy_dispersion = None try: bkgfile = phafile.replace("pha", "bkg") with fits.open(dirname / bkgfile, memmap=False) as hdulist: data_bkg = _read_ogip_hdulist(hdulist) counts_off = CountsSpectrum( energy_hi=data_bkg["energy_hi"], energy_lo=data_bkg["energy_lo"], data=data_bkg["data"], ) acceptance_off = data_bkg["backscal"] except OSError: # TODO : Add logger and echo warning counts_off, acceptance_off = None, None arffile = phafile.replace("pha", "arf") aeff = EffectiveAreaTable.read(dirname / arffile) mask_safe = np.logical_not(data["quality"]) return cls( counts=counts, aeff=aeff, counts_off=counts_off, edisp=energy_dispersion, livetime=data["livetime"], mask_safe=mask_safe, acceptance=data["backscal"], acceptance_off=acceptance_off, name=str(data["obs_id"]), gti=data["gti"], )
def test_get_bias_energy(): """Obs read from file""" rmffile = "$GAMMAPY_DATA/joint-crab/spectra/hess/rmf_obs23523.fits" edisp = EDispKernel.read(rmffile) thresh_lo = edisp.get_bias_energy(0.1) assert_allclose(thresh_lo.to("TeV").value, 0.9174, rtol=1e-4)
def from_ogip_files(cls, filename): """Read `~gammapy.spectrum.SpectrumDatasetOnOff` from OGIP files. BKG file, ARF, and RMF must be set in the PHA header and be present in the same folder. The naming scheme is fixed to the following scheme: * PHA file is named ``pha_obs{name}.fits`` * BKG file is named ``bkg_obs{name}.fits`` * ARF file is named ``arf_obs{name}.fits`` * RMF file is named ``rmf_obs{name}.fits`` with ``{name}`` the dataset name. Parameters ---------- filename : str OGIP PHA file to read """ filename = make_path(filename) dirname = filename.parent with fits.open(filename, memmap=False) as hdulist: counts = RegionNDMap.from_hdulist(hdulist, format="ogip") acceptance = RegionNDMap.from_hdulist(hdulist, format="ogip", ogip_column="BACKSCAL") if "GTI" in hdulist: gti = GTI(Table.read(hdulist["GTI"])) else: gti = None mask_safe = RegionNDMap.from_hdulist(hdulist, format="ogip", ogip_column="QUALITY") mask_safe.data = np.logical_not(mask_safe.data) phafile = filename.name try: rmffile = phafile.replace("pha", "rmf") kernel = EDispKernel.read(dirname / rmffile) edisp = EDispKernelMap.from_edisp_kernel(kernel, geom=counts.geom) except OSError: # TODO : Add logger and echo warning edisp = None try: bkgfile = phafile.replace("pha", "bkg") with fits.open(dirname / bkgfile, memmap=False) as hdulist: counts_off = RegionNDMap.from_hdulist(hdulist, format="ogip") acceptance_off = RegionNDMap.from_hdulist( hdulist, ogip_column="BACKSCAL") except OSError: # TODO : Add logger and echo warning counts_off, acceptance_off = None, None arffile = phafile.replace("pha", "arf") aeff = EffectiveAreaTable.read(dirname / arffile) return cls( counts=counts, aeff=aeff, counts_off=counts_off, edisp=edisp, livetime=counts.meta["EXPOSURE"] * u.s, mask_safe=mask_safe, acceptance=acceptance, acceptance_off=acceptance_off, name=str(counts.meta["OBS_ID"]), gti=gti, )