astropy: error creating a new FITS image with data & header
(UPDATE BY @embray: @mrawls found that ndarrays with an ‘object’ dtype were mistakenly being used to write FITS images. That said, PyFITS should catch unsupported data types sooner and raise a more helpful error message (I think there are places where it does do this, but this particular corner case slips through the cracks).
I am attempting to create a new FITS image file with a single column of spectral data and a header. I’m relatively new to python and astropy, so any help is appreciated. I’m using Python 2.7 and astropy version 0.2.4 on my Macbook Pro, which is running Mountain Lion 10.8.5.
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.io.fits import getdata
from astropy.io.fits import getheader
headernote = 'Data and wavelength scale modified w/2nd order telluric correction.'
wavestart = 3550.
dwave = 0.0455
infiles = "infiles_9246715.txt"
f1 = open(infiles) # open a text file containing a list of FITS filenames
# NOTE brandnewspeclist is a previously-defined list of numpy arrays.
# Each array is ~155000 values long, and contains floats between -0.17 and 2.0.
# I tried adding 1.0 to all values so everything was > 0, but there was no change.
i = 0
# Loop over each file, this time to create new versions in new files.
for line in f1:
infile = line.rstrip()
outfile = 'new_' + infile
# Read in the original FITS header
head = getheader(infile)
# Make a new FITS file with the new data and old header
n = np.arange(100.0)
#hdu = n #works with this
hdu = brandnewspeclist[i] #doesn't work with this
#print type(n), type(hdu) # I checked, and both are numpy.ndarrays
fout = open('test' + str(i) + '.txt', 'w')
for j in range(len(brandnewspeclist[i])):
fout.write('%f \n' % brandnewspeclist[i][j]) # outfile doesn't have any nans
# This is where it breaks and gives me a KeyError: 'object'
# (unless I've set hdu = n instead of brandnewspeclist[i])
fits.writeto(outfile, hdu, header=head)
# I tried this approach instead, but got the same error
#hdu = fits.PrimaryHDU(brandnewspeclist[i])
#hdu.writeto(outfile, header=head)
# Next step: create a new header object
newhead = getheader(outfile)
# Update the two header values to change the wavelength solution
# Note: this isn't working either. The code ran when I used 'n' instead of
# 'brandnewspeclist[i]' as hdu, but these header values didn't change.
# At the moment, however, this is a secondary concern.
newhead['cdelt1'] = (dwave, headernote)
newhead['crval1'] = (wavestart, headernote)
i = i + 1
f1.close()
And the error this generates is…
ERROR: KeyError: 'object' [astropy.io.fits.hdu.image]
Traceback (most recent call last):
File "telluric.py", line 230, in <module>
fits.writeto(outfile, hdu, header=head)
File "/Library/Python/2.7/site-packages/astropy/io/fits/convenience.py", line 402, in writeto
hdu = _makehdu(data, header)
File "/Library/Python/2.7/site-packages/astropy/io/fits/convenience.py", line 756, in _makehdu
hdu = _BaseHDU(data, header)
File "/Library/Python/2.7/site-packages/astropy/io/fits/hdu/image.py", line 795, in __init__
scale_back=scale_back)
File "/Library/Python/2.7/site-packages/astropy/io/fits/hdu/image.py", line 139, in **init**
self.data = data
File "/Library/Python/2.7/site-packages/astropy/utils/misc.py", line 262, in **set**
self._fset(obj, val)
File "/Library/Python/2.7/site-packages/astropy/io/fits/hdu/image.py", line 215, in data
self._bitpix = _ImageBaseHDU.ImgCode[data.dtype.name]
KeyError: 'object'
About this issue
- Original URL
- State: open
- Created 11 years ago
- Comments: 15 (9 by maintainers)
@aishrimc This seems to be a different issue altogether. If you also encounter the error after you upgraded could you open a new issue with a reproducible example?
some_array of shape [384,384,1]
was created from a binary file of shape [384,384,384], by picking out a slice from the third axis, namely:some_array=initial_array[:,:,192]
, which gave a [384,384] array, and then reshaping:np.reshape(some_array,(384,384,1))
I will upgrade and try in Astropy latest release under 3.x. Thanks @pllim