从 Azure 文件存储读取 NetCDF 文件

Read NetCDF file from Azure file storage

我已将文件上传到我的 Azure 文件存储帐户并创建了 SAS(共享访问签名)。假设有问题的文件名为 fileA.nc

现在,Python3,我正在尝试阅读 fileA.nc:

from netCDF4 import Dataset

url ='https://<my-azure-resource-group>.file.core.windows.net/<some-file-share>/fileA.nc<SAS-token>';
dataset = Dataset(url)

print(dataset.variables.keys())

上面的代码不起作用,反而给我以下错误:

Traceback (most recent call last): File "yadaYadaYada/test.py", line 8, in dataset = Dataset(url) File "netCDF4/_netCDF4.pyx", line 1848, in netCDF4._netCDF4.Dataset.init (netCDF4/_netCDF4.c:13983) OSError: NetCDF: Malformed or unexpected Constraint

这是line 8:

 dataset = Dataset(url)

我知道 URL 提供的作品。如果我将其粘贴到浏览器中,文件将下载...

我查看了 netCDF4 documentation,上面写着:

Remote OPeNDAP-hosted datasets can be accessed for reading over http if a URL is provided to the Dataset constructor instead of a filename. However, this requires that the netCDF library be built with OPenDAP support, via the --enable-dap configure option (added in version 4.0.1).

但是,我不知道如何判断 Pycharms 安装 netcdf4 时是否使用了 --enable-dap argument,但我无法想象为什么不会。此外,如果我坚持指向某些 HTML 的 url,我会在错误转储中得到 HTML,因此我认为 netcdf4 实际上是在尝试加载远程数据集所以问题出在其他地方。

非常感谢您的帮助。也许有人知道另一个 Python 3 netCDF 库可以让我从 Azure 加载我的数据集?

更新

好的,我现在可以 confirm python netcdf4 库确实启用了 --OPenDAP:

Hello again, netCDF4 1.0.4 with OpenDAP support is now available in the conda respoitory on Unix. To install: $ conda install netcdf4

  • Ilan

我找到了解决办法。事实证明,您无法直接从 Azure 文件共享读取,即使当您将 link 粘贴到浏览器中的文件时,文件开始下载。

我需要做的是在我的 OS 上安装文件共享。就我而言,我使用的是 Windows,但这也可以使用 Linux 来完成。以下代码应相应修改,然后放入命令提示符:

net use <drive-letter>: \<storage-account-name>.file.core.windows.net\<share-name>

example :
net use z: \samples.file.core.windows.net\logs

安装文件共享后,您可以像读取外部硬盘一样从中读取数据。您可能需要添加权限,但我没有。

这里是 link 安装文件共享的文档:Documentation