GliderToolsCommunity / GliderTools

A toolkit for processing Seaglider base station NetCDF files: despiking, smoothing, outlier detection, backscatter, fluorescence quenching, calibration, gridding, interpolation.
https://glidertools.readthedocs.io
GNU Affero General Public License v3.0
68 stars 28 forks source link

Issue with sunrise and sunset calculations #202

Open tjryankeogh opened 5 months ago

tjryankeogh commented 5 months ago

There is an issue in the calculation of sunrise and sunset.

For some longitudes >~70E the sunrise is later than sunset.

Example data:

'2021-03-06' lat = 20.2 lon = 154.7

sunrise = '2021-03-06T19:56:00.000000' sunset = '2021-03-06T07:47:00.000000'

If we change lon to 65E then there is no issue:

sunrise = '2021-03-06T01:57:00.000000' sunset = '2021-03-06T13:47:00.000000'

MartinMohrmann commented 5 months ago

Welcome and thank you for reporting the issue! I just had a quick look at the sunset_sunrise function used in GliderTools, and I can see that it returns the results in "Coordinated universal time" (UTC). Your coordinates lie in a time zone that is UTC+10, which means to get the local time you would have to add 10 h to the result (or use some inbuild python time zone conversion magic possibly). I have not (yet) checked if the time zones are handled correctly in related functions such as the quenching correction. So feel free to report back if you suspect a bug somewhere else. It could be a good idea to above information to the function documentation.

tjryankeogh commented 5 months ago

But if you add in the timezone hours, +10h, you would still need to add to both sunrise and sunset and therefore sunrise would still technically be after sunset. I have checked this with the Astral package as well (the original package used in GliderTools for sunrise/sunset calculations) and the issue persists there as well. I believe it is an issue with the almanacs used by both Skyfield and Astral. But perhaps we need a fix that says if sunrise is greater sunset that the date should be adjusted. The day and night batches in the quenching correction rely on the sunrise being less than sunset to correctly group the profiles.

MartinMohrmann commented 5 months ago

Ah, I see. This means we will need a patch to make GliderTools quenching correction work at your longitudes, preferably directly in our sunset_sunrise function or even upstream in the skyfield/astral packages if a bug exists there. In case your find a good universal solution, please share it. Either here or as a pull request. :) (Contribution Guide).

MartinMohrmann commented 4 months ago

I addressed this issue in pull request #204, branch fix_#202. Feel free to use and comment/test.