[AstroPy] Convolving FITS images

Adam Ginsburg adam.g.ginsburg at gmail.com
Fri Oct 10 19:11:26 EDT 2014

Hi Rene,
    Everything you asked for can be accomplished with a few astropy utilities:

from astropy.io import fits
data = fits.getdata('filename.fits')
header = fits.getheader('filename.fits')

from astropy.convolution import convolve,Gaussian2DKernel
fwhm = 5
smoothed = convolve(data, Gaussian2DKernel(stddev=fwhm/2.35)

import aplpy
smoothed_figure = aplpy.FITSFigure(fits.PrimaryHDU(data=smoothed,

# Alternatively, display and smooth simultaneously:
fig = aplpy.FITSFigure('filename.fits')
# but this is less flexible

Vertical slicing is a little more difficult if you want to preserve
world coordinate information, but doable:
from astropy import wcs
w = wcs.WCS(header)
# Determine the approximate pixel scale, assuming square pixels [you
are likely to get an important warning here]
# Also, this may require the development version of astropy
cd = wcs.utils.celestial_pixel_scale(w)
slice = data[50,:] # grab a slice along one direction
offsets = np.arange(slice) * cd

# then, print to file:
with open('outfile','w') as f:
    for position,value in zip(offsets,slice):
       f.write("{0:10f} {1:10f}\n".format(position,value)

This is all untested, so excuse typos, but it provides at least a
skeleton working example, if I understood your question.

On Fri, Oct 10, 2014 at 10:30 PM, Rene Plume <rplume at ucalgary.ca> wrote:
> Hi!
> I'm fairly new to python and have been trying to figure out how to use it
> (using both astropy and aplpy) to do some rather standard things without too
> much success.  For example, how would I read in a FITS image (I figured this
> part out), plot it in wcs coordinates, and then convolve the image with a
> telescope beam of a fixed angular FWHM?  I then want to take a vertical
> slice of my image so that I have an output ascii file of (say) RA (or RA
> offset from some reference position) versus flux.
> Thanks!
> ##########################
> René Plume
> Department of Physics & Astronomy
> University of Calgary
> Phone:(403) 220-6909
> URL: www.ism.ucalgary.ca
> email: rplume at ucalgary.ca
> "I will be so brief I have already finished"
>                 - Salvador Dali in the World's shortest speech
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy

Adam Ginsburg
Fellow, European Southern Observatory

More information about the AstroPy mailing list