In [ ]:
from __future__ import print_function, division

Images in Astronomy

In this lesson we are going to look at aspects of processing and viewing images specific to Astronomy and Solar Astronomy. By the end of this lesson you should understand:

  • Projected Coordinate Systems in Images
  • World Coordinate Systems
  • Using WCS to calculate coordinates in images
  • Plotting images with WCS in images
  • Using SunPy Map

Projected Coordinate Systems

When taking images of the sky, we are projecting the spherical celestial coordinate system onto a 2-dimensional plane, which means that there is no simple linear relation between pixel coordinates and celestial coordinates

There are multiple coordinate systems used to describe the locations in 2D and 3D space for both Astronomy and Solar Physics. We shall use a couple of these systems here as examples but if you want to know more about them there are many of resources avalible.

World Coordinate System

The FITS files have a standard for describing the physical coordinate system associated with imaging data, this is called the world coordinate system or WCS, sometimes the specific FITS version of this is referred to as FITS-WCS.

There are multiple papers describing the FITS-WCS standard for various types of data, there is a list here: http://fits.gsfc.nasa.gov/fits_wcs.html

As you learned in the previous lesson we can load FITS files with Astropy. To demonstrate a simple example of a FITS file with FITS-WCS information in the header we shall use an image from SunPy:


In [ ]:
from sunpy.data.sample import AIA_171_IMAGE
from astropy.io import fits

hdulist = fits.open(AIA_171_IMAGE)
hdulist.verify('silentfix')
hdulist[0].header

As you can see there are lots of keys in this and most other real world FITS headers. The ones we need to understand for FITS-WCS are:

Reference Pixel and Coordinate:


In [ ]:
header = hdulist[0].header

print(header['CRVAL1'], header['CRVAL2'])
print(header['CRPIX1'], header['CRPIX2'])

Pixel resolution (at the reference pixel):


In [ ]:
print(header['CDELT1'], header['CDELT2'])

Rotation angle, in degress (at the reference pixel):


In [ ]:
print(header['CROTA2'])

Coordinate System and Projection:


In [ ]:
print(header['CTYPE1'], header['CTYPE2'])

Keyword Extraction

Extract and print out the TELESCOP value from the header.

Next, extract the WAVELNTH and WAVEUNIT values, use these to construct an astropy Quantity object for the wavelength of this image.


In [ ]:
header['TELESCOP']

In [ ]:
import astropy.units as u
u.Quantity(header['WAVELNTH'], unit=header['WAVEUNIT'])

We could now sit down and work out how to convert from a pixel coordinate to a physical coordinate described by this header (Helioprojective).

However, we can cheat and just use Astropy.


In [ ]:
from astropy.wcs import WCS

wcs = WCS(header)

We can convert from pixel to world coordinate:


In [ ]:
wcs.wcs_pix2world(((100, 100),), 0)

Or back again:


In [ ]:
wcs.wcs_world2pix([[  3.59725669e+02,  -2.74328093e-01]], 0)

The last parameter to the two above examples is the 'origin' parameter. It is a flag that tells WCS if you indexes should be 0-based (like numpy) or 1-based (like FITS). Here we are using 0 as we want to convert to and from numpy indexes of the array.

How large is the image?


To get a little practise using Astropy's WCS calculate the world coordinates of the following pixels: [-500, 0] [500, 500] [0, 0]

In [ ]:
print(wcs.wcs_pix2world(((-500, 0),), 1))
print(wcs.wcs_pix2world(((500, 500),), 1))
print(wcs.wcs_pix2world(((0, 0),), 1))

Plotting with wcsaxes

In this section we are going to use the wcsaxes package to make WCS aware image plots.


In [ ]:
import wcsaxes

For this example we are going to use a Hubble image.


In [ ]:
from astropy.io import fits

hdulist = fits.open('./h_n4571_f555_mosaic.fits.gz')
hdulist

In [ ]:
wcs = WCS(hdulist[0].header)

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt

In [ ]:
ax = plt.subplot(111, projection=wcs)
ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')

This image now has physcial labels in the native coordinate system of the image. We can see what the coordinate system and projection of this image is using the 'CTYPE' header entries we saw earlier.


In [ ]:
print(hdulist[0].header['CTYPE1'], hdulist[0].header['CTYPE2'])

We can tell that this is in the FK5 coordinate system by the presence of a 'equinox' entry in the header:


In [ ]:
hdulist[0].header['equinox']

