# 3D Vector Field interpolation (maybe with DIVAnd.jl)

**URL:** https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233
**Category:** Offtopic
**Tags:** interpolations
**Created:** [November 9, 2021, 11:29pm UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233 "2021-11-09T23:29:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dadaforpeace](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dadaforpeace](https://discourse.julialang.org/u/dadaforpeace)
#### Post date: [November 9, 2021, 11:29pm UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233/1 "2021-11-09T23:29:59Z")

</div>

Dear community,

I would like to interpolate my 3D Vector Field with DIVAnd.jl ([GitHub - gher-uliege/DIVAnd.jl: DIVAnd performs an n-dimensional variational analysis of arbitrarily located observations](https://github.com/gher-ulg/DIVAnd.jl)) without any success. I have the following data: [MPCDF DataShare](https://datashare.mpcdf.mpg.de/s/2LFNRdwwePbesMs)

When I plot the data with PlotlyJS I get the following picture:  
(Dots color and size the length of the vectors)

 ![Screenshot 2021-11-10 at 00.17.05](https://global.discourse-cdn.com/julialang/original/3X/4/3/43db1b272f4df2b507c16bd90cbbc94893561a07.png)

(Vectors size and color length of vectors)

 ![Screenshot 2021-11-10 at 00.17.32](https://global.discourse-cdn.com/julialang/original/3X/9/f/9f7e6a5ade0e03731fcf5c4501e94d61629d2f36.png)

I am now trying to use the example found here which is 2D: [https://gher-ulg.github.io/DIVAnd.jl/latest/index.html#Examples](https://gher-ulg.github.io/DIVAnd.jl/latest/index.html#Examples)

I get stuck when I am trying to use DIVAndrun. Here is my code:

```julia
using CSV
using DataFrames
using DIVAnd, PyPlot

data = DataFrame(CSV.File("211107_2025 LOGGER04.mod.csv",header=["X","Y","Z","U","V","W","Mod"]))

x = range(minimum(data[:,1]),stop=maximum(data[:,1]),length=393)
y = range(minimum(data[:,2]),stop=maximum(data[:,2]),length=398)
z = range(minimum(data[:,3]),stop=maximum(data[:,3]),length=300)

mask,(pm,pn,po),(xi,yi,zi) = DIVAnd_rectdom(x,y,z)

# normalized obs. error variance and correlation length
epsilon2 = 1 / 200
len = 0.2

# call DIVAnd
fi, s = DIVAndrun(mask,
            (pm,pn,po),
            (xi,yi,zi),
    (data[:,1],data[:,2],data[:,3]),
            data[:,7],
    len,epsilon2; 
    velocity = (data[:,4]data[:,5],data[:,6])
)

```

The error is:  
MethodError: no method matching \*(::Vector{Float64}, ::Vector{Float64})  
Closest candidates are:  
\*(::Any, ::Any, ::Any, ::Any…) at operators.jl:560  
\*(::StridedMatrix{T}, ::StridedVector{S}) where {T\<:Union{Float32, Float64, ComplexF32, ComplexF64}, S\<:Real} at /Users/julia/buildbot/worker/package\_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/matmul.jl:44  
\*(::StridedVecOrMat{T} where T, ::LinearAlgebra.Adjoint{var"#s814", var"#s813"} where {var"#s814", var"#s813"\<:LinearAlgebra.LQPackedQ}) at /Users/julia/buildbot/worker/package\_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/lq.jl:254  
…

Stacktrace:  
[1] top-level scope  
@ In[13]:2  
[2] eval  
@ ./boot.jl:360 [inlined]  
[3] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base ./loading.jl:1116

I know that the Problem lies with the structure of the location of my observation

```julia
(data[:,1],data[:,2],data[:,3])

```

and the velocities

```julia
velocity = (data[:,4]data[:,5],data[:,6])

```

I do not know how to transform them into tuples with n elements which says the documentation. Has anybody a clue?

If there is a smarter way to do this, please let me know.

Thanks

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 10, 2021, 8:06pm UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233/2 "2021-11-10T20:06:09Z")

</div>

> [@dadaforpeace](#):
>
> how to transform them into tuples with n elements

Know nothing about that very interesting package but for the question quoted the following might help:

```julia
x = [1.0, 2, 3, 4] # 4-element Vector{Float64}
xt = Tuple(x) # NTuple{4, Float64}

```

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [November 23, 2021, 8:57am UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233/3 "2021-11-23T08:57:00Z")

</div>

Presently DIVAnd works only on scalar fields. So one way is to interpolate/analyse each velocity component independently and then reassemble afterwards. Quick and easy (if your entry data contain the vector components)

That however completely ignores that it is a vector field on which you might want to add some more constraints (like non-divergence for incompressible flows). A way to do this is explained here for 2D surface currents:

[https://github.com/gher-ulg/DIVAnd\_HFRadar.jl](https://github.com/gher-ulg/DIVAnd_HFRadar.jl)

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [November 23, 2021, 9:07am UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233/4 "2021-11-23T09:07:29Z")

</div>

Please also note that when referring to the advection constraint in DIVAnd, we mean that you provide a velocity field which is used to guide the interpolation of the scalar field you want to analyse/interpolate. So the parameter “velocity” is NOT meant to allow for an interpolation of a velocity field into a regular grid but is there so that the user can provide the velocity field on a regular grid and which can help during the interpolation of sparse data of a scalar onto a regular grid.  
Hope it clarifies ?

---

<div class="post-metadata">

### Author: ![JM\_Beckers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jm_beckers/32/22482_2.png) [@JM\_Beckers](https://discourse.julialang.org/u/JM_Beckers)
#### Post date: [November 24, 2021, 8:41am UTC](https://discourse.julialang.org/t/3d-vector-field-interpolation-maybe-with-divand-jl/71233/5 "2021-11-24T08:41:18Z")

</div>

By the way, a simple and efficient way to make the analysis componentwise (assuming you have all three components at the same location) is explained here:

[DIVAnd.jl documentation · DIVAnd](https://gher-ulg.github.io/DIVAnd.jl/latest/#How-can-I-speed-up-analysis-using-observations-which-always-have-the-same-coordinates)?
