Thursday, July 3, 2014

Finding the stars

Earth Date: 3rd July
Location:  Elk Grove, California

This post is a little more technically oriented. I have included it because some of my friends, such as Dan and Oscar, may find this useful.

The basic problem I was trying to solve was to find a way to find the rising and setting times of interesting stars and globular clusters. There are multiple iApplications that do a reasonable job. I have tried Star Chart, Night Sky, and SkyView Free. While these programs have their different strengths, most of them aim tell you what is up now (and may give you a way to show where it will be in a few hours). 

What I wanted was a way to ask questions such as
"When will Betelgeuse be visible from Gardiner, Montana on the evening of July 25th, 2014"
and it could tell me, so I would know when I would need to get up. I am not interesting in just grabbing random shots! The additional constraint would be whatever solution I had could not rely on internet access, as we would spend a lot of time in the backcountry. I needed something that I could run off my laptop.

This task can be broken into three sub-tasks:
  1. A way to convert between "Earth coordinates" (latitude, longitude, time) and "Celestial coordinates" (Right ascension, declination) to "Observer coordinates" (altitude and azimuth).
  2. A table of latitudes and longitudes of the cities we visited.
  3. A table of Right ascension and declinations of interesting objects.
For the uninitiated, Right ascension and declination are analogs of longitude and latitude for the (distant) stars. The idea is that each astronomical object is at a particular RA and DEC, and then that sphere rotates once per day around the Earth.  

The first step is simply a task in spherical geometry, but there are quite a few subtle complications that come into play. For example: daylight savings time, taking into account leap years (and the leap seconds), .....

Instead of reinventing the wheel, I looked at what was already out there. The most useful package for my task was PyEphem, an ephemeris for python. Instead of describing it in detail, here is a short example of it in action:

import ephem
from ephem import cities
from ephem import stars
from our_cities import *

star_name = "Betelgeuse"
the_star = ephem.star( star_name )
sun   = ephem.Sun()

#
# Now try using the our_cities object
#
name = "elk grove"
the_city  = our_cities[ name ]
custom    = ephem.Observer()
custom.lat = the_city['lat']
custom.long= the_city['long']
custom.elevation = the_city['elevation']
custom.pressure = 0
custom.horizon = "-0:34"

our_shift = the_city['shift']*ephem.hour

for count in range(30):
    print "%s is up in %s from %s (sunrise at %s) to %s (sunset at %s)" % (star_name, name, ephem.Date( custom.next_rising(the_star) + our_shift), ephem.Date(custom.next_rising(sun) + our_shift), 
                                                                                   ephem.Date( custom.next_setting(the_star) + our_shift), ephem.Date( custom.next_setting(sun) + our_shift))
    custom.date = custom.next_setting(the_star)

I had to cheat a little and spend a little while getting a list of cities and their longitude and latitude. Here is the contents of our_cities.py
our_cities = {
  'crescent city': {'long' : '-124.2017', 'lat': '41.7558', 'elevation': 0 , 'shift': -7},
  'portland'     : {'long' : '-122.6819', 'lat': '45.52'  , 'elevation': 0 , 'shift': -7},
  'rainier'      : {'long' : '-121.76',   'lat': '46.8529', 'elevation': 50, 'shift': -7},
  'seattle'      : {'long' : '-122.3331', 'lat': '47.6097', 'elevation': 0 , 'shift': -7},
  'anacortes'    : {'long' : '-122.6236', 'lat': '48.5019', 'elevation': 0 , 'shift': -7},
  'glacier'      : {'long' : '-113.7183', 'lat': '48.6967', 'elevation': 1500, 'shift':-6},
  'gardiner'     : {'long' : '-110.7139', 'lat': '45.0369', 'elevation': 1500, 'shift':-6},
  'tetons'       : {'long' : '-110.7008', 'lat': '43.8333', 'elevation': 1000, 'shift':-5},
  'faithful'     : {'long' : '-110.8282', 'lat': '44.4605', 'elevation': 2240, 'shift':-5},
  'prismatic'    : {'long' : '-110.5250', 'lat': '44.5250', 'elevation': 2220, 'shift':-5},
  'sacramento'   : {'long' : '-121.4689', 'lat': '38.5556', 'elevation': 10  , 'shift':-7},
  'elk grove'    : {'long' : '-121.3819', 'lat': '38.4383', 'elevation': 10,   'shift':-7},
  'whittier'     : {'long' : '-118.0244', 'lat': '33.9656', 'elevation': 0,    'shift':-7},
  'joshua tree'  : {'long' : '-115.8982', 'lat': '33.7884', 'elevation': 900,  'shift':-7}
}

Not bad, but perhaps Patty Jula can point me in a more elegant solution for getting longitude and latitude than just plugging names into Google?

1 comment:

  1. Oh man, Python does everything! Really cool idea. The zip file in this website contains a table with lat/lon for 43,000+ locations in the United States http://www.nws.noaa.gov/geodata/catalog/national/html/cities.htm Yay!

    ReplyDelete