Skip to content

Commit

Permalink
add moon calculations (#4)
Browse files Browse the repository at this point in the history
* add `getSunPosition` method

* add `getMoonPosition` method

* add `getMoonIllumination` method

* update docs
  • Loading branch information
g-rppl authored Sep 8, 2023
1 parent 1a02de1 commit d5dfb90
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 124 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
push:
branches:
- main
- dev
tags: ['*']
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
Expand Down Expand Up @@ -34,4 +34,21 @@ jobs:
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
token: ${{ secrets.CODECOV_TOKEN }}
docs:
name: Documentation
runs-on: ubuntu-latest
permissions:
contents: write
statuses: write
steps:
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
with:
version: '1'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: julia --project=docs/ docs/make.jl
25 changes: 0 additions & 25 deletions .github/workflows/documentation.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SunCalc"
uuid = "46d852a4-2fba-44bf-a445-ee38df28a2fc"
authors = ["Georg Rüppel <g-rppl@posteo.de>"]
version = "1.1.0"
version = "1.2.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

## About

Julia implementation of the [SunCalc](https://github.com/mourner/suncalc/) package for calculating sun position and sunlight phases
(times for sunrise, sunset, dusk, etc.) for the given location and time.
Julia implementation of the [SunCalc](https://github.com/mourner/suncalc/) package for
calculating sun position, sunlight phases (times for sunrise, sunset, dusk, etc.),
moon position, and lunar phase for the given location and time for the given location and time.

Most calculations are based on the formulas given in the Astronomy Answers articles
about [position of the sun](https://www.aa.quae.nl/en/reken/zonpositie.html)
Expand All @@ -20,7 +21,10 @@ in the [Twilight article on Wikipedia](https://en.wikipedia.org/wiki/Twilight).

## Installation

The package is registered in the [General registry](https://github.com/JuliaRegistries/General)
and can therefore be installed with:

```julia
using Pkg
pkg.add("SunCalc")
```
```
1 change: 1 addition & 0 deletions docs/src/assets/logo.SVG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions docs/src/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ SunCalc.getSunPosition
## Sun light times
```@docs
SunCalc.getSunlightTimes
```

## Moon position
```@docs
SunCalc.getMoonPosition
```

## Moon illumination
```@docs
SunCalc.getMoonIllumination
```
8 changes: 6 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## About

Julia implementation of the [SunCalc](https://github.com/mourner/suncalc/) package for calculating sun position and sunlight phases
(times for sunrise, sunset, dusk, etc.) for the given location and time.
Julia implementation of the [SunCalc](https://github.com/mourner/suncalc/) package for
calculating sun position, sunlight phases (times for sunrise, sunset, dusk, etc.),
moon position, and lunar phase for the given location and time for the given location and time.

Most calculations are based on the formulas given in the Astronomy Answers articles
about [position of the sun](https://www.aa.quae.nl/en/reken/zonpositie.html)
Expand All @@ -11,6 +12,9 @@ in the [Twilight article on Wikipedia](https://en.wikipedia.org/wiki/Twilight).

## Installation

The package is registered in the [General registry](https://github.com/JuliaRegistries/General)
and can therefore be installed with:

```julia
using Pkg
pkg.add("SunCalc")
Expand Down
9 changes: 6 additions & 3 deletions src/SunCalc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ using Dates
using TimeZones
using DataFrames

export getSunlightTimes, getSunPosition
export getSunPosition, getSunlightTimes
export getMoonPosition, getMoonIllumination

include("utilis.jl")

include("sunlightTimes.jl")
include("sunPosition.jl")
include("sunlightTimes.jl")
include("moonPosition.jl")
include("moonIllumination.jl")

end # module
end
72 changes: 72 additions & 0 deletions src/moonIllumination.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

"""
getMoonIllumination(
time::Union{Date,DateTime,ZonedDateTime};
keep=[:fraction, :phase, :angle])
Calculate moon illumination for the given time. Return a `NamedTuple` or `DataFrame`.
Available variables:
| Variable | Description |
| :--------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `fraction` | Illuminated fraction of the moon; varies from 0.0 (new moon) to 1.0 (full moon) |
| `phase` | Moon phase; varies from 0.0 to 1.0, described below |
| `angle` | Midpoint angle in radians of the illuminated limb of the moon reckoned eastward from the north point of the disk; the moon is waxing if the angle is negative, and waning if positive | |
Moon phase value should be interpreted like this:
- `0`: New Moon
- *Waxing Crescent*
- `0.25`: First Quarter
- *Waxing Gibbous*
- `0.5`: Full Moon
- *Waning Gibbous*
- `0.75`: Last Quarter
- *Waning Crescent*
# Examples
```julia
using Dates, SunCalc
getMoonIllumination(Date(2000, 07, 01))
getMoonIllumination(now(), keep=[:fraction])
```
"""
function getMoonIllumination(
time::Union{Date,DateTime,ZonedDateTime};
keep=[:fraction, :phase, :angle])

available_var = [:fraction, :phase, :angle]

@assert all(keep .∈ [available_var]) "
$(keep[Not(keep .∈ [available_var])]) is not a valid variable."

if isa(time, ZonedDateTime)
time = DateTime(time, UTC)
end

data = moonIllumination(DateTime(time))

return data[keep]
end

function getMoonIllumination(
time::Union{Vector{Date},Vector{DateTime},Vector{ZonedDateTime}};
keep=[:fraction, :phase, :angle])

available_var = [:fraction, :phase, :angle]

@assert all(keep .∈ [available_var]) "
$(keep[Not(keep .∈ [available_var])]) is not a valid variable."

time = map(x -> isa(x, ZonedDateTime) ? DateTime(x, UTC) : x, time)

data = DataFrame(time=time)

ill = DataFrame(moonIllumination.(DateTime.(data.time)))

data = hcat(data, ill[:, keep])

return data
end
70 changes: 70 additions & 0 deletions src/moonPosition.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

"""
getMoonPosition(
time::Union{DateTime,ZonedDateTime},
lat::Real,
lon::Real;
keep=[:altitude, :azimuth, :distance, :parallacticAngle])
Calculate the sun position for the given time and location. Return a `NamedTuple`
or `DataFrame`.
Available variables:
| Variable | Description |
| :----------------- | :----------------------------------------- |
| `altitude` | Moon altitude above the horizon in radians |
| `azimuth` | Moon azimuth in radians |
| `distance` | Distance to moon in kilometres |
| `parallacticAngle` | Parallactic angle of the moon in radians |
# Examples
```julia
using Dates, SunCalc
getMoonPosition(DateTime(2000, 07, 01, 12, 00, 00), 54, 9)
getMoonPosition(now(), 54, 9; keep=[:altitude])
```
"""
function getMoonPosition(
time::Union{DateTime,ZonedDateTime},
lat::Real,
lon::Real;
keep=[:altitude, :azimuth, :distance, :parallacticAngle])

available_var = [:altitude, :azimuth, :distance, :parallacticAngle]

@assert all(keep .∈ [available_var]) "
$(keep[Not(keep .∈ [available_var])]) is not a valid variable."

if isa(time, ZonedDateTime)
time = DateTime(time, UTC)
end

data = moonPosition(time, lat, lon)

return data[keep]
end

function getMoonPosition(
time::Union{Vector{DateTime},Vector{ZonedDateTime}},
lat::Real,
lon::Real;
keep=[:altitude, :azimuth, :distance, :parallacticAngle])

available_var = [:altitude, :azimuth, :distance, :parallacticAngle]

@assert all(keep .∈ [available_var]) "
$(keep[Not(keep .∈ [available_var])]) is not a valid variable."

time = map(x -> isa(x, ZonedDateTime) ? DateTime(x, UTC) : x, time)

data = DataFrame(time=time, lat=lat, lon=lon)

pos = DataFrame(moonPosition.(data.time, data.lat, data.lon))

data = hcat(data, pos[:, keep])

return data
end
33 changes: 20 additions & 13 deletions src/sunPosition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ Calculate the sun position for the given time and location. Return a `NamedTuple
Available variables:
| Variable | Description |
| :-------- | :-------------------------------------------------------------------------------------------------------------------------- |
|`altitude` | sun altitude above the horizon in radians, e.g. 0 at the horizon and π/2 at the zenith (straight over your head). |
|`azimuth` | sun azimuth in radians (direction along the horizon, measured from south to west), e.g. 0 is south and π * 3/4 is northwest.|
| Variable | Description |
| :--------- | :-------------------------------------------------------------------------------------------------------------------------- |
| `altitude` | Sun altitude above the horizon in radians, e.g. 0 at the horizon and π/2 at the zenith (straight over your head) |
| `azimuth` | Sun azimuth in radians (direction along the horizon, measured from south to west), e.g. 0 is south and π * 3/4 is northwest |
# Examples
```jldoctest
julia> using Dates, SunCalc
julia> getSunPosition(DateTime(2000, 07, 01, 12, 00, 00), 54, 9.0)
(altitude = 1.021444013872015, azimuth = 0.23904867335099955)
```julia
using Dates, SunCalc
getSunPosition(DateTime(2000, 07, 01, 12, 00, 00), 54, 9)
getSunPosition(now(), 54, 9; keep=[:altitude])
```
"""
function getSunPosition(time::Union{DateTime,ZonedDateTime}, lat::Real, lon::Real;
function getSunPosition(
time::Union{DateTime,ZonedDateTime},
lat::Real,
lon::Real;
keep=[:altitude, :azimuth])

available_var = [:altitude, :azimuth]
Expand All @@ -35,13 +39,16 @@ function getSunPosition(time::Union{DateTime,ZonedDateTime}, lat::Real, lon::Rea
time = DateTime(time, UTC)
end

data = getPosition(time, lat, lon)
data = sunPosition(time, lat, lon)

return data[keep]
end

function getSunPosition(time::Union{Vector{DateTime},Vector{ZonedDateTime}},
lat::Real, lon::Real; keep=[:altitude, :azimuth])
function getSunPosition(
time::Union{Vector{DateTime},Vector{ZonedDateTime}},
lat::Real,
lon::Real;
keep=[:altitude, :azimuth])

available_var = [:altitude, :azimuth]

Expand All @@ -52,7 +59,7 @@ function getSunPosition(time::Union{Vector{DateTime},Vector{ZonedDateTime}},

data = DataFrame(time=time, lat=lat, lon=lon)

pos = DataFrame(getPosition.(data.time, data.lat, data.lon))
pos = DataFrame(sunPosition.(data.time, data.lat, data.lon))

data = hcat(data, pos[:, keep])

Expand Down
Loading

2 comments on commit d5dfb90

@g-rppl
Copy link
Owner Author

@g-rppl g-rppl commented on d5dfb90 Sep 8, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/91062

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" d5dfb90c4386155eeaba5cd1fa424c6888377dea
git push origin v1.2.0

Please sign in to comment.