简体   繁体   English

从bz2打开适合文件

[英]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. 我正在使用astropy.io.fits来读取拟合文件。 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) 目前我通过api获取我的.fits.bz2文件,所以我希望能够像hdulist = fits.open(fit)一样阅读它们

But it just throws up TypeError: must be encoded string without NULL bytes, not str . 但它只是抛出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文档中你可以发现:

astropy.io.fits.open takes a file name, an open file or a file-like object as it's first argument astropy.io.fits.open获取文件名,打开文件或类文件对象,因为它是第一个参数

You are attempting to pass the contents from the file into astropy , rather than the file object itself. 您正在尝试将文件中的内容传递给astropy ,而不是文件对象本身。

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. 为了让你的例子工作,你需要将bz2返回的文件对象传递给astropy,它将像未压缩文件一样处理它。

from astropy.io import fits
import bz2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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