Transformations
Extinction
By leveraging DustExtinction.jl we can apply common reddening laws to our spectra.
julia> using Spectra, Unitful, Measurements, Random
julia> rng = Random.seed!(0);
julia> wave = (1:0.5:3)u"μm";
julia> sigma = randn(rng, size(wave));
julia> flux = (100 .± sigma)u"W/m^2/μm"
5-element Vector{Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(μm^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}}}:
100.0 ± 0.94 W μm^-1 m^-2
100.0 ± 0.13 W μm^-1 m^-2
100.0 ± 1.5 W μm^-1 m^-2
100.0 ± 0.12 W μm^-1 m^-2
100.0 ± -1.2 W μm^-1 m^-2
julia> spec = spectrum(wave, flux)
SingleSpectrum(Quantity{Float64, 𝐋, Unitful.FreeUnits{(μm,), 𝐋, nothing}}, Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(μm^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}})
spectral axis (5,): 1.0 μm .. 3.0 μm
flux axis (5,): 100.0 ± 0.94 W μm^-1 m^-2 .. 100.0 ± -1.2 W μm^-1 m^-2
meta: Dict{Symbol, Any}()
julia> red = redden(spec, 0.3)
SingleSpectrum(Quantity{Float64, 𝐋, Unitful.FreeUnits{(μm,), 𝐋, nothing}}, Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(μm^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}})
spectral axis (5,): 1.0 μm .. 3.0 μm
flux axis (5,): 89.44 ± 0.84 W μm^-1 m^-2 .. 98.1 ± 1.2 W μm^-1 m^-2
meta: Dict{Symbol, Any}()
julia> flux_axis(red)
5-element Vector{Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(μm^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}}}:
89.44 ± 0.84 W μm^-1 m^-2
94.35 ± 0.13 W μm^-1 m^-2
96.4 ± 1.5 W μm^-1 m^-2
97.48 ± 0.12 W μm^-1 m^-2
98.1 ± 1.2 W μm^-1 m^-2
julia> deredden!(red, 0.3)
SingleSpectrum(Quantity{Float64, 𝐋, Unitful.FreeUnits{(μm,), 𝐋, nothing}}, Quantity{Measurement{Float64}, 𝐌 𝐋^-1 𝐓^-3, Unitful.FreeUnits{(μm^-1, m^-2, W), 𝐌 𝐋^-1 𝐓^-3, nothing}})
spectral axis (5,): 1.0 μm .. 3.0 μm
flux axis (5,): 100.0 ± 0.94 W μm^-1 m^-2 .. 100.0 ± 1.2 W μm^-1 m^-2
meta: Dict{Symbol, Any}()
julia> flux_axis(red) ≈ flux_axis(spec)
trueAPI/Reference
Spectra.redden — Function
redden(::AbstractSpectrum, Av; Rv = 3.1, law = DustExtinction.CCM89)Redden a spectrum using common color laws provided by DustExtinction.jl. Av is the total extinction, Rv is the selective extinction (3.1 is a common value for the Milky Way) and law is the color law to use for determining the extinction.
Spectra.redden! — Function
redden!(::AbstractSpectrum, Av; Rv = 3.1, law = DustExtinction.CCM89)In-place version of redden
Spectra.deredden — Function
deredden(::AbstractSpectrum, Av; Rv = 3.1, law = DustExtinction.CCM89)Deredden a spectrum using common color laws provided by DustExtinction.jl. Av is the total extinction, Rv is the selective extinction (3.1 is a common value for the Milky Way) and law is the color law to use for determining the extinction.
Spectra.deredden! — Function
deredden!(::AbstractSpectrum, Av; Rv = 3.1, law = DustExtinction.CCM89)In-place version of deredden
Resampling
External interpolators, e.g., from DataInterpolations.jl or Interpolations.jl, can be used to resample spectra onto a given wavelength grid. Starting with a sample spectrum spec, we first create a SpectrumResampler object resampler which stores the initial spectrum and interpolator interp together. We then apply this object to the wavelength grid of our choice to produce the resampled spectrum. We show example usage in the docstring below:
Spectra.SpectrumResampler — Type
SpectrumResampler(spec::Spectrum, interp)Type representing the spectrum spec with interpolator interp.
Interpolation methods from many packages can be used without issue. Below we show example usage of DataInterpolations.jl and Interpolations.jl.
First, we set up an arbitrary spectrum and a linear interpolator from DataInterpolations.jl:
julia> using Spectra: SpectrumResampler, spectrum, spectral_axis, flux_axis
julia> using DataInterpolations: LinearInterpolation, ExtrapolationType
julia> spec = spectrum([20, 40, 120, 160, 200], [1, 3, 7, 6, 20]);
julia> interp = LinearInterpolation(flux_axis(spec), spectral_axis(spec);
extrapolation = ExtrapolationType.Constant);Now, we construct the SpectrumResampler and define the new wavelength grid that we want to resample the original spectrum to:
julia> resampler = SpectrumResampler(spec, interp);
julia> wave_sampled = [10, 50, 90, 130, 140, 170, 210, 220, 230];To perform the resampling, you call the resampler with the desired wavelength grid.
julia> result = resampler(wave_sampled);The resampled wavelength and flux can be obtained with the wave and flux methods.
julia> result isa Spectrum
true
julia> spectral_axis(result) == wave_sampled
true
julia> flux_axis(result) == interp(wave_sampled)
trueUse of Interpolations.jl follows the same general procedure, but using a different interp:
julia> using Interpolations: linear_interpolation, Flat
julia> interp = linear_interpolation(spectral_axis(spec), flux_axis(spec); extrapolation_bc = Flat());
julia> resampler = SpectrumResampler(spec, interp);
julia> result = resampler(wave_sampled);
julia> result isa Spectrum
true
julia> spectral_axis(result) == wave_sampled
true
julia> flux_axis(result) == interp(wave_sampled)
true