Skip to content

sixdouglas/suncalc

Repository files navigation

SunCalc

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).

Usage example

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)
}

Reference

Sunlight times

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

sunrise (top edge of the sun appears on the horizon)

sunriseEnd

sunrise ends (bottom edge of the sun touches the horizon)

goldenHourEnd

morning golden hour (soft light, best time for photography) ends

solarNoon

solar noon (sun is in the highest position)

goldenHour

evening golden hour starts

sunsetStart

sunset starts (bottom edge of the sun touches the horizon)

sunset

sunset (sun disappears below the horizon, evening civil twilight starts)

dusk

dusk (evening nautical twilight starts)

nauticalDusk

nautical dusk (evening astronomical twilight starts)

night

night starts (dark enough for astronomical observations)

nadir

nadir (darkest moment of the night, sun is in the lowest position)

nightEnd

night ends (morning astronomical twilight starts)

nauticalDawn

nautical dawn (morning nautical twilight starts)

dawn

dawn (morning nautical twilight ends, morning civil twilight starts)

Sun position

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 and PI/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 Math.PI * 3/4 is northwest

Moon position

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

Moon illumination

suncalc.GetMoonIllumination(timeAndDate time.Time)

Returns an object with the following properties:

  • 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:

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).

Moon rise and set times

suncalc.GetMoonTimes(date time.Time, latitude float64, longitude float64[, inUTC bool])

Returns an object with the following properties:

  • Rise: moonrise time as Date

  • Set: moonset time as Date

  • 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.

Changelog

1.1.0 - Mai 23, 2020

  • 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 be nil

  • DayTime structure members changed. MorningName becomes Name and Time becomes Value of type sql.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

1.0.2 - Mai 21, 2019

  • Finish documentation.

  • Make sure all struct fields are visible from outside.

1.0.1 - Mai 18, 2019

  • Place SunCalc in his own package.

  • Make some struct variables visible from outside.

1.0.0 - Mai 18, 2019

  • First commit.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages