Skip to content

Commit

Permalink
easy mode constructor for multiple ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwheeler committed Oct 11, 2024
1 parent a99dd1d commit 8ea0a13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/wavelengths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ struct Wavelengths{F} <: AbstractArray{F,1}
all_freqs::Vector{F}

"""
TODO
Korg.Wavelengths(wl_params...; air_wavelengths=false, wavelength_conversion_warn_threshold=1e-4)
Construct a `Wavelengths` object which represents the (possibly non-contiguous) wavelengths for
which to compute a spectrum. The wavelengths can be specified with an upper and lower bound, or
a vector of upper and lower bounds. For example,
Korg.Wavelengths(5000, 5500)
Korg.Wavelengths([(5000, 5500), (6000, 6500)])
# Keyword Arguments
- `air_wavelengths` (default: `false`): Whether or not the input wavelengths are air wavelengths to
be converted to vacuum wavelengths by Korg. The conversion will not be exact, so that the
Expand All @@ -14,8 +22,9 @@ struct Wavelengths{F} <: AbstractArray{F,1}
wavelength conversions yourself, see [`air_to_vacuum`](@ref) and [`vacuum_to_air`](@ref).)
- `wavelength_conversion_warn_threshold` (default: 1e-4): see `air_wavelengths`. (In Å.)
"""
function Wavelengths(wl_ranges::AbstractVector;
air_wavelengths=false, wavelength_conversion_warn_threshold=1e-4)
function Wavelengths(wl_ranges::AbstractVector{R};
air_wavelengths=false,
wavelength_conversion_warn_threshold=1e-4) where R<:AbstractRange
# if the first wavelength is > 1, assume it's in Å and convert to cm
if first(first(wl_ranges)) >= 1
wl_ranges = wl_ranges .* 1e-8
Expand Down Expand Up @@ -71,19 +80,15 @@ function Wavelengths(wls::AbstractVector{<:Real}; tolerance=1e-6, kwargs...)
Wavelengths([range(first(wls), last(wls); length=length(wls))]; kwargs...)
end
end
# handle integer args
function Wavelengths(λ_start::Integer, λ_stop::Integer, λ_step=0.01; kwargs...)
Wavelengths(Float64(λ_start), Float64(λ_stop), λ_step; kwargs...)
end
function Wavelengths(λ_start::Integer, λ_stop, λ_step=0.01; kwargs...)
Wavelengths(Float64(λ_start), λ_stop, λ_step; kwargs...)
end
function Wavelengths(λ_start, λ_stop::Integer, λ_step=0.01; kwargs...)
Wavelengths(λ_start, Float64(λ_stop), λ_step; kwargs...)
function Wavelengths(wls::AbstractVector, λ_step=0.01; kwargs...)
if λ_step isa Integer
λ_step = convert(Float64, λ_step)
end
Wavelengths([range(; start=λ_start, stop=λ_stop, step=λ_step) for (λ_start, λ_stop) in wls];
kwargs...)
end
# easy mode: pass in a single start and stop
function Wavelengths(λ_start, λ_stop, λ_step=0.01; kwargs...)
Wavelengths([range(; start=λ_start, stop=λ_stop, step=λ_step)]; kwargs...)
Wavelengths([(λ_start, λ_stop)], λ_step; kwargs...)
end

# implement the AbstractArray interface
Expand Down
4 changes: 4 additions & 0 deletions test/wavelengths.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@testset "Wavelengths" begin
@testset "constructor" begin
wls = Korg.Wavelengths(15000, 15500)
@test wls == Korg.Wavelengths([(15000, 15500)])
@test wls == Korg.Wavelengths(15000:0.01:15500)
@test wls == Korg.Wavelengths([15000:0.01:15500])
@test wls == Korg.Wavelengths(collect(15000:0.01:15500))

@test Korg.Wavelengths(15000, 15500, 1.0) == Korg.Wavelengths(15000, 15500, 1)
@test Korg.Wavelengths(15000, 15500, 1) == Korg.Wavelengths(15000:1.0:15500)

# test automatic air to vacuum conversion
vac_wls = 15000:0.01:15500
air_wls = Korg.air_to_vacuum.(15000:0.01:15500)
Expand Down

0 comments on commit 8ea0a13

Please sign in to comment.