How to save a vector into NetCDF?

I’m trying to use NCDatasets.jl to create a NetCDF file. I wonder if anyone could show me how to save a vector into a NetCDF variable?

I have a vector D as below:
D = fill(2022,(18,));

Right now, the only way I could make it work is as below:

defDim(ds, "year",  18);
defDim(ds, "col",   1);
v1 = defVar(ds, "year", Int64, ("year", "col"));
v1[:,1] = D;

Basically, I had to create a useless col dimension. How do I do this without defining it? Many thanks

The NetCDF format is generally used for data that is dimensional, i.e f(lat, lon) or similar. I’m not sure if it is possible to save just a dimensionless vector to NetCDF. In general when writing NetCDF files I recommend to follow the CF conventions. It might seem a bit long, but they do have some nice conventions and then your files will be easily readable in various software. I recently registered PlateMotionRequests.jl where I have some code to create simple NetCDF files. See the source, in particular write_netcdf which creates the NetCDF dataset from a Table. I don’t know how to write irregular data to NetCDF yet.

1 Like

Thanks for the reply, and the useful information! I’m able to figure it out myself. Below was how I did it:

defDim(ds, "year",  18);
v1 = defVar(ds, "year", Int64, ("year", ));
v1[:] = D;

The trick is the magic comma. Without it, it won’t work.


aha! thanks forr sharing, good to know that it is possible after all.

1 Like