我在一个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()
它说它也不能写入类似文件的对象。
有没有办法从URL打开.fits文件?还是将返回的.fits文件字节转换urlopen()
回HDUList?
根据的文档astropy.io.fits.open
,它具有从URL读取.fits文件的内容的选项:
缓存:布尔值,可选
如果文件名是URL,则使用download_file打开文件。这指定是否在Astropy的下载缓存中本地保存文件(默认值:True)。
这意味着您不必使用urllib2
。你可以只给target_url
于fits.open
马上,因为它调用astropy.utils.data.download_file
打开之前的URL。请参阅下面的代码。
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>]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句