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

Precise nearest neighbor mapping for extremely close points (e.g., weirs/levess) #207

Merged
merged 3 commits into from
Apr 11, 2021
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
2 changes: 1 addition & 1 deletion @msh/msh.m
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ function plotter(cmap,round_dec,yylabel,apply_pivot)
end

function obj = carryoverweirs(obj,obj1)
idx1 = ourKNNsearch(obj.p',obj1.p',1);
idx1 = nearest_neighbor_map(obj, obj1,'precise');
if isempty(obj.bd)
obj.bd.nbou=0;
obj.bd.nvell=[];
Expand Down
1 change: 1 addition & 0 deletions @msh/private/GridData.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
mindepth = -inf ;
maxdepth = +inf ;
rms_slope_calc = true;
slope_calc = 'rms';
if ~isempty(varargin)
varargin=varargin{1} ;
names = {'K','type','interp','nan','N','mindepth','maxdepth','ignoreOL','slope_calc'};
Expand Down
36 changes: 21 additions & 15 deletions utilities/Make_f24.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
function obj = Make_f24( obj, avisoloc, saldata )
% obj = Make_f24( obj, avisoloc, saldata )
function obj = Make_f24( obj, saldata, plot_on )
% obj = Make_f24( obj, saldata, plot_on )
% Takes a msh object and interpolates the global SAL term into the f24
% struct
% avisoloc is the directory where the saldata is located (netcdf files).
% Assumes that saldata is in the MATLAB path
% The saldata required can be downloaded from:
% saldata = 'FES2004' : Source at: ftp://ftp.legos.obs-mip.fr/pub/soa/...
% maree/tide_model/global_solution/fes2004/
%
% saldata = 'FES2014' : Source at: ftp://ftp.legos.obs-mip.fr/pub/...
% FES2012-project/data/LSA/FES2014/
%
% by default saldata = 'FES2014' and avisoloc is the current directory
% by default saldata = 'FES2014'
%
% plot_on - 1/true: to plot and print F24 values for checking
% 0/false: no plotting by default
%
% Created by William Pringle. July 11 2018 updated to Make_f## style
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand All @@ -19,12 +22,12 @@
'with tidal potential information'])
end

if nargin < 2
avisoloc = '';
end
if nargin < 3
if nargin < 2 || isempty(saldata)
saldata = 'FES2014';
end
if nargin < 3 || isempty(plot_on)
plot_on = false;
end

ll0 = obj.f15.slam(1) ;
if ( ll0 < 0 )
Expand All @@ -41,7 +44,7 @@

% choose tidal database file names and directories
database = strtrim(upper(saldata)) ;
direc = strtrim(avisoloc) ;
direc = '';

% % Load tide grid data
if strcmp(database,'FES2004')
Expand Down Expand Up @@ -115,11 +118,14 @@
phs(phs < 0) = phs(phs < 0) + 360;

% Plot interpolated results
figure(1); fastscatter(VX(:,1),VX(:,2),amp);
title(obj.f24.tiponame{icon})
colorbar;
pause(2)

if plot_on
figure(1); fastscatter(VX(:,1),VX(:,2),amp);
colorbar;
constituent = obj.f24.tiponame{icon};
title(constituent)
print(['F24_' constituent '_check'],'-dpng')
end

% Put into the struct
obj.f24.Val(icon,:,:) = [kvec'; amp'; phs'];
end
Expand Down
26 changes: 24 additions & 2 deletions utilities/nearest_neighbor_map.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
function ind = nearest_neighbor_map(m_old, m_new)
function ind = nearest_neighbor_map(m_old, m_new, type)
% ind = nearest_neighbor_map(m_old, m_new, type)
%
% Determine the indices of the nearest neighbors from m_old.p to m_new.p
% to do for example a trivial transfer of nodal attributes or bathymetry.
%
% Inputs
% m_old: the old mesh object
% m_new: the new mesh object
% type (optional): 'approx' to use ourKNNsearch ANN wrapper
% 'precise' to use built in MATLAB knnsearch
%
% Output
% The indices of points from m_old to m_new
%
% Example
% Transfer bathymetry data from one grid to a new one.
% m_new.b = m_old.b(ind)
ind = ourKNNsearch(m_old.p',m_new.p',1);

% checking type input
if nargin < 3 || isempty(type)
type = 'approx';
end
% check that knnsearch built-in exists if precise selection,
% otherwise use approx
if strcmp(type,'precise') && ~exist('knnsearch')
type = 'approx';
end

if strcmp(type,'approx')
ind = ourKNNsearch(m_old.p',m_new.p',1);
elseif strcmp(type,'precise')
ind = knnsearch(m_old.p,m_new.p);
else
error(['Unknown selection for type: ' type])
end

end