Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more specific error types #85

Merged
merged 1 commit into from
May 12, 2024
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
Use more specific error types
@Assert can get optimized away by the compiler, and generic error() uses
ErrorException, which doesn't provide much information.

Used DomainError for an argument being outside a valid set, and used
ArgumentError for multiple arguments relating to each other (e.g.,
multiple arrays having the same length)
  • Loading branch information
abhro committed May 12, 2024
commit 0da83d34b26bb53def44f50a1106e8a85684bc1c
4 changes: 3 additions & 1 deletion src/aitoff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ aitoff(l::Real, b::Real) = aitoff(promote(float(l), float(b))...)
aitoff(lb::Tuple{Real, Real}) = aitoff(lb...)

function aitoff(l::AbstractArray{L}, b::AbstractArray{B}) where {L<:Real,B<:Real}
@assert length(l) == length(b)
if length(l) != length(b)
throw(ArgumentError("l and b arrays must have the same length"))
end
typel = float(L)
x = similar(l, typel)
y = similar(b, typel)
Expand Down
4 changes: 3 additions & 1 deletion src/altaz2hadec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ altaz2hadec(altaz::Tuple{Real, Real}, lat::Real) = altaz2hadec(altaz..., lat)

function altaz2hadec(alt::AbstractArray{R}, az::AbstractArray{<:Real},
lat::AbstractArray{<:Real}) where {R<:Real}
@assert length(alt) == length(az) == length(lat)
if !(length(alt) == length(az) == length(lat))
throw(ArgumentError("alt, az, and lat arrays must have the same length"))
end
typealt = float(R)
ha = similar(alt, typealt)
dec = similar(alt, typealt)
Expand Down
13 changes: 10 additions & 3 deletions src/bprecess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const Mbprec =
# commented, in case someone is interested.
function _bprecess(ra::T, dec::T, parallax::T, radvel::T,
epoch::T, muradec::Vector{T}) where {T<:AbstractFloat}
@assert length(muradec) == 2
if length(muradec) != 2
throw(DomainError("muradec must have length 2"))
end
sinra, cosra = sincos(deg2rad(ra))
sindec, cosdec = sincos(deg2rad(dec))
r0 = SVector(cosra*cosdec, sinra*cosdec, sindec)
Expand Down Expand Up @@ -91,7 +93,10 @@ function bprecess(ra::AbstractArray{R}, dec::AbstractArray{<:Real},
muradec::AbstractArray{<:Real};
parallax::AbstractArray{<:Real}=zeros(R, length(ra)),
radvel::AbstractArray{<:Real}=zeros(R, length(ra))) where {R<:Real}
@assert length(ra) == length(dec) == size(muradec)[2] == length(parallax) == length(radvel)
if !(length(ra) == length(dec) == size(muradec, 2) == length(parallax) == length(radvel))
throw(ArgumentError(
"ra, dec, muradec[:,2], parallax, and radvel arrays should be of the same length"))
end
typer = float(R)
ra1950 = similar(ra, typer)
dec1950 = similar(dec, typer)
Expand All @@ -104,7 +109,9 @@ end

function bprecess(ra::AbstractArray{R}, dec::AbstractArray{D},
epoch::Real=2000.0) where {R<:Real,D<:Real}
@assert length(ra) == length(dec)
if length(ra) != length(dec)
throw(ArgumentError("ra and dec arrays should be of the same length"))
end
typer = float(R)
ra1950 = similar(ra, typer)
dec1950 = similar(dec, typer)
Expand Down
4 changes: 3 additions & 1 deletion src/co_aberration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ co_aberration(jd::Real, ra::Real, dec::Real, eps::Real=NaN) =

function co_aberration(jd::AbstractVector{R}, ra::AbstractVector{R},
dec::AbstractVector{R}, eps::Real=NaN) where {R<:Real}
@assert length(jd) == length(ra) == length(dec) "jd, ra and dec vectors should be of the same length"
if !(length(jd) == length(ra) == length(dec))
throw(ArgumentError("jd, ra and dec vectors should be of the same length"))
end
typejd = float(R)
ra_out = similar(ra, typejd)
dec_out = similar(dec, typejd)
Expand Down
5 changes: 3 additions & 2 deletions src/co_nutate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ co_nutate(jd::Real, ra::Real, dec::Real) =

