Commit d87fcbc4 authored by Ramesh Karuppusamy's avatar Ramesh Karuppusamy
Browse files

First commit of python-based rise-set, cloned from Patrick's github version

parent ac06f89a
import utils
import sites
"""
A module containing useful argparse actions
to be included in command-line parses of
multiple programs.
Patrick Lazarus, June 9, 2013
"""
import argparse
import utils
import sources
import sites
class AppendSourceCoords(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
srclist = getattr(namespace, self.dest)
srclist.append(sources.Source.from_string(values))
class ExtendSourceCoords(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
srclist = getattr(namespace, self.dest)
srclist.extend_from_file(values)
class ParseTime(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
timestr = values
time = utils.parse_timestr(timestr)
setattr(namespace, self.dest, time)
class ParseDate(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
datestr = values
date = utils.parse_datestr(datestr)
setattr(namespace, self.dest, date)
class ListSitesAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string):
print "Available observing sites:"
for sitename in sites.registered_sites:
print " %s" % sitename
sys.exit()
This diff is collapsed.
# Custom Errors
class BadDateFormat(Exception):
pass
class BadSiteFileFormat(Exception):
pass
class BadSourceStringFormat(Exception):
pass
class RiseSetErrors(Exception):
pass
class SourceIsCircumpolar(RiseSetErrors):
pass
class SourceNeverRises(RiseSetErrors):
pass
class MultipleRiseSets(RiseSetErrors):
pass
class SystemCallError(Exception):
pass
#!/usr/bin/env python
"""
hor2eq.py
Convert horizon coordinates (altitude/azimuth) to equatorial coordinates.
Patrick Lazarus, Sept. 24, 2013
"""
import argparse
import datetime
import utils
import actions
import sites
def main():
site = sites.load(args.site)
if args.lst is None:
lst = site.lstnow()
else:
lst = args.lst
alt_deg, az_deg = args.coords
ra_deg, decl_deg = site.get_skyposn(alt_deg, az_deg, lst=lst)
lst_hms = utils.deg_to_hmsstr(lst*15)[0]
print "%s oriented at (alt=%g deg, az=%g deg) at LST=%s is pointed towards" \
"\n RA=%s\n Dec=%s" % (site.name, alt_deg, az_deg, lst_hms, \
utils.deg_to_hmsstr(ra_deg)[0], utils.deg_to_dmsstr(decl_deg)[0])
if __name__=='__main__':
parser = argparse.ArgumentParser(description="Convert horizon coordinates " \
"(altitude/azimuth) to equatorial coordinates.")
parser.add_argument('--lst', dest='lst', type=str, default=None, \
action=actions.ParseTime, \
help="LST to use. (Default: Now)")
parser.add_argument('--list-sites', action=actions.ListSitesAction, \
nargs=0, \
help="List registered observing sites, and exit.")
parser.add_argument('--site', dest='site', type=str, \
default=sites.registered_sites[0], \
help="Name of observing site to load. Use " \
"'--list-sites' to get a list of accepted " \
"sites. (Default: '%s')" % \
sites.registered_sites[0])
parser.add_argument("coords", nargs=2, type=float, metavar="COORD", \
help="Coordinates. Two floating-point values: " \
"altitude (in deg) and azimuth (in deg)")
args = parser.parse_args()
main()
#!/usr/bin/env python
import argparse
import datetime
import utils
import actions
import sites
def main():
site = sites.load(args.site)
if args.date is None:
date = datetime.date.today()
else:
date = args.date
print "Converting LST to UTC for %s on %s" % \
(site.name, date.strftime("%b %d, %Y"))
for lst in args.lst:
lst_hours = utils.parse_timestr(lst)
lst_hms = utils.deg_to_hmsstr(lst_hours*15)
utc_hours = site.lst_to_utc(lst_hours, date)
utc_hms = utils.deg_to_hmsstr(utc_hours*15)
print " LST: %s = UTC: %s" % (lst_hms[0], utc_hms[0])
if __name__=='__main__':
parser = argparse.ArgumentParser(description="Convert LST times to " \
"UTC for a given observing site. ")
parser.add_argument('--date', dest='date', type=str, default=None, \
action=actions.ParseDate, \
help="Date to use. (Default: today)")
parser.add_argument('--list-sites', action=actions.ListSitesAction, \
nargs=0, \
help="List registered observing sites, and exit.")
parser.add_argument('--site', dest='site', type=str, \
default=sites.registered_sites[0], \
help="Name of observing site to load. Use " \
"'--list-sites' to get a list of accepted " \
"sites. (Default: '%s')" % \
sites.registered_sites[0])
parser.add_argument("lst", nargs="+", type=str, \
help="LST times to convert to UTC.", \
default=[])
args = parser.parse_args()
main()
import re
registered_modes = ["graphical", \
"batch", \
"text", \
]
DEFAULT = 'graphical'
def run(modename, *args, **kwargs):
matching_modes = [m for m in registered_modes \
if re.search(modename, m, re.IGNORECASE)]
if len(matching_modes) == 1:
mode = __import__(matching_modes[0], globals())
mode.run(*args, **kwargs)
elif len(matching_modes) == 0:
raise ValueError("Mode cannot be run. Name provided is not " \
"recognized: '%s'" % modename)
elif len(matching_modes) > 1:
raise ValueError("Mode name provided in ambiguous. The following " \
"modes match: '%s'" % "', '".join(matching_modes))
else:
raise ValueError("Should not be here! Negative number of matching " \
"modes? len(matching_modes)=%d; modename='%s'" % \
len(matching_modes, modename))
import datetime
import errors
import utils
def run(site, lst, date, targets, testsources, calibrators, args):
if date is None:
date = datetime.date.today()
if lst is None:
utc = utils.utcnow()
lst = site.utc_to_lst(utc=utc, date=date)
datestr = date.strftime("%b %d, %Y")
lststr = utils.deg_to_hmsstr(lst*15)[0]
utc = site.lst_to_utc(lst=lst, date=date)
utcstr = utils.deg_to_hmsstr(utc*15)[0]
print "%s\tLST: %s\tUTC: %s\n" % (datestr, lststr, utcstr)
for srclist in [calibrators, targets, testsources]:
for src in srclist:
ra_deg, dec_deg = src.get_posn(lst, date)
rastr = "R.A. (J2000): %s" % utils.deg_to_hmsstr(ra_deg, 2)[0]
decstr = "Dec. (J2000): %s" % utils.deg_to_dmsstr(dec_deg, 2)[0]
print "%-20s%-27s%27s" % (src.name, rastr, decstr)
try:
risetime, settime = src.get_rise_set_times(site, date)
except errors.SourceIsCircumpolar:
srctypestr = "(%s)" % srclist.name
print "%-20sSource is circumpolar." % srctypestr
except errors.SourceNeverRises:
srctypestr = "(%s)" % srclist.name
print "%-20sSource never rises." % srctypestr
except errors.MultipleRiseSets:
srctypestr = "(%s)" % srclist.name
print "%-20sMultiple rise/set times?!" % srctypestr
except:
srctypestr = "(%s)" % srclist.name
print "%-20sError! Oops..." % srctypestr
raise
else:
if src.is_visible(site, lst, date):
eventstr = "Source sets in %s" % \
utils.deg_to_hmsstr(((settime-lst)%24)*15)[0]
else:
eventstr = "Source rises in %s" % \
utils.deg_to_hmsstr(((risetime-lst)%24)*15)[0]
risetosetstr = "Rise to set time: %s" % \
utils.deg_to_hmsstr(((settime-risetime)%24)*15)[0]
riselststr = "Rise (LST): %s" % \
utils.deg_to_hmsstr((risetime%24)*15)[0]
riseutcstr = "Rise (UTC): %s" % \
utils.deg_to_hmsstr((site.lst_to_utc(risetime, \
date)%24)*15)[0]
setlststr = "Set (LST): %s" % \
utils.deg_to_hmsstr((settime%24)*15)[0]
setutcstr = "Set (UTC): %s" % \
utils.deg_to_hmsstr((site.lst_to_utc(settime, \
date)%24)*15)[0]
srctypestr = "(%s)" % srclist.name
print "%-20s%-27s%27s" % (srctypestr, risetosetstr, eventstr)
print " "*20 + "%-22s%22s" % (riselststr, setlststr)
print " "*20 + "%-22s%22s" % (riseutcstr, setutcstr)
if src.notes:
print ""
print " "*20 + "NOTES: %s" % src.notes
print ""
print ""
#alt, az = src.get_altaz(site, lst, date)
#if alt > site.horizon(az):
# altstr = u"Alt.: %.2f\u00b0" % alt)
# azstr = u"Az.: %.2f\u00b0" % az)
# altabove = u"Alt. above horizon: %.2f\u00b0" % \
# (alt - site.horizon(az)))
This diff is collapsed.
import datetime
import os.path
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import utils
import sources
import sourcelist
class SkyViewFigure(matplotlib.figure.Figure):
def __init__(self, site, targets, testsources, calibrators,
lst=None, date=None, \
*args, **kwargs):
"""Constructor for SkyViewFigure.
Inputs:
site: A ObsSite object representing the observing site.
targets: A SourceList object of science targets.
testsources: A SourceList object of sources used to
test the observing system.
calibrators: A SourceList object of calibrator sources.
lst: The local sidereal time to use (Default: now!).
date: The date to use, a datetime.date object (Default: today).
Outputs:
skyfig: The SkyViewFigure instance.
"""
super(SkyViewFigure, self).__init__(*args, **kwargs)
self.site = site
self.targets = targets
self.testsources = testsources
self.calibrators = calibrators
self.bgstars = sourcelist.SourceList(name='Background Stars', \
src_cls=sources.BackgroundStar, \
pickable=False)
bgstarfn = os.path.join(os.path.dirname(__file__), 'bsc.txt')
self.bgstars.extend_from_file(bgstarfn)
self.lst = lst
self.date = date
self.show_targets = True
self.show_test = True
self.show_cals = True
self.path = None
self.selected = None
self.selected_scatt = None
self.selected_list = None
self.select_name_text = None
self.sun = sources.Sun()
self.sun_list = sourcelist.SourceList(name="The Sun", \
src_cls=sources.Sun)
self.sun_list.append(self.sun)
def plot(self):
"""Create the plot, and add all the unchanging figures.
Inputs:
None
Outputs:
None
"""
self.clear() # Clear the figure, just in case.
# Print information
self.text(0.02, 0.95, self.site.name, size=32, \
ha='left', va='center')
if self.site.lon < 0:
londir = "W"
else:
londir = "E"
lonstr = "%s %s" % (utils.deg_to_dmsstr(abs(self.site.lon))[0], londir)
if self.site.lat < 0:
latdir = "S"
else:
latdir = "N"
latstr = "%s %s" % (utils.deg_to_dmsstr(abs(self.site.lat))[0], latdir)
self.text(0.02, 0.91, "%s, %s" % (lonstr, latstr), size='x-small', \
ha='left', va='center')
datetimestrs = self.get_datetime_strs()
self.datetimetext = self.text(0.98, 0.95, '\n'.join(datetimestrs), \
size='medium', ha='right', va='top')
self.horizon_polarax = self.add_axes([0.05, 0.1, 0.8, 0.8], \
projection='polar')
self.horizon_polarax.set_theta_zero_location('N')
# Set theta to increase clockwise
self.horizon_polarax.set_theta_direction(-1)
az_deg = np.linspace(0, 360, 361, endpoint=True)
az_rad = np.deg2rad(az_deg)
horizon = 90-self.site.horizon(az_deg)
self.horizon_polarax.plot(az_rad, horizon, \
ls='-', lw=2, c='#006400', zorder=3)
maxza = max((90, 90-min(horizon))) # Maximum zenith angle to plot
self.horizon_polarax.fill_between(az_rad, horizon, \
y2=maxza, facecolor='#228B22', \
edgecolor='none', alpha=0.5, zorder=3)
self.horizon_polarax.fill_between(az_rad, horizon, \
y2=maxza, facecolor='#228B22', \
edgecolor='none', alpha=1.0, zorder=0)
# Grid of altitudes and azimuths
alts, azs = np.meshgrid(np.linspace(0,90, 361), np.linspace(0,360, 721))
above_horizon = self.site.above_horizon(alts, azs)
can_point = self.site.pointing(alts, azs)
self.horizon_polarax.contourf(np.deg2rad(azs), 90-alts, \
~above_horizon | can_point, [-1, 0], colors='r', \
alpha=0.5, zorder=3)
self.horizon_polarax.contour(np.deg2rad(azs), 90-alts, \
~above_horizon | can_point, [-1, 0], colors='r', \
zorder=3, linewidths=2)
maxpointza = 90-np.min(alts[can_point & above_horizon])
self.sky_fill = self.horizon_polarax.fill_between(az_rad, horizon, \
y2=0, facecolor='none', \
edgecolor='none', alpha=1.0, zorder=-2)
self.horizon_polarax.set_rlim(0, min(maxza, maxpointza+5))
def coord_formatter(az_rad, za):
az = np.rad2deg(az_rad)%360
alt = 90-za
horalt = self.site.horizon(az)
if alt > horalt:
status = "(above horizon)"
else:
status = "(below horizon)"
# note: "\u00b0" is the unicode character for the degree symbol
string = u"Az: %.2f\u00b0, Alt: %.2f\u00b0 %s" % \
(az, alt, status)
return string
self.horizon_polarax.format_coord = coord_formatter
# Format zenith angle so it is actually displayed as altitude
def alt_formatter(za, index):
return u"%g\u00b0" % (90-za)
fmt = matplotlib.ticker.FuncFormatter(alt_formatter)
self.horizon_polarax.yaxis.set_major_formatter(fmt)
# Celestial Pole
pole_za_deg = 90-np.abs(self.site.lat)
if self.site.lat > 0:
# Northern observing site
pole_az_rad = 0
elif self.site.lat < 0:
# Southern observing site
pole_az_rad = np.pi
# Plot Celestial Pole
self.pole_scatt = self.horizon_polarax.scatter(pole_az_rad, pole_za_deg, \
marker='.', s=20, c='k', zorder=0)
# Plot the Sun
az_rads, zas = self.sun_list.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.sun_scatt = self.horizon_polarax.scatter(az_rads, zas, \
picker=self.sun_list.pickable,
marker=self.sun_list.get_marker(), \
c=self.sun_list.get_colours(), \
s=self.sun_list.get_sizes(), \
edgecolors=self.sun_list.get_edgecolours(), \
zorder=self.sun_list.get_zorder())
# Set sky colour
skycolour = self.sun.get_skycolour(self.site, lst=self.lst, \
date=self.date)
self.sky_fill.set_facecolor(skycolour)
# Plot background stars
az_rads, zas = self.bgstars.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.bgstars_scatt = self.horizon_polarax.scatter(az_rads, zas, \
picker=self.bgstars.pickable,
marker=self.bgstars.get_marker(), \
c=self.bgstars.get_colours(), \
s=self.bgstars.get_sizes(), \
edgecolors=self.bgstars.get_edgecolours(), \
zorder=self.bgstars.get_zorder())
# Adjust grid lines and labels
if self.sun.is_night(self.site, lst=self.lst, date=self.date):
self.horizon_polarax.yaxis.grid(c='w')
self.horizon_polarax.xaxis.grid(c='w')
self.horizon_polarax.yaxis.set_tick_params(labelcolor='w')
self.pole_scatt.set_color('w')
self.bgstars_scatt.set_visible(True)
else:
self.bgstars_scatt.set_visible(False)
# Plot targets
alt, az = self.targets.get_altaz(self.site, lst=self.lst)
za = 90-alt
az_rad = np.deg2rad(az)
self.target_scatt = self.horizon_polarax.scatter(az_rad, za, \
picker=True, marker='*', c='#FA8072', s=200, zorder=2)
# Plot testsources
alt, az = self.testsources.get_altaz(self.site, lst=self.lst)
za = 90-alt
az_rad = np.deg2rad(az)
self.test_scatt = self.horizon_polarax.scatter(az_rad, za, \
picker=True, marker='o', c='#1E90FF', s=100, zorder=2)
# Plot calibrators
alt, az = self.calibrators.get_altaz(self.site, lst=self.lst)
za = 90-alt
az_rad = np.deg2rad(az)
self.cal_scatt = self.horizon_polarax.scatter(az_rad, za, \
picker=True, marker='D', c='#DEB887', s=80, zorder=2)
# Add a lengend to the figure
self.legend((self.target_scatt, self.test_scatt, self.cal_scatt), \
("Target pulsars", "Test pulsars", "Calibrators"), \
loc='lower left', prop={'size':'small'}, \
markerscale=0.5, scatterpoints=3)
# Connect event handlers
self.connect_event_triggers()
def get_datetime_strs(self):
"""Get a list of datetime strings to display.
Inputs:
None
Output:
datetimestrs: A list of date/time informational strings.
"""
datetimestrs = []
if self.lst is None:
datetimestrs.append("Current date: %s" % \
datetime.date.today().strftime("%b %d, %Y"))
datetimestrs.append("Current LST: %s" % \
utils.deg_to_hmsstr(self.site.lstnow()*15)[0].split('.')[0])
datetimestrs.append("Current UTC: %s" % \
datetime.datetime.utcnow().strftime('%H:%M:%S'))
else:
datetimestrs.append("Date selected: %s" % \
self.date.strftime("%b %d, %Y"))
datetimestrs.append("LST selected: %s" % \
utils.deg_to_hmsstr(self.lst*15)[0])
datetimestrs.append("UTC selected: %s" % \
utils.deg_to_hmsstr(self.site.lst_to_utc(self.lst, self.date)*15)[0])
return datetimestrs
def update(self):
# Update LST
if self.lst is None:
datetimestrs = self.get_datetime_strs()
self.datetimetext.set_text('\n'.join(datetimestrs))
# Move sun
az_rads, zas = self.sun_list.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.sun_scatt.set_offsets(zip(az_rads, zas))
# Update sky
skycolour = self.sun.get_skycolour(self.site, lst=self.lst, \
date=self.date)
self.sky_fill.set_facecolor(skycolour)
# Adjust grid lines and
if self.sun.is_night(self.site, lst=self.lst, date=self.date):
self.horizon_polarax.yaxis.grid(c='w')
self.horizon_polarax.xaxis.grid(c='w')
self.horizon_polarax.yaxis.set_tick_params(labelcolor='w')
self.pole_scatt.set_color('w')
self.bgstars_scatt.set_visible(True)
else:
self.horizon_polarax.yaxis.grid(c='k')
self.horizon_polarax.xaxis.grid(c='k')
self.horizon_polarax.yaxis.set_tick_params(labelcolor='k')
self.pole_scatt.set_color('k')
self.bgstars_scatt.set_visible(False)
# Move targets
az_rads, zas = self.targets.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.target_scatt.set_offsets(zip(az_rads, zas))
# Move testsources
az_rads, zas = self.testsources.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.test_scatt.set_offsets(zip(az_rads, zas))
# Move calibrators
az_rads, zas = self.calibrators.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.cal_scatt.set_offsets(zip(az_rads, zas))
# Move background stars
az_rads, zas = self.bgstars.get_plotcoords(self.site, \
lst=self.lst, date=self.date)
self.bgstars_scatt.set_offsets(zip(az_rads, zas))
if self.selected is not None:
lsts = np.linspace(0, 24, 100)
path_alts, path_azs = self.selected.get_altaz(self.site, \
lst=lsts, date=self.date)
path_azs_rad = np.deg2rad(path_azs)
path_zas = 90 - path_alts