【发布时间】:2015-05-11 06:30:25
【问题描述】:
我有一个包含 netCDF (.nc) 文件的大文件夹,每个文件的名称相似。数据文件包含时间、经度、纬度和月降水量的变量。目标是获得每个月 X 年的平均月降水量。因此,最后我将有 12 个值代表每个纬度和经度 X 年的平均月降水量。多年来,每个文件都位于同一位置。 每个文件都以相同的名称开头并以“date.sub.nc”结尾,例如:
'data1.somthing.somthing1.avg_2d_Ind_Nx.200109.SUB.nc'
'data1.somthing.somthing1.avg_2d_Ind_Nx.200509.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201104.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201004.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201003.SUB.nc'
'data2.somthing.somthing1.avg_2d_Ind_Nx.201103.SUB.nc'
'data1.somthing.somthing1.avg_2d_Ind_Nx.201203.SUB.nc'
结尾是 YearMonth.SUB.nc 到目前为止我所拥有的是:
array=[]
f = nc.MFDataset('data*.nc')
precp = f.variables['prectot']
time = f.variables['time']
array = f.variables['time','longitude','latitude','prectot']
我得到一个 KeyError: ('time', 'longitude', 'latitude', 'prectot')。有没有办法组合所有这些数据,以便我能够操纵它?
【问题讨论】:
-
“组合”数据是什么意思?感谢您的
f = nc.MFDataset...行,它已经全部在一个 MFDataset 对象中。换句话说,f.variables['prectot'][:]数组已经是一个 3D 数组,其维度(时间、纬度、经度)包含每个维度(时间、纬度、经度)的prectot值。 -
另外,回复:您的 KeyError,
f.variables是一个字典,对于任何字典,您一次只能访问它的一个值,即f.variables['time']或f.variables['longitude'],而不是 @987654329 @。但正如我之前的评论所说,无论如何你只需要f.variables['prectot'](只要我理解正确)。 -
我明白了,我不确定 MFDataset 实际上做了什么。我尝试了 glob.glob 函数,但这只是列出了我所有的文件。谢谢。
标签: python netcdf cdo-climate