使用多个拟合图像制作图像立方体

Making an Image Cube using multiple fits images

我有一堆适合的文件可以使用下面的脚本读取

from astropy.io import fits
hdu = fits.open('file.fits')
data = hdu[0].data

我正在尝试使用从多个拟合文件中读取的数据制作图像立方体。 (图像立方体是包含来自多个拟合文件的数据的 3D 图像,其中 x 和 y 轴是 2D 图像维度,第 3 轴是时间或频率)

我相信这可以使用 spectral _cube 模块来完成,但是大多数文档只讨论如何读取图像立方体,而不是如何使用单独的拟合文件制作图像立方体。

到目前为止,我已经尝试了以下脚本。

#In the below script data is a 3D numpy array
from spectral_cube import SpectralCube
cube = SpectralCube(data=data)
cube.write('new_cube.fits', format='fits')

但是,上面的脚本给出了一个错误,说需要 3 个参数,而只给出了 2 个参数。

显然,制作图像立方体不需要使用spectral_cube模块。使用 AstroPy python 模块可以很容易地完成。下面是脚本。

from astropy.io import fits
import numpy as np

cube = np.zeros((50,1000,1000)) #Here 1000x1000 is the dimension of the individual fits images and 50 is the third perpendicular axis(time/freq)

for i in range(50):
    hdu = fits.open('image' + str(i) + '.fits') #The fits images that you want to combine have the name string 'image' + str(i) + '.fits'
    data = hud[0].data[:,:]
    cube[i,:,:] = data

hdu_new = fits.PrimaryHDU(cube)
hdu_new.writeto('cube.fits')

执行此操作的最简单方法是将您想要在立方体中拥有的图像简单地放入一个 numpy 数组中,然后将该数组保存为适合文件。您也可以直接将它们保存到 numpy 数组中,但是如果您在 for 循环中添加列表会更容易,而不是像我在这里那样为每个图像显式地添加列表。

import numpy as np
from astropy import fits

# Read the images you want to concatenate into a cube
img1 = fits.getdata('img1.fits')
img2 = fits.getdata('img2.fits')

# Make a list that will hold all your images
img_list = []
img_list.append(img1)
img_list.append(img2)

# Cast the list into a numpy array
img_array = np.array(img_list)

# Save the array as fits - it will save it as an image cube
fits.writeto('mycube.fits', img_array)