SunCalc is a tiny BSD-licensed Golang library for calculating sun position, sunlight phases (times for sunrise, sunset, dusk, etc.), moon position and lunar phase for the given location and time, created originally in Javascript by [Vladimir Agafonkin](http://agafonkin.com/en) ([@mourner](https://github.com/mourner)) as a part of the [SunCalc.net project](http://suncalc.net).
Translated in GOLANG by Douglas Six
Most calculations are based on the formulas given in the excellent Astronomy Answers articles about [position of the sun](http://aa.quae.nl/en/reken/zonpositie.html) and [the planets](http://aa.quae.nl/en/reken/hemelpositie.html). You can read about different twilight phases calculated by SunCalc in the [Twilight article on Wikipedia](http://en.wikipedia.org/wiki/Twilight).
package main
import (
"fmt"
"github.com/pschou/go-suncalc"
"math"
"time"
)
func main() {
var now = time.Now()
// get today's sunlight times for London
lat, long := 51.5, -0.1
// get the times for today, latitude, longitude, height below or above the
// horizon, and in timezone
var times = suncalc.GetTimes(now, lat, long, 0, time.UTC)
for _, oneTime := range times {
fmt.Printf("%-13s %d-%02d-%02d %02d:%02d:%02d\n", string(oneTime.Name),
oneTime.Value.Time.Year(), oneTime.Value.Time.Month(), oneTime.Value.Time.Day(),
oneTime.Value.Time.Hour(), oneTime.Value.Time.Minute(), oneTime.Value.Time.Second())
}
// format sunrise time from the Date object
fmt.Printf("Sunrise / Sunset time: %d:%02d / %d:%02d\n",
times[suncalc.Sunrise].Value.Time.Hour(), times[suncalc.Sunrise].Value.Time.Minute(),
times[suncalc.Sunset].Value.Time.Hour(), times[suncalc.Sunset].Value.Time.Minute(),
)
// get position of the sun (azimuth and altitude) at today's sunrise
var sunrisePos = suncalc.GetPosition(times[suncalc.Sunrise].Value.Time, lat, long)
// get sunrise azimuth in degrees
var sunriseAzimuth = sunrisePos.Azimuth * 180 / math.Pi
fmt.Printf("Sunrise Azimuth: %f deg\n", sunriseAzimuth)
// get position of the sun (azimuth and altitude) at today's sunset
var sunsetPos = suncalc.GetPosition(times[suncalc.Sunset].Value.Time, lat, long)
// get the sunset azimuth in degrees
var sunsetAzimuth = sunsetPos.Azimuth * 180 / math.Pi
fmt.Printf("Sunset Azimuth: %f deg\n", sunsetAzimuth)
// get current position of the sun (azimuth and altitude) at today's sunrise
var sunPos = suncalc.GetPosition(now, lat, long)
fmt.Printf("Sun Azimuth: %f deg\n", sunPos.Azimuth*180/math.Pi)
fmt.Printf("Sun Altitude: %f deg\n", sunPos.Altitude*180/math.Pi)
}
suncalc.GetTimes(date time.Time, latitude float64, longitude float64, height float64, location *time.Location)
Returns an object with the following properties (each is a Date
object):
Property |
Description |
---|---|
|
sunrise (top edge of the sun appears on the horizon) |
|
sunrise ends (bottom edge of the sun touches the horizon) |
|
morning golden hour (soft light, best time for photography) ends |
|
solar noon (sun is in the highest position) |
|
evening golden hour starts |
|
sunset starts (bottom edge of the sun touches the horizon) |
|
sunset (sun disappears below the horizon, evening civil twilight starts) |
|
dusk (evening nautical twilight starts) |
|
nautical dusk (evening astronomical twilight starts) |
|
night starts (dark enough for astronomical observations) |
|
nadir (darkest moment of the night, sun is in the lowest position) |
|
night ends (morning astronomical twilight starts) |
|
nautical dawn (morning nautical twilight starts) |
|
dawn (morning nautical twilight ends, morning civil twilight starts) |
suncalc.GetPosition(timeAndDate time.Time, latitude float64, longitude float64)
Returns an object with the following properties:
-
Altitude
: sun altitude above the horizon in radians, e.g.0
at the horizon andPI/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 andMath.PI * 3/4
is northwest
suncalc.GetMoonPosition(date time.Time, latitude float64, longitude float64)
Returns an object with the following properties:
-
Altitude
: moon altitude above the horizon in radians -
Azimuth
: moon azimuth in radians -
Distance
: distance to moon in kilometers -
ParallacticAngle
: parallactic angle of the moon in radians
suncalc.GetMoonIllumination(timeAndDate time.Time)
Returns an object with the following properties:
-
Fraction
: illuminated fraction of the moon; varies from0.0
(new moon) to1.0
(full moon) -
Phase
: moon phase; varies from0.0
to1.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:
Phase |
Name |
---|---|
0 |
New Moon |
Waxing Crescent |
|
0.25 |
First Quarter |
Waxing Gibbous |
|
0.5 |
Full Moon |
Waning Gibbous |
|
0.75 |
Last Quarter |
Waning Crescent |
By subtracting the parallacticAngle
from the angle
one can get the zenith angle of the moons bright limb (anticlockwise).
The zenith angle can be used do draw the moon shape from the observers perspective (e.g. moon lying on its back).
suncalc.GetMoonTimes(date time.Time, latitude float64, longitude float64[, inUTC bool])
Returns an object with the following properties:
-
Rise
: moonrise time asDate
-
Set
: moonset time asDate
-
AlwaysUp
:true
if the moon never rises/sets and is always above the horizon during the day -
AlwaysDown
:true
if the moon is always below the horizon
By default, it will search for moon rise and set during local user’s day (from 0 to 24 hours).
If inUTC
is set to true, it will instead search the specified date from 0 to 24 UTC hours.
-
suncalc.GetTimes()
now takes two additional parameters:-
height
: positive elevation position. Can be set to 0, if not known. -
location
: time.Location for the result Time. It can benil
-
-
DayTime
structure members changed.MorningName
becomesName
andTime
becomesValue
of typesql.NullTime
to have the valuable information about the validity of the date. In fact, in some places and time some values are not possible: close to the polars the sun does not set in summer and does not rise in winter. -
Add some unit tests
-
Update Documentation
-
Place SunCalc in his own package.
-
Make some struct variables visible from outside.