Skip to content

source - Source Class

The Source class loads and manages time-resolved spectral models for astrophysical transients.

Source(
filepath: str | Path,
min_energy: u.Quantity | None = None,
max_energy: u.Quantity | None = None,
ebl: str | None = None,
times: list[u.Quantity] | None = None,
distance: Distance | None = None,
z: float | None = None,
)

Parameters:

ParameterTypeDescription
filepathstr | PathPath to spectral model file (CSV, FITS, or directory)
min_energyu.Quantity | NoneMinimum energy for analysis
max_energyu.Quantity | NoneMaximum energy for analysis
eblstr | NoneEBL model name ("franceschini", "dominguez", etc.)
timeslist[u.Quantity] | NoneSpecific times to load (optional)
distanceDistance | NoneDistance to source (overrides metadata distance)
zfloat | NoneRedshift (overrides metadata distance/redshift)

Note: Only one of distance or z can be provided, not both.

Example:

from sensipy.source import Source
from sensipy.util import get_data_path
from astropy import units as u
from astropy.coordinates import Distance
# Get path to package mock data
mock_data_path = get_data_path("mock_data/GRB_42_mock.csv")
# Basic usage
source = Source(
filepath=str(mock_data_path),
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
ebl="franceschini"
)
# With explicit distance
source_with_distance = Source(
filepath=str(mock_data_path),
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
distance=Distance(z=0.5),
ebl="franceschini"
)
# With explicit redshift
source_with_z = Source(
filepath=str(mock_data_path),
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
z=0.5,
ebl="franceschini"
)
AttributeTypeDescription
energyu.QuantityEnergy grid
timesu.QuantityTime grid
spectranp.ndarray2D array of flux values [energy, time]
filepathPathPath to source file
SpectralGridRegularGridInterpolatorInterpolation grid

Simulate observation to determine required exposure time.

observe(
sensitivity: Sensitivity,
start_time: u.Quantity,
min_energy: u.Quantity,
max_energy: u.Quantity,
target_precision: u.Quantity = 1 * u.s,
max_time: u.Quantity = 12 * u.h,
sensitivity_mode: Literal["sensitivity", "photon_flux"] = "sensitivity",
n_time_steps: int = 10,
) -> dict

Returns: Dictionary with detection results including obs_time, seen, start_time, end_time, metadata.

Example:

result = source.observe(
sensitivity=sens,
start_time=30 * u.min,
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
)
print(result['obs_time'] if result['seen'] else "Not detectable")

Get differential flux spectrum at a given time.

get_spectrum(time: u.Quantity, energy: u.Quantity | None = None) -> u.Quantity

Returns: Flux values at the specified time (and energy if provided).

spectrum = source.get_spectrum(time=100 * u.s)
flux_at_1TeV = source.get_spectrum(time=100 * u.s, energy=1 * u.TeV)

Get differential flux at a given energy.

get_flux(energy: u.Quantity, time: u.Quantity | None = None) -> u.Quantity

Returns: Flux values at the specified energy (and time if provided).

lightcurve = source.get_flux(energy=1 * u.TeV)
flux_value = source.get_flux(energy=1 * u.TeV, time=100 * u.s)

Set or update EBL absorption model.

set_ebl_model(ebl: str | None, z: float | None = None, distance: Distance | None = None) -> bool

Parameters:

  • ebl: EBL model name or None to remove
  • z: Redshift (optional, auto-detected from metadata if available)
  • distance: Distance to source as astropy Distance object (optional)

Note: Only one of distance or z can be provided, not both.

Returns: True if the distance was changed, False otherwise.

from astropy.coordinates import Distance
# Using redshift
source.set_ebl_model("franceschini", z=0.5)
# Using distance
distance = Distance(z=0.5)
source.set_ebl_model("franceschini", distance=distance)
# Remove EBL
source.set_ebl_model(None)

Create a Gammapy PowerLawSpectralModel representing the spectrum at a given time.

get_powerlaw_spectrum(
time: u.Quantity,
amplitude: u.Quantity | None = None,
reference: u.Quantity = 1 * u.TeV,
use_ebl: bool | None = None,
) -> PowerLawSpectralModel

Parameters:

ParameterTypeDescription
timeu.QuantityTime at which to evaluate the spectrum
amplitudeu.Quantity | NoneOptional amplitude at reference energy (auto-calculated if None)
referenceu.QuantityReference energy for power law (default: 1 TeV)
use_eblbool | NoneApply EBL absorption. If None, defaults to True if EBL model is set, otherwise False

Returns: Wrapped PowerLawSpectralModel with automatic energy range in plot() method.

Examples:

from sensipy.source import Source
from sensipy.util import get_data_path
import astropy.units as u
mock_data_path = get_data_path("mock_data/GRB_42_mock.csv")
source = Source(
filepath=str(mock_data_path),
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
)
time = 100 * u.s
# Explicitly disable EBL (or use_ebl=None defaults to False when no EBL model is set)
spectrum = source.get_powerlaw_spectrum(time, use_ebl=False)
# Plot automatically uses source.min_energy and source.max_energy
spectrum.plot()

Create a template spectral model from the spectrum at a given time.

get_template_spectrum(
time: u.Quantity,
scaling_factor: int | float = 1,
use_ebl: bool | None = None,
) -> ScaledTemplateModel

Parameters:

ParameterTypeDescription
timeu.QuantityTime at which to extract the spectrum
scaling_factorint | floatInitial scaling factor (default: 1)
use_eblbool | NoneApply EBL absorption. If None, defaults to True if EBL model is set, otherwise False

Returns: Wrapped ScaledTemplateModel with automatic energy range in plot() method.

Examples:

from sensipy.source import Source
from sensipy.util import get_data_path
import astropy.units as u
mock_data_path = get_data_path("mock_data/GRB_42_mock.csv")
source = Source(
filepath=str(mock_data_path),
min_energy=20 * u.GeV,
max_energy=10 * u.TeV,
)
time = 100 * u.s
# Explicitly disable EBL (or use_ebl=None defaults to False when no EBL model is set)
template = source.get_template_spectrum(time, use_ebl=False)
# Plot automatically uses source.min_energy and source.max_energy
template.plot()

Visualize the time-energy spectral evolution.

show_spectral_pattern(
resolution: int = 100,
return_plot: bool = False,
cutoff_flux: u.Quantity = 1e-20 * u.Unit("1 / (cm2 s GeV)"),
) -> plt | None
source.show_spectral_pattern(resolution=100)

Property that returns a copy of the user-defined metadata dictionary. Metadata can be accessed via attribute notation (like pandas DataFrame columns) or through the dictionary.

# Access via attribute notation (recommended)
print(source.event_id)
print(source.distance)
print(source.eiso)
# Or access via the metadata dictionary
meta = source.metadata
print(meta.get('event_id'))
print(meta.get('distance'))
print(meta.get('eiso'))
# Set custom metadata
source.my_custom_field = "value"
print(source.my_custom_field)
  • CSV: Recommended format with time, energy, flux columns
  • FITS: HDU structure with energy, time, and flux arrays
  • Text directory: Legacy format with multiple .txt files

See Spectral Models for format details.