关于python:调整子图大小后如何更改标头?

How to change the header after resizing a submap?

我加载了完整的地图

1
2
3
4
5
6
from astropy.io import fits
from astropy.wcs import wcs

mapheader = fits.getheader(MapFile, 0)
mapdata = fits.getdata(MapFile, 0)
w = wcs.WCS(mapheader)

我从中取一个平方的子图
假设中心在RA,DEC中,以度为单位
这可以使用CutOut2D轻松完成

1
2
3
4
5
6
7
from astropy.nddata import Cutout2D
from astropy import coordinates
from astropy import units as u

center = coordinates.SkyCoord(RA*u.deg, DEC*u.deg, frame='fk5')
submap = Cutout2D(mapdata, center, size=16*u.pix, wcs=w)
submapheader = submap.wcs.to_header()

标头中的相关区别在于,它移动参考像素" CRPIX"

例如,如果我调整图像大小,则进行插值并传递16像素的图像
到128像素

1
2
from scipy.misc import imresize
newsubmap = imresize(submap.data, (128,128), interp=‘cubic’)

我应该如何更改标题以在newsubmap上获得良好的投影?

我尝试将参考像素乘以调整大小因子,在此示例中为128,但这并不简单


根据您的情况,scipy.misc.imresize将图像调整为(128, 128)。鉴于您的陈述:

I tried multiplying the reference pixel by the resizing factor, which is 128 in this example, but it's not simple as that.

我认为这是这里的第一个陷阱。您是否真的确定要调整为(128, 128)的大小,还是要"放大" 128倍,甚至是" 1.28"(请注意,imresize使用小数!)

1
2
3
4
5
6
>>> from scipy.misc import imresize
>>> import numpy as np
>>> imresize(np.ones((1000, 1000)), (100, 100)).shape  # to (100, 100)
(100, 100)
>>> imresize(np.ones((1000, 1000)), 50).shape  # half the size. 50->50%
(500, 500)

请确保您正确使用imresize

因此,下一步是重塑WCS的形状。幸运的是,WCS允许切片,因此,如果您知道原始形状和大小调整因子,这将非常容易。

假设您有一个WCS像这样:

1
2
3
4
5
6
7
8
9
>>> im.wcs
WCS Keywords
Number of WCS axes: 2
CTYPE : 'PIXEL'  'PIXEL'  
CRVAL : 2044.203  239.489  
CRPIX : 1022.1  119.7  
PC1_1 PC1_2  : 2.0  0.0  
PC2_1 PC2_2  : 0.0  2.0  
CDELT : 1.0  1.0

您可以将其切成薄片:

1
2
3
4
5
6
7
8
9
>>> im.wcs[::2, ::2]  # half the size
WCS Keywords
Number of WCS axes: 2
CTYPE : 'PIXEL'  'PIXEL'  
CRVAL : 2044.203  239.489  
CRPIX : 511.30000000000001  60.100000000000001  
PC1_1 PC1_2  : 2.0  0.0  
PC2_1 PC2_2  : 0.0  2.0  
CDELT : 2.0  2.0

或以小于1的步长对其进行切片以增加它:

1
2
3
4
5
6
7
8
9
>>> im.wcs[::1/128, ::1/128]  # assuming you increase each axis by a factor of 128.
WCS Keywords
Number of WCS axes: 2
CTYPE : 'PIXEL'  'PIXEL'  
CRVAL : 2044.203  239.489  
CRPIX : 130765.3  15258.1  
PC1_1 PC1_2  : 2.0  0.0  
PC2_1 PC2_2  : 0.0  2.0  
CDELT : 0.0078125  0.0078125

请注意,然后会忽略PCCD以及可能的SIP和其他变形。您必须手动处理它们。但是,将处理CDELT值,因此将正确处理简单的FITS文件。

注意:我删除了NAXIS关键字,因为它们可能在下一个版本中更改,因此无论如何它们都不可靠。这些当前未处理,在下一版本中,仅当"开始,停止和步进"为整数或"无"时,它们才会处理。