Python 使用Julia NetCDF.jl包向netCDF4变量追加值
当我将代码从Python传输到Julia时,我需要使用Julia中的NetCDF.jl包将数据存储到netCDF4文件中。我需要将2D数组附加到netCDF变量中,方法与我在以下Python代码中所做的相同:Python 使用Julia NetCDF.jl包向netCDF4变量追加值,python,julia,netcdf,Python,Julia,Netcdf,当我将代码从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 simula
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)...])
如果任何人有任何提示,我们都非常欢迎。我是
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");