我在 URL 上有一个 .fits 文件,我想将其读入 Python,就好像它就在我的机器上一样。我试过的是:
import urllib2 as url, astropy.io.fits as fits
target_url = 'https://s3.amazonaws.com/bdnyc/spex_prism_U50171_0835%2B19_chiu06.fits'
obj = url.urlopen(target_url)
dat = fits.open(obj)
但我只是得到IOError: File-like object does not have a 'write' method, required for mode 'ostream'。
即使我在 fits.open()
中设置了 mode='readonly'
,它也说它不能写入类似文件的对象。
有没有办法从 URL 打开 .fits 文件?或者将 urlopen()
返回的 .fits 文件字节转换回 HDUList?
最佳答案
基于documentation of astropy.io.fits.open
,它可以选择从 URL 读取 .fits 文件的内容:
cache : bool, optional
If the file name is a URL, download_file is used to open the file. This specifies whether or not to save the file locally in Astropy’s download cache (default: True).
这意味着您不必使用 urllib2
。您可以立即将 target_url
提供给 fits.open
,因为它会在打开 URL 之前调用 astropy.utils.data.download_file
。请参阅下面的代码。
In [1]: import astropy.io.fits as fits
In [2]: target_url = 'https://s3.amazonaws.com/bdnyc/spex_prism_U50171_0835%2B19_chiu06.fits'
In [3]: dat = fits.open(target_url)
In [4]: dat
Out[4]: [<astropy.io.fits.hdu.image.PrimaryHDU at 0x219a9e8>]
关于python - 如何从 astropy 中的 URL 打开 FITS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643206/