我正在使用来自多个netcdf文件的数据(在我的计算机上的一个文件夹中)。每个文件保存整个美国5年的数据。位置是基于x和y坐标的索引引用的。我正在尝试为多个位置(网格单元)创建一个时间序列,将5年周期编译为20年周期(这将组合4个文件)。现在我可以从一个位置的所有文件中提取数据,并使用numpy append将其编译成一个数组。但是,我想提取多个位置的数据,将其放入一个矩阵中,其中行是位置,列包含时间序列降水数据。我想我必须创建一个列表或字典,但我不确定如何在循环中将数据分配给列表/字典。
我是python和netCDF新手,如果这是一个简单的解决方案,请原谅我。我一直在用这段代码作为指南,但还没有找到如何格式化它以满足我的需要:Python Reading Multiple NetCDF Rainfall files of variable size
这是我的代码:import glob
from netCDF4 import dataset
import numpy as np
# Define x & y index for grid cell of interest
# Pittsburgh is 37,89
yindex = 37 #first number
xindex = 89 #second number
# Path
path = '/Users/LMC/Research Data/NARCCAP/'
folder = 'MM5I_ccsm/'
## load data file names
all_files = glob.glob(path + folder+'*.nc')
all_files.sort()
## initialize np arrays of timeperiods and locations
yindexlist = [yindex,'38','39'] # y indices for all grid cells of interest
xindexlist = [xindex,xindex,xindex] # x indices for all grid cells of interest
ngridcell = len(yindexlist)
ntimestep = 58400 # This is for 4 files of 14600 timesteps
## Initialize np array
timeseries_per_gridcell = np.empty(0)
## START LOOP FOR FILE IMPORT
for timestep, datafile in enumerate(all_files):
fh = Dataset(datafile,mode='r')
days = fh.variables['time'][:]
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
precip = fh.variables['pr'][:]
for i in range(1):
timeseries_per_gridcell = np.append(timeseries_per_gridcell,precip[:,yindexlist[i],xindexlist[i]]*10800)
fh.close()
print timeseries_per_gridcell
我把3个文件放在dropbox上,这样你就可以访问它们,但我只能发布2个链接。这些是: