onekiloparsec / SwiftAA

The most comprehensive collection of accurate astronomical algorithms in (C++, Objective-C and) Swift.
http://www.onekiloparsec.dev/
MIT License
171 stars 31 forks source link

Calculating the rise and set times for a star #95

Closed stefanengel closed 3 years ago

stefanengel commented 4 years ago

Hi there,

thank you for this awesome framework! Is it possible to use SwiftAA to calculate when a star will rise and set for a given date and location on earth?

I tried the following:

        let star = AstronomicalObject(
            name: "Sirius",
            coordinates: EquatorialCoordinates(
                rightAscension: Hour(.plus, 6, 45, 9.25),
                declination: Degree(.minus, 16, 42, 47.3)
            ),
            julianDay: JulianDay(Date())
        )
        let result = star.riseTransitSetTimes(
            for: GeographicCoordinates(
                positivelyWestwardLongitude: Degree(.plus, 7, 46, 42),
                latitude: Degree(.plus, 49, 9, 3),
                altitude: 210)
        )

Unfortunately it doesn't work since riseTransitSetTimes will call the unimplemented initializer of AstronomicalObject and cause a fatalError. Is there another way to get the rise and set times of a star?

onekiloparsec commented 4 years ago

Oh that's a very good point. Indeed AstronomicalObject conforms to the CelestialBody protocol, which gives access to the riseTransitSetTimes function. This is an inconsistency, and I thank you for pointing it out!

Now, the computation of Rise and Set times of a planet is based on an interpolation between 3 days where coordinates are not the same. This condition is not satisfied by an Astronomical object...

Hence, one must search a bit a solution.

onekiloparsec commented 3 years ago

@stefanengel I have implemented a solution (which also homogenize the return types of twilights).

But I would rather prefer make a release with unit tests validating the result. I couldn't find any reliable source so far. Do you have, for a given location on Earth, (fairly) precise of expected rise and set times (< 1 minute) of an object?

onekiloparsec commented 3 years ago

Note that I've pushed my code, and now the project includes a Swift Playground. Your example is inside it. You can use it to test the results.

stefanengel commented 3 years ago

@onekiloparsec thank you for the solution! :)

Unfortunately I do not have examples with highly accurate rise and set times, I use Stellarium most of the time. The playground example gives me plausible results as far as I can tell.

stefanengel commented 3 years ago

Testing a bit further I found that for polaris I do not get rise and set times for my current location (which is expected) but there is no transitError as well:

let polaris = AstronomicalObject(
    name: "Polaris",
    coordinates: EquatorialCoordinates(rightAscension: Hour(.plus, 2, 31, 47.08), declination: Degree(.plus, 89, 15, 50.9)
    ),
    julianDay: JulianDay(year: 2020, month: 9, day: 6)
)
let results = polaris.riseTransitSetTimes(
    for: GeographicCoordinates(
        positivelyWestwardLongitude: Degree(.plus, 7, 46, 42),
        latitude: Degree(.plus, 49, 9, 3),
        altitude: 210)
)

If I understand the api correctly there should be a CelestialBodyTransitError of type alwaysAboveAltitude?

onekiloparsec commented 3 years ago

Hello @stefanengel You are perfectly right. This mistake is due to a inconsistency in using the RiseTransitSetTimes in the Earth twilights function. I have corrected the code. I've added a test, and will try to add some more, then I'll make a release with all these fixed.

onekiloparsec commented 3 years ago

SwiftAA v2.3 released, with updated interface for tiwlights and rise, set, times. As far as I can tell, I can now close this issue. Feel free to open a new one if you think something's still missing / incorrect.