当我将我的代码从Python传输到Julia时,我需要使用Julia中的NetCDF.jl包将数据存储到netCDF4文件中。我需要将2D数组附加到netCDF变量中,就像我在以下Python代码中执行它一样:
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()
我正在试图弄清楚如何去做,正如我从文档中理解的那样,我应该探索的方法是 -
NetCDF.putvar( nc, varname, vals, start=[1,1,...], count=[size(vals)...])
如果有人提示他是最受欢迎的..
答案 0 :(得分:1)
我是NetCDF
的新手,但在任何地方,我希望您能找到以下帮助:
using NetCDF
ndim = 128;
xdimension = 0.75;
ydimension = 0.75;
x_range = linspace(0, xdimension, ndim);
y_range = linspace(0, ydimension, ndim);
filename = "py_netcdf4.nc";
varname = "field";
range= collect(1:5);
nccreate(filename , varname, "tic", range, "x", collect(x_range), "y", collect(y_range))
nccreate(filename , "time", "tic", range)
field = zeros(length(range),length(x_range),length(y_range));
for i = range
field[i,:,:] = rand(length(x_range),length(y_range));
end
time=50.0*collect(range);
ncwrite (field, filename, varname);
ncwrite (time, filename, "time");