hablullah / go-sampa

Package for calculating Sun and Moon position
MIT License
9 stars 0 forks source link

No moon events when it does not pass the meridian #1

Open qaribhaider opened 1 month ago

qaribhaider commented 1 month ago

Hello Team 👋

We are facing issues while trying to get moon events for the dates mainly where the Moon does not pass the meridian.

Here is a replication example:

func main() {
    // Prepare location
    tz, err := time.LoadLocation("Europe/Berlin")
    if err != nil {
        fmt.Println("Error loading timezone:", err)
        return
    }
    place := sampa.Location{Latitude: 51.067, Longitude: 13.103}

    // Fetch Moon events
    dt := time.Date(2024, 05, 22, 0, 0, 0, 0, tz)
    moonEvents, err := sampa.GetMoonEvents(dt, place, nil)
    if err != nil {
        fmt.Println("Error fetching moon events:", err)
        return
    }

    // Print the moon events
    fmt.Println("Moon Events:", moonEvents)
}

This outputs:

Moon Events: {{0001-01-01 00:00:00 +0000 UTC 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } {0001-01-01 00:00:00 +0000 UTC 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } {0001-01-01 00:00:00 +0000 UTC 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 } map[]}

Can you please look into this and advice a possible solution?

The moon rise and set events are available for the same date on timeanddate.com

Screenshot 2024-05-23 at 4 06 31 PM
RadhiFadlillah commented 5 days ago

Hi Qarib, sorry for late response.

In this package, the events are calculated based on transit time. So, when we ask it to calculate events for, say, 3 July, the calculation steps are:

  1. User want data for 3 July 2024.
  2. Calculate transit time that occurred in 3 July 2024.
  3. Calculate rise and set time from the transit time.

So, rise and set time are based on transit time, not based on the date. This is why there are days when rise is occurred in previous day and set is occurred in the next day.

To illustrate it, let's modify the code to calculate for entire month:

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/hablullah/go-sampa"
    "github.com/markkurossi/tabulate"
)

func main() {
    // Prepare location
    tz, err := time.LoadLocation("Europe/Berlin")
    if err != nil {
        fmt.Println("Error loading timezone:", err)
        return
    }
    place := sampa.Location{Latitude: 51.067, Longitude: 13.103}

    // Prepare table
    tab := tabulate.New(tabulate.ASCII)
    tab.Header("Date")
    tab.Header("Moon rise")
    tab.Header("Transit")
    tab.Header("Moon set")

    // Calculate Moon events
    start := time.Date(2024, 5, 1, 0, 0, 0, 0, tz)
    finish := start.AddDate(0, 1, 0)

    for dt := start; dt.Before(finish); dt = dt.AddDate(0, 0, 1) {
        e, err := sampa.GetMoonEvents(dt, place, nil)
        if err != nil {
            fmt.Println("Error fetching moon events:", err)
            return
        }

        // Put the event to table
        row := tab.Row()
        row.Column(dt.Format("02 Jan 2006"))
        row.Column(strTime(e.Moonrise.DateTime))
        row.Column(strTime(e.Transit.DateTime))
        row.Column(strTime(e.Moonset.DateTime))
    }

    // Print the table
    tab.Print(os.Stdout)
}

func strTime(dt time.Time) string {
    if dt.IsZero() {
        return ""
    }

    return dt.Format("02 Jan 15:04:05 MST")
}

And here is its output:

