简体   繁体   中英

Open fits file from bz2

So I have a compressed fits file which I read like so:

File = bz2.BZ2File(fname)
fits = File.read()

I'm using astropy.io.fits to read fits files. At the moment I obtain my .fits.bz2 files by api so I would ideally like to be able to read them like hdulist = fits.open(fit)

But it just throws up TypeError: must be encoded string without NULL bytes, not str . ( I have checked that the file is good by unpacking it manually and reading it like above).

I guess my question is a general one: How to pass an open file to another function?

From the documentation for astropy you can find that:

astropy.io.fits.open takes a file name, an open file or a file-like object as it's first argument

You are attempting to pass the contents from the file into astropy , rather than the file object itself.

To get your example to work you would instead need to just pass the file object returned by bz2 into astropy which will process it like an uncompressed file.

from astropy.io import fits
import bz2

decompressed_file = bz2.BZ2File("example.fits.bz2")
hdulist = fits.open(decompressed_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM