将WCS坐标分配给FITS图像

Tea*_*hey 4 python physics astronomy fits pyfits

我一直在疯狂搜索文档,却找不到答案。

我正在用python生成FITS图像,需要为图像分配WCS坐标。我知道有很多方法可以通过将点源与已知目录进行匹配来完成此操作,但是在这种情况下,我正在生成尘埃图,因此,据我所知,点源匹配不起作用。

因此,图像是形状的二维Numpy数组(240,240)。它的写法是这样的(x和y坐标分配有点怪异,以某种方式起作用):

H, xedges, yedges = np.histogram2d(glat, glon, bins=[ybins, xbins], weights=Av)
count, x, y = np.histogram2d(glat, glon, bins=[ybins, xbins])
H/=count
hdu = pyfits.PrimaryHDU(H)
hdu.writeto(filename)

>>> print H.shape
(240,240)
Run Code Online (Sandbox Code Playgroud)

一切都可以正常工作。对于分配银河坐标,您似乎需要做的只是:

glon_coords = np.linspace(np.amin(glon), np.amax(glon), 240)
glat_coords = np.linspace(np.amin(glat), np.amax(glat), 240)
Run Code Online (Sandbox Code Playgroud)

但是我不了解FITS图像如何存储这些坐标,所以我不知道如何编写它们。我也尝试过在SAO DS9中分配它们,但是没有运气。我只需要将这些坐标分配给图像的简单方法。

感谢您的任何帮助,您可以提供。

小智 5

我建议您开始使用astropy。对于您的项目而言,astropy.wcs包可以帮助您编写FITS WCS标头,并且astropy.io.fits API与您现在使用的pyfits基本相同。此外,帮助页面非常出色,我要做的就是翻译其WCS构建页面以匹配您的示例。

对于您的问题:FITS不会用坐标“标记”每个像素。我想可以创建一个像素查找表或类似的表,但是实际的WCS是X,Y像素到天文坐标(在您的情况下为“ Galactic”)的算法转换。一个漂亮的页面在这里

我要指出的示例在这里:

http://docs.astropy.org/en/latest/wcs/index.html#building-a-wcs-structure-programmatically

这是我未经测试的项目伪代码

# untested code

from __future__ import division # confidence high

# astropy
from astropy.io import fits as pyfits
from astropy import wcs

# your code
H, xedges, yedges = np.histogram2d(glat, glon, bins=[ybins, xbins], weights=Av)
count, x, y = np.histogram2d(glat, glon, bins=[ybins, xbins])
H/=count

# characterize your data in terms of a linear translation from XY pixels to 
# Galactic longitude, latitude. 

# lambda function given min, max, n_pixels, return spacing, middle value.
linwcs = lambda x, y, n: ((x-y)/n, (x+y)/2)

cdeltaX, crvalX = linwcs(np.amin(glon), np.amax(glon), len(glon))
cdeltaY, crvalY = linwcs(np.amin(glat), np.amax(glat), len(glat))

# wcs code ripped from 
# http://docs.astropy.org/en/latest/wcs/index.html

w = wcs.WCS(naxis=2)

# what is the center pixel of the XY grid.
w.wcs.crpix = [len(glon)/2, len(glat)/2]

# what is the galactic coordinate of that pixel.
w.wcs.crval = [crvalX, crvalY]

# what is the pixel scale in lon, lat.
w.wcs.cdelt = numpy.array([cdeltX, cdeltY])

# you would have to determine if this is in fact a tangential projection. 
w.wcs.ctype = ["GLON-TAN", "GLAT-TAN"]

# write the HDU object WITH THE HEADER
header = w.to_header()
hdu = pyfits.PrimaryHDU(H, header=header)
hdu.writeto(filename)
Run Code Online (Sandbox Code Playgroud)