使用xarray读取授权的openap url



我想使用xarray打开一个opendap url。它需要UCAR RDA所持有的授权:

https://rda.ucar.edu/datasets/ds084.1/!描述

一个文件的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'

Siphon的session_manager可能会暗示auth是什么样子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

感谢Ryan May为我指出https://publicwiki.deltares.nl/display/OET/Accessing+netCDF+data+via+OPeNDAP+on+password+protected+servers

在我的主目录中创建点文件允许我读取url。可能不是最干净的,我想象可能会导致VM/集群上的问题,但工作。仍然希望backend_kwargs的方法。

在主目录下创建一个.netrc文件,如下所示:

machine rda.ucar.edu
login USR
password PWD

和主目录下的.dodsrc文件,如下所示:

HTTP.COOKIEJAR=<HOME_DIR>/.cookies
HTTP.NETRC=<HOME_DIR>/.netrc

你现在可以传递需要认证的url:

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)

如果您不是绝对需要使用OPeNDAP,但只是想要一些类似OPeNDAP的东西与xarray接口,您可以使用THREDDS的CDMRemote协议代替。在这种情况下,我们可以通过requests使用Siphon对基本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)

相关内容

  • 没有找到相关文章

最新更新