我正在学习如何使用netCDF4使用Pyhton模块在水蟒。我试图将值附加到我创建的两个变量time和field: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

time = 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):

time[i] = i*50.0

field[i,:,:] = np.random.uniform(size=(len(x_range), len(y_range)))

root_grp.close

但现在当我打印其中一个变量时,我得到它是空的(!!)以下内容:Python 2.7.10 |Anaconda 2.4.1 (64-bit)| (default, Sep 15 2015, 14:50:01)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

Anaconda is brought to you by Continuum Analytics.

Please check out: http://continuum.io/thanks and https://anaconda.org

>>> from netCDF4 import Dataset

>>> root_grp = Dataset('py_netcdf4.nc', 'r', format='NETCDF4')

>>> print root_grp.variables["field"][:,:,:]

[]

>>>

我在这里做错什么了?

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