简体   繁体   English

排列netCDF4文件中的数据

[英]Arrange data in netCDF4 file

I am trying to figure out how to use Python module for netCDF4 handling. 我试图弄清楚如何使用Python模块进行netCDF4处理。 I would like to save simulation data into a file, such that for every time I have a grid of values of a certain field. 我想将模拟数据保存到文件中,这样每次我都有一个特定字段值的网格。 I adapted an example that I have found for my use: 我改编了一个发现示例供我使用:

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

My question is - what is the preferred way now to add mode information that I would like to calculate for each time - such as the mean, max, and min values of the field every time? 我的问题是-现在添加我想每次计算的模式信息的首选方式是什么-例如每次字段的平均值,最大值和最小值?

You can add attributes to variables simply with: 您可以使用以下方法简单地将属性添加到变量中:

field.mean=np.mean(field)

This is rather limited of course, for more data You should probably just create new variables like field_mean, field_max or similar. 当然,这相当有限,对于更多数据,您可能应该只创建新的变量,例如field_mean, field_max或类似变量。 I do not think it is described in 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 . 我认为http://cfconventions.org/https://geo-ide.noaa.gov/wiki/index.php?title=NetCDF_Attribute_Convention_for_Dataset_Discovery中没有对此进行描述,但是您可能希望浏览此处提到的不同NetCDF约定http://www.unidata.ucar.edu/software/netcdf/conventions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM