爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 833|回复: 2

[求助] python批量读取nc文件报错RuntimeError: NetCDF: HDF error

[复制链接]

新浪微博达人勋

发表于 2023-11-12 19:04:42 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
原代码如下:
import numpy as np
import pandas as pd
import xarray as xr
import netCDF4 as nc
import math
import h5netcdf

u = np.zeros([300,4,17,121,113])
v = np.zeros([300,4,17,121,113])
ws = np.zeros([300,4,121,113])

dataset1 = xr.open_dataset('E:/LLWS-data/MERRA2/MERRA2_200.instU_3d_ana_Np.199805.nc4.nc4')
print(dataset1)
lat = dataset1.lat
lon = dataset1.lon
lev = dataset1.lev
print(lev)
time = dataset1.time
print(time)
u1 = dataset1.U.loc[:,1000:500,:,:]


ii = 0
for i in range(1998,2001):  
    for j in range(1,13):
        if j <10 :
            dataset = xr.open_dataset(path+'MERRA2_200.instU_3d_ana_Np.'+str(i)+'0'+str(j)+'.nc4.nc4')
            uwnd = dataset.U.loc[:,1000:500,:,:]
            vwnd = dataset.V.loc[:,1000:500,:,:]
        elif j>=10 :
            dataset = xr.open_dataset(path+'MERRA2_200.instU_3d_ana_Np.'+str(i)+str(j)+'.nc4.nc4')
            uwnd = dataset.U.loc[:,1000:500,:,:]
            vwnd = dataset.V.loc[:,1000:500,:,:]
        u[ii,:,:,:,:] = uwnd
        v[ii,:,:,:,:] = vwnd
        ii+=1


报错如下:
C:\Users\YANG'Y~1\AppData\Local\Temp/ipykernel_9864/1490155395.py in <module>     32             uwnd = dataset.U.loc[:,1000:500,:,:]     33             vwnd = dataset.V.loc[:,1000:500,:,:]---> 34         u[ii,:,:,:,:] = uwnd     35         v[ii,:,:,:,:] = vwnd     36         ii+=1D:\anaconda3\lib\site-packages\xarray\core\common.py in __array__(self, dtype)    141     142     def __array__(self: Any, dtype: DTypeLike = None) -> np.ndarray:--> 143         return np.asarray(self.values, dtype=dtype)    144     145     def __repr__(self) -> str:D:\anaconda3\lib\site-packages\xarray\core\dataarray.py in values(self)    640         type does not support coercion like this (e.g. cupy).    641         """--> 642         return self.variable.values    643     644     @values.setterD:\anaconda3\lib\site-packages\xarray\core\variable.py in values(self)    510     def values(self):    511         """The variable's data as a numpy.ndarray"""--> 512         return _as_array_or_item(self._data)    513     514     @values.setterD:\anaconda3\lib\site-packages\xarray\core\variable.py in _as_array_or_item(data)    250     TODO: remove this (replace with np.asarray) once these issues are fixed    251     """--> 252     data = np.asarray(data)    253     if data.ndim == 0:    254         if data.dtype.kind == "M":D:\anaconda3\lib\site-packages\xarray\core\indexing.py in __array__(self, dtype)    550     551     def __array__(self, dtype=None):--> 552         self._ensure_cached()    553         return np.asarray(self.array, dtype=dtype)    554 D:\anaconda3\lib\site-packages\xarray\core\indexing.py in _ensure_cached(self)    547     def _ensure_cached(self):    548         if not isinstance(self.array, NumpyIndexingAdapter):--> 549             self.array = NumpyIndexingAdapter(np.asarray(self.array))    550     551     def __array__(self, dtype=None):D:\anaconda3\lib\site-packages\xarray\core\indexing.py in __array__(self, dtype)    520     521     def __array__(self, dtype=None):--> 522         return np.asarray(self.array, dtype=dtype)    523     524     def __getitem__(self, key):D:\anaconda3\lib\site-packages\xarray\core\indexing.py in __array__(self, dtype)    421     def __array__(self, dtype=None):    422         array = as_indexable(self.array)--> 423         return np.asarray(array[self.key], dtype=None)    424     425     def transpose(self, order):D:\anaconda3\lib\site-packages\xarray\coding\variables.py in __array__(self, dtype)     68      69     def __array__(self, dtype=None):---> 70         return self.func(self.array)     71      72     def __repr__(self):D:\anaconda3\lib\site-packages\xarray\coding\variables.py in _apply_mask(data, encoded_fill_values, decoded_fill_value, dtype)    135 ) -> np.ndarray:    136     """Mask all matching values in a NumPy arrays."""--> 137     data = np.asarray(data, dtype=dtype)    138     condition = False    139     for fv in encoded_fill_values:D:\anaconda3\lib\site-packages\xarray\core\indexing.py in __array__(self, dtype)    421     def __array__(self, dtype=None):    422         array = as_indexable(self.array)--> 423         return np.asarray(array[self.key], dtype=None)    424     425     def transpose(self, order):D:\anaconda3\lib\site-packages\xarray\backends\netCDF4_.py in __getitem__(self, key)     91      92     def __getitem__(self, key):---> 93         return indexing.explicit_indexing_adapter(     94             key, self.shape, indexing.IndexingSupport.OUTER, self._getitem     95         )D:\anaconda3\lib\site-packages\xarray\core\indexing.py in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)    710     """    711     raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)--> 712     result = raw_indexing_method(raw_key.tuple)    713     if numpy_indices.tuple:    714         # index the loaded np.ndarrayD:\anaconda3\lib\site-packages\xarray\backends\netCDF4_.py in _getitem(self, key)    104             with self.datastore.lock:    105                 original_array = self.get_array(needs_lock=False)--> 106                 array = getitem(original_array, key)    107         except IndexError:    108             # Catch IndexError in netCDF4 and return a more informativesrc\\netCDF4\\_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__()src\\netCDF4\\_netCDF4.pyx in netCDF4._netCDF4.Variable._get()src\\netCDF4\\_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()RuntimeError: NetCDF: HDF error
之前读取ERA5数据是可以的,但是再读取MERRA2数据就报错了,根据知乎上关于xarray无法提取nc文件的变量数据,出现NetCDF: HDF error的解决方案 - 知乎 (zhihu.com)评论和题主的方法试过,还是报错,还有其他的解决办法吗?还请大家帮忙看看!
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2023-11-12 19:18:40 | 显示全部楼层
试试xr.open_mfdataset()看看?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

新浪微博达人勋

 楼主| 发表于 2023-11-12 20:04:35 | 显示全部楼层
tulalang 发表于 2023-11-12 19:18
试试xr.open_mfdataset()看看?

我试了一下,还是报的这个错,能输出变量,但不能提取
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表