Skip to content

Can't update data in a fits table #5192

@csnap001

Description

@csnap001

Hello, I asked this question in the wrong section previously. I'm trying to update a .fits file using astropy.io.fits. I do not have PyFits, I'm using astropy in its place.
for the following code the contents of Qname are ['003511.48-004918.0' '003657.17-100810.6' '005118.28+135448.1' '011845.55+133327.2']. However, this data was originally grabbed from a .fits file as follows

thdu = fits.open('dr7qso.fits')#raw information
tdata = thdu[1].data#gathering the data
name = tdata.field('SDSSJ')

name then rewritten into a numpy array and saved as Qname.
In addition I have the following import at the start of my code,

from astropy.io import fits

c1 = fits.Column('sdss_name', format = 'A18', array = Qname)
c2 = fits.Column('Plate', format = 'J', array = Pl)
c3 = fits.Column('Fiber', format = 'J', array = Fib)
c4 = fits.Column('MJD', format = 'J', array = date)
c5 = fits.Column('OIIrew', format = 'D', array = OIIrew)
c6 = fits.Column('OIIfwhm', format = 'D', array = OIIfwhm)
c7 = fits.Column('OIIIrew', format = 'D', array = OIIIrew)
c8 = fits.Column('OIIIfwhm', format = 'D', array = OIIIfwhm)
c9 = fits.Column('z_dr7', format = 'D', array = reds)
c10 = fits.Column('ContPow', format = 'D', array = contPower)
c11 = fits.Column('OIIPow', format = 'D', array = OIIpower)
c12 = fits.Column('r', format = 'D', array = rMag)
c13 = fits.Column('g-i', format = 'D', array = giMag)
c14 = fits.Column('r-z', format = 'D', array = rzMag)

OIIrew = np.array(OIIrew, dtype = np.float64)
OIIfwhm = np.array(OIIfwhm, dtype = np.float64)
OIIIrew = np.array(OIIIrew, dtype = np.float64)
OIIIfwhm = np.array(OIIIfwhm, dtype = np.float64)
Qname = np.array(Qname, dtype = np.object_)
reds = np.array(reds, dtype = np.float64)
contPower = np.array(contPower, dtype = np.float64)
OIIpower = np.array(OIIpower, dtype = np.float64)
rMag = np.array(rMag, dtype = np.float64)
giMag = np.array(giMag, dtype = np.float64)
rzMag = np.array(rzMag, dtype = np.float64)
Pl = np.array(Pl, dtype = np.string_)
Fib = np.array(Fib, dtype = np.string_)
date = np.array(date, dtype = np.string_)

if(not(path.exists('new.fits'))):
    newhdu = fits.new_table([c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14])
    newhdu.writeto('new.fits')
else:
    fits.open('new.fits')
    fits.update('new.fits', Qname, 'sdss_name')
    fits.update('new.fits', Pl, 'Plate')
    fits.update('new.fits', Fib, 'Fiber')
    fits.update('new.fits', date, 'MJD')
    fits.update('new.fits', OIIrew, 'OIIrew')
    fits.update('new.fits', OIIfwhm, 'OIIfwhm')
    fits.update('new.fits', OIIIrew, 'OIIIrew')
    fits.update('new.fits', OIIIfwhm, 'OIIIfwhm')
    fits.update('new.fits', reds, 'z_dr7')
    fits.update('new.fits', contPower, 'ContPow')
    fits.update('new.fits', OIIpower, 'OIIPow')
    fits.update('new.fits', rMag, 'r')
    fits.update('new.fits', giMag, 'g-i')
    fits.update('new.fits', rzMag, 'r-z')
    fits.flush()
    fits.close()

and here is the traceback

raceback (most recent call last):

File "", line 1, in 
runfile('C:/Users/owner/Documents/.spyder2/fittingTest.py', wdir='C:/Users/owner/Documents/.spyder2')

File "C:\Users\owner\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)

File "C:\Users\owner\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/owner/Documents/.spyder2/fittingTest.py", line 854, in 
fits.update('new.fits', Qname, 'sdss_name')

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\io\fits\convenience.py", line 525, in update
new_hdu = _makehdu(data, header)

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\io\fits\convenience.py", line 772, in _makehdu
hdu = ImageHDU(data)

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\io\fits\hdu\image.py", line 1011, in init
scale_back=scale_back)

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\io\fits\hdu\image.py", line 138, in init
self.data = data

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\utils\decorators.py", line 526, in set
ret = self.fset(obj, val)

File "C:\Users\owner\Anaconda2\lib\site-packages\astropy\io\fits\hdu\image.py", line 236, in data
self._bitpix = DTYPE2BITPIX[data.dtype.name]

KeyError: 'object'

Thanks, I really don't understand where the issue is coming from

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions