考虑以下代码(下载test.fits):
from astropy.io import fits
from photutils.utils import cutout_footprint
# Read fits file.
hdulist = fits.open('test.fits')
hdu_data = hdulist[0].data
hdulist.close()
# Some center and box to crop
xc, yc, xbox, ybox = 267., 280., 50., 100.
# Crop image.
hdu_crop = cutout_footprint(hdu_data, (xc, yc), (ybox, xbox))[0]
# Add comment to header
prihdr = hdulist[0].header
prihdr['COMMENT'] = "= Cropped fits file")
# Write cropped frame to new fits file.
fits.writeto('crop.fits', hdu_crop, prihdr)
原始(左)和裁剪(右)图像如下所示:
框架中心的星星的(ra,dec)equatorial coordinates是:
Original frame: 12:10:32 +39:24:17
Cropped frame: 12:12:07 +39:06:50
为什么裁剪框架中的坐标不同?
这是使用两种不同方法解决此问题的两种方法。
from astropy.io import fits
from photutils.utils import cutout_footprint
from astropy.wcs import WCS
from astropy.nddata.utils import Cutout2D
import datetime
# Read fits file.
hdulist = fits.open('test.fits')
hdu = hdulist[0].data
# Header
hdr = hdulist[0].header
hdulist.close()
# Some center and box to crop
xc, yc, xbox, ybox = 267., 280., 50., 100.
# First method using cutout_footprint
# Crop image.
hdu_crop = cutout_footprint(hdu, (xc, yc), (ybox, xbox))[0]
# Read original WCS
wcs = WCS(hdr)
# Cropped WCS
wcs_cropped = wcs[yc - ybox // 2:yc + ybox // 2, xc - xbox // 2:xc + xbox // 2]
# Update WCS in header
hdr.update(wcs_cropped.to_header())
# Add comment to header
hdr['COMMENT'] = "= Cropped fits file ({}).".format(datetime.date.today())
# Write cropped frame to new fits file.
fits.writeto('crop.fits', hdu_crop, hdr)
# Second method using Cutout2D
# Crop image
hdu_crop = Cutout2D(hdu, (xc, yc), (xbox, ybox), wcs=WCS(hdr))
# Cropped WCS
wcs_cropped = hdu_crop.wcs
# Update WCS in header
hdr.update(wcs_cropped.to_header())
# Add comment to header
hdr['COMMENT'] = "= Cropped fits file ({}).".format(datetime.date.today())
# Write cropped frame to new fits file.
fits.writeto('crop.fits', hdu_crop.data, hdr)
答案 0 :(得分:3)
photutils.utils.cutout_footprint仅剪切像素,它不会更新用于在像素和世界坐标之间转换的WCS。
答案 1 :(得分:2)
The coordinates changed because you sliced the image but you did not alter the WCS informations (especially the reference pixel values).
One way would be to use astropy.WCS
:
from astropy.wcs import WCS
wcs = WCS(hdulist[0].header)
wcs_cropped = wcs[280-50 : 280+50 , 267-25 : 267+25]
and then copy this updated wcs to your header:
prihdr.update(wcs_cropped.to_header())
before saving the file.
I'm not sure about what cutout_footprint
does so maybe you need to change the slice indices when creating wcs_cropped
.
There is an convenience functionality in astropy.nddata
named Cutout2D
which updates the WCS
by default.
答案 2 :(得分:1)
另一个答案是因为它需要一个额外的包:ccdproc
,尤其是基于astropy.nddata.NDData
的课程CCDData
:
它简化了阅读文件:
from ccdproc import CCDData
ccd = CCDData.read(filename, unit='erg/cm**2/s/AA')
必须指定单位,因为标题单元与astropy.units
模块不兼容。
关于CCDData
的重要一点是,您(大部分)不需要照顾units
,wcs
,header
和masks
通过自己,它们被保存为属性,最基本的操作相应地保留(和更新)它们。例如切片:
xc, yc, xbox, ybox = 267., 280., 50., 100.
ccd_cropped = ccd[int(yc - ybox // 2) : int(yc + ybox // 2),
int(xc - xbox // 2) : int(xc + xbox // 2)]
此切片ccd_cropped
已更新WCS信息,因此您只需继续处理它(如再次保存):
ccd_cropped.write('cropped.fits')
它应该有正确更新的坐标。切片部分实际上也可以使用astropy.nddata.NDDataRef
,只有read
和write
部分在ccdproc.CCDData
中明确实现