Skip to content

Commit

Permalink
Use a KD-tree for nearest neighbor interpolation (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukem12345 authored Dec 16, 2024
1 parent d104143 commit 57854ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
NearestNeighbors = "b8a86587-4115-5ab1-83bc-aa920d37bbce"
NetCDF = "30363a11-5582-574a-97bb-aa9a979735b9"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Expand Down
7 changes: 5 additions & 2 deletions docs/src/ebm_melt/ebm_melt.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using CoordRefSystems
using CairoMakie
using LinearAlgebra
using MLStyle
using NearestNeighbors
using NetCDF
using OrdinaryDiffEq
Point3D = Point3{Float64}
Expand Down Expand Up @@ -61,9 +62,11 @@ p_sph = map(point(s)) do p
end
# Note: You can instead use an algebraic parameterization, rather than nearest-neighbor interpolation.
# Note: You can alternatively set a value to 0.0 if the distance to the nearest-neighbor is greater than some threshold.
lat, lon = lat[:], lon[:]
ll = hcat(lat, lon)'
kdt = KDTree(ll)
sit_sph_idxs = map(p_sph) do p
argmin(map(i -> sqrt((lat[i] - p[1])^2 + (lon[i] - p[2])^2), eachindex(lat)))
nn(kdt, p)[1]
end
sit_sph = map(sit_sph_idxs, p_sph) do i, p
Expand Down

0 comments on commit 57854ba

Please sign in to comment.