{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sunrise and Sunset Times\n",
"\n",
"This tutorial computes sunrise and sunset times for every day of 2024 at a given location, then visualizes how daylight hours change over the course of the year.\n",
"\n",
"`satkit.sun.rise_set` returns the sunrise and sunset times in UTC for a given date and ground location. The results are converted to local time (US Eastern) for display. The shaded region in the plot represents daylight hours."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import satkit as sk\n",
"import datetime\n",
"from zoneinfo import ZoneInfo\n",
"import matplotlib.pyplot as plt\n",
"import scienceplots # noqa: F401\n",
"plt.style.use([\"science\", \"no-latex\", \"../satkit.mplstyle\"])\n",
"%config InlineBackend.figure_formats = ['svg']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute Rise and Set Times\n",
"\n",
"For each day in 2024, compute the sunrise and sunset in UTC, then convert to US Eastern time using Python's built-in `zoneinfo` module."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Array of times for each day of 2024\n",
"basetime = sk.time(2024, 1, 1)\n",
"timearr = [basetime + sk.duration(days=i) for i in range(365)]\n",
"\n",
"# Coordinates of Arlington, MA\n",
"coord = sk.itrfcoord(latitude_deg=42.1514, longitude_deg=-71.1516)\n",
"\n",
"\n",
"# sunrise, sunset in UTC\n",
"riseset = [sk.sun.rise_set(t, coord) for t in timearr]\n",
"rise, set = zip(*riseset)\n",
"\n",
"# Convert to Eastern Time\n",
"eastern = ZoneInfo(\"America/New_York\")\n",
"drise = [r.datetime().astimezone(eastern) for r in rise]\n",
"dset = [s.datetime().astimezone(eastern) for s in set]\n",
"\n",
"# Hour of day, in [0,24]\n",
"risefrac = [r.hour + r.minute / 60 + r.second / 3600 for r in drise]\n",
"setfrac = [s.hour + s.minute / 60 + s.second / 3600 for s in dset]\n",
"\n",
"# Convert hour of day to a time\n",
"risetime = [datetime.time(hour=r.hour, minute=r.minute, second=r.second) for r in drise]\n",
"settime = [datetime.time(hour=s.hour, minute=s.minute, second=s.second) for s in dset]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize\n",
"\n",
"Plot sunrise and sunset times over the year. The shaded area represents daylight hours. Note the characteristic asymmetry around the solstices and the abrupt shifts from daylight saving time transitions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def frac2str(y):\n",
" return y.strftime(\"%H:%M:%S\")\n",
"\n",
"\n",
"risestring = [frac2str(r) for r in risetime]\n",
"setstring = [frac2str(s) for s in settime]\n",
"\n",
"fig, ax = plt.subplots(figsize=(8, 5))\n",
"dates = [x.as_datetime() for x in timearr]\n",
"ax.plot(dates, risefrac, color='#006450', label='Sunrise')\n",
"ax.plot(dates, setfrac, color='#006450', label='Sunset')\n",
"ax.fill_between(dates, risefrac, setfrac, alpha=0.2, color='#006450')\n",
"ax.set_xlabel(\"Date\")\n",
"ax.set_ylabel(\"Local Hour of Day\")\n",
"ax.set_title(\"Sunrise and Sunset Times for 2024 in Arlington, MA\")\n",
"ax.set_ylim(0, 24)\n",
"ax.set_yticks([0, 6, 12, 18, 24])\n",
"ax.set_yticklabels([\"Midnight\", \"6 AM\", \"Noon\", \"6 PM\", \"Midnight\"])\n",
"ax.legend()\n",
"fig.autofmt_xdate()\n",
"plt.tight_layout()\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}