# DimensionalData: How to use a dimension whose name is stored in a variable

**URL:** https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098
**Category:** Geo
**Tags:** dimensionaldata, rasters
**Created:** [June 22, 2024, 4:51pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098 "2024-06-22T16:51:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![aasdelat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasdelat/32/45268_2.png) [@aasdelat](https://discourse.julialang.org/u/aasdelat)
#### Post date: [June 22, 2024, 4:51pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/1 "2024-06-22T16:51:12Z")

</div>

I have to use data with one time dimension and two spatial dimensions (longitude and latitude). I use the Rasters.jl library to manage them, that creates [DimensionalData.jl](https://github.com/rafaqz/DimensionalData.jl) arrays.

Sometimes, I have a variable, say slp, with three dimensions: Ti, longitude and latitude.

But sometimes, I can have the same variable, slp, with to dimensions: Ti and points. Points are integer numbers running from one to the number of grid points. Additionaly, in the same data set, I have two variables: longitude and latitude. And each of these two variables has one dimension: points. So, points is a dimension that is shared among slp, longitude and latitude.

If I want to select a spatial window of my data, between lon1 and lon2, and lat1 and lat2, then I can do as follows:

```julia
ds[points = Where( p -> ds["longitude"][p] ∈ lon1..lon2 && ds["latitude"][p] ∈ lat1..lat2 )]

```

But it is the case that sometimes, I do not know the name of the dimension `points`, so that it can be, instead of `points`, `values` (for example). I can detect the name of this dimension and store it as a string variable, say, `spatial_dim_name`. But I do not know how to use it. I have tried:

```julia
julia> spatial_dim_name = "values"
julia> ds[spatial_dim_name = Where(p->
                              ds[lon_name][p] ∈ lon1..lon2
                              &&
                              ds[lat_name][p] ∈ lat1..lat2
                              )
                           ]
┌ Warning: (Dim{:spatial_dim_name},) dims were not found in object.
└ @ DimensionalData.Dimensions ~/.julia/packages/DimensionalData/BoJag/src/Dimensions/primitives.jl:777
┌ Warning: (Dim{:spatial_dim_name},) dims were not found in object.
└ @ DimensionalData.Dimensions ~/.julia/packages/DimensionalData/BoJag/src/Dimensions/primitives.jl:777

```

And the selection is not made.  
Does anybody know how I can do this?

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [June 22, 2024, 7:50pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/2 "2024-06-22T19:50:26Z")

</div>

`otherdims` is the DD function for getting unknown dimensions. It will get the “other” dimensions to the ones you know.

`rebuild` is a function that rebuilds most DD objects like all `Dimension`s with new contents.

So you can do :

```julia
thedim = first(otherdims(raster, (X, Y, Ti)))
raster[rebuild(thedim, myselector)]

```

Where `myselector` is your `Where` selector

(It’s a bit harder to do the keyword syntax programmatically like this, so we switch to the dimensions wrapper syntax by just rebuilding dimensions with new contents. Most of the internals of DD and Rasters look like the code above)

---

<div class="post-metadata">

### Author: ![aasdelat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasdelat/32/45268_2.png) [@aasdelat](https://discourse.julialang.org/u/aasdelat)
#### Post date: [June 22, 2024, 10:27pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/3 "2024-06-22T22:27:49Z")

</div>

Great!, @Raf.  
In order to be more precise and use the dimension name stored as a string in a variable, I have coded:

```julia
spatial_coord_name = "values"
spatial_dim = dims(raster, Symbol(spatial_coord_name))
raster_selected = raster[
    rebuild(
            spatial_dim,
            Where(c->
                       raster["longitude"][c] ∈ lon1..lon2
                       &&
                       raster["latitude"][c] ∈ lat1..lat2
            )#Where
    )#rebuild
]#ds

```

---

<div class="post-metadata">

### Author: ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)
#### Post date: [June 22, 2024, 10:46pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/4 "2024-06-22T22:46:20Z")

</div>

Why is that more precise? A type would usually be considered more precise than a string. In any real internal code you always should prefer the type/object because it’s easy on the compiler and will usually be a cost free abstraction.

At least just use a Symbol, the string is pretty redundant.

---

<div class="post-metadata">

### Author: ![Fliks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fliks/32/2494_2.png) [@Fliks](https://discourse.julialang.org/u/Fliks)
#### Post date: [June 23, 2024, 9:57pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/5 "2024-06-23T21:57:34Z")

</div>

Couldn’t I use the getindex on the dimension instead of rebuild?  
Like this:

```julia
raster[thedim[myselector]]

```

---

<div class="post-metadata">

### Author: ![aasdelat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasdelat/32/45268_2.png) [@aasdelat](https://discourse.julialang.org/u/aasdelat)
#### Post date: [June 26, 2024, 8:36pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/6 "2024-06-26T20:36:59Z")

</div>

Ok, it is more precise to use the type than the string. What I mean by “more precise” here is “to more precisely address the original question”, that was “How to use a dimension whose name is stored in a variable”.  
But, now that you say this, I will see if my code admits a recoding in which I use the type instead of the string.

---

<div class="post-metadata">

### Author: ![aasdelat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aasdelat/32/45268_2.png) [@aasdelat](https://discourse.julialang.org/u/aasdelat)
#### Post date: [June 27, 2024, 9:47pm UTC](https://discourse.julialang.org/t/dimensionaldata-how-to-use-a-dimension-whose-name-is-stored-in-a-variable/116098/8 "2024-06-27T21:47:51Z")

</div>

> [@Fliks](#):
>
> `raster[thedim[myselector]]`

Yes! this also works!  
But I did not ever imagine this way of doing it.
