[英]Switch between coordinates of pixel and physical coordinates in n-dimensional FITS image using Astropy
I have a FITS image with a header containing information about the coordinates along each axis 我有一个FITS图像,其标题包含有关沿每个轴的坐标的信息
NAXIS = 3
NAXIS1 = 259
NAXIS2 = 272
NAXIS3 = 100
CDELT1 = -0.08333333330000001
CDELT2 = 0.08333333330000001
CDELT3 = 0.1
CRPIX1 = 130.5
CRPIX2 = 132.1906015909634
CRPIX3 = 0
CRVAL1 = 255.0
CRVAL2 = 60.0
CRVAL3 = 15.45
Is there an easy way (eg and Astropy function) to get the actual (physical) coordinates from the pixel coordinates? 是否有一种简单的方法(例如和Astropy函数)从像素坐标中获取实际(物理)坐标?
Conversely, is there a built-in function in Astropy to get the coordinates of the closest pixel from actual coordinates? 相反,在Astropy中是否有内置函数可以从实际坐标中获取最近像素的坐标?
Edit: I found the WCS.all_pix2world
function, but I don't understand how to use it and I didn't find any examples of use. 编辑:我找到了
WCS.all_pix2world
函数,但我不知道如何使用它,也没有找到任何使用示例。
From the documentation: 从文档中:
There are two accepted forms for the positional arguments:
- 2 arguments: An N x naxis array of coordinates, and an origin.
- more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.
In the first case, what is N
? 在第一种情况下,
N
多少? Wouldn't origin
always be [0,0,0]
? origin
永远不会是[0,0,0]
吗?
N
is the number of points for which you want to do the conversion, ie you can compute the pixel or world coordinates for an array of N points. N
是要对其进行转换的点数,即可以为N个点的数组计算像素或世界坐标。
origin
is just of matter of convention, about the coordinate of the upper left corner. origin
只是约定俗成的问题,关于左上角的坐标。 With Python arrays use 0 based indexing, so it is 0. See example below: 对于Python数组,请使用基于0的索引,因此它为0。请参见下面的示例:
In [1]: from astropy.wcs import WCS
In [2]: from astropy.io import fits
In [3]: hdr = fits.Header.fromstring("""\
...: NAXIS = 3
...: NAXIS1 = 259
...: NAXIS2 = 272
...: NAXIS3 = 100
...: CDELT1 = -0.08333333330000001
...: CDELT2 = 0.08333333330000001
...: CDELT3 = 0.1
...: CRPIX1 = 130.5
...: CRPIX2 = 132.1906015909634
...: CRPIX3 = 0
...: CRVAL1 = 255.0
...: CRVAL2 = 60.0
...: CRVAL3 = 15.45
...: """, sep='\n')
In [4]: wcs = WCS(hdr)
In [5]: wcs
Out[5]:
WCS Keywords
Number of WCS axes: 3
CTYPE : '' '' ''
CRVAL : 255.0 60.0 15.45
CRPIX : 130.5 132.1906015909634 0.0
PC1_1 PC1_2 PC1_3 : 1.0 0.0 0.0
PC2_1 PC2_2 PC2_3 : 0.0 1.0 0.0
PC3_1 PC3_2 PC3_3 : 0.0 0.0 1.0
CDELT : -0.0833333333 0.0833333333 0.1
NAXIS : 259 272 100
Now you can compute the world coordinate of the upper left corner: 现在您可以计算左上角的世界坐标:
In [6]: wcs.all_pix2world([[0, 0, 0]], 0)
Out[6]: array([[265.79166666, 49.06744987, 15.55 ]])
Here you could use an array of Nx3 indices. 在这里,您可以使用Nx3索引数组。
So if you have pixel indices and want to convert them to sky coordinates, you need to use origin=0, and the same for the opposite, to convert sky coordinates to pixel indices with wcs.all_world2pix
. 因此,如果您有像素索引并想将它们转换为天空坐标,则需要使用origin = 0,反之亦然,使用
wcs.all_world2pix
将天空坐标转换为像素索引。
Using origin=1 may sometimes be useful if you have pixel indices stored in a catalog using the FITS/Fortran convention. 如果您使用FITS / Fortran约定将像素索引存储在目录中,则使用origin = 1有时会很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.