function co_nutate(jd::AbstractVector{P}, ra::AbstractVector{<:Real},
dec::AbstractVector{<:Real}) where {P<:Real}
@assert length(jd) == length(ra) == length(dec) "jd, ra and dec vectors
should be of the same length"
if !(length(jd) == length(ra) == length(dec))
throw(ArgumentError("jd, ra and dec vectors should be of the same length"))
end
typejd = float(P)
ra_out = similar(jd, typejd)
dec_out = similar(dec, typejd)
Expand Down
4 changes: 3 additions & 1 deletion src/deredd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ deredd(Eby::Real, by::Real, m1::Real, c1::Real, ub::Real) =
function deredd(Eby::AbstractArray{E}, by::AbstractArray{<:Real},
m1::AbstractArray{<:Real}, c1::AbstractArray{<:Real},
ub::AbstractArray{<:Real}) where {E<:Real}
@assert length(Eby) == length(by) == length(m1) == length(c1) == length(ub)
if !(length(Eby) == length(by) == length(m1) == length(c1) == length(ub))
throw(ArgumentError("Eby, by, m1, c1, and ub arrays should be of the same length"))
end
typeeby = float(E)
by0 = similar(Eby, typeeby)
m0 = similar(Eby, typeeby)
Expand Down
4 changes: 3 additions & 1 deletion src/eci2geo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ eci2geo(xyz::Tuple{Real, Real, Real}, jd::Real) =

function eci2geo(x::AbstractArray{X}, y::AbstractArray{<:Real}, z::AbstractArray{<:Real},
jd::AbstractArray{<:Real}) where {X<:Real}
@assert length(x) == length(y) == length(z) == length(jd)
if !(length(x) == length(y) == length(z) == length(jd))
throw(ArgumentError("x, y, z, and jd arrays should be of the same length"))
end
typex = float(X)
lat = similar(x, typex)
long = similar(x, typex)
Expand Down
4 changes: 3 additions & 1 deletion src/eqpole.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ eqpole(l::Real, b::Real; southpole::Bool=false) =

function eqpole(l::AbstractArray{L}, b::AbstractArray{<:Real};
southpole::Bool=false) where {L<:Real}
@assert length(l) == length(b)
if length(l) != length(b)
throw(ArgumentError("l and b arrays should be of the same length"))
end
typel = float(L)
x = similar(l, typel)
y = similar(l, typel)
Expand Down
7 changes: 5 additions & 2 deletions src/euler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
function _euler(ai::T, bi::T, select::Integer, FK4::Bool, radians::Bool) where {T<:AbstractFloat}

if select>6 || select<1
error("Input for coordinate transformation should be an integer in the range 1:6")
throw(DomainError(
"Input for coordinate transformation should be an integer in the range 1:6"))
end

if FK4
Expand Down Expand Up @@ -98,7 +99,9 @@ euler(aibi::Tuple{Real, Real}, select::Integer; FK4::Bool=false, radians::Bool=f

function euler(ai::AbstractVector{R}, bi::AbstractVector{<:Real}, select::Integer;
FK4::Bool=false, radians::Bool=false) where {R<:Real}
@assert length(ai) == length(bi) "ai and bi arrays should be of the same length"
if length(ai) != length(bi)
throw(ArgumentError("ai and bi arrays should be of the same length"))
end
typeai = float(R)
ai_out = similar(ai, typeai)
bi_out = similar(bi, typeai)
Expand Down
7 changes: 5 additions & 2 deletions src/gal_uvw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ function gal_uvw(ra::AbstractArray{R}, dec::AbstractArray{<:Real},
pmra::AbstractArray{<:Real}, pmdec::AbstractArray{<:Real},
vrad::AbstractArray{<:Real}, plx::AbstractArray{<:Real};
lsr::Bool=false) where {R<:Real}
@assert length(ra) == length(dec) == length(pmra) ==
length(pmdec) == length(vrad) == length(plx)
if !(length(ra) == length(dec) == length(pmra) ==
length(pmdec) == length(vrad) == length(plx))
throw(ArgumentError(
"ra, dec, pmra, pmdec, vrad, and plx arrays must all have the same length"))
end
typer = float(R)
u = similar(ra, typer)
v = similar(ra, typer)
Expand Down
3 changes: 2 additions & 1 deletion src/gcirc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function gcirc(units::Integer, ra1::T, dec1::T, ra2::T, dec2::T) where {T<:Abstr
φ_2 = deg2rad(dec2)
else
# In any other case throw an error.
error("units must be 0 (radians), 1 (hours, degrees) or 2 (degrees)")
throw(DomainError(
"units must be 0 (radians), 1 (hours, degrees) or 2 (degrees)"))
end
Δφ_2 = (φ_2 - φ_1) * 0.5
Δλ_2 = (λ_2 - λ_1) * 0.5
Expand Down
5 changes: 4 additions & 1 deletion src/geo2eci.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ geo2eci(lla::Tuple{Real, Real, Real}, jd::Real) =
function geo2eci(lat::AbstractArray{LA}, long::AbstractArray{<:Real},
alt::AbstractArray{<:Real},
jd::AbstractArray{<:Real}) where {LA<:Real}
@assert length(lat) == length(long) == length(alt) == length(jd)
if !(length(lat) == length(long) == length(alt) == length(jd))
throw(ArgumentError(
"lat, long, alt, and jd arrays must have the same length"))
end
typela = float(LA)
x = similar(lat, typela)
y = similar(lat, typela)
Expand Down
8 changes: 6 additions & 2 deletions src/geo2geodetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ geo2geodetic(lla::Tuple{Real, Real, Real}, eq::Real, pol::Real) =
function geo2geodetic(lat::AbstractArray{LA}, long::AbstractArray{<:Real},
alt::AbstractArray{<:Real},
eq::Real, pol::Real) where {LA<:Real}
@assert length(lat) == length(long) == length(alt)
if !(length(lat) == length(long) == length(alt))
throw(ArgumentError("lat, long, and alt arrays must have the same length"))
end
typela = float(LA)
outlat = similar(lat, typela)
outlong = similar(lat, typela)
Expand All @@ -183,7 +185,9 @@ geo2geodetic(lla::Tuple{Real, Real, Real}, planet::AbstractString="earth") =
function geo2geodetic(lat::AbstractArray{LA}, long::AbstractArray{<:Real},
alt::AbstractArray{<:Real},
planet::AbstractString="earth") where {LA<:Real}
@assert length(lat) == length(long) == length(alt)
if !(length(lat) == length(long) == length(alt))
throw(ArgumentError("lat, long, and alt arrays must have the same length"))
end
typela = float(LA)
outlat = similar(lat, typela)
outlong = similar(lat, typela)
Expand Down
4 changes: 3 additions & 1 deletion src/geo2mag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ geo2mag(lat::Real, long::Real, year::Real=Dates.year(Dates.now())) =

function geo2mag(lat::AbstractArray{LA}, long::AbstractArray{<:Real},
year::Real=Dates.year(Dates.now())) where {LA<:Real}
@assert length(lat) == length(long)
if length(lat) != length(long)
throw(ArgumentError("lat and long arrays should be of the same length"))
end
typela = float(LA)
maglat = similar(lat, typela)
maglong = similar(lat, typela)
Expand Down
8 changes: 6 additions & 2 deletions src/geodetic2geo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ geodetic2geo(lla::Tuple{Real, Real, Real}, eq::Real, pol::Real) =
function geodetic2geo(lat::AbstractArray{LA}, long::AbstractArray{<:Real},
alt::AbstractArray{<:Real}, eq::Real,
pol::Real) where {LA<:Real}
@assert length(lat) == length(long) == length(alt)
if !(length(lat) == length(long) == length(alt))
throw(ArgumentError("lat, long, and alt arrays must have the same length"))
end
typela = float(LA)
outlat = similar(lat, typela)
outlong = similar(lat, typela)
Expand All @@ -151,7 +153,9 @@ function geodetic2geo(lat::AbstractArray{LA},
long::AbstractArray{<:Real},
alt::AbstractArray{<:Real},
planet::AbstractString="earth") where {LA<:Real}
@assert length(lat) == length(long) == length(alt)
if !(length(lat) == length(long) == length(alt))
throw(ArgumentError("lat, long, and alt arrays must have the same length"))
end
typela = float(LA)
outlat = similar(lat, typela)
outlong = similar(lat, typela)
Expand Down
4 changes: 3 additions & 1 deletion src/hadec2altaz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ hadec2altaz(hadec::Tuple{Real, Real}, lat::Real; ws::Bool=false) =

function hadec2altaz(ha::AbstractArray{R}, dec::AbstractArray{<:Real},
lat::AbstractArray{<:Real}; ws::Bool=false) where {R<:Real}
@assert length(ha) == length(dec) == length(lat)
if !(length(ha) == length(dec) == length(lat))
throw(ArgumentError("ha, dec, and lat arrays must have the same length"))
end
typeha = float(R)
alt = similar(ha, typeha)
az = similar(ha, typeha)
Expand Down
7 changes: 4 additions & 3 deletions src/helio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const record = Dict(1=>"mercury", 2=>"venus", 3=>"earth", 4=>"mars", 5=>"jupiter
function _helio(jd::T, num::Integer, radians::Bool) where {T<:AbstractFloat}

if num<1 || num>9
error("Input should be an integer in the range 1:9 denoting planet number")
throw(DomainError("Input should be an integer in the range 1:9 denoting planet number"))
end
t = (jd - J2000) / JULIANCENTURY
body = record[num]
Expand Down Expand Up @@ -115,8 +115,9 @@ helio(jd::Real, num::Integer, radians::Bool=false) =

function helio(jd::AbstractVector{P}, num::AbstractVector{<:Real},
radians::Bool = false) where {P<:Real}
@assert length(jd) == length(num) "jd and num vectors should
be of the same length"
if length(jd) != length(num)
throw(ArgumentError("jd and num vectors should be of the same length"))
end
typejd = float(P)
hrad_out = similar(jd, typejd)
hlong_out = similar(jd, typejd)
Expand Down
7 changes: 4 additions & 3 deletions src/imf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ function imf(mass::AbstractVector{T}, expon::AbstractVector{T},
mass_range::AbstractVector{T}) where {T<:AbstractFloat}
ne_comp = length(expon)
if length(mass_range) != ne_comp + 1
error("Length of array mass_range is not one more than that of expon")
throw(ArgumentError(
"Length of array mass_range is not one more than that of expon"))
end
integ = Vector{T}(undef, ne_comp)
joint = ones(T, ne_comp)
Expand All @@ -19,10 +20,10 @@ function imf(mass::AbstractVector{T}, expon::AbstractVector{T},
joint[i] = joint[i-1]*(mass_range[i]^(expon[i-1] - expon[i]))
end
end
norm = joint./(dot(integ, joint))
norm = joint ./ (dot(integ, joint))
psi = fill!(similar(mass), 0)
for i = 1:ne_comp
test = findall(mass_range[i].< mass.<mass_range[i+1])
test = findall(mass_range[i] .< mass .< mass_range[i+1])
if length(test) !=0
psi[test] = norm[i].*(mass[test].^expon[i])
end
Expand Down
14 changes: 11 additions & 3 deletions src/jprecess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const Mjprec =
# commented, in case someone is interested.
function _jprecess(ra::T, dec::T, parallax::T, radvel::T, epoch::T,
muradec::Vector{T}) where {T<:AbstractFloat}
@assert length(muradec) == 2
if length(muradec) != 2
throw(DomainError("muradec must have length 2"))
end
sinra, cosra = sincos(deg2rad(ra))
sindec, cosdec = sincos(deg2rad(dec))
if isfinite(epoch) && epoch != 1950
Expand Down Expand Up @@ -94,7 +96,11 @@ function jprecess(ra::AbstractArray{R}, dec::AbstractArray{<:Real},
muradec::AbstractArray{<:Real};
parallax::AbstractArray{<:Real}=zeros(R, length(ra)),
radvel::AbstractArray{<:Real}=zeros(R, length(ra))) where {R<:Real}
@assert length(ra) == length(dec) == size(muradec)[2] == length(parallax) == length(radvel)
if !(length(ra) == length(dec) == size(muradec)[2] == length(parallax) == length(radvel))
# TODO write more helpful error message
throw(ArgumentError(
"ra, dec, muradec[:,2], parallax, and radvel must have the same length"))
end
typer = float(R)
ra2000 = similar(ra, typer)
dec2000 = similar(dec, typer)
Expand All @@ -107,7 +113,9 @@ end

function jprecess(ra::AbstractArray{R}, dec::AbstractArray{<:Real},
epoch::Real=1950.0) where {R<:Real}
@assert length(ra) == length(dec)
if length(ra) != length(dec)
throw(ArgumentError("ra and dec arrays should be of the same length"))
end
typer = float(R)
ra2000 = similar(ra, typer)
dec2000 = similar(dec, typer)
Expand Down
5 changes: 3 additions & 2 deletions src/juldate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ function juldate(dt::DateTime)
Dates.hour(dt), Dates.minute(dt),
Dates.second(dt), Dates.millisecond(dt)

if year == 0
throw(DomainError("There is no year zero in Julian Calendar"))
end
if year < 0
year += 1
elseif year == 0
error("There is no year zero in Julian Calendar")
end
# Do not include leap year in January and February.
if month < 3
Expand Down
4 changes: 3 additions & 1 deletion src/kepler_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Copyright (C) 2016 Mosè Giordano.

function kepler_solver(_M::Real, e::Real)
@assert 0 <= e <= 1 "eccentricity must be in the range [0, 1]"
if e < 0 || e > 1
throw(DomainError("eccentricity must be in the range [0, 1]"))
end
# M must be in the range [-pi, pi], see Markley (1995), page 2.
M = rem2pi(_M, RoundNearest)
T = float(promote_type(typeof(M), typeof(e)))
Expand Down
4 changes: 3 additions & 1 deletion src/mag2geo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ mag2geo(lat::Real, long::Real, year::Real=Dates.year(Dates.now())) =

function mag2geo(lat::AbstractArray{LA}, long::AbstractArray{LO},
year::Real=Dates.year(Dates.now())) where {LA<:Real, LO<:Real}
@assert length(lat) == length(long)
if length(lat) != length(long)
throw(ArgumentError("lat and long arrays should be of the same length"))
end
typela = float(LA)
geolat = similar(lat, typela)
geolong = similar(lat, typela)
Expand Down
2 changes: 1 addition & 1 deletion src/ordinal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Code of this function is based on IDL Astronomy User's Library.
"""
function ordinal(num::Integer)
a = num % 100
if a== 11 || a == 12 || a == 13
if a == 11 || a == 12 || a == 13
suffix = "th"
else
a = num % 10
Expand Down
4 changes: 3 additions & 1 deletion src/planet_coords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ planet_coords(date::Real, num::Integer) = _planet_coords(float(date), num)

function planet_coords(date::AbstractVector{R},
num::AbstractVector{<:Integer}) where {R<:Real}
@assert length(date) == length(num) "date and num arrays should be of the same length"
if length(date) != length(num)
throw(ArgumentError("date and num arrays should be of the same length"))
end
typedate = float(R)
ra_out = similar(date, typedate)
dec_out = similar(date, typedate)
Expand Down
4 changes: 3 additions & 1 deletion src/polrec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ polrec(r_a::Tuple{Real, Real}; degrees::Bool=false) = polrec(r_a...,

function polrec(r::AbstractArray{R}, a::AbstractArray{A};
degrees::Bool=false) where {R<:Real, A<:Real}
@assert length(r) == length(a)
if length(r) != length(a)
throw(ArgumentError("r and a arrays should be of the same length"))
end
typer = float(R)
x = similar(r, typer)
y = similar(r, typer)
Expand Down
3 changes: 2 additions & 1 deletion src/posang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function posang(units::Integer, ra1::T, dec1::T, ra2::T, dec2::T) where {T<:Abst
dec2_rad = deg2rad(dec2)
else
# In any other case throw an error.
error("units must be 0 (radians), 1 (hours, degrees) or 2 (degrees)")
throw(DomainError(
"units must be 0 (radians), 1 (hours, degrees) or 2 (degrees)"))
end
sin_radif, cos_radif = sincos(ra2_rad - ra1_rad)
sin_dec1, cos_dec1 = sincos(dec1_rad)
Expand Down
Loading
Loading