# Find nearest value in AxisArray

**URL:** <https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820>\
**Category:** General Usage\
**Created:** [February 4, 2022, 7:52pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820 "2022-02-04T19:52:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chiil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiil/32/27474_2.png) [@Chiil](https://discourse.julialang.org/u/Chiil)\
**Post date:** [February 4, 2022, 7:52pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/1 "2022-02-04T19:52:08Z")

</div>

I am playing around with AxisArrays, as it seems like a great package for adding axis with geographical locations to gridded data. What I would like is to let AxisArray give me the value of the nearest location, without having to specify the tolerance each time. Is it somehow possible?

```julia
using AxisArrays

x = 0:10:100 |> collect
a = AxisArray(rand(length(x)); x=x)

println(a[x=29..33])
println(a[x=30..30])
println(a[atvalue(29; atol=5)])
println(a[atvalue(29)]) # Does not work.

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 5, 2022, 12:18pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/2 "2022-02-05T12:18:50Z")

</div>

> [@Chiil](#):
>
> ```julia
> using AxisArrays
> 
> x = 0:10:100 |> collect
> a = AxisArray(rand(length(x)); x=x)
> 
> println(a[x=29..33])
> println(a[x=30..30])
> println(a[atvalue(29; atol=5)])
> println(a[atvalue(29)]) # Does not work.
> 
> ```

Fails with

```julia
ERROR: LoadError: BoundsError: attempt to access 11-element Vector{Int64} at index

```

This looks like a bug to me. Maybe you should file an issue against `AxisArrays`?

---

<div class="post-metadata">

**Author:** ![Chiil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiil/32/27474_2.png) [@Chiil](https://discourse.julialang.org/u/Chiil)\
**Post date:** [February 5, 2022, 12:26pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/3 "2022-02-05T12:26:49Z")

</div>

I think it is because the default tolerance seems to be zero. I would prefer the default tolerance to be very large. I would like to use it for gridded data and get the nearest value to an arbitrary coordinate.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [February 5, 2022, 12:32pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/4 "2022-02-05T12:32:49Z")

</div>

I can understand that, but an issue might be better place to discuss this?

---

<div class="post-metadata">

**Author:** ![Chiil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiil/32/27474_2.png) [@Chiil](https://discourse.julialang.org/u/Chiil)\
**Post date:** [February 5, 2022, 12:41pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/5 "2022-02-05T12:41:41Z")

</div>

Will do:  
[https://github.com/JuliaArrays/AxisArrays.jl/issues/205](https://github.com/JuliaArrays/AxisArrays.jl/issues/205)

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [February 5, 2022, 12:45pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/6 "2022-02-05T12:45:38Z")

</div>

Also, note that just having the tolerance be very large wouldn’t do what you want - it doesn’t get the _nearest_ index to what we specify, it seems to be getting the _smallest_ index within the tolerance:

```julia

julia> println(a[atvalue(29; atol=10)])
0.9605168735870881

julia> println(a[x=30..30])
[0.02237417395875274]

julia> println(a[x=20..20])
[0.9605168735870881]

```

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [February 5, 2022, 12:50pm UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/7 "2022-02-05T12:50:37Z")

</div>

```julia
julia> println(a[atvalue(29; atol=100)])
0.9605168735870881

julia> println(a[x=10..10])
[0.9994774509360341]

julia> println(a[x=0..0])
[0.5920127185825809]

```

So not really the smallest index either, that would have given the `x=0..0`th value here. I’m not sure what it’s actually doing.

---

<div class="post-metadata">

**Author:** ![gszep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gszep/32/14836_2.png) [@gszep](https://discourse.julialang.org/u/gszep)\
**Post date:** [April 17, 2022, 10:42am UTC](https://discourse.julialang.org/t/find-nearest-value-in-axisarray/75820/8 "2022-04-17T10:42:01Z")

</div>

Is there a way to return a default value such as `NaN` or `0` instead of throwing a `BoundsError` when there is no value within array `a` in the vicinity of a given index along `x`? Similar to the behvaiour of [DefaultDict and DefaultOrderedDict · DataStructures.jl](https://juliacollections.github.io/DataStructures.jl/latest/default_dict/)
