Skip to content

Instantly share code, notes, and snippets.

View astrofrog's full-sized avatar

Thomas Robitaille astrofrog

View GitHub Profile
@astrofrog
astrofrog / sma.py
Created March 18, 2011 03:30
Interface to the SMA sensitivity/beam calculator at http://sma1.sma.hawaii.edu/beamcalc.html
import os
import urllib
import string
URL = "http://sma1.sma.hawaii.edu/beamcalc.html"
VALID_SIDEBANDS = ['lower', 'upper']
VALID_PWVS = [0.2, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
@astrofrog
astrofrog / join.py
Created March 20, 2011 02:11
Join two ATpy tables (work in progress, and to be included in ATpy in future)
import atpy
import numpy as np
def join(table1, table2):
# Check that primary key is set in Table 1
if table1._primary_key is None:
raise Exception("Primary key of table 1 has to be set")
@astrofrog
astrofrog / libfreenect.diff
Created April 23, 2011 21:21
Diff file to install libfreenect on Mac with a MacPorts installation of libusb and EPD 64-bit
diff --git a/wrappers/python/setup.py b/wrappers/python/setup.py
index 7907702..8fdfee0 100755
--- a/wrappers/python/setup.py
+++ b/wrappers/python/setup.py
@@ -5,11 +5,14 @@ from Cython.Distutils import build_ext
ext_modules = [Extension("freenect", ["freenect.pyx"],
libraries=['usb-1.0', 'freenect', 'freenect_sync'],
- runtime_library_dirs=['/usr/local/lib', '/usr/local/lib64', '/usr/lib/'],
+ runtime_library_dirs=['/usr/local/lib', '/usr/lib/', '/opt/local/lib'],
@astrofrog
astrofrog / pnpoly.py
Created May 24, 2011 14:51
Check if point is inside polygon
def pnpoly(vx, vy, x, y):
'''
Check if a point (x,y) is inside a polygon (vx, vy)
Polygon vertices have to be given as Numpy arrays
'''
xi = vx
xj = np.roll(vx, 1)
yi = vy
yj = np.roll(vy, 1)
c = ((yi > y) != (yj > y)) & (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
@astrofrog
astrofrog / raster_axes.py
Created June 9, 2011 16:21
How to plot a million points in matplotlib
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
import matplotlib.colors as colors
def make_colormap(color):
r, g, b = colors.colorConverter.to_rgb(color)
import string
import urllib
from xml.etree.ElementTree import ElementTree
from StringIO import StringIO
search_author = "Goodman, A."
base_url = '''http://adsabs.harvard.edu/cgi-bin/nph-abs_connect?db_key=AST&'''
parameters = {}
@astrofrog
astrofrog / redirect_stdout.py
Created December 7, 2011 20:35
How to redirect stdout and stderr to file for a block of code
class log_to_file():
def __init__(self, filename):
self.f = open(filename, 'wb')
def __enter__(self):
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = self.f
sys.stderr = self.f
@astrofrog
astrofrog / async_plotting.py
Created December 10, 2011 00:11
Asynchronous Plotting in Matplotlib: rather than call savefig directly, add plots to an asynchronous queue to avoid holding up the main program. Makes use of multiple processes to speed up the writing out. Suggestions welcome!
import time
import multiprocessing as mp
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
@astrofrog
astrofrog / map_method.py
Created January 4, 2012 19:59
How to use map with methods
# The following is Steven Bethard's functions to pickle methods - required to
# use multiprocessing.Pool with model.run
import copy_reg
import types
def _pickle_method(method):
func_name = method.im_func.__name__
obj = method.im_self
cls = method.im_class
Freezing version number to astropy/version.py
running build_sphinx
creating docs/_build
creating docs/_build/doctrees
creating docs/_build/html
running build
running build_py
creating build/lib.macosx-10.6-x86_64-2.7
creating build/lib.macosx-10.6-x86_64-2.7/astropy
copying astropy/__init__.py -> build/lib.macosx-10.6-x86_64-2.7/astropy