首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xarray在迭代opendap数据集时出现HDF错误

xarray在迭代opendap数据集时出现HDF错误
EN

Stack Overflow用户
提问于 2021-05-05 07:54:33
回答 1查看 139关注 0票数 0

我正在尝试使用Windows10上的xarray和python3.7从OpenDAP服务器下载一些数据,遍历了一系列的站点和年份,并编写了本地文件。下面的一个简单示例分别使用其中的两个,对我来说可能会失败:

代码语言:javascript
复制
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的值似乎不会影响行为...我真的不理解这个限制,但我已经尝试过了。有没有其他方法可以让它工作呢?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-05-08 15:05:02

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

代码语言:javascript
复制
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,据我所知这更常见(?)。我怀疑这是否能解决你的问题,但你可以试一试。

下面是您的代码的一个稍微修改过的版本:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67393561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档