使用Astropy将3d Numpy数组写入FITS文件 [英] Write 3d Numpy array to FITS file with Astropy

查看:122
本文介绍了使用Astropy将3d Numpy数组写入FITS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D NumPy数组(即(10,256,256)),代表256x256图像.我想使用astropy.io.fits将此数组写入FITS文件,以便可以使用ds9 -mecube打开文件并在框架中移动.我的尝试如下所示

I have a 3D NumPy array (i.e. (10, 256, 256)) representing 256x256 images. I would like to write this array to a FITS file using astropy.io.fits so that I can open the file using ds9 -mecube and move through the frames. My attempt is shown below

export_array = numpy.array(images) #Create an array from a list of images
print export_array.shape ## (10, 256, 256)

hdu = fits.PrimaryHDU(export_array)
hdulist = fits.HDUList([hdu])
hdulist.writeto(out_file_name)
hdulist.close()

这将给我一个FITS文件,该文件实际上包含3D阵列.但是,如果我使用ds9 -mecube打开,则只能看到第一张图像.无论如何,是否可以使用astropy.io.fits创建具有此功能的FITS文件?也许我缺少ds9的某些功能?

This will give me a FITS file which does in fact contain the 3D array. However if I open with ds9 -mecube I can only see the first image. Is there anyway to create the FITS file with this functionality using astropy.io.fits? Or is there perhaps some functionality with ds9 that I am missing?

推荐答案

我不使用ds9,但是显然-mecube选项的意思是多扩展多维数据集".文档说:将多扩展FITS文件作为数据多维数据集加载.您只是将单个数组作为数据多维数据集编写.要将其写为多扩展FITS,您可以执行以下操作:

I don't use ds9, but apparently the -mecube option means "multi-extension cube". The docs say "Load a multi-extension FITS file as a data cube. You're just writing a single array as a data cube. To write it as a multi-extension FITS you might do something like:

hdul = fits.HDUList()
hdul.append(fits.PrimaryHDU())

for img in export_array:
    hdul.append(fits.ImageHDU(data=img))

hdul.writeto('output.fits')

(您不需要调用hdul.close()-仅当从现有文件加载HDUList并且您要关闭基础文件对象时才执行任何操作;对于HDUList无效从头开始在内存中创建).

(You don't need to call hdul.close()--that only does anything if the HDUList was loaded from an existing file and you want to close the underlying file object; it has no effect for an HDUList created from scratch in memory).

我不知道ds9期望将多扩展FITS文件作为数据多维数据集加载是什么-这不是任何特定的FITS约定,文档也不清楚.但这可能就是这样.

I don't know exactly what ds9 is expecting for loading a multi-extension FITS file as a data cube--this isn't any specific FITS convention and the docs arent't clear. But it's probably something like that.

所有这些,根据ds9文档,您根本不需要使用它.如果不使用-mecube选项,它将仅读取主HDU中的3D数组作为数据多维数据集.

All that said, according to the ds9 docs you don't need to use this at all. If you don't use the -mecube option it will just read the 3D array in the primary HDU as a data cube.

这篇关于使用Astropy将3d Numpy数组写入FITS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