我想通过python读取CMIP6的.nc文件数据,python版本3.9.7,netCDF4版本1.5.8,hdf5版本1.10.6,h5py版本3.2.1,以下的内容能够正常输出:
import netCDF4 as nc
#查看nc文件的目录
nc_obj=nc.Dataset(r'E:\projet\long_time_QZGY\CMIP6\albc_Eday_IPSL-CM6A-LR_land-hist_r1i1p1f1_gr_18500101-20141231.nc')
print(nc_obj)
print('lat:',nc_obj.variables['lat'][:])
print('lon:',nc_obj.variables['lon'][:])
print('albc:',len(nc_obj.variables['albc']))
print('time:',len(nc_obj.variables['time']))
print('time_bounds:',nc_obj.variables['time_bounds'])
但当我想要查看变量具体内容时却报错
albc=nc_obj.variables['albc'][:]
time=nc_obj.variables['time'][:]
RuntimeError Traceback (most recent call last)
C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_18496/2086705156.py in <module>
35 # #用.data直接把masked_array中的data数据读出。
36 # time = nc.num2date(nc_obj.variables['time'][:],'days since 1850-1-1 00:00:00').data
---> 37 time=nc_obj.variables['time'][:]
38 # # time_bounds=nc_obj.variables['time_bounds'][:]
39 # print(lat)
src\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
以及我想将nc数据转成nparray时也会报 NetCDF: HDF error
--> 251 u_arr = np.asarray(nc_data_obj.variables['albc']) # 这里根据需求输入想要转换的波段名称
252 #print('time_1=',time_1.min(),'time_max=',time_1.max())
253
D:\Anaconda\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order, like)
100 return _asarray_with_like(a, dtype=dtype, order=order, like=like)
101
--> 102 return array(a, dtype, copy=False, order=order)
103
104
src\netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Variable.__array__()
src\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
这是什么原因呢?