将多个 NetCDF 文件合并为时间序列多维数组 python

2024-04-02

我正在使用多个 netcdf 文件(在我的计算机上的文件夹中)中的数据。每个文件保存整个美国 5 年的数据。根据 x 和 y 坐标的索引引用位置。我正在尝试为多个位置(网格单元)创建一个时间序列,将 5 年周期编译为 20 年周期(这将合并 4 个文件)。现在,我可以从一个位置的所有文件中提取数据,并使用 numpy 追加将其编译为数组。但是,我想提取多个位置的数据,将其放入矩阵中,其中行是位置,列包含时间序列降水数据。我想我必须创建一个列表或字典,但我不太确定如何在循环内将数据分配到列表/字典。

我是 python 和 netCDF 的新手,所以如果这是一个简单的解决方案,请原谅我。我一直使用这段代码作为指导,但还没有弄清楚如何格式化它以实现我想要做的事情:Python 读取多个可变大小的 NetCDF Rainfall 文件 https://stackoverflow.com/questions/18939210/python-reading-multiple-netcdf-rainfall-files-of-variable-size?newreg=63b025fb70454ce99c76846c5091694f

这是我的代码:

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 个链接。它们是:

https://www.dropbox.com/s/rso0hce8bq7yi2h/pr_MM5I_ccsm_2041010103.nc?dl=0 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 https://www.dropbox.com/s/j56undjvv7iph0f/pr_MM5I_ccsm_2046010103.nc?dl=0


很好的开始,我会推荐以下内容来帮助解决您的问题。

首先,检查一下ncrcat http://nco.sourceforge.net/nco.html#ncrcat-netCDF-Record-Concatenator快速将各个 netCDF 文件连接成一个文件。我强烈建议下载 NCO 来进行 netCDF 操作,特别是在这种情况下,它将简化您以后的 Python 编码。

假设这些文件的名称是precip_1.nc, precip_2.nc, precip_3.nc, and precip_4.nc。您可以沿着记录维度将它们连接起来形成一个新的precip_all.nc记录尺寸为长度 58400

ncrcat precip_1.nc precip_2.nc precip_3.nc precip_4.nc -O precip_all.nc

在 Python 中,我们现在只需要读入新的单个文件,然后提取并存储所需网格单元的时间序列。像这样的事情:

import netCDF4
import numpy as np

yindexlist = [1,2,3]
xindexlist = [4,5,6]
ngridcell = len(xidx)
ntimestep = 58400

# Define an empty 2D array to store time series of precip for a set of grid cells
timeseries_per_grid_cell = np.zeros([ngridcell, ntimestep])

ncfile = netCDF4.Dataset('path/to/file/precip_all.nc', 'r')

# Note that precip is 3D, so need to read in all dimensions
precip = ncfile.variables['precip'][:,:,:]

for i in range(ngridcell):
     timeseries_per_grid_cell[i,:] = precip[:, yindexlist[i], xindexlist[i]]

ncfile.close()

如果您必须仅使用 Python,则需要跟踪各个文件形成的时间索引块以生成完整的时间序列。 58400/4 = 每个文件 14600 个时间步。因此,您将有另一个循环来读取每个单独的文件并存储相应的时间片段,即第一个文件将填充 0-14599,第二个文件将填充 14600-29199,等等。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将多个 NetCDF 文件合并为时间序列多维数组 python 的相关文章

随机推荐