sg2

package module
v0.0.0-...-9a19e3a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: LGPL-3.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sg2 (Go)

A small, pure-Go implementation of the solar-position portion of Solar Geometry 2 (SG2), returning topocentric azimuth and geometric altitude.

API

func SunPosition(point geom.Point, instant time.Time) (azimuth, altitude float64)

Coordinates follow the go-geom XY/XYZ convention:

  • X: longitude, degrees east, in [-180, 180].
  • Y: latitude, degrees north, in [-90, 90].
  • Z (optional): ellipsoidal height in metres; omitted Z is treated as zero.
  • time.Time: an absolute instant. The calculation converts it to UTC.

The return values are degrees. Azimuth is clockwise from true north in [0, 360). Altitude is the geometric topocentric altitude, before atmospheric refraction. This matches the core SG2 topoc.gamma_S0 result; the separate pressure/temperature refraction correction is intentionally not part of this one-function API.

The function has no error result. An empty point or invalid coordinate returns (math.NaN(), math.NaN()).

Example

package main

import (
    "fmt"
    "time"

    "github.com/twpayne/go-geom"
    "github.com/example/sg2go"
)

func main() {
    p := *geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{2.3522, 48.8566})
    azimuth, altitude := sg2.SunPosition(p, time.Date(2024, 6, 21, 12, 0, 0, 0, time.UTC))
    fmt.Printf("azimuth %.3f°, altitude %.3f°\\n", azimuth, altitude)
}

Replace the example module path in the import statement with the module path you use for this package.

Implementation and validation

The implementation evaluates SG2's ten-term fitted heliocentric-longitude series directly; it does not use the optional precomputed daily interpolation tables and has no CGo dependency. It follows the corrected sidereal-time coefficient (6.300388099) and the corrected Julian-day / topocentric-hour-angle expressions present in the reference implementation. The journal article contains typos in some equations; the solposx reference implementation explicitly documents corrections.

The published error bound is around 0.003 degrees for the 1980–2030 interval. Outside that interval the function still evaluates, but that accuracy claim should not be assumed.

Run tests with:

go test ./...

TestAgainstPythonSG2C automatically runs three comparisons against solposx.solarposition.sg2_c, which wraps the official SG2 C library, when both solposx and sg2 are installed in the active Python environment. Otherwise that integration test is skipped. Install the optional reference packages using python3 -m pip install solposx sg2.

License

This translation is distributed under LGPL-3.0-only. See LICENSE. SG2 is based on Ph. Blanc and L. Wald, “The SG2 algorithm for a fast and accurate computation of the position of the Sun for multi-decadal time period”, Solar Energy 86 (2012), 3072–3083, https://doi.org/10.1016/j.solener.2012.07.018.

Documentation

Overview

Package sg2 computes the topocentric geometric position of the Sun using the Solar Geometry 2 (SG2) algorithm of Blanc and Wald (2012).

Geographic points use the conventional GIS axis order: X is longitude and Y is latitude, both in decimal degrees. If the point has a Z coordinate it is interpreted as ellipsoidal height in metres; otherwise height is zero.

SunPosition returns azimuth clockwise from true north in [0, 360) degrees and geometric altitude in degrees (positive above the horizon). Altitude is not corrected for atmospheric refraction, matching the core SG2 output (topocentric gamma_S0). The supplied time is interpreted as an absolute instant and converted to UTC; UTC approximates UT1 for this implementation.

SG2's published accuracy is approximately 0.003 degrees over 1980–2030. The direct trigonometric series is evaluated without the optional precomputed daily interpolation tables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SunPosition

func SunPosition(point geom.Point, instant time.Time) (azimuth, altitude float64)

SunPosition returns the Sun's azimuth and geometric altitude, in degrees, for a geographic point and an absolute time.

The geom.Point ordinates are longitude (X), latitude (Y), and optional ellipsoidal height in metres (Z). Empty points, non-finite coordinates, or coordinates outside the geographic ranges return (NaN, NaN). Longitude is restricted to [-180, 180] and latitude to [-90, 90].

No error can be returned because the requested API has no error result.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址