python - 如何从 astropy 中的 URL 打开 FITS 文件?

标签 python urllib2 astropy

我在 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/

相关文章:

Python 3 只能在某些文件夹中导入 astropy

python - 如何用pyfits合并两个表?

python - 使用 BeautifulSoup 将所有 href 抓取到列表中

python - 使用 python 使用子进程从 7zip 存档中解压选定的文件

python - 当 URL 存在时 urllib2 捕获 404 错误

python - 在 Python 中使用 OAuth 的 Twitter 流在两台配置相同的机器上表现不同

python - 如何从 float 中删除字符?

python - Django 1.9 URLField 删除必要的 http ://prefix

python - python中使用POST上传文件。去哪里看?

python - astropy 适合多个 header