question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't update data in a fits table

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
SaOgazcommented, Jul 22, 2016

Hi @csnap001, for this kind of question, you may have better luck asking on http://stackoverflow.com/ or another programming question website, as this is more of a general programming question and not an issue with the astropy library itself. The github issue pages are more geared towards bugs and or feature request.

0reactions
saimncommented, May 11, 2017

Closing as there is no issue in astropy.io.fits here (I think). Note that for FITS table you have also the more convenient Table interface: http://docs.astropy.org/en/latest/io/unified.html#fits

Read more comments on GitHub >

github_iconTop Results From Across the Web

FITS File Handling (astropy.io.fits)
The open() function supports a memmap=True argument that allows the array data of each HDU to be accessed with mmap, rather than being...
Read more >
FITS File handling (astropy.io.fits)
Working with large files. The open() function supports a memmap=True argument that allows the array data of each HDU to be accessed with...
Read more >
How does one update a FITS data array with PyFITS?
I'm trying to create a new FITS file out of two older ones using PyFITS. import pyfits from sys import stdout from sys...
Read more >
Create a make table query - Microsoft Support
For example, suppose you have a table (or tables) of past sales data, and you use that data in reports. The sales figures...
Read more >
lecture-3---numerical-manipulation-ii.pdf - Mubdi Rahman
FITS files can store multidimensional data. (commonly 2 or 3 dimensions). Any given FITS file can contain multiple images (or tables) called extensions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found