Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/DustExtinction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,9 @@ include("dust_maps.jl")
include("fittable_laws.jl")
include("mixture_laws.jl")

# --------------------------------------------------------------------------------
# Here be codegen!

# generate unitful support for the following laws
# this can be removed when julia support is pinned to 1.3 or higher,
# at which point adding `(l::ExtinctionLaw)(wave::Quantity)` is possible, until then
# using this code-gen does the trick but requires manually editing
# instead of providing support for all <: ExtinctionLaw
for law in [CCM89, OD94, CAL00, GCC09, VCG04, FM90, G16, G03_SMCBar, G03_LMCAve, F99, F04, F19, M14]
(l::law)(wavelength::U.Quantity) = l(U.ustrip(U.u"Å", wavelength)) * U.u"mag"
end
# generate unitful support
(l::ExtinctionLaw)(wavelength::U.Quantity) = l(U.ustrip(U.u"Å", wavelength)) * U.u"mag"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're editing this I wonder if it makes sense to check dimensions here by dispatching as (l::ExtinctionLaw)(wavelength::U.Length) because wavelengths only make sense as lengths.

Copy link
Member Author

@icweaver icweaver May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Making the switch seems to make the error message a bit more opaque though:

With (l::ExtinctionLaw)(wavelength::U.Quantity)

julia> CCM89()(1e4/5)
2.8425264357868345

julia> CCM89()(2000u"")
2.8425264357868345 mag

julia> CCM89()(2000u"kg")
ERROR: DimensionError: Å and kg are not dimensionally compatible.
Stacktrace:
 [1] #s103#141
   @ ~/.julia/packages/Unitful/nwwOk/src/conversion.jl:28 [inlined]
 [2] var"#s103#141"(::Any, s::Any, t::Any)
   @ Unitful ./none:0
 [3] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
   @ Core ./boot.jl:707
 [4] uconvert(a::Unitful.FreeUnits{(Å,), 𝐋, nothing}, x::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
   @ Unitful ~/.julia/packages/Unitful/nwwOk/src/conversion.jl:93
 [5] ustrip
   @ ~/.julia/packages/Unitful/nwwOk/src/utils.jl:37 [inlined]
 [6] (::CCM89)(wavelength::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
   @ DustExtinction ~/projects/DustExtinction.jl/src/DustExtinction.jl:167
 [7] top-level scope
   @ REPL[30]:1

With (l::ExtinctionLaw)(wavelength::U.Length)

julia> CCM89()(1e4/5)
2.8425264357868345

julia> CCM89()(2000u"")
2.8425264357868345 mag

julia> CCM89()(2000u"kg")
ERROR: MethodError: no method matching (::CCM89)(::Quantity{Int64, 𝐌, Unitful.FreeUnits{(kg,), 𝐌, nothing}})
The object of type `CCM89` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

Closest candidates are:
  (::CCM89)(::T) where T<:Real
   @ DustExtinction ~/projects/DustExtinction.jl/src/color_laws/ccm89.jl:25
  (::DustExtinction.ExtinctionLaw)(::Union{Quantity{T, 𝐋, U}, Level{L, S, Quantity{T, 𝐋, U}} where {L, S}} where {T, U})
   @ DustExtinction ~/projects/DustExtinction.jl/src/DustExtinction.jl:167

Stacktrace:
 [1] top-level scope
   @ REPL[25]:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I guess the type conversion ends up requiring a length anyway, nbd


# --------------------------------------------------------------------------------

function __init__()
# register our data dependencies
Expand Down
4 changes: 2 additions & 2 deletions src/color_laws.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Base.@kwdef struct OD94 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::OD94)(wave::T) where T
function (law::OD94)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return ccm89_invum(x, law.Rv, od94_ca, od94_cb)
Expand Down Expand Up @@ -50,7 +50,7 @@ fit value for such galaxies was 4.05±0.80.
Base.@kwdef struct CAL00 <: ExtinctionLaw
Rv::Float64 = 4.05
end
function (law::CAL00)(wave::T) where T
function (law::CAL00)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
if wave < 6300
Expand Down
2 changes: 1 addition & 1 deletion src/color_laws/ccm89.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Base.@kwdef struct CCM89 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::CCM89)(wave::T) where T
function (law::CCM89)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return ccm89_invum(x, law.Rv, ccm89_ca, ccm89_cb)
Expand Down
6 changes: 3 additions & 3 deletions src/color_laws/fitzpatrick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Base.@kwdef struct F99 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::F99)(wave::T) where T
function (law::F99)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return f99_invum(x, law.Rv)
Expand Down Expand Up @@ -169,7 +169,7 @@ Base.@kwdef struct F04 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::F04)(wave::T) where T
function (law::F04)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return f04_invum(x, law.Rv)
Expand Down Expand Up @@ -246,7 +246,7 @@ Base.@kwdef struct F19 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::F19)(wave::T) where T
function (law::F19)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return f19_invum(x, law.Rv)
Expand Down
2 changes: 1 addition & 1 deletion src/color_laws/gcc09.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Base.@kwdef struct GCC09 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::GCC09)(wave::T) where T
function (law::GCC09)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return gcc09_invum(x, law.Rv)
Expand Down
2 changes: 1 addition & 1 deletion src/color_laws/m14.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Base.@kwdef struct M14 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::M14)(wave::T) where T
function (law::M14)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return m14_invum(x, law.Rv)
Expand Down
2 changes: 1 addition & 1 deletion src/color_laws/vcg04.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Base.@kwdef struct VCG04 <: ExtinctionLaw
Rv::Float64 = 3.1
end

function (law::VCG04)(wave::T) where T
function (law::VCG04)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return vcg04_invum(x, law.Rv)
Expand Down
2 changes: 1 addition & 1 deletion src/fittable_laws.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ FM90(coeffs, x0=4.60, gamma=0.99) = FM90(coeffs..., x0, gamma)

bounds(::Type{<:FM90}) = (912, 3200)

function (law::FM90)(wave::T) where T
function (law::FM90)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))

x = aa_to_invum(wave)
Expand Down
6 changes: 3 additions & 3 deletions src/mixture_laws.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ bounds(::Type{<:G03_SMCBar}) = (1000.0, 33333.3)
bounds(::Type{<:G03_LMCAve}) = (1000.0, 33333.3)


function (law::G03_SMCBar)(wave::T) where T
function (law::G03_SMCBar)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return g03_invum(x, law.Rv)
end

function (law::G03_LMCAve)(wave::T) where T
function (law::G03_LMCAve)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return g03lmc_invum(x, law.Rv)
Expand Down Expand Up @@ -180,7 +180,7 @@ end

bounds(::Type{<:G16}) = (1000.0, 33333.3)

function (law::G16)(wave::T) where T
function (law::G16)(wave::T) where T <: Real
checkbounds(law, wave) || return zero(float(T))
x = aa_to_invum(wave)
return g16_invum(x, law.Rv, law.f_A)
Expand Down
Loading