The Grid Object

Here we show how to instantiate a Grid object and use it to explore a grid file.

The Grid object needs a file to load, these are HDF5 files that are available through the synthesizer-download command line tool (for more details see the introduction to grids. By default, once downloaded these files are stored in the GRID_DIR directory. The default location of this directory is platform dependent, but the location can be found by import it and printing it.

[2]:
from synthesizer import GRID_DIR

print(GRID_DIR)
/home/runner/.local/share/Synthesizer/grids

This directory can be overriden by setting the SYNTHESIZER_GRID_DIR environment variable.

Assuming the grid file is in the default location, all we need to do is pass the name of the grid we want to load to the Grid constructor. Note that the name of the grid can include the extension or not. If the extension is not included, it is assumed to be "hdf5".

Here we will load the test grid (a simplified BPASS 2.2.1 grid).

[3]:
from synthesizer import Grid

grid = Grid("test_grid.hdf5")

If we are loading a grid from a different location we can just pass that path to the grid_dir argument.

[5]:
grid = Grid("test_grid.hdf5", grid_dir="../../../tests/test_grid")

Printing a summary of the Grid

We can have a look at what the loaded grid contains by simply printing the grid.

[6]:
print(grid)
+--------------------------------------------------------------------------------+
|                                      GRID                                      |
+-----------------------------+--------------------------------------------------+
| Attribute                   | Value                                            |
+-----------------------------+--------------------------------------------------+
| grid_dir                    | '../../../tests/test_grid'                       |
+-----------------------------+--------------------------------------------------+
| grid_name                   | 'test_grid'                                      |
+-----------------------------+--------------------------------------------------+
| grid_ext                    | 'hdf5'                                           |
+-----------------------------+--------------------------------------------------+
| grid_filename               | '../../../tests/test_grid/test_grid.hdf5'        |
+-----------------------------+--------------------------------------------------+
| reprocessed                 | True                                             |
+-----------------------------+--------------------------------------------------+
| lines_available             | True                                             |
+-----------------------------+--------------------------------------------------+
| naxes                       | 2                                                |
+-----------------------------+--------------------------------------------------+
| date_created                | '2025-03-05'                                     |
+-----------------------------+--------------------------------------------------+
| synthesizer_grids_version   | '0.1.dev613+ga9a6aa9'                            |
+-----------------------------+--------------------------------------------------+
| synthesizer_version         | '0.8.5b1.dev124+ge16216b5'                       |
+-----------------------------+--------------------------------------------------+
| has_lines                   | True                                             |
+-----------------------------+--------------------------------------------------+
| has_spectra                 | True                                             |
+-----------------------------+--------------------------------------------------+
| ndim                        | 3                                                |
+-----------------------------+--------------------------------------------------+
| new_line_format             | True                                             |
+-----------------------------+--------------------------------------------------+
| nlam                        | 9244                                             |
+-----------------------------+--------------------------------------------------+
| nlines                      | 215                                              |
+-----------------------------+--------------------------------------------------+
| shape                       | (51, 13, 9244)                                   |
+-----------------------------+--------------------------------------------------+
| available_spectra           | [incident, linecont, nebular, transmitted,       |
|                             |  total, nebular_continuum]                       |
+-----------------------------+--------------------------------------------------+
| axes                        | [ages, metallicities]                            |
+-----------------------------+--------------------------------------------------+
| available_lines (215,)      | [He 2 1025.27A, O 6 1031.91A, O 6 1037.61A, ...] |
+-----------------------------+--------------------------------------------------+
| line_lams (215,)            | 1.03e+03 Å -> 2.48e+04 Å (Mean: 5.98e+03 Å)      |
+-----------------------------+--------------------------------------------------+
| incident_axes               | ['ages' 'metallicities']                         |
+-----------------------------+--------------------------------------------------+
| spec_names (4,)             | [incident, transmitted, nebular, ...]            |
+-----------------------------+--------------------------------------------------+
| lam (9244,)                 | 1.30e-04 Å -> 2.99e+11 Å (Mean: 9.73e+09 Å)      |
+-----------------------------+--------------------------------------------------+
| line_ids (215,)             | [He 2 1025.27A, O 6 1031.91A, O 6 1037.61A, ...] |
+-----------------------------+--------------------------------------------------+
| spectra                     | incident: ndarray                                |
|                             | linecont: ndarray                                |
|                             | nebular: ndarray                                 |
|                             | transmitted: ndarray                             |
|                             | total: ndarray                                   |
|                             | nebular_continuum: ndarray                       |
+-----------------------------+--------------------------------------------------+
| line_lums                   | nebular: unyt_array                              |
|                             | linecont: unyt_array                             |
|                             | nebular_continuum: unyt_array                    |
|                             | transmitted: unyt_array                          |
|                             | incident: unyt_array                             |
|                             | total: unyt_array                                |
+-----------------------------+--------------------------------------------------+
| line_conts                  | nebular: unyt_array                              |
|                             | linecont: unyt_array                             |
|                             | nebular_continuum: unyt_array                    |
|                             | transmitted: unyt_array                          |
|                             | incident: unyt_array                             |
|                             | total: unyt_array                                |
+-----------------------------+--------------------------------------------------+
| _axes_values                | ages: ndarray                                    |
|                             | metallicities: ndarray                           |
+-----------------------------+--------------------------------------------------+
| log10_specific_ionising_lum | HI: ndarray                                      |
|                             | HeII: ndarray                                    |
+-----------------------------+--------------------------------------------------+
| axes_values                 | ages: ndarray                                    |
|                             | metallicities: ndarray                           |
+-----------------------------+--------------------------------------------------+

In this instance, its a stellar grid with the incident spectrum defined by the axes_values, ages and metallicities. The grid also contains some useful quantites like the photon rate (log10_specific_ionising_luminosity) available for fully ionising hydrogen and helium.

Since this grid is a cloudy processed grid, there are additional spectra or line data that are available to extract or manipulate. These include (but not limited to)

  • spectra

    • nebular: is the nebular continuum (including line emission) predicted by the photoionisation model

    • linecont: this is the line contribution to the spectrum

    • transmitted: this is the incident spectra that is transmitted through the gas in the photoionisation modelling; it has zero flux at shorter wavelength of the lyman-limit

    • wavelength: the wavelength covered

  • lines

    • id: line id, this is the same as used in cloudy (see Linelist generation)

    • luminosity: the luminosity of the line

    • nebular_continuum: the underlying nebular continuum at the line

    • transmitted: this is the transmitted luminosity at the line

    • wavelength: the wavelength of the line

A similar structure is also followed for AGN grids, where the axes could either be described by mass (black hole mass), acretion_rate_eddington (the accretion rate normalised to the eddington limit for the mass), cosine_inclination (cosine value describing the inclination of the AGN), or the temperature (blackbody temperature of the big bump component), alpha-ox (X-ray to UV ratio) , alpha-uv (low-energy slope of the big bump component), alpha-x (slope of the X-ray component).

Limiting the Grid

A Grid can also take various arguments to limit the size of the grid, e.g. by isolating the Grid to a wavelength region of interest. This is particularly useful when making a large number of spectra from a high resolution Grid, where the memory footprint can become large.

Passing a wavelength array

If you only care about a grid of specific wavelength values, you can pass this array and the Grid will automatically be interpolated onto the new wavelength array using SpectRes.

[7]:
# Define a new set of wavelengths
new_lams = np.logspace(2, 5, 1000) * angstrom

# Create a new grid
grid = Grid("test_grid", new_lam=new_lams)
print(grid.shape)
(51, 13, 1000)

Passing wavelength limits

If you don’t want to modify the underlying grid resolution, but only care about a specific wavelength range, you can pass limits to truncate the grid at.

Note however, this may result in line quantities outside that range no longer being available.

[8]:
# Create a new grid
grid = Grid("test_grid", lam_lims=(10**3 * angstrom, 10**4 * angstrom))
print(grid.shape)
(51, 13, 691)
/home/runner/work/synthesizer/synthesizer/src/synthesizer/grid.py:190: RuntimeWarning: The following lines are outside the wavelength range of the grid: [np.str_('H 1 1.00494m'), np.str_('S 2 1.02867m'), np.str_('S 2 1.03205m'), np.str_('S 2 1.03364m'), np.str_('He 1 1.08291m'), np.str_('He 1 1.08303m'), np.str_('H 1 1.09381m'), np.str_('O 1 1.12863m'), np.str_('O 1 1.12864m'), np.str_('O 1 1.12869m'), np.str_('O 1 1.12870m'), np.str_('O 1 1.12873m'), np.str_('Ni 2 1.19102m'), np.str_('Fe 2 1.25668m'), np.str_('Fe 2 1.27877m'), np.str_('H 1 1.28181m'), np.str_('Fe 2 1.29427m'), np.str_('Fe 2 1.32055m'), np.str_('Fe 2 1.32777m'), np.str_('Fe 2 1.37181m'), np.str_('Fe 2 1.53348m'), np.str_('Fe 2 1.59948m'), np.str_('Fe 2 1.64355m'), np.str_('Fe 2 1.66377m'), np.str_('Fe 2 1.67688m'), np.str_('Fe 2 1.71113m'), np.str_('Fe 2 1.74494m'), np.str_('Fe 2 1.79711m'), np.str_('Fe 2 1.80002m'), np.str_('Fe 2 1.80940m'), np.str_('H 1 1.87510m'), np.str_('Fe 2 1.89541m'), np.str_('Ni 2 1.93877m'), np.str_('Fe 2 1.95361m'), np.str_('Si 6 1.96247m'), np.str_('H 1 2.16553m'), np.str_('Si 7 2.48071m')]
  self._get_lines_grid()

Ignoring spectra or lines

It is also possible to ignore spectra or lines. This can be useful if, for example, you have a large multi-dimensional grid and only want to consider lines since these are much smaller in memory.

[9]:
# Create a new grid without spectra
grid = Grid("test_grid", ignore_spectra=True)

print(grid.available_spectra)

# Create a new grid without lines
grid = Grid("test_grid", ignore_lines=True)
[]

Plot a single grid point

We can plot the spectra at the location of a single point in our grid. First, we choose some age and metallicity.

[10]:
# Return to the unmodified grid
grid = Grid("test_grid")

log10age = 6.0  # log10(age/yr)
Z = 0.01  # metallicity

We then get the index location of that grid point for this age and metallicity

[11]:
grid_point = grid.get_grid_point(log10ages=log10age, metallicity=Z)

We can then loop over the available spectra (contained in grid.spec_names) and plot

[12]:
for spectra_type in grid.available_spectra:
    # Get `Sed` object
    sed = grid.get_sed_at_grid_point(grid_point, spectra_type=spectra_type)

    # Mask zero valued elements
    mask = sed.lnu > 0
    plt.plot(
        np.log10(sed.lam[mask]),
        np.log10(sed.lnu[mask]),
        lw=1,
        alpha=0.8,
        label=spectra_type,
    )

plt.legend(fontsize=8, labelspacing=0.0)
plt.xlim(2.3, 8)
plt.ylim(19, 25)
plt.xlabel(r"$\rm log_{10}(\lambda/\AA)$")
plt.ylabel(r"$\rm log_{10}(L_{\nu}/erg\ s^{-1}\ Hz^{-1} M_{\odot}^{-1})$")
[12]:
Text(0, 0.5, '$\\rm log_{10}(L_{\\nu}/erg\\ s^{-1}\\ Hz^{-1} M_{\\odot}^{-1})$')
../_images/emission_grids_grids_example_23_1.png

Plot ionising luminosities

We can also plot properties over the entire age and metallicity grid, such as the ionising luminosity.

In the examples below we plot ionising luminosities for HI and HeII

[13]:
fig, ax = grid.plot_specific_ionising_lum(ion="HI")
../_images/emission_grids_grids_example_25_0.png
[14]:
fig, ax = grid.plot_specific_ionising_lum(ion="HeII")
../_images/emission_grids_grids_example_26_0.png