Skip to content

Instantly share code, notes, and snippets.

@transientlunatic
Last active December 14, 2021 18:14
Show Gist options
  • Save transientlunatic/d0344210cb80379c75e7cfeda9ad83eb to your computer and use it in GitHub Desktop.
Save transientlunatic/d0344210cb80379c75e7cfeda9ad83eb to your computer and use it in GitHub Desktop.
Calculate the earliest sunset and the latest sunrise in winter.
"""
This file contains some code which is useful for plotting,
but if you're following the tutorials you shouldn't need to worry about it.
"""
from cycler import cycler
blueprint = {
"xtick.labelsize":10,
"xtick.major.size": 5,
"xtick.minor.visible": True,
"xtick.color": "k",
"ytick.labelsize":10,
"ytick.major.size": 5,
"ytick.minor.visible": True,
# Lines
"lines.linewidth": 2,
"axes.prop_cycle": cycler("color", ['#FFFFFF', '#CCCCCC', '#AAAAAA']),
# Fonts
"font.monospace": ["Source code pro"],
"font.sans-serif": ["Source sans pro"],
"font.family": "monospace",
# Grid
"axes.grid": True,
"axes.grid.which": "both",
"grid.color": "#CED8F7",
"grid.alpha": 0.3,
"grid.linestyle": "-",
"grid.linewidth": 0.25,
# Plot display
"figure.dpi": 300,
# Plot facecolors
"axes.facecolor": "#11496f",
"figure.facecolor": "#FFFFFFFF"
}
pastle = {
"xtick.labelsize":10,
"xtick.major.size": 5,
"xtick.minor.visible": True,
"xtick.color": "k",
"ytick.labelsize":10,
"ytick.major.size": 5,
"ytick.minor.visible": True,
# Lines
"lines.linewidth": 2,
"axes.prop_cycle": cycler("color", ['#8dd3c7', '#ffffb3', '#bebada',
'#fb8072', '#80b1d3', '#fdb462',
'#b3de69', '#fccde5', '#d9d9d9',
'#bc80bd', '#ccebc5', '#ffed6f']),
# Fonts
"font.monospace": ["Source code pro"],
"font.sans-serif": ["Source sans pro"],
"font.family": "monospace",
# Grid
"grid.color": "#4298bd",
"grid.alpha": 0.5,
# Display
"figure.dpi": 300,
# Face colors
"axes.facecolor": "#ecf5f8",
"figure.facecolor": "#FFFFFFFF"
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import ephem
import datetime
import pandas
import matplotlib.pyplot as plt
# I've written this for Glasgow, Scotland, but you can adapt it to anywhere by getting the coordinates for your town.
# You can normally get these from Wikipedia - there's a link in the top right hand corner of most articles for places;
# click that link, and alongside lots of other useful stuff it'll give you a decimal representation you can use here.
glasgow = ephem.Observer()
glasgow.lat, glasgow.lon = "55.860916", "-4.251433"
# This just makes an array of 60 dates to use to calculate the sunrise and set times
start = datetime.datetime.strptime("2019-11-15", "%Y-%m-%d")
numdays=60
date_list = [start + datetime.timedelta(days=x) for x in range(numdays)]
def rise_set_times(dates, observatory):
"""
Calculate the local sunrise, sunset, and local noon (sun transit) times for each day in an array, and return a DataFrame.
Parameters
----------
dates: list of `datetime.datetime` objects
A list of dates where the sunrise and set should be computed
observatory: `ephem.Observer`
An ephem object describing the location of the observatory.
Returns
-------
results : `pandas.DataFrame`
A dataframe with the various times, with the date as the index.
"""
results = []
for date in dates:
observatory.date = date.strftime("%Y-%m-%d")
data = {
"rise": observatory.next_rising(ephem.Sun()).datetime(),
"date": date,
"set": observatory.next_setting(ephem.Sun()).datetime(),
"transit": observatory.next_transit(ephem.Sun()).datetime()
}
observatory.horizon = '-6'
data['civil start'] = observatory.next_rising(ephem.Sun(), use_center=True).datetime()
data['civil end'] = observatory.next_setting(ephem.Sun(), use_center=True).datetime()
results.append(data)
return pandas.DataFrame(results).set_index("date")
results = rise_set_times(date_list, glasgow)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment