def test_abund_change_file(): """Can we change the abundance setting: file This test hard-codes the number of elements expected in the file. """ from sherpa.astro import xspec elems = {n: i * 0.1 for i, n in enumerate(ELEMENT_NAMES)} tfh = NamedTemporaryFile(mode='w', suffix='.xspec') for n in ELEMENT_NAMES: tfh.write("{}\n".format(elems[n])) tfh.flush() oval = xspec.get_xsabund() try: xspec.set_xsabund(tfh.name) abund = xspec.get_xsabund() out = {n: xspec.get_xsabund(n) for n in ELEMENT_NAMES} finally: xspec.set_xsabund(oval) assert abund == 'file' for n in ELEMENT_NAMES: assert out[n] == pytest.approx(elems[n])
def setUp(self): self._old_logger_level = logger.getEffectiveLevel() logger.setLevel(logging.ERROR) from sherpa.astro.io import read_pha from sherpa.astro import xspec # Ensure we have a known set of XSPEC settings. # At present this is just the abundance and cross-section, # since the cosmology settings do not affect any of the # models used here. # self._xspec_settings = { 'abund': xspec.get_xsabund(), 'xsect': xspec.get_xsxsect() } xspec.set_xsabund('angr') xspec.set_xsxsect('bcmc') pha_fname = self.make_path("9774.pi") self.data = read_pha(pha_fname) self.data.notice(0.5, 7.0) bkg_fname = self.make_path("9774_bg.pi") self.bkg = read_pha(bkg_fname) abs1 = xspec.XSphabs('abs1') p1 = PowLaw1D('p1') self.model = abs1 + p1 self.model_mult = abs1 * p1 pi2278 = self.make_path("pi2278.fits") pi2286 = self.make_path("pi2286.fits") self.data_pi2278 = read_pha(pi2278) self.data_pi2286 = read_pha(pi2286)
def test_abund_element(): """Can we access the elemental settings? """ from sherpa.astro import xspec oval = xspec.get_xsabund() try: xspec.set_xsabund('wilm') h = xspec.get_xsabund('H') he = xspec.get_xsabund('He') si = xspec.get_xsabund('Si') ar = xspec.get_xsabund('Ar') k = xspec.get_xsabund('K') fe = xspec.get_xsabund('Fe') finally: xspec.set_xsabund(oval) # These values were found from HEASOFT version 6.19 # spectral/manager/abundances.dat # The values are given to two decimal places in this file. # It is not worth testing all settings, since we are not # testing the XSPEC implementation itself, just our use of it. # assert h == pytest.approx(1.0) assert he == pytest.approx(9.77e-2) assert si == pytest.approx(1.86e-05) assert ar == pytest.approx(2.57e-06) assert k == pytest.approx(0.0) assert fe == pytest.approx(2.69e-05)
def test_abund_default(): """Check the expected default setting for the abundance. Ideally this test would be run before any other tests of XSPEC are made (i.e. any XSPEC code is called). """ from sherpa.astro import xspec oval = xspec.get_xsabund() assert oval in DEFAULT_ABUND
def tearDown(self): from sherpa.astro import xspec self._xspec_settings = { 'abund': xspec.get_xsabund(), 'xsect': xspec.get_xsxsect() } xspec.set_xsabund(self._xspec_settings['abund']) xspec.set_xsxsect(self._xspec_settings['xsect']) if hasattr(self, "_old_logger_level"): logger.setLevel(self._old_logger_level)
def reset_xspec(): from sherpa.astro import xspec # Ensure we have a known set of XSPEC settings. # At present this is just the abundance and cross-section, # since the cosmology settings do not affect any of the # models used here. # abund = xspec.get_xsabund() xsect = xspec.get_xsxsect() xspec.set_xsabund('angr') xspec.set_xsxsect('bcmc') yield xspec.set_xsabund(abund) xspec.set_xsxsect(xsect)
def fix_xspec(clean_astro_ui, hide_logging): """As of XSPEC 12.11.1 it's useful to fix abundance/cross sections rather than rely on reading from the user's directory This requires XSPEC support. """ # As of XSPEC 12.10.1, it is safest to explicitly set # the state to a known value. For now just pick the # "easy" settings. # abund = xspec.get_xsabund() xsect = xspec.get_xsxsect() xspec.set_xsabund('angr') xspec.set_xsxsect('bcmc') yield xspec.set_xsabund(abund) xspec.set_xsxsect(xsect)