Reputation: 2442
As I am transferring my code from Python to Julia, I would need to use the NetCDF.jl package in Julia to store data into netCDF4 files. I need to append 2D arrays into netCDF variables, in the same way I am doing it in the following Python code:
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()
I am trying to figure out how to do it, and as I understand from the documentation the method I should explore is -
NetCDF.putvar( nc, varname, vals, start=[1,1,...], count=[size(vals)...])
If anyone has a hint he is most welcome..
Upvotes: 1
Views: 546
Reputation: 5746
I'm new to NetCDF
but anywhere, I wish you find the following helpful:
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");
Upvotes: 1