sixdouglas / suncalc

Other
26 stars 2 forks source link

sunrise - every library has its own results? #11

Closed simonbuehler closed 1 month ago

simonbuehler commented 1 month ago

hi,

i used your code and did a quick compare with suncalc.org (should be suncalc js) - but the results were off, so i searched further and what i encountered was a real mess:

grafik this is (from left to right)

timeanddate.com sonnenuntergang.de google suncalc.org

and my test script:

package main

import (
    "fmt"
    "github.com/sixdouglas/suncalc"
    "math"
    "text/tabwriter"
    "time"
    "os"
)

func main() {
    // Specify time zone (e.g., "Europe/Berlin")
    location, err := time.LoadLocation("Europe/Berlin")
    if err != nil {
        fmt.Println("Error loading time zone:", err)
        return
    }

    // Get the current time in the specified time zone
    var now = time.Now().In(location)

    // Location coordinates for your specified lat/long
    lat, long :=  52.5234051, 13.4113999

    // Option 1: Get solar times without an observer
    fmt.Println("Comparing Solar Times With and Without Observer:")

    // Observer setup
    observer := suncalc.Observer{
        Latitude:  lat,
        Longitude: long,
        Height:    402, // Höhe des Beobachters
        Location:  location,
    }

    // Get times with and without observer
    timesWithoutObserver := suncalc.GetTimes(now, lat, long)
    timesWithObserver := suncalc.GetTimesWithObserver(now, observer)

    // Tabwriter for formatted output
    writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.AlignRight|tabwriter.Debug)
    fmt.Fprintln(writer, "Event\tWithout Observer\tWith Observer")

    // Loop through all available daytime names and print them side by side
    for _, timeOfDay := range suncalc.DayTimeNames {
        timeWithoutObserver := timesWithoutObserver[timeOfDay].Value
        timeWithObserver := timesWithObserver[timeOfDay].Value
        if !timeWithoutObserver.IsZero() || !timeWithObserver.IsZero() {
            fmt.Fprintf(writer, "%-13s\t%s\t%s\n",
                string(timeOfDay),
                formatTime(timeWithoutObserver),
                formatTime(timeWithObserver),
            )
        }
    }
    writer.Flush()

    // Sun position comparison for sunrise and sunset
    fmt.Println("\nSun Position Comparison at Sunrise and Sunset:")
    fmt.Println("Without Observer:")
    printSunPosition(timesWithoutObserver[suncalc.Sunrise].Value, lat, long, "Sunrise")
    printSunPosition(timesWithoutObserver[suncalc.Sunset].Value, lat, long, "Sunset")

    fmt.Println("\nWith Observer:")
    printSunPosition(timesWithObserver[suncalc.Sunrise].Value, lat, long, "Sunrise")
    printSunPosition(timesWithObserver[suncalc.Sunset].Value, lat, long, "Sunset")

    // Current sun position
    fmt.Println("\nCurrent Sun Position:")
    printSunPosition(now, lat, long, "Now")
}

// Function to print the sun position at a specific time
func printSunPosition(t time.Time, lat, long float64, label string) {
    pos := suncalc.GetPosition(t, lat, long)
    azimuth := pos.Azimuth * 180 / math.Pi
    altitude := pos.Altitude * 180 / math.Pi
    fmt.Printf("%s - Azimuth: %f deg, Altitude: %f deg\n", label, azimuth, altitude)
}

// Helper function to format time for output
func formatTime(t time.Time) string {
    if t.IsZero() {
        return "N/A"
    }
    return t.Format("2006-01-02 15:04:05")
}

can anyone shed some lights on this - are this rounding errors, different float precisions ? whats the "real" sunrise for 52.5234051, 13.4113999 (I took Berlin) at 9.September 2024 ?

edit: grafik google was one day off

simonbuehler commented 1 month ago

closing as js an go versions do give the same results