xarray 在遍历 opendap 数据集时出现 HDF 错误

问题描述

我正在尝试在 Windows 10 上使用 xarray 和 python 3.7 从 OpenDAP 服务器下载一些数据,遍历一系列站点和年份并写入本地文件。下面的一个简单示例使用了两个,但对我来说会失败:

import xarray as xr
stations = ["pxsc1","obxc1"]
for station in stations:
    for year in ["2019","2020"]: 
        print(f"Working on station: {station} year: {year}")
        ndbc_url  = f"http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/{station}/{station}h{year}.nc"
        print(ndbc_url)
        fileout = "noaa_stdmet_{}_{}.nc".format(station,year)
        print(fileout)
        with xr.open_dataset(ndbc_url,engine='netcdf4') as remote:
            remote.to_netcdf(fileout,format="NETCDF4_CLASSIC")

一个示例似乎运行正常,然后出现如下所示的 HDF 错误HDF5_USE_FILE_LOCKING 的值似乎不会影响行为......我不太明白这个限制,但我已经尝试过了。还有其他方法可以让它发挥作用吗?

Working on station: pxsc1 year: 2019
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/pxsc1/pxsc1h2019.nc
noaa_stdmet_pxsc1_2019.nc
Working on station: pxsc1 year: 2020
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/pxsc1/pxsc1h2020.nc
noaa_stdmet_pxsc1_2020.nc
Traceback (most recent call last):
  File "F:\atmospheric\noaa\ndbc_xarray2.py",line 57,in <module>
    ndbc_opendap()
  File "F:\atmospheric\noaa\ndbc_xarray2.py",line 54,in ndbc_opendap
    remote.to_netcdf(fileout,format="NETCDF4")
  File "C:\Users\eli\miniconda3\envs\schimpy_env\lib\site-packages\xarray\core\common.py",line 1499,in __exit__
    self.close()
  File "C:\Users\eli\miniconda3\envs\schimpy_env\lib\site-packages\xarray\core\common.py",line 1294,in close
    self._close()
  File "C:\Users\eli\miniconda3\envs\schimpy_env\lib\site-packages\xarray\backends\netCDF4_.py",line 512,in close
    self._manager.close(**kwargs)
  File "C:\Users\eli\miniconda3\envs\schimpy_env\lib\site-packages\xarray\backends\file_manager.py",line 222,in close
    file.close()
  File "netCDF4\_netCDF4.pyx",line 2276,in netCDF4._netCDF4.Dataset.close
  File "netCDF4\_netCDF4.pyx",line 2260,in netCDF4._netCDF4.Dataset._close
  File "netCDF4\_netCDF4.pyx",line 1754,in netCDF4._netCDF4._ensure_nc_success
RuntimeError: NetCDF: HDF error

 

解决方法

这似乎不是你的代码的问题,因为在我的电脑上一切正常:

Working on station: pxsc1 year: 2019
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/pxsc1/pxsc1h2019.nc
noaa_stdmet_pxsc1_2019.nc
Working on station: pxsc1 year: 2020
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/pxsc1/pxsc1h2020.nc
noaa_stdmet_pxsc1_2020.nc
Working on station: obxc1 year: 2019
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/obxc1/obxc1h2019.nc
noaa_stdmet_obxc1_2019.nc
Working on station: obxc1 year: 2020
http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/obxc1/obxc1h2020.nc
noaa_stdmet_obxc1_2020.nc

不过,我认为是因为计算能力有限。我假设 Python 仍然存储了第一个数组(工作得很好)但没有足够的内存来存储第二个数组。这就是它返回 RuntimeError 并可能重新启动内核 (?) 的原因。

尝试在 with 语句的末尾添加 remote.close()。这可能会解决它。

是否有必要使用NETCDF4_CLASSIC?我更喜欢使用 NETCDF4 代替,据我所知,这更常见(?)。我怀疑这是否能解决您的问题,但您可以尝试一下。

以下是对您的代码稍加修改的版本:

import xarray as xr 

stations = ['pxsc1','obxc1']
year = [str(x) for x in range(2019,2020+1,1)]

url_base = 'http://dods.ndbc.noaa.gov/thredds/dodsC/data/stdmet/'
files_in = [f"{url_base}{s}/{s}h{y}.nc" for s in stations for y in year]
files_out = [f"noaa_stdmet_{s}_{y}.nc" for s in stations for y in year]

def load(file,fileout):
    print(f"open file: {file}")
    with xr.open_dataset(file,engine='netcdf4') as remote:
        print(f"writing file: {fileout}")
        remote.to_netcdf(fileout,format="NETCDF4")
        remote.close()

for fin,fout in list(zip(files_in,files_out)):
    load(fin,fout)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...