Spectrum
Here we will go over the different spectral types and how we use them.
Types
Spectra are defined as possible subtypes of AbstractSpectrum. You can use these directly for construction, or use the catch-all spectrum function, which is preferred.
Spectra.AbstractSpectrum — Type
AbstractSpectrum{S <: Number, F <: Number}An abstract holder for astronomical spectra. All types inheriting from this must have the following fields:
spectral_axis::Array{S, M}flux_axis::Array{F, N}meta::Dict{Symbol, Any}
See SingleSpectrum, EchelleSpectrum, and IFUSpectrum for different subtypes.
Spectra.Spectrum — Type
Spectrum <: AbstractSpectrumA spectrum or spectra stored as arrays of real numbers. For UV/VIS/IR spectra, the spectral_axis is assumed to be wavelengths (in angstrom). For X-ray spectra, the spectral_axis is assumed to be energies (in keV).
Spectra.SingleSpectrum — Type
SingleSpectrum <: AbstractSpectrumAn instance of Spectrum where the spectral and flux axes are both 1D arrays, i.e., $M = N = 1$.
Both spectral and flux axis vectors must have the same length, $m = n$, respectively.
Examples
julia> spec = Spectrum([6, 7, 8, 9], [2, 3, 4, 5], Dict())
SingleSpectrum(Int64, Int64)
spectral axis (4,): 6 .. 9
flux axis (4,): 2 .. 5
meta: Dict{Symbol, Any}()See EchelleSpectrum and IFUSpectrum for working with instances of higher dimensional spectra.
Spectra.EchelleSpectrum — Type
EchelleSpectrum <: AbstractSpectrumAn instance of Spectrum where the spectral and flux axes are both 2D arrays, i.e., $M = N = 2$.
The spectral and flux matrices are both $m$ rows in spectral axis by $n$ columns in echelle order.
Examples
julia> wave = reshape(1:40, 10, 4)
10×4 reshape(::UnitRange{Int64}, 10, 4) with eltype Int64:
1 11 21 31
2 12 22 32
3 13 23 33
4 14 24 34
5 15 25 35
6 16 26 36
7 17 27 37
8 18 28 38
9 19 29 39
10 20 30 40
julia> flux = repeat(1:4, 1, 10)'
10×4 adjoint(::Matrix{Int64}) with eltype Int64:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
julia> spec = Spectrum(wave, flux, Dict())
EchelleSpectrum(Int64, Int64)
# orders: 4
spectral axis (10, 4): 1 .. 40
flux axis (10, 4): 1 .. 4
meta: Dict{Symbol, Any}()
julia> spec[1] # Indexing returns a `SingleSpectrum`
SingleSpectrum(Int64, Int64)
spectral axis (10,): 1 .. 10
flux axis (10,): 1 .. 1
meta: Dict{Symbol, Any}(:Order => 1)See SingleSpectrum for a 1D variant, and IFUSpectrum for a 3D variant.
Spectra.IFUSpectrum — Type
IFUSpectrum <: AbstractSpectrumAn instance of Spectrum where the spectral axis is a 1D array and flux axis is a 3D array, i.e., $M = 1$ and $N = 3$.
The spectral vector is of length $m$ and flux 3D array has shape $(m \times n \times k)$ where each entry of the flux along the spectral axis is an $n \times k$ matrix.
Examples
julia> using Random
julia> rng = Random.seed!(0)
TaskLocalRNG()
julia> wave, flux = [20, 40, 120, 160, 200], rand(rng, 5, 10, 6);
julia> spec = Spectrum(wave, flux, Dict())
IFUSpectrum(Int64, Float64)
spectral axis (5,): 20 .. 200
flux axis (5, 10, 6): 0.4552384158732863 .. 0.11698905483599475
meta: Dict{Symbol, Any}()
julia> spec[begin] # IFU image at first wavelength
10×6 Matrix{Float64}:
0.455238 0.828104 0.735106 0.042069 0.554894 0.0715802
0.746943 0.149248 0.864755 0.116243 0.519913 0.310438
0.0997382 0.523732 0.315933 0.935547 0.274589 0.250664
0.470257 0.654557 0.351769 0.812597 0.158201 0.617466
0.678779 0.312182 0.0568161 0.622296 0.61899 0.191777
0.385452 0.345902 0.448835 0.041962 0.458694 0.791756
0.908402 0.609104 0.108874 0.430905 0.91365 0.430885
0.0256413 0.0831649 0.179467 0.799997 0.982336 0.721449
0.408092 0.361884 0.849442 0.527004 0.341892 0.499461
0.239929 0.3754 0.247219 0.92438 0.733984 0.432918
julia> spec[begin:3] # IFU spectrum at first three wavelengths
IFUSpectrum(Int64, Float64)
spectral axis (3,): 20 .. 120
flux axis (3, 10, 6): 0.4552384158732863 .. 0.35149138733595564
meta: Dict{Symbol, Any}()
julia> spec[:, begin, begin] # 1D spectrum at spaxel (1, 1)
SingleSpectrum(Int64, Float64)
spectral axis (5,): 20 .. 200
flux axis (5,): 0.4552384158732863 .. 0.02964765308691042
meta: Dict{Symbol, Any}()See SingleSpectrum for a 1D variant, and EchelleSpectrum for a 2D variant.
Constructors
Spectra.spectrum — Function
spectrum(spectral_axis, flux_axis, [meta])Construct a spectrum given the spectral axis and flux axis. This will automatically dispatch the correct spectrum type given the shape and element type of the given flux. Any keyword arguments will be accessible from the spectrum as properties.
Examples
julia> wave = range(1e4, 4e4, length=1000);
julia> flux = 100 .* ones(size(wave));
julia> spec = spectrum(wave, flux)
SingleSpectrum(Float64, Float64)
spectral axis (1000,): 10000.0 .. 40000.0
flux axis (1000,): 100.0 .. 100.0
meta: Dict{Symbol, Any}()
julia> spec = spectrum(wave, flux, name="Just Noise")
SingleSpectrum(Float64, Float64)
spectral axis (1000,): 10000.0 .. 40000.0
flux axis (1000,): 100.0 .. 100.0
meta: Dict{Symbol, Any}(:name => "Just Noise")
julia> spec.name
"Just Noise"There is easy integration with Unitful.jl and its sub-projects and Measurements.jl
julia> using Random
julia> rng = Random.seed!(0)
TaskLocalRNG()
julia> using Unitful, UnitfulAstro, Measurements
julia> wave = range(1, 4, length=1000)u"μm";
julia> sigma = randn(rng, size(wave));
julia> flux = (100 .± sigma)u"erg/cm^2/s/angstrom";
julia> spec = spectrum(wave, flux)
SingleSpectrum(Quantity{Float64, 𝐋, Unitful.FreeUnits{(μm,), 𝐋, nothing}}, Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(Å^-1, erg, cm^-2, s^-1), 𝐌 𝐋^-1 𝐓^-3, nothing}})
spectral axis (1000,): 1.0 μm .. 4.0 μm
flux axis (1000,): 100.0 ± -0.23 erg Å^-1 cm^-2 s^-1 .. 100.0 ± 0.25 erg Å^-1 cm^-2 s^-1
meta: Dict{Symbol, Any}()For an echelle spectrum, all orders must have the same length, so be sure to pad any ragged orders with NaN.
julia> wave = reshape(range(100, 1e4, length=1000), 100, 10);
julia> flux = repeat(1:10.0, 1, 100)';
julia> spec = spectrum(wave, flux)
EchelleSpectrum(Float64, Float64)
# orders: 10
spectral axis (100, 10): 100.0 .. 10000.0
flux axis (100, 10): 1.0 .. 10.0
meta: Dict{Symbol, Any}()Basic operations
For more advanced transformations, see Transformations
Getters
Spectra.spectral_axis — Method
spectral_axis(spec::AbstractSpectrum)Return the spectral axis of spec.
Spectra.flux_axis — Method
flux_axis(spec::AbstractSpectrum)Return the flux axis of spec.
Spectra.meta — Method
meta(spec::AbstractSpectrum)Return the meta data associated with spec.
Array interface
| Function |
|---|
Base.argmax(::AbstractSpectrum) |
Base.argmin(::AbstractSpectrum) |
Base.eltype(::AbstractSpectrum) |
Base.findmax(::AbstractSpectrum) |
Base.findmin(::AbstractSpectrum) |
Base.iterate(::AbstractSpectrum) |
Base.length(::AbstractSpectrum) |
Base.maximum(::AbstractSpectrum) |
Base.minimum(::AbstractSpectrum) |
Base.size(::AbstractSpectrum) |
Arithmetic
| Function |
|---|
+(::AbstractSpectrum, A) |
-(::AbstractSpectrum, A) |
*(::AbstractSpectrum, A) |
/(::AbstractSpectrum, A) |
Base.(==)(::AbstractSpectrum, ::AbstractSpectrum) |
Unitful helpers
Unitful.unit — Function
Unitful.unit(::AbstractSpectrum)Get the units of a spectrum. Returns a tuple of the spectral axis units and flux/sigma units
Examples
julia> using Random
julia> rng = Random.seed!(0)
TaskLocalRNG()
julia> using Unitful, UnitfulAstro
julia> wave = range(1e4, 3e4, length=1000);
julia> flux = wave .* 10 .+ randn(rng, 1000);
julia> spec = spectrum(wave * u"angstrom", flux * u"W/m^2/angstrom");
julia> w_unit, f_unit = unit(spec)
(Å, W Å^-1 m^-2)Unitful.ustrip — Function
Unitful.ustrip(::AbstractSpectrum)Remove the units from a spectrum. Useful for processing spectra in tools that don't play nicely with Unitful.jl
Examples
julia> using Random
julia> rng = Random.seed!(0)
TaskLocalRNG()
julia> using Unitful, UnitfulAstro
julia> wave = range(1e4, 3e4, length=1000);
julia> flux = wave .* 10 .+ randn(rng, 1000);
julia> spec = spectrum(wave*u"angstrom", flux*u"W/m^2/angstrom")
SingleSpectrum(Quantity{Float64, 𝐋, Unitful.FreeUnits{(Å,), 𝐋, nothing}}, Quantity{Float64, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(Å^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}})
spectral axis (1000,): 10000.0 Å .. 30000.0 Å
flux axis (1000,): 99999.76809093042 W Å^-1 m^-2 .. 300000.2474309158 W Å^-1 m^-2
meta: Dict{Symbol, Any}()
julia> ustrip(spec)
SingleSpectrum(Float64, Float64)
spectral axis (1000,): 10000.0 .. 30000.0
flux axis (1000,): 99999.76809093042 .. 300000.2474309158
meta: Dict{Symbol, Any}()Plotting
We provide simple plotting recipes for spectra using Plots.jl
using Plots, Spectra
wave = range(1e3, 5e4, length=100)
spec = blackbody(wave, 2000)
plot(spec)GKS: cannot open display - headless operation mode active