def read_pha(filename): """Read PHA file Parameters ---------- filename : str or `Path` PHA file name Returns ------- data : dict Dict with counts, acceptance and mask_safe """ data = {} with fits.open(filename, memmap=False) as hdulist: data["counts"] = RegionNDMap.from_hdulist(hdulist, format="ogip") data["acceptance"] = RegionNDMap.from_hdulist( hdulist, format="ogip", ogip_column="BACKSCAL") if "GTI" in hdulist: data["gti"] = GTI(Table.read(hdulist["GTI"])) data["mask_safe"] = RegionNDMap.from_hdulist(hdulist, format="ogip", ogip_column="QUALITY") return data
def read_bkg(filename): """Read PHA background file Parameters ---------- filename : str or `Path` PHA file name Returns ------- data : dict Dict with counts_off and acceptance_off """ with fits.open(filename, memmap=False) as hdulist: counts_off = RegionNDMap.from_hdulist(hdulist, format="ogip") acceptance_off = RegionNDMap.from_hdulist(hdulist, ogip_column="BACKSCAL", format="ogip") return {"counts_off": counts_off, "acceptance_off": acceptance_off}
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(str(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(str(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 = RegionNDMap.read(dirname / arffile, format="ogip-arf") 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, )