我想用xarray打开一个opendap url。它需要授权,因为它在UCAR RDA:
https://rda.ucar.edu/datasets/ds084.1/#!description
一个文件的url如下'https://rda.ucar.edu/thredds/dodsC/files/g/ds084.1/2020/20200101/gfs.0p25.2020010100.f000.grib2‘
我不确定我能否以backend_kwarg的身份通过授权?
下面的代码将给出一条错误消息
import xarray as xr
url = "https://rda.ucar.edu/thredds/dodsC/files/g/ds084.1/2020/20200101/gfs.0p25.2020010100.f000.grib2"
ds = xr.open_dataset(url)
Traceback (most recent call last):
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 199, in _acquire_with_cache_info
file = self._cache[self._key]
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/lru_cache.py", line 53, in __getitem__
value = self._cache[key]
KeyError: [<class 'netCDF4._netCDF4.Dataset'>, ('https://rda.ucar.edu/thredds/dodsC/files/g/ds084.1/2020/20200101/gfs.0p25.2020010100.f000.grib2',), 'r', (('clobber', True), ('diskless', False), ('format', 'NETCDF4'), ('persist', False))]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/api.py", line 572, in open_dataset
store = opener(filename_or_obj, **extra_kwargs, **backend_kwargs)
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/netCDF4_.py", line 364, in open
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/netCDF4_.py", line 314, in __init__
self.format = self.ds.data_model
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/netCDF4_.py", line 373, in ds
return self._acquire()
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/netCDF4_.py", line 367, in _acquire
with self._manager.acquire_context(needs_lock) as root:
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 187, in acquire_context
file, cached = self._acquire_with_cache_info(needs_lock)
File "/Users/ray.bell/miniconda/envs/test_env/lib/python3.8/site-packages/xarray/backends/file_manager.py", line 205, in _acquire_with_cache_info
file = self._opener(*self._args, **kwargs)
File "netCDF4/_netCDF4.pyx", line 2357, in netCDF4._netCDF4.Dataset.__init__
File "netCDF4/_netCDF4.pyx", line 1925, in netCDF4._netCDF4._ensure_nc_success
OSError: [Errno -78] NetCDF: Authorization failure: b'https://rda.ucar.edu/thredds/dodsC/files/g/ds084.1/2020/20200101/gfs.0p25.2020010100.f000.grib2'虹吸的session_manager可能会暗示身份验证看起来像https://unidata.github.io/siphon/latest/examples/Basic_Usage.html#sphx-glr-examples-basic-usage-py / https://github.com/Unidata/siphon/blob/master/siphon/http_util.py#L52
发布于 2021-02-13 05:41:40
感谢Ryan May将我引向https://publicwiki.deltares.nl/display/OET/Accessing+netCDF+data+via+OPeNDAP+on+password+protected+servers
在我的home dir中创建dot文件允许我读取url。可能不是最干净的,我想可能会在虚拟机/集群上引起问题,但可以正常工作。仍然希望有一种backend_kwargs方法。
在您的主目录中创建一个文件.netrc,如下所示:
machine rda.ucar.edu
login USR
password PWD在您的主目录中有一个文件.dodsrc,如下所示:
HTTP.COOKIEJAR=<HOME_DIR>/.cookies
HTTP.NETRC=<HOME_DIR>/.netrc您现在可以传递需要身份验证的urls:
import xarray as xr
url = "https://rda.ucar.edu/thredds/dodsC/files/g/ds084.1/2020/20200101/gfs.0p25.2020010100.f000.grib2"
ds = xr.open_dataset(url)发布于 2021-02-13 09:04:54
如果您完全不需要使用OPeNDAP,但只需要某种与xarray接口的类似OPeNDAP的东西,那么您可以使用THREDDS的CDMRemote协议。在这种情况下,我们可以通过requests利用信标对基本HTTP身份验证的支持
from siphon.catalog import TDSCatalog
from siphon.http_util import session_manager
# Set options for Siphon's HTTP session manager--in this case user/password
session_manager.set_session_options(auth=('MYUSER', 'MYPASSWORD'))
cat = TDSCatalog('https://rda.ucar.edu/thredds/catalog/files/g/ds084.1/2020/20200101/catalog.xml')
selected_dataset = cat.datasets[0]
ds = selected_dataset.remote_access(service='CDMRemote', use_xarray=True)https://stackoverflow.com/questions/66178846
复制相似问题