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/","c")

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 :


A relative solution (but not that i want) i found here .

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("./","c")
defDim(ds, "lon", length(15:55))
v = defVar(ds, "lon", Float32, ("lon",))
v[:] = 15:55

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("./","c")
v = defVar(ds, "lon", 15:55, ("lon",)) 

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: ./
Group: /

   lon = 41

  lon   (41)
    Datatype:    Int64
    Dimensions:  lon

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

1 Like