I want to create an empty FITS image, by giving input dimension. I am doing that because I iterate over the content of the image and modify it as well, so I need to initialize the file at first with an empty image.
Using astropy, this was easy, but I am switching library to FitsIO and I haven't been able to translate this code to something actually working. I have been looking in the github project of FitsIO and I found an API called write_empty_hdu but I am clearly misusing it.
Here is my function:
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)
And here is the result:
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'
Do you know how I could write this image creation with FitsIO?
Thanks!
In general, the Python built in introspection and help is very strong. For example:
>>> 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']
As you can see there is no write_empty_hdu
, but there is a write
, which looks promising. So now:
>>>help(fitsio.write)
will show you all you need to know. In your case you probably want:
fitsio.write('somefile',np.empty(shape=(3,4)),header={'a': '','b': 'a','c': 3},clobber=True)
Note the numpy.empty
may write arbitrary values - so you may want zeros
to make sure you do not convince the data was something real.