首页 > 解决方案 > 使用多个适合图像制作图像立方体

问题描述

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

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 个。

标签: pythonastronomyfits

解决方案


最简单的方法是简单地将您想要在立方体中的图像放入一个numpy数组中,然后将该数组保存为 fit 文件。您也可以直接将它们保存到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)

推荐阅读