I think these other geolocation matters could be managed (up to the error in the model that Mathematica uses to determine sunrise/sunset).
The first change that I'd make would be to my previous suggestion (the second line),
location = $GeoLocationCity; (* set the location*)
tz = location["TimeZone"][[2]]; (* which timezone *)
locationname = location[[2, 1]]; (* name of the location for displaying purposes*)
so that the timezone is determined from the location (which could be provided either via $GeoLocationCity
, or otherwise specified (see for Tromso below).
Next, a helper function to "fix" the times for sunrise/sunset for areas outside of the tropics.
fixTropics[raw_] := Module[{dom, sunrise, sunset},
dom = raw[[1, 3]];(*actual day of month*)
sunrise = DateList[raw[[2, 1]]][[3]];
sunset = DateList[raw[[3, 1]]][[3]];
If[And[dom == sunrise, dom == sunset, sunrise + raw[[2, 2]]/24. < sunset + raw[[3, 2]]/24.],
raw(*sunrise and sunset on same day, and correct order*),
ReplacePart[raw, {
{2, 1, 3} -> dom,
{2, 2} -> Mod[Round[raw[[2, 2]], 12], 24],
{3, 1, 3} -> dom,
{3, 2} -> (Round[raw[[3, 2]],12] /. {0 -> 23 + 59/60. + 59/3600.})
}]
]
]
Then, insert this line immediately after your table that creates sunsetrisedata
sunsetrisedata = fixTropics /@ sunsetrisedata;
You should now be able to run your very cool code for almost any geolocation without issues (up to the model Mathematica uses). You could test this, for example, by setting the location to Tromso in Norway, which is well known for hosting the Midnight Sun Marathon each year.
location = Entity["City", {"Tromso", "Troms", "Norway"}];
You will see that the annuli are slightly truncated near the start of intervals of perpetual day or perpetual night, which seems to be due to my coarse fixTropics[]
function and the nature of the algorithms that Sunset[]
and Sunrise[]
use internally (and documented in their help pages). Still, your code works!
Some brief comments on what the helper function tries to do:
- The dates generated from the
Trace
line occasionally produces things like {2022,4,0}
, for example, instead of {2022,3,31}
. This isn't a problem because the rest of your code manages it. However, my plan was to have the helper function "fix" days where the day-of-the-month for the first date in each row of sunsetrisedata
was not the same as the day-of-the-month in the sunrise date and the sunset date. This is the reason for the DateList[]
in the first few rows of the helper function (which converts {2022,4,30}
to {2022,3,31,0,0,0.}
, for comparison purposes).
- The "fix" involves rounding the sunrise and sunset times either to 12 (if perpetual night) or outwards to 0 and 24, respectively (if perpetual day).