— jupyter: jupytext: formats: ipynb,md text_representation: extension: .md format_name: markdown format_version: ‘1.2’ jupytext_version: 1.7.1 kernelspec: display_name: Python 3 language: python name: python3 —

Working with XSPEC models

One of the most powerful aspects of XSPEC is a huge modeling community. While in 3ML, we are focused on building a powerful and modular data analysis tool, we cannot neglect the need for many of the models thahat already exist in XSPEC and thus provide support for them via astromodels directly in 3ML.

For details on installing astromodels with XSPEC support, visit the 3ML or astromodels installation page.

Let’s explore how we can use XSPEC spectral models in 3ML.

[2]:
import matplotlib.pyplot as plt

We do not load the models by default as this takes some time and 3ML should load quickly. However, if you need the XSPEC models, they are imported from astromodels like this:

[4]:
from astromodels.xspec.factory import *
Loading xspec models...done

The models are indexed with XS_ before the typical XSPEC model names.

[5]:
plaw = XS_powerlaw()
phabs = XS_phabs()
phabs
[5]:
  • description: The phabs model from XSpec (https://heasarc.gsfc.nasa.gov/xanadu/xspec/manual/XspecModels.html)
  • formula: $n.a.$
  • parameters:
    • nh:
      • value: 1.0
      • desc: (see https://heasarc.gsfc.nasa.gov/xanadu/xspec/manual/XspecModels.html)
      • min_value: 0.0
      • max_value: 1000000.0
      • unit:
      • is_normalization: False
      • delta: 0.001
      • free: True

The spectral models behave just as any other astromodels spectral model and can be used in combination with other astromodels spectral models.

[6]:
from astromodels import Powerlaw

am_plaw = Powerlaw()

plaw_with_abs = am_plaw * phabs


fig, ax = plt.subplots()

energy_grid = np.linspace(0.1, 10.0, 1000)

ax.loglog(energy_grid, plaw_with_abs(energy_grid))
ax.set_xlabel("energy")
ax.set_ylabel("flux")
[6]:
Text(0, 0.5, 'flux')
../_images/notebooks_xspec_models_10_1.png

XSPEC Settings

Many XSPEC models depend on external abundances, cross-sections, and cosmological parameters. We provide an interface to control these directly.

Simply import the XSPEC settings like so:

[7]:
from astromodels.xspec.xspec_settings import *

Calling the functions without arguments simply returns their current settings

[8]:
xspec_abund()
[8]:
'angr'
[9]:
xspec_xsect()
[9]:
'vern'
[10]:
xspec_cosmo()
[10]:
(70.0, 0.0, 0.7300000190734863)

To change the settings for abundance and cross-section, provide strings with the normal XSPEC naming conventions.

[11]:
xspec_abund("wilm")
xspec_abund()
[11]:
'wilm'
 Solar Abundance Vector set to wilm:  Wilms, J., Allen, A. & McCray, R. ApJ 542 914 (2000) (abundances are set to zero for those elements not included in the paper).
[12]:
xspec_xsect("bcmc")
xspec_xsect()
[12]:
'bcmc'
 Cross Section Table set to bcmc:  Balucinska-Church and McCammon, 1998

To alter the cosmological parameters, one passes either the parameters that should be changed, or all three:

[13]:
xspec_cosmo(H0=68.0)
xspec_cosmo()
[13]:
(68.0, 0.0, 0.7300000190734863)
[14]:
xspec_cosmo(H0=68.0, q0=0.1, lambda_0=70.0)
xspec_cosmo()
[14]:
(68.0, 0.10000000149011612, 70.0)