File talk:Planet distances.svg
Jump to navigation
Jump to search
Context
[edit]For background about this image, see w:Wikipedia:Reference desk/Archives/Science/2014 November 10#Nearest-planet-to-Earth graph
Python code
[edit]This is the Python code I used to gather the data from Fourmilab. This floods the server with one request for each day queried, so it should probably be used with some caution. It outputs the distances to standard output as a semicolon-separated table. Jao (talk) 08:39, 12 November 2014 (UTC)
#! /usr/bin/env python3 import argparse import datetime import dateutil.parser import urllib.request PLANETS = ['Sun', 'Mercury', 'Venus', 'Mars'] def get_distances(url): d = {} html = urllib.request.urlopen(url) for line in html: data = line.decode(encoding='iso-8859-1').split() for planet in PLANETS: if data[0] == '<b>' + planet + '</b>': d[planet] = data[6] return d def daterange(startdate, enddate): for n in range(int((enddate - startdate).days) + 1): yield startdate + datetime.timedelta(n) def print_distances(startdate, enddate): print('Date', end='') for planet in PLANETS: print(';' + planet, end='') print() for date in daterange(startdate, enddate): iso = date.isoformat() distances = get_distances( 'http://www.fourmilab.ch/cgi-bin/Solar?date=1&utc=' + iso) print(iso, end='') for planet in PLANETS: print(';' + distances[planet], end='') print() if __name__ == '__main__': parser = argparse.ArgumentParser( description='Print planet distances for a range of dates.') parser.add_argument('dates', metavar='date', type=str, nargs=2, help='the start and end dates in ISO format') args = parser.parse_args() print_distances( dateutil.parser.parse(args.dates[0]), dateutil.parser.parse(args.dates[1]), )