1

I'm extracting extensions from a multi-extension FITS file, manipulate the data, and save the data (with the extension's header information) to a new FITS file.

To my knowledge pyfits.writeto() does the task. However, when I give it a data parameter in the form of an array, it gives me the error:

    'AttributeError: 'numpy.ndarray' object has no attribute 'lower''

Here is a sample of my code:

    'file = 'hst_11166_54_wfc3_ir_f110w_drz.fits'
     hdulist = pyfits.open(dir + file)'
     sci = hdulist[1].data # science image data
     exp = hdulist[5].data # exposure time data
     sci = sci*exp # converts electrons/second to electrons
     file = 'test_counts.fits'

     hdulist.writeto(file,sci,clobber=True)

     hdulist.close()

I appreciate any help with this. Thanks in advance.

  • 1
    Even if you can, avoid the use of the variable name `file` since it means sth in Python. Also, you have an extra `'` before `file`, is it a mistake you did while copying the code? Besides, can we see the whole code? The error is talking about the `lower` method, but I don't see it in your code. – tomasyany Aug 01 '15 at 03:06

2 Answers2

1

You're confusing the HDUList.writeto method, and the writeto function.

What you're calling is a method on the HDUList object that is returned when you call pyfits.open. You can think of this object as something like a file handle to your original drizzled FITS file. You can manipulate this object in place and either write it out to a new file or save updates in place (if you open the file in mode='update').

The writeto function on the other hand is not tied to any existing file. It's just a high-level function for writing an array out to a file. In your example you could write your array of electron counts out like:

pyfits.writeto(filename, data)

This will create a single-HDU FITS file with the array data in the PRIMARY HDU.

Do be aware of the admonishment at the top of this section of the docs: http://docs.astropy.org/en/v1.0.3/io/fits/index.html#convenience-functions

The functions like pyfits.writeto are there for convenience in interactive work, but are not recommendable for use in code that will be run repeatedly, as in a script. Instead have a look at these instructions to start.

Community
  • 1
  • 1
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
-1

It is probably because you should use hdulist.writeto(file, clobber=True). There is only one required argument: https://pythonhosted.org/pyfits/api_docs/api_hdulists.html#pyfits.HDUList.writeto If you give a second argument, it is used for output_verify which should be a string, not a numpy array. This probably explains your AttributeError ....

saimn
  • 443
  • 2
  • 10
  • Although you're correct that they're not passing correct arguments to the writeto method, it's more that they're confusing two different interfaces in the API. See my answer. – Iguananaut Aug 04 '15 at 14:18