Skip to content

Commit

Permalink
position angle (#32)
Browse files Browse the repository at this point in the history
* add position_angle

* add position_angle to readme

* put actual test into inferred test
  • Loading branch information
mileslucas authored Mar 25, 2020
1 parent 12d19bd commit 27b4ece
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ FK5Coords{2000,Float64}(1.1102233723050067e-7, 4.411803426976326e-8)
julia> separation(c1, ICRSCoords(1., 0.)) # radians
1.0

julia> position_angle(c1, ICRSCoords(1, 0)) |> rad2deg
90.0
```

For more information, visit the [documentation](https://juliaastro.github.io/skycoords.jl/stable)
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ GalCoords{Float64}(1.6814027872278692, -1.0504884034813007)
```@docs
SkyCoords.str2rad
separation
position_angle
```
11 changes: 11 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ julia> rad2deg(separation(mizar, alcor)) * 60 # Arcminutes
11.8097230039347
```

with an angle

```jldoctest sep
julia> position_angle(mizar, alcor) # radians
1.244602401241819
julia> position_angle(mizar, alcor) |> rad2deg # degrees
71.31046476300408
```

## Accuracy

All the supported conversions have been compared to the results of
Expand Down
36 changes: 35 additions & 1 deletion src/SkyCoords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ __precompile__()
module SkyCoords
using StaticArrays

export AbstractSkyCoords, ICRSCoords, GalCoords, FK5Coords, separation
export AbstractSkyCoords,
ICRSCoords,
GalCoords,
FK5Coords,
separation,
position_angle

include("types.jl")

Expand Down Expand Up @@ -224,4 +229,33 @@ separation(c1::T, c2::T) where {T<:AbstractSkyCoords} =
separation(c1::T1, c2::T2) where {T1<:AbstractSkyCoords,T2<:AbstractSkyCoords} =
separation(c1, convert(T1, c2))


"""
position_angle(c1::AbstractSkyCoords, c2::AbstractSkyCoords) -> angle
Return position angle between two sky coordinates, in positive radians.
# Examples
```jldoctest
julia> c1 = ICRSCoords(0, 0); c2 = ICRSCoords(deg2rad(1), 0);
julia> position_angle(c1, c2) |> rad2deg
90.0
```
"""
position_angle(c1::T, c2::T) where {T <: AbstractSkyCoords} = _position_angle(lon(c1), lat(c1), lon(c2), lat(c2))
position_angle(c1::T1, c2::T2) where {T1 <: AbstractSkyCoords,T2 <: AbstractSkyCoords} = position_angle(c1, convert(T1, c2))


function _position_angle(λ1, ϕ1, λ2, ϕ2)
sin_Δλ, cos_Δλ = sincos(λ2 - λ1)
sin_ϕ1, cos_ϕ1 = sincos(ϕ1)
sin_ϕ2, cos_ϕ2 = sincos(ϕ2)

x = sin_ϕ2 * cos_ϕ1 - cos_ϕ2 * sin_ϕ1 * cos_Δλ
y = sin_Δλ * cos_ϕ2

return mod2pi(atan(y, x))
end

end # module
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,29 @@ end
@test C("12:0:0", "90:0:0") == C(π, π / 2)
end
end

# Test separation between coordinates and conversion with mixed floating types.
@testset "Position Angles" begin
c1 = ICRSCoords(0, 0)
c2 = ICRSCoords(deg2rad(1), 0)

# interface
@test @inferred position_angle(c1, c2) @inferred position_angle(c1, c2 |> GalCoords)
@test position_angle(c1, c2) == position_angle(c1, c2 |> GalCoords)

# accuracy
@test position_angle(c1, c2) π / 2

c3 = ICRSCoords(deg2rad(1), deg2rad(0.1))
@test position_angle(c1, c3) < π / 2

c4 = ICRSCoords(0, deg2rad(1))
@test position_angle(c1, c4) 0

# types
for T in [ICRSCoords, GalCoords, FK5Coords{2000}]
c1 = T(0, 0)
c2 = T(deg2rad(1), 0)
@test position_angle(c1, c2) π / 2
end
end

0 comments on commit 27b4ece

Please sign in to comment.