# NCDatasets.jl: define values of dimensions

Dear all,
Using NCDatasets,

To write a data to netcdf, i can define the dimensions with this way:

``````ds = NCDataset("/tmp/test.nc","c")
defDim(ds,"lon",100)
``````

However, is there any way to define the values of my dimensions ??

Like, ` defDim(ds,"lon",collect(-179.5:179.5))`

Similar way is doing NetCDF.jl :

``````nccreate(path,"rad","lon",collect(-179.5:179.5),lonatts,"lat",collect(-89.5:89.5),latatts);
``````

A relative solution (but not that i want) i found here https://github.com/Alexander-Barth/NCDatasets.jl/issues/90 .

The values of the dimensions can be defined by defining a proper variable (`defVar`) which has the same name as the dimension and values the values you need.

As the issue you linked says, it is

``````ds = Dataset("./test.nc","c")
defDim(ds, "lon", length(15:55))
v = defVar(ds, "lon", Float32, ("lon",))
v[:] = 15:55
close(ds)
``````

By definition the CF standard doesn’t define dimensions with values, only with the length they have.

1 Like

As a follow-up, you can now also use:

``````ds = Dataset("./test.nc","c")
v = defVar(ds, "lon", 15:55, ("lon",))
close(ds)
``````

The dimension is automatically created if you provide directly the data (rather than just the type) to `defVar`. The first `"lon"` is the variable name and the second is the dimension name (which are typically the same).

``````NCDataset: ./test.nc
Group: /

Dimensions
lon = 41

Variables
lon   (41)
Datatype:    Int64
Dimensions:  lon
``````

ah, awesome, thanks, I can simplify the writing code in ClimateBase.jl using this!

1 Like