Wednesday, June 29, 2016

Test-driving pyds9

After farting around with matplotlib's crappy imshow, I went sniffling back to cozy ds9.  I know that STScI developers are suggesting we all move to Ginga, but it seems a little too bleeding edge just yet.  So, I figured out how to call ds9 using the pyds9 package, which uses xpa under the hood to manipulate the ds9 window. 

This is a basic example of using the pyds9 package to plot image data in the ds9 viewer, using Python.

 This example loads data from a fits file into a numpy ndarray, does math on that array, and then plots the array in ds9. Written 6/2016, as a self-tutorial. Use it if it helps you.
import numpy as np
from astropy.io import fits
from astropy.utils.data import download_file
from astropy.io.fits import getdata
import pyds9

image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )
(im_int, hdr) = getdata(image_file, header=True) #image is numpy array
im = im_int.astype(np.float64)  # convert data from int to float
im +=0.01 # im is a numpy array, so we can do math on it.
d = pyds9.DS9('foo1')  # start ds9.  'd' is the way to call ds9
d.set_np2arr(im) # sending ndarray im directly to ds9
d.set("colorbar no")   # example of manipulating the ds9 window
d.set("scale zscale")  # example of manipulating the ds9 window
d.set("zoom to 0.6 0.6")
That works great, and is very cozy if you're already used to xpaset.  Score!

No comments:

Post a Comment