+-------------+----------------------+----------------------+----------------------+
| Date        | Moon rise            | Transit              | Moon set             |
+-------------+----------------------+----------------------+----------------------+
| 01 May 2024 | 01 May 03:19:30 CEST | 01 May 07:16:10 CEST | 01 May 11:22:37 CEST |
| 02 May 2024 | 02 May 03:42:23 CEST | 02 May 08:10:10 CEST | 02 May 12:50:34 CEST |
| 03 May 2024 | 03 May 03:59:49 CEST | 03 May 09:01:42 CEST | 03 May 14:18:31 CEST |
| 04 May 2024 | 04 May 04:14:17 CEST | 04 May 09:51:33 CEST | 04 May 15:45:52 CEST |
| 05 May 2024 | 05 May 04:27:32 CEST | 05 May 10:40:56 CEST | 05 May 17:13:26 CEST |
| 06 May 2024 | 06 May 04:41:03 CEST | 06 May 11:31:10 CEST | 06 May 18:42:25 CEST |
| 07 May 2024 | 07 May 04:56:21 CEST | 07 May 12:23:30 CEST | 07 May 20:13:15 CEST |
| 08 May 2024 | 08 May 05:15:25 CEST | 08 May 13:18:43 CEST | 08 May 21:44:16 CEST |
| 09 May 2024 | 09 May 05:41:04 CEST | 09 May 14:16:47 CEST | 09 May 23:10:35 CEST |
| 10 May 2024 | 10 May 06:17:06 CEST | 10 May 15:16:34 CEST | 11 May 00:24:50 CEST |
| 11 May 2024 | 11 May 07:07:04 CEST | 11 May 16:15:59 CEST | 12 May 01:21:38 CEST |
| 12 May 2024 | 12 May 08:11:05 CEST | 12 May 17:12:48 CEST | 13 May 02:01:30 CEST |
| 13 May 2024 | 13 May 09:24:15 CEST | 13 May 18:05:34 CEST | 14 May 02:28:54 CEST |
| 14 May 2024 | 14 May 10:40:16 CEST | 14 May 18:53:53 CEST | 15 May 02:48:19 CEST |
| 15 May 2024 | 15 May 11:55:02 CEST | 15 May 19:38:14 CEST | 16 May 03:02:55 CEST |
| 16 May 2024 | 16 May 13:07:15 CEST | 16 May 20:19:37 CEST | 17 May 03:14:43 CEST |
| 17 May 2024 | 17 May 14:17:17 CEST | 17 May 20:59:08 CEST | 18 May 03:25:04 CEST |
| 18 May 2024 | 18 May 15:26:17 CEST | 18 May 21:37:59 CEST | 19 May 03:34:57 CEST |
| 19 May 2024 | 19 May 16:35:44 CEST | 19 May 22:17:20 CEST | 20 May 03:45:17 CEST |
| 20 May 2024 | 20 May 17:47:01 CEST | 20 May 22:58:20 CEST | 21 May 03:57:04 CEST |
| 21 May 2024 | 21 May 19:01:14 CEST | 21 May 23:42:06 CEST | 22 May 04:11:41 CEST |
| 22 May 2024 |                      |                      |                      |
| 23 May 2024 | 22 May 20:18:40 CEST | 23 May 00:29:37 CEST | 23 May 04:31:24 CEST |
| 24 May 2024 | 23 May 21:37:16 CEST | 24 May 01:21:23 CEST | 24 May 04:58:55 CEST |
| 25 May 2024 | 24 May 22:52:21 CEST | 25 May 02:17:08 CEST | 25 May 05:38:46 CEST |
| 26 May 2024 | 25 May 23:57:09 CEST | 26 May 03:15:32 CEST | 26 May 06:34:55 CEST |
| 27 May 2024 | 27 May 00:46:57 CEST | 27 May 04:14:31 CEST | 27 May 07:47:16 CEST |
| 28 May 2024 | 28 May 01:22:27 CEST | 28 May 05:11:58 CEST | 28 May 09:10:17 CEST |
| 29 May 2024 | 29 May 01:47:34 CEST | 29 May 06:06:33 CEST | 29 May 10:37:13 CEST |
| 30 May 2024 | 30 May 02:06:12 CEST | 30 May 06:58:05 CEST | 30 May 12:03:56 CEST |
| 31 May 2024 | 31 May 02:21:07 CEST | 31 May 07:47:15 CEST | 31 May 13:29:16 CEST |
+-------------+----------------------+----------------------+----------------------+

As you can see, on 22 May the Moon never reach the meridian, which means no transit, which means rise and set can't be calculated. However, the Moon rise and Moon set are actually occurred, but it's chained to yesterday and tomorrow events.

This output format might be a bit confusing compared to the one that provided by TimeAndDate, however one advantage of this format is consistency: the transit time will always occurred between rise and set, which is not really clear from TimeAndDate table:

image

So, as summary, this behavior is as intended, but I guess I need to update docs to make this behavior clear.

Feel free to mention me if there are still issues with this :smiley:

qaribhaider commented 10 hours ago

@RadhiFadlillah Thank you for taking time out to explain this in detail. The approach here seems to be different than how a user would generally perceive it, however as this is the intended one then feel free to close this issue. Thanks again :)