我有一大堆的配合文件,可以使用下面的脚本读取
from astropy.io import fits
hdu = fits.open('file.fits')
data = hdu[0].data
我试图让使用读取多个适合文件数据的图像立方体。 (图像立方体是包含从多个拟合数据文件,其中x和y轴是2D图像尺寸和第三轴是时间或频率上的3D图像)
我相信它可以使用频谱立方体模块来完成,但是大多数文档仅约如何读取图像立方体,而不是如何让一个使用单独的适合文件的谈判。
到目前为止,我已经尝试下面的脚本。
#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')
然而,上面的脚本给出了一个错误说法,而只有2给出需要3个参数。
很显然,一个并不需要使用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
阵列,但附加表是比较容易,如果你的循环做一个,而不是明确地做它的每个图像,就像我在这里做。
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)