我在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'.
即使我在mode='readonly'
中设置了fits.open()
,它也表示它不能写入类似文件的对象。
有办法从.fits打开文件吗?还是将urlopen()
返回的urlopen()
文件字节转换回HDUList?
发布于 2015-09-17 19:37:22
基于astropy.io.fits.open
,它可以选择从URL读取.fits文件的内容:
缓存: bool,可选 如果文件名是URL,则使用download_file打开文件。这指定是否在Astropy的下载缓存中本地保存文件(默认值: 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>]
https://stackoverflow.com/questions/32643206
复制