Skip to content

Instantly share code, notes, and snippets.

@eirc
Created May 1, 2014 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eirc/8c128e8e2cbe91bbdc66 to your computer and use it in GitHub Desktop.
Save eirc/8c128e8e2cbe91bbdc66 to your computer and use it in GitHub Desktop.
Calculate coverage percentage of solar eclipses by moons on their respective planets
#!/usr/bin/env ruby
# Solar eclipses on the earth by the moon have the peculiar characteristic of the moon having about the same apparent
# size as the sun in the sky thus obscuring it exactly. Does this happen on other planets too? This script queries
# the Wolfram Alpha engine for this information for every planet and moon. Run it to find out!
#
# For a really scientific approach check out this paper: http://astrogeo.oxfordjournals.org/content/40/3/3.18.short
#
# Instructions:
#
# Get a Wolfram Alpha API account & key (free)
# Install 'wolfram' gem
# Run with: WOLFRAM_APPID='your-key' ruby eclipses.rb
require 'wolfram'
require 'logger'
require 'benchmark'
require 'yaml'
LOGGER = Logger.new 'wolfram.log'
MAX_RETRIES = 5
def wolfram(query)
tries ||= MAX_RETRIES
result = nil
LOGGER.info "Wolfram query: #{query}"
benchmark = Benchmark.realtime { result = Wolfram::HashPresenter.new(Wolfram.fetch(query)).to_hash }
LOGGER.info "Result: #{result}"
LOGGER.info "Completed in: #{benchmark}s"
result[:pods]['Result'][0]
rescue
if (tries -= 1) > 0
LOGGER.error 'No results, retrying'
retry
else
LOGGER.error "Tried #{MAX_RETRIES} times and failed"
'error'
end
end
YAML::load(DATA).each do |planet, moons|
apparent_sun_size_query = "(convert Sun radius to kilometers)/(convert #{planet} orbit distance to kilometers)"
moons.each do |moon|
apparent_moon_size_query = "(convert #{moon} radius to kilometers)/(convert #{moon} orbit distance to kilometers)"
eclipse_percentage_query = "(#{apparent_moon_size_query})/(#{apparent_sun_size_query})"
printf "%-7s - %-10s : %s\n", planet, moon, wolfram(eclipse_percentage_query)
end
end
__END__
Earth: [ 'Moon' ]
Mars: [ 'Phobos', 'Deimos' ]
Jupiter: [ 'Io', 'Europa', 'Ganymede', 'Callisto', 'Amalthea', 'Himalia', 'Elara', 'Pasiphae', 'Sinope',
'Lysithea', 'Carme', 'Ananke', 'Leda', 'Thebe', 'Adrastea', 'Metis', 'Callirrhoe', 'Themisto',
'Megaclite', 'Taygete', 'Chaldene', 'Harpalyke', 'Kalyke', 'Iocaste', 'Erinome', 'Isonoe',
'Praxidike', 'Autonoe', 'Thyone', 'Hermippe', 'Aitne', 'Eurydome', 'Euanthe', 'Euporie',
'Orthosie', 'Sponde', 'Kale', 'Pasithee', 'Hegemone', 'Mneme', 'Aoede', 'Thelxinoe', 'Arche',
'Kallichore', 'Helike', 'Carpo', 'Eukelade', 'Cyllene', 'Kore', 'Herse' ]
Saturn: [ 'Mimas', 'Enceladus', 'Tethys', 'Dione', 'Rhea', 'Titan', 'Hyperion', 'Iapetus', 'Erriapus',
'Phoebe', 'Janus', 'Epimetheus', 'Helene', 'Telesto', 'Calypso', 'Kiviuq', 'Atlas', 'Prometheus',
'Pandora', 'Pan', 'Ymir', 'Paaliaq', 'Tarvos', 'Ijiraq', 'Suttungr', 'Mundilfari', 'Albiorix',
'Skathi', 'Siarnaq', 'Thrymr', 'Narvi', 'Methone', 'Pallene', 'Polydeuces', 'Daphnis', 'Aegir',
'Bebhionn', 'Bergelmir', 'Bestla', 'Farbauti', 'Fenrir', 'Fornjot', 'Hati', 'Hyrrokkin', 'Kari',
'Loge', 'Skoll', 'Surtur', 'Greip', 'Jarnsaxa', 'Tarqeq', 'Anthe', 'Aegaeon' ]
Uranus: [ 'Cordelia', 'Ophelia', 'Bianca', 'Cressida', 'Desdemona', 'Juliet', 'Portia', 'Rosalind',
'Mab', 'Belinda', 'Perdita', 'Puck', 'Cupid', 'Miranda', 'Francisco', 'Ariel', 'Umbriel',
'Titania', 'Oberon', 'Caliban', 'Stephano', 'Trinculo', 'Sycorax', 'Margaret', 'Prospero',
'Setebos', 'Ferdinand' ]
Neptune: [ 'Triton', 'Nereid', 'Naiad', 'Thalassa', 'Despina', 'Galatea', 'Larissa', 'Proteus', 'Halimede',
'Psamathe', 'Sao', 'Laomedeia', 'Neso' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment