# 需引入netcdf库
import netCDF4 as nc
# 文件路径
fn = '2018.nc'
# 默认为读文件,此处 'r' 可省略
data = nc.Dataset(fn, 'r')
# 查看nc文件信息
print('------------文件信息--------------------')
print(data)
# 查看nc文件中的变量
print('--------------文件变量-------------------')
for i in data.variables.keys():
print(i)
# 查看每个变量的信息
print('--------------变量的信息----------------------')
print('--------------latitude----------------------')
print(data.variables['latitude'])
print('--------------u10----------------------')
print(data.variables['u10'])
# 读取数据值
lat = data.variables['latitude'][:].data
lon = data.variables['longitude'][:].data
time = data.variables['time'][:].data
u10 = data.variables['u10'][:].data
v10 = data.variables['v10'][:].data
d2m = data.variables['d2m'][:].data
t2m = data.variables['t2m'][:].data
sp = data.variables['sp'][:].data
tcwv = data.variables['tcwv'][:].data
tp = data.variables['tp'][:].data