Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a KD-tree for nearest neighbor interpolation #293

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading