Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
LudwigBoess authored Oct 15, 2024
2 parents 7b7c930 + a900b63 commit 774a46f
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 27 deletions.
18 changes: 16 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SkyCoords"
uuid = "fc659fc5-75a3-5475-a2ea-3da92c065361"
authors = "Kyle Barbary, Mosé Giordano, and contributors"
version = "1.2.0"
version = "1.4.0"

[deps]
AstroAngles = "5c4adb95-c1fc-4c53-b4ea-2a94080c53d2"
Expand All @@ -10,9 +10,23 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[weakdeps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
AccessorsExt = "Accessors"
UnitfulExt = "Unitful"

[compat]
AstroAngles = "0.1"
Accessors = "0.1"
AstroAngles = "0.2"
ConstructionBase = "1"
Rotations = "1"
StaticArrays = "0.8, 0.9, 1"
Unitful = "1.15"
julia = "1.6"

[extras]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
6 changes: 3 additions & 3 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[deps]
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
AstroAngles = "5c4adb95-c1fc-4c53-b4ea-2a94080c53d2"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
Documenter = "0.26, 0.27"
Documenter = "1"
1 change: 0 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ makedocs(
canonical = "https://juliaastro.github.io/SkyCoords.jl",
),
pages = pages,
strict = true,
)

deploydocs(repo = "github.com/JuliaAstro/SkyCoords.jl", push_preview=true)
26 changes: 13 additions & 13 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ DocTestSetup = :(using SkyCoords)
## Index

```@index
pages = ["api.md"]
Pages = ["api.md"]
```

## Types
Expand All @@ -31,20 +31,20 @@ ICRSCoords{Float64}(0.0, 0.0)
```

- using `convert`
```jldoctest convsetup
julia> convert(GalCoords, c1)
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```
```jldoctest convsetup
julia> convert(GalCoords, c1)
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```
- using constructors
```jldoctest convsetup
julia> GalCoords(c1)
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```
```jldoctest convsetup
julia> GalCoords(c1)
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```
- using `|>`
```jldoctest convsetup
julia> c1 |> GalCoords
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```
```jldoctest convsetup
julia> c1 |> GalCoords
GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```

## Functions

Expand Down
18 changes: 17 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SkyCoords.jl provides a type system for astronomical coordinate systems with app
From the Julia REPL

```julia-repl
(v1.2) pkg> add SkyCoords
(v1.6) pkg> add SkyCoords
julia> using SkyCoords
```
Expand Down Expand Up @@ -50,6 +50,22 @@ julia> c2.l # Note that galactic coordinate fields are l, b
julia> c1 |> FK5Coords{2000} # Can use piping syntax for conversion
FK5Coords{2000, Float64}(1.1102233723050067e-7, 4.411803426976326e-8)
```
### Units

There is built-in support for units via [Unitful.jl](https://github.com/PainterQubits/Unitful.jl)

```jldoctest unitangles
julia> using Unitful
julia> c = ICRSCoords(0.11255u"°", 0.00091u"rad")
ICRSCoords{Float64}(0.0019643680731196178, 0.00091)
julia> c2 = FK5Coords{2000}(0.1u"rad", 0.5)
FK5Coords{2000, Float64}(0.1, 0.5)
julia> SkyCoords.lat(u"μrad", c)
910.0 μrad
```

### Parsing from strings

Expand Down
23 changes: 23 additions & 0 deletions ext/AccessorsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module AccessorsExt
using Accessors
import Accessors: set
using SkyCoords
using SkyCoords: lat, lon

set(x::GalCoords, ::typeof(lon), v) = @set x.l = v
set(x::GalCoords, ::typeof(lat), v) = @set x.b = v
set(x::EclipticCoords, ::typeof(lon), v) = @set x.lon = v
set(x::EclipticCoords, ::typeof(lat), v) = @set x.lat = v
set(x::ICRSCoords, ::typeof(lon), v) = @set x.ra = v
set(x::ICRSCoords, ::typeof(lat), v) = @set x.dec = v
set(x::FK5Coords, ::typeof(lon), v) = @set x.ra = v
set(x::FK5Coords, ::typeof(lat), v) = @set x.dec = v

set(x::CartesianCoords, ::typeof(vec), v) = @set x.vec = v

set(x::CartesianCoords, ::typeof(cartesian), v::CartesianCoords) = cartesian(v)
set(x::CartesianCoords, ::typeof(spherical), v::AbstractSkyCoords) = cartesian(v)
set(x::AbstractSkyCoords, ::typeof(cartesian), v::CartesianCoords) = spherical(v)
set(x::AbstractSkyCoords, ::typeof(spherical), v::AbstractSkyCoords) = spherical(v)

end
18 changes: 18 additions & 0 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module UnitfulExt

using Unitful
using SkyCoords

_COORDTYPES_LATLON = Union{ICRSCoords, GalCoords, FK5Coords, EclipticCoords}

(::Type{T})(lon::Quantity, lat) where {T<:_COORDTYPES_LATLON} = T(ustrip(u"rad", lon), lat)
(::Type{T})(lon, lat::Quantity) where {T<:_COORDTYPES_LATLON} = T(lon, ustrip(u"rad", lat))
(::Type{T})(lon::Quantity, lat::Quantity) where {T<:_COORDTYPES_LATLON} = T(ustrip(u"rad", lon), ustrip(u"rad", lat))

SkyCoords.lon(u::Unitful.Units, c) = SkyCoords.lon(c) * u"rad" |> u
SkyCoords.lat(u::Unitful.Units, c) = SkyCoords.lat(c) * u"rad" |> u

SkyCoords.separation(u::Unitful.Units, c1, c2) = SkyCoords.separation(c1, c2) * u"rad" |> u
SkyCoords.position_angle(u::Unitful.Units, c1, c2) = SkyCoords.position_angle(c1, c2) * u"rad" |> u

end
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct EclipticCoords{e,T<:AbstractFloat} <: AbstractSkyCoords
EclipticCoords{e,T}(lon, lat) where {e,T<:AbstractFloat} = new(mod2pi(lon), lat)
end
EclipticCoords{e}(lon::T, lat::T) where {e,T<:AbstractFloat} = EclipticCoords{e,T}(lon, lat)
EclipticCoords{e}(lon::Real, lat::Real) where {e} = EclipticCoords(promote(float(lon), float(lat))...)
EclipticCoords{e}(lon::Real, lat::Real) where {e} = EclipticCoords{e}(promote(float(lon), float(lat))...)
EclipticCoords{e}(c::AbstractSkyCoords) where {e} = convert(EclipticCoords{e}, c)
EclipticCoords{e,F}(c::AbstractSkyCoords) where {e,F} = convert(EclipticCoords{e,F}, c)
constructorof(::Type{<:EclipticCoords{e}}) where {e} = EclipticCoords{e}
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
AstroAngles = "5c4adb95-c1fc-4c53-b4ea-2a94080c53d2"
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand All @@ -8,3 +9,4 @@ PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
61 changes: 55 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using AstroAngles
using Accessors
using Unitful
using ConstructionBase: setproperties
using DelimitedFiles
using LinearAlgebra: normalize
Expand All @@ -21,7 +23,7 @@ include("astropy.jl")
c5 = ICRSCoords(ℯ, 1 + pi / 2)
@test separation(c1, c5) separation(c5, c1) separation(c1, convert(GalCoords, c5))
separation(convert(FK5Coords{1980}, c5), c1) 1
for T in (GalCoords, FK5Coords{2000})
for T in (GalCoords, FK5Coords{2000}, EclipticCoords{2000})
c2 = convert(T{Float32}, c1)
c3 = convert(T{Float64}, c1)
c4 = convert(T{BigFloat}, c1)
Expand All @@ -40,6 +42,8 @@ end
GalCoords,
FK5Coords{2000},
FK5Coords{1970},
EclipticCoords{2000},
EclipticCoords{1970},
]
@test C(hms"0h0m0", dms"0d0m0") == C(0.0, 0.0)
@test C(hms"12h0.0m0.0s", dms"90:0:0") == C(π, π / 2)
Expand All @@ -66,7 +70,7 @@ end
@test position_angle(c1, c4) 0

# types
for T in [ICRSCoords, GalCoords, FK5Coords{2000}]
for T in [ICRSCoords, GalCoords, FK5Coords{2000}, EclipticCoords{2000}]
c1 = T(0, 0)
c2 = T(deg2rad(1), 0)
@test position_angle(c1, c2) π / 2
Expand All @@ -75,7 +79,7 @@ end



@testset "Offset ($T1, $T2)" for T1 in [ICRSCoords, GalCoords, FK5Coords{2000}], T2 in [ICRSCoords, GalCoords, FK5Coords{2000}]
@testset "Offset ($T1, $T2)" for T1 in [ICRSCoords, GalCoords, FK5Coords{2000}, EclipticCoords{2000}], T2 in [ICRSCoords, GalCoords, FK5Coords{2000}, EclipticCoords{2000}]
# simple integration tests, depend that separation and position_angle are accurate
c1s = [
T1(0, -π/2), # south pole
Expand Down Expand Up @@ -125,7 +129,7 @@ end
end

@testset "cartesian" begin
for CT in [ICRSCoords, FK5Coords{2000}, FK5Coords{1975}, GalCoords]
for CT in [ICRSCoords, FK5Coords{2000}, FK5Coords{1975}, EclipticCoords{2000}, EclipticCoords{1975}, GalCoords]
@test cartesian(CT(0, 0)) |> vec [1, 0, 0]
@test cartesian(CT(0, π/2)) |> vec [0, 0, 1]
@test cartesian(CT/2, 0)) |> vec [0, 1, 0]
Expand Down Expand Up @@ -160,11 +164,56 @@ end
@test setproperties(ICRSCoords(1, 2), ra=3) == ICRSCoords(3, 2)
@test setproperties(GalCoords(1, 2), l=3) == GalCoords(3, 2)
@test setproperties(FK5Coords{2000}(1, 2), ra=3) == FK5Coords{2000}(3, 2)
@test setproperties(EclipticCoords{2000}(1, 2), lon=3) == EclipticCoords{2000}(3, 2)
@test setproperties(cartesian(ICRSCoords(1, 2)), vec=[1., 0, 0]) == cartesian(ICRSCoords(0, 0))
end

VERSION > v"1.9-DEV" && @testset "Accessors" begin
@testset for c in (ICRSCoords(1, 0.5), FK5Coords{2000}(1, 0.5), GalCoords(1, 0.5), EclipticCoords{2000}(1, 0.5))
Accessors.test_getset_laws(lon, c, 1.5, 0.123)
Accessors.test_getset_laws(lat, c, 1.5, 0.123)

cart = cartesian(c)
cart1 = @set lat(spherical(cart)) = 0.123
@test typeof(cart1) == typeof(cart)
@test lat(spherical(cart1)) 0.123

c1 = @set vec(cartesian(c)) = [1., 0, 0]
@test typeof(c1) == typeof(c)
@test lat(c1) == 0
@test lon(c1) == 0

Accessors.test_getset_laws(spherical, c, c1, c; cmp = )
Accessors.test_getset_laws(cartesian, c, cart1, cart; cmp = )
Accessors.test_getset_laws(spherical, cart, c1, c; cmp = )
Accessors.test_getset_laws(cartesian, cart, cart1, cart; cmp = )
end
end

VERSION > v"1.9-DEV" && @testset "Unitful" begin
@test ICRSCoords(1u"rad", 0.5) === ICRSCoords(1, 0.5)
@test GalCoords(1u"rad", 0.5u"rad") === GalCoords(1, 0.5)
@test FK5Coords{2000}(1u"°", 0.5) === FK5Coords{2000}(deg2rad(1), 0.5)
@test EclipticCoords{2000}(1u"°", 0.5u"°") === EclipticCoords{2000}(deg2rad(1), deg2rad(0.5))

@test SkyCoords.lat(u"rad", ICRSCoords(1, 0.5)) === 0.5u"rad"
@test SkyCoords.lon(u"°", ICRSCoords(1, 0.5)) === rad2deg(1)u"°"

@test separation(u"rad", ICRSCoords(1, 0.5), ICRSCoords(1, -0.2)) === 0.7u"rad"
@test separation(u"°", ICRSCoords(1, 0.5), ICRSCoords(1, -0.2)) === rad2deg(0.7)u"°"

@test position_angle(u"rad", ICRSCoords(1, 0.5), ICRSCoords(1, -0.2)) === Float64(π)*u"rad"
@test position_angle(u"°", ICRSCoords(1, 0.5), ICRSCoords(1, -0.2)) === 180.0u"°"

# offset() works without special Unitful support
@test offset(ICRSCoords(1, 0.5), 0.1u"rad", 2) === offset(ICRSCoords(1, 0.5), 0.1, 2)
@test offset(ICRSCoords(1, 0.5), 0.1u"rad", 2u"rad") === offset(ICRSCoords(1, 0.5), 0.1, 2)
@test offset(ICRSCoords(1, 0.5), 0.1, 100u"°") === offset(ICRSCoords(1, 0.5), 0.1, deg2rad(100))
@test offset(ICRSCoords(1, 0.5), 0.1u"°", 100u"°") === offset(ICRSCoords(1, 0.5), deg2rad(0.1), deg2rad(100))
end

@testset "equality" begin
@testset for T in [ICRSCoords, GalCoords, FK5Coords{2000}]
@testset for T in [ICRSCoords, GalCoords, FK5Coords{2000}, EclipticCoords{2000}]
c1 = T(1., 2.)
c2 = T(1., 2.001)
c3 = T{Float32}(1., 2.)
Expand All @@ -184,7 +233,7 @@ end
end

@testset "conversion" begin
systems = (ICRSCoords, FK5Coords{2000}, FK5Coords{1975}, GalCoords)
systems = (ICRSCoords, FK5Coords{2000}, FK5Coords{1975}, EclipticCoords{2000}, EclipticCoords{1975}, GalCoords)
for IN_SYS in systems, OUT_SYS in systems
coord_in = IN_SYS(rand(rng), rand(rng))
coord_out = convert(OUT_SYS, coord_in)
Expand Down

0 comments on commit 774a46f

Please sign in to comment.