如何使用 FitsIO (python) 创建一个空的 FITS 图像?

How to create an empty FITS image with FitsIO (python)?

我想通过提供输入维度来创建一个空的 FITS 图像。我这样做是因为我遍历图像的内容并修改它,所以我需要首先用一个空图像初始化文件。

使用 astropy,这很容易,但我正在将库切换到 FitsIO,但我无法将此代码转换为实际工作的代码。我一直在寻找 FitsIO 的 github 项目,我发现了一个名为 write_empty_hdu 的 API,但我显然误用了它。

这是我的函数:

 def create_image(self, x_size, y_size, header=None):
    """!
    @brief Create a FITS file containing an empty image.
    @param x_size  The number of pixels on the x axis.
    @param y_size  The number of pixels on the y axis.
    """
     if header is not None:
         self.fits.write_empty_hdu(dims=[x_size, y_size], header=header, clobber=True)
    else:
         self.fits.write_empty_hdu(dims=[x_size, y_size], clobber=True) 

结果如下:

ERROR : 'FITS' object has no attribute 'write_empty_hdu'

Traceback (most recent call last):

File "/home/user/Work/Projects/DET_CL_PZWAV/DET_CL_PZWav/python/DET_CL_PZWav/LE3_DET_CL_PZWav.py", line 159, in mainMethod pixel_grid.initialize_map()

File "/home/user/Work/Projects/DET_CL_PZWAV/DET_CL_PZWav/python/DET_CL_PZWav/Grid.py", line 232, in initialize_map

self.wavelet_map.create_image(self._pixel_number['Ny'], self._pixel_number['Nx'])

File "/home/user/Work/Projects/DET_CL_PZWAV/DET_CL_PZWav/python/DET_CL_PZWav/PixelSmoothedImage.py", line 57, in create_image super().create_image(x_size, y_size)

File "/home/user/Work/Projects/DET_CL_PZWAV/DET_CL_PZWav/python/DET_CL_PZWav/FITSImage.py", line 81, in create_image self.fits.write_empty_hdu(dims=[x_size, y_size], clobber=True)

AttributeError: 'FITS' object has no attribute 'write_empty_hdu'

你知道我如何用 FitsIO 编写这个图像创作吗?

谢谢!

总的来说,Python内置的自省和帮助很强。例如:

>>> import fitsio
>>> dir(fitsio)
['ASCII_TBL', 'BINARY_TBL', 'FITS', 'FITSCard', 'FITSHDR', 'FITSRecord', 'FITSRuntimeWarning', 'IMAGE_HDU', 'READONLY', 'READWRITE', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_fitsio_wrap', 'cfitsio_version', 'fitslib', 'read', 'read_header', 'read_scamp_head', 'test', 'util', 'write']

如您所见,没有 write_empty_hdu,但有一个 write,看起来很有希望。所以现在:

>>>help(fitsio.write)

会告诉你所有你需要知道的。在你的情况下你可能想要:

fitsio.write('somefile',np.empty(shape=(3,4)),header={'a': '','b': 'a','c': 3},clobber=True)

请注意 numpy.empty 可能会写入任意值 - 因此您可能希望 zeros 确保您不相信数据是真实的。

使用非常有用的 kabanus 输入,我查看了 FitsIO 中 FITS class 的可用 API,我发现 create_hdu_image

最后,我的功能不过是直接调用FitsIO:

def create_image(self, x_size, y_size, header=None):
    """!
    @brief Create a FITS file containing an empty image.
    @param x_size  The number of pixels on the x axis.
    @param y_size  The number of pixels on the y axis.
    """
    self.fits.create_image_hdu(img=None, dims=[x_size, y_size], dtype=("f8", "f8"), extver=0, header=header)