Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping very large fits files using specified boundaries

I have a large fits file (over 30,000 x 30,000) pixels. IRAF cannot handle this size of image. How can one crop a file of this size while retaining correct header information, as IRAF does when using its standard cropping mode?

like image 628
ConnorG Avatar asked Sep 22 '15 22:09

ConnorG


1 Answers

You can do this sort of cropping with astropy.io.fits, though it's not trivial yet. Since astropy.io.fits uses memory mapping by default, it should be able to handle arbitrarily large files (within some practical limits). If you want non-python solutions, look here for details about postage stamp creation.

from astropy.io import fits
from astropy import wcs
f = fits.open('file.fits')
w = wcs.WCS(f[0].header)
newf = fits.PrimaryHDU()
newf.data = f[0].data[100:-100,100:-100]
newf.header = f[0].header
newf.header.update(w[100:-100,100:-100].to_header())

See also this pull request, which implements a convenience Cutout2D function, though this is not yet available in a released version of astropy. Its usage can be seen in the documentation, modified to include WCS:

from astropy.nddata import Cutout2D
position = (49.7, 100.1)
shape = (40, 50)
cutout = Cutout2D(f[0].data, position, shape, wcs=w)

There are more examples here

like image 154
keflavich Avatar answered Sep 23 '22 02:09

keflavich