我正在使用来自多个 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 个文件放在保管箱中以便您可以访问它们,但我只允许发布 2 个链接。他们是:
https://www.dropbox.com/s/rso0hce8bq7yi2h/pr_MM5I_ccsm_2041010103.nc?dl=0 https://www.dropbox.com/s/j56undjvv7iph0f/pr_MM5I_ccsm_2046010103.nc?dl=0
原文由 LCook 发布,翻译遵循 CC BY-SA 4.0 许可协议
好的开始,我会推荐以下内容来帮助解决您的问题。
首先,检查 ncrcat 以快速将您的各个 netCDF 文件连接成一个文件。我强烈建议下载 NCO 以进行 netCDF 操作,尤其是在这种情况下,它将简化您以后的 Python 编码。
Let’s say the files are named
precip_1.nc
,precip_2.nc
,precip_3.nc,
andprecip_4.nc
.您可以沿着记录维度将它们连接起来以形成一个新的precip_all.nc
其记录维度长度为 58400在 Python 中,我们现在只需要读入那个新的单个文件,然后提取并存储所需网格单元的时间序列。是这样的:
如果您必须只使用 Python,则需要跟踪各个文件形成的时间索引块以构成完整的时间序列。 58400⁄4 = 每个文件 14600 个时间步长。所以你将有另一个循环来读取每个单独的文件并存储相应的时间片,即第一个文件将填充 0-14599,第二个 14600-29199,等等。