{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sunrise and Sunset Times" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import satkit as sk\n", "import datetime\n", "import pytz\n", "import plotly.graph_objects as go" ] }, { "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", "drise = [r.datetime().astimezone(pytz.timezone(\"America/New_York\")) for r in rise]\n", "dset = [s.datetime().astimezone(pytz.timezone(\"America/New_York\")) 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": "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 = go.Figure()\n", "fig.add_trace(\n", " go.Scatter(\n", " x=[x.datetime() for x in timearr],\n", " y=risefrac,\n", " customdata=risestring,\n", " name=\"Sunrise\",\n", " mode=\"lines\",\n", " line=dict(color=\"rgb(0,100,80)\"),\n", " hovertemplate=\"Date: %{x}
Sunrise: %{customdata}\", # Custom hover text\n", " )\n", ")\n", "fig.add_trace(\n", " go.Scatter(\n", " x=[x.datetime() for x in timearr],\n", " y=setfrac,\n", " name=\"SunSet\",\n", " mode=\"lines\",\n", " fill=\"tonexty\",\n", " customdata=setstring,\n", " fillcolor=\"rgba(0,100,80,0.2)\",\n", " line=dict(color=\"rgb(0,100,80)\"),\n", " hovertemplate=\"Date: %{x}
Sunset: %{customdata}\", # Custom hover text\n", " )\n", ")\n", "fig.update_yaxes(title=\"Local Hour of Day\")\n", "fig.update_xaxes(title=\"Date\")\n", "fig.update_layout(\n", " title=\"Sunrise and Sunset Times for 2024 in Arlington, MA\",\n", " xaxis=dict(\n", " gridcolor=\"#dddddd\",\n", " gridwidth=1,\n", " showline=True,\n", " mirror=True,\n", " linewidth=2,\n", " linecolor=\"black\",\n", " ),\n", " yaxis=dict(\n", " gridcolor=\"#dddddd\",\n", " gridwidth=1,\n", " showline=True,\n", " mirror=True,\n", " linewidth=2,\n", " linecolor=\"black\",\n", " range=[0, 24],\n", " tickvals=[0, 6, 12, 18, 24], # Set y-axis tick positions\n", " ticktext=[\"Midnight\", \"6 AM\", \"Noon\", \"6 PM\", \"Midnight\"],\n", " ),\n", " plot_bgcolor=\"white\",\n", " paper_bgcolor=\"white\",\n", " width=600,\n", " height=520,\n", ")\n", "fig.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.13.0" } }, "nbformat": 4, "nbformat_minor": 2 }