BaseSpectrum

class synphot.spectrum.BaseSpectrum(modelclass, clean_meta=False, **kwargs)[source]

Bases: object

Base class to handle spectrum or bandpass.

Note

Until astropy.modeling can handle units, all parameters are converted to pre-defined internal units.

Parameters:
modelclasscls

Model class from astropy.modeling.

clean_metabool

Scrub “expr” and “header” entries from input metadata before merging. Set this to True when those entries no longer make sense in self. This is automatically set to True regardless for spectrum arithmetic.

kwargsdict

Model parameters accepted by modelclass. Each parameter can be either a Quantity or number. If the latter, assume pre-defined internal units.

Attributes:
metadict

Metadata associated with the spectrum or bandpass model (warnings, legacy SYNPHOT expression, FITS header, etc).

Raises:
synphot.exceptions.SynphotError

Invalid model.

Attributes Summary

model

Model of the spectrum/bandpass.

warnings

Dictionary of warning key-value pairs related to spectrum/bandpass.

waverange

Range of waveset.

waveset

Optimal wavelengths for sampling the spectrum or bandpass.

Methods Summary

__call__(wavelengths)

Sample the spectrum or bandpass.

avgwave([wavelengths])

Calculate the average wavelength.

barlam([wavelengths])

Calculate mean log wavelength.

force_extrapolation()

Force the underlying model to extrapolate.

from_spectrum1d(spec[, keep_neg])

Create a spectrum from specutils.Spectrum1D object.

integrate([wavelengths, integration_type])

Perform integration.

pivot([wavelengths])

Calculate pivot wavelength.

plot([wavelengths])

Plot the spectrum.

taper([wavelengths])

Taper the spectrum or bandpass.

to_spectrum1d([wavelengths])

Create a specutils.Spectrum1D object from spectrum.

Attributes Documentation

model

Model of the spectrum/bandpass.

warnings

Dictionary of warning key-value pairs related to spectrum/bandpass.

waverange

Range of waveset.

waveset

Optimal wavelengths for sampling the spectrum or bandpass.

Methods Documentation

__call__(wavelengths)[source]

Sample the spectrum or bandpass.

Parameters:
wavelengthsarray-like or Quantity

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom.

Returns:
sampled_resultQuantity

Sampled flux or throughput in pre-defined internal unit. Might have negative values.

avgwave(wavelengths=None)[source]

Calculate the average wavelength.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

Returns:
avg_waveQuantity

Average wavelength.

barlam(wavelengths=None)[source]

Calculate mean log wavelength.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

Returns:
bar_lamQuantity

Mean log wavelength.

force_extrapolation()[source]

Force the underlying model to extrapolate.

An example where this is useful: You create a source spectrum with non-default extrapolation behavior and you wish to force the underlying empirical model to extrapolate based on nearest point.

Note

This is only applicable to Empirical1D model and should still work even if the source spectrum has been redshifted.

Returns:
is_forcedbool

True if the model is successfully forced to be extrapolated, else False.

classmethod from_spectrum1d(spec, keep_neg=False)[source]

Create a spectrum from specutils.Spectrum1D object.

Parameters:
specspecutils.Spectrum1D
keep_negbool

See Empirical1D.

Returns:
spBaseSourceSpectrum

Empirical spectrum.

integrate(wavelengths=None, integration_type=None, **kwargs)[source]

Perform integration.

When integration is not analytical and wavelengths are provided, flux or throughput is first resampled. This is useful when user wants to integrate at specific end points or use custom spacing; In that case, user can pass in desired sampling array generated with numpy.linspace(), numpy.logspace(), etc. If not provided, then waveset is used.

When integration is analytical, wavelengths provided are only used if applicable for the particular model. If the model does not support analytical integration, it will fall back to simple trapezoid integration.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for integration. This is ignored by analytical integration if not applicable. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

integration_type{None, ‘trapezoid’, ‘analytical’}

Defines how the integration is done, either by simple trapezoid integration or analytical formula. If None, the option is pulled from synphot.config.conf.default_integrator. If analytical integration is requested but no possible, trapezoid integration is done anyway.

flux_unitstr, Unit, or None

This option is only available for source spectrum. For trapezoid integration, flux is converted to this unit for sampling before integration. For analytical integration, while calculation is done differently, providing this option would result in equivalent behavior as trapezoid integration, where possible, for consistency. If not given, internal unit is used.

kwargsdict

This option is only available for source spectrum. Other optional keywords besides flux_unit to __call__ for sampling when integration type is not analytical.

Returns:
resultQuantity

Integrated result.

Raises:
NotImplementedError

Invalid integration type.

synphot.exceptions.SynphotError

waveset is needed but undefined or cannot integrate natively in the given flux_unit.

pivot(wavelengths=None)[source]

Calculate pivot wavelength.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

Returns:
pivwvQuantity

Pivot wavelength.

plot(wavelengths=None, **kwargs)[source]

Plot the spectrum.

Note

Uses matplotlib.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

titlestr

Plot title.

xlog, ylogbool

Plot X and Y axes, respectively, in log scale. Default is linear scale.

left, rightNone or number

Minimum and maximum wavelengths to plot. If None, uses the whole range. If a number is given, must be in Angstrom.

bottom, topNone or number

Minimum and maximum flux/throughput to plot. If None, uses the whole range. If a number is given, must be in internal unit.

save_asstr

Save the plot to an image file. The file type is automatically determined by given file extension.

Raises:
synphot.exceptions.SynphotError

Invalid inputs.

taper(wavelengths=None)[source]

Taper the spectrum or bandpass.

The wavelengths to use for the first and last points are calculated by using the same ratio as for the 2 interior points.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for tapering. If not a Quantity, assumed to be in Angstrom. If None, waveset is used.

Returns:
spBaseSpectrum

Tapered empirical spectrum or bandpass. self is returned if already tapered (e.g., box model).

to_spectrum1d(wavelengths=None, **kwargs)[source]

Create a specutils.Spectrum1D object from spectrum.

Parameters:
wavelengthsarray-like, Quantity, or None

Wavelength values for sampling. If not a Quantity, assumed to be in Angstrom. If None, self.waveset is used.

flux_unitstr, Unit, or None

This option is not applicable to unitless spectrum like bandpass. Flux is converted to this unit before written out. If not given, internal unit is used. Count and magnitudes are not supported.

Returns:
specspecutils.Spectrum1D
Raises:
ImportError

specutils is not installed.