# NCDatasets.jl: define values of dimensions

**URL:** <https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892>\
**Category:** Geo\
**Tags:** question\
**Created:** [August 5, 2021, 2:53pm UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892 "2021-08-05T14:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inna](https://avatars.discourse-cdn.com/v4/letter/i/8e8cbc/32.png) [@Inna](https://discourse.julialang.org/u/Inna)\
**Post date:** [August 5, 2021, 2:53pm UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892/1 "2021-08-05T14:53:45Z")

</div>

Dear all,  
Using NCDatasets,

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

```julia
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 :

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

```

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

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 5, 2021, 2:59pm UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892/2 "2021-08-05T14:59:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 5, 2021, 3:00pm UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892/3 "2021-08-05T15:00:41Z")

</div>

As the issue you linked says, it is

```julia
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.

---

<div class="post-metadata">

**Author:** ![Alexander-Barth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander-barth/32/3692_2.png) [@Alexander-Barth](https://discourse.julialang.org/u/Alexander-Barth)\
**Post date:** [June 15, 2022, 9:41am UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892/4 "2022-06-15T09:41:30Z")

</div>

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

```julia
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).

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

Dimensions
   lon = 41

Variables
  lon (41)
    Datatype: Int64
    Dimensions: lon

```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 15, 2022, 9:48am UTC](https://discourse.julialang.org/t/ncdatasets-jl-define-values-of-dimensions/65892/5 "2022-06-15T09:48:03Z")

</div>

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