在 netCDF4 文件中排列数据

Arrange data in netCDF4 file

我正在尝试弄清楚如何使用 Python 模块进行 netCDF4 处理。我想将模拟数据保存到一个文件中,这样每次我都有一个特定字段的值网格。我改编了一个 example that I have found 供我使用:

from netCDF4 import Dataset
import numpy as np

root_grp = Dataset('py_netcdf4.nc', 'w', format='NETCDF4')
root_grp.description = 'Example simulation data'

ndim = 128 # Size of the matrix ndim*ndim
xdimension = 0.75
ydimension = 0.75
# dimensions
root_grp.createDimension('time', None)
root_grp.createDimension('x', ndim)
root_grp.createDimension('y', ndim)

# variables
prec = root_grp.createVariable('time', 'f8', ('time',))
x = root_grp.createVariable('x', 'f4', ('x',))
y = root_grp.createVariable('y', 'f4', ('y',))
field = root_grp.createVariable('field', 'f8', ('time', 'x', 'y',))

# data
x_range =  np.linspace(0, xdimension, ndim)
y_range =  np.linspace(0, ydimension, ndim)
x[:] = x_range
y[:] = y_range
for i in range(5):
    field[i,:,:] = np.random.uniform(size=(len(x_range), len(y_range)))


root_grp.close

我的问题是 - 现在添加我想每次计算的模式信息的首选方法是什么 - 例如每次字段的平均值、最大值和最小值?

您可以简单地为变量添加属性:

field.mean=np.mean(field)

这当然是相当有限的,为了获得更多数据,您可能应该只创建新变量,如 field_mean, field_max 或类似变量。我不认为它在 http://cfconventions.org/ or https://geo-ide.noaa.gov/wiki/index.php?title=NetCDF_Attribute_Convention_for_Dataset_Discovery but You may want to browse through different NetCDF conventions mentioned here http://www.unidata.ucar.edu/software/netcdf/conventions.html.

中有描述