0

I am working with FITS files and to open a FITS file in the usual way I have to go through two commands:

hdu = fits.open('image1.fits')
data = hdu[0].data

Now I do realize that this is necessary in order to get the primary data of the FITS file from the header. But I was wondering if there was any way to convert it to a single command that would just get me the primary data of a FITS file just by mentioning the name of the file. (Or any other way to converting this two step operation into a single step operation.)

Blue Panda
  • 15
  • 4

3 Answers3

0

Apparently the answer was very straightforward and I found it myself after thinking about it a bit. All you have to do it write it like this:

data = fits.open('image1.fits')[0].data
Blue Panda
  • 15
  • 4
0

Although you can put the command in one line as such:

data = fits.open('image1.fits')[0].data

Best practice is to use a with statement:

with open("image1.fits", "r") as f:
    data = f[0].data

See What is the python “with” statement designed for?

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
0

There's nothing wrong with the other answers, but a more specific answer to this exists and it's getdata()

See also Convenience Functions.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63