There is also a quick way to generate an Astropy coordinate frame from a WCS object, which confirms this diagnosis.


In [ ]:
from astropy.wcs.utils import wcs_to_celestial_frame
wcs_to_celestial_frame(wcs)

for more information on the very useful astropy.coordinates module see http://docs.astropy.org/en/stable/coordinates/

Add some labels


Now we have a nice plot with physically meaningful ticks, we should label our axes.
Add labels to the axes saying "Right Ascension [degrees]" and "Declination [degrees]"
Also overlay a coordinate grid using: ax.coords.grid() Look up the documentation for this method to see what parameters you can specify.

In [ ]:
ax = plt.subplot(111, projection=wcs)
ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')
ax.set_xlabel("Right Ascension [degrees]")
ax.set_ylabel("Declination [degrees]")
ax.coords.grid(color='white', alpha=0.5, linestyle='solid')

Now we have a nice plot, we can do a couple of things to plot.

Overplotting in Pixel Coordinates


In [ ]:
ax = plt.subplot(111, projection=wcs)

ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')
ax.set_xlabel("Right Ascension [degrees]")
ax.set_ylabel("Declination [degrees]")
ax.coords.grid(color='white', alpha=0.5, linestyle='solid')

ax.plot(3000, 3000, 'o')

Overplotting in World Coordinates


In [ ]:
ax = plt.subplot(111, projection=wcs)

ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')
ax.set_xlabel("Right Ascension [degrees]")
ax.set_ylabel("Declination [degrees]")
ax.coords.grid(color='white', alpha=0.5, linestyle='solid')

ax.set_autoscale_on(False)

ax.plot(3000, 3000, 'o')
# Overplot in FK5 in Degrees
ax.plot(189.25, 14.23, 'o', transform=ax.get_transform('world'))

Add some labels

Now overplot two lines on the image, one where you specified the line in pixel coordinates, and one where you specified the line in FK5 world coordinates.

In [ ]:
ax = plt.subplot(111, projection=wcs)

ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')
ax.set_xlabel("Right Ascension [degrees]")
ax.set_ylabel("Declination [degrees]")
ax.coords.grid(color='white', alpha=0.5, linestyle='solid')
ax.set_autoscale_on(False)

ax.plot([2500, 3500], [2500,3500], 'cyan')
# Overplot in FK5 in Degrees
ax.plot([189.232, 189.25], [14.218, 14.23], 'yellow', transform=ax.get_transform('world'))

Overplotting Another Coordinate System


In [ ]:
ax = plt.subplot(111, projection=wcs)
ax.imshow(hdulist[0].data, cmap='gray', vmax=1000, interpolation=None, origin='lower')
ax.set_xlabel("Right Ascension [degrees]")
ax.set_ylabel("Declination [degrees]")
ax.coords.grid(color='white', alpha=0.5, linestyle='solid')

overlay = ax.get_coords_overlay('galactic')
overlay.grid(color='orange', alpha=1, linestyle='solid')
overlay['l'].set_axislabel("Galactic Longitude [degrees]")
overlay['b'].set_axislabel("Galactic Latitude [degrees]")

SunPy Map

The SunPy Map class is a wrapper for solar images which makes some of the above opertations easier.


In [ ]:
import sunpy.map
from sunpy.data.sample import AIA_171_ROLL_IMAGE

amap = sunpy.map.Map(AIA_171_ROLL_IMAGE)
amap.peek()

This has done a quick plot of the test image AIA_171_ROLL_IMAGE using wcsaxes. If we want to customise the plot we use the plot() method.


In [ ]:
import astropy.units as u

In [ ]:
amap = sunpy.map.Map(AIA_171_ROLL_IMAGE)

im = amap.plot()
ax = plt.gca()
ax.set_autoscale_on(False)

x = 500*u.arcsec
y = -300*u.arcsec

ax.plot(x.to(u.deg), y.to(u.deg), 'o', transform=ax.get_transform('world'))

In [ ]:
amap.pixel_to_data(100*u.pix, 200*u.pix)

In [ ]:
amap.data_to_pixel(0*u.arcsec, 0*u.arcsec)

Rotate your Owl

Why is the Sun wonky? Use the [`rotate()`](http://docs.sunpy.org/en/stable/code_ref/map.html#sunpy.map.mapbase.GenericMap.rotate) method of SunPy Map to align the coordinate grid to the pixel grid in this sample image. Once you have run rotate, plot the resulting image, and compare with the one above.

In [ ]:
mr = amap.rotate()
mr.peek()