Skip to content

Commit 8fdc5ba

Browse files
authored
Merge pull request #207 from CHLNDDEV/precise_knn_map
Precise nearest neighbor mapping for extremely close points (e.g., weirs/levess)
2 parents f54a6d9 + 2b75424 commit 8fdc5ba

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

@msh/msh.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2566,7 +2566,7 @@ function plotter(cmap,round_dec,yylabel,apply_pivot)
25662566
end
25672567

25682568
function obj = carryoverweirs(obj,obj1)
2569-
idx1 = ourKNNsearch(obj.p',obj1.p',1);
2569+
idx1 = nearest_neighbor_map(obj, obj1,'precise');
25702570
if isempty(obj.bd)
25712571
obj.bd.nbou=0;
25722572
obj.bd.nvell=[];

@msh/private/GridData.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
mindepth = -inf ;
7676
maxdepth = +inf ;
7777
rms_slope_calc = true;
78+
slope_calc = 'rms';
7879
if ~isempty(varargin)
7980
varargin=varargin{1} ;
8081
names = {'K','type','interp','nan','N','mindepth','maxdepth','ignoreOL','slope_calc'};

utilities/Make_f24.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
function obj = Make_f24( obj, avisoloc, saldata )
2-
% obj = Make_f24( obj, avisoloc, saldata )
1+
function obj = Make_f24( obj, saldata, plot_on )
2+
% obj = Make_f24( obj, saldata, plot_on )
33
% Takes a msh object and interpolates the global SAL term into the f24
44
% struct
5-
% avisoloc is the directory where the saldata is located (netcdf files).
5+
% Assumes that saldata is in the MATLAB path
66
% The saldata required can be downloaded from:
77
% saldata = 'FES2004' : Source at: ftp://ftp.legos.obs-mip.fr/pub/soa/...
88
% maree/tide_model/global_solution/fes2004/
99
%
1010
% saldata = 'FES2014' : Source at: ftp://ftp.legos.obs-mip.fr/pub/...
1111
% FES2012-project/data/LSA/FES2014/
12-
%
13-
% by default saldata = 'FES2014' and avisoloc is the current directory
12+
% by default saldata = 'FES2014'
13+
%
14+
% plot_on - 1/true: to plot and print F24 values for checking
15+
% 0/false: no plotting by default
16+
%
1417
% Created by William Pringle. July 11 2018 updated to Make_f## style
1518
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619

@@ -19,12 +22,12 @@
1922
'with tidal potential information'])
2023
end
2124

22-
if nargin < 2
23-
avisoloc = '';
24-
end
25-
if nargin < 3
25+
if nargin < 2 || isempty(saldata)
2626
saldata = 'FES2014';
2727
end
28+
if nargin < 3 || isempty(plot_on)
29+
plot_on = false;
30+
end
2831

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

4245
% choose tidal database file names and directories
4346
database = strtrim(upper(saldata)) ;
44-
direc = strtrim(avisoloc) ;
47+
direc = '';
4548

4649
% % Load tide grid data
4750
if strcmp(database,'FES2004')
@@ -115,11 +118,14 @@
115118
phs(phs < 0) = phs(phs < 0) + 360;
116119

117120
% Plot interpolated results
118-
figure(1); fastscatter(VX(:,1),VX(:,2),amp);
119-
title(obj.f24.tiponame{icon})
120-
colorbar;
121-
pause(2)
122-
121+
if plot_on
122+
figure(1); fastscatter(VX(:,1),VX(:,2),amp);
123+
colorbar;
124+
constituent = obj.f24.tiponame{icon};
125+
title(constituent)
126+
print(['F24_' constituent '_check'],'-dpng')
127+
end
128+
123129
% Put into the struct
124130
obj.f24.Val(icon,:,:) = [kvec'; amp'; phs'];
125131
end

utilities/nearest_neighbor_map.m

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
1-
function ind = nearest_neighbor_map(m_old, m_new)
1+
function ind = nearest_neighbor_map(m_old, m_new, type)
2+
% ind = nearest_neighbor_map(m_old, m_new, type)
3+
%
24
% Determine the indices of the nearest neighbors from m_old.p to m_new.p
35
% to do for example a trivial transfer of nodal attributes or bathymetry.
46
%
57
% Inputs
68
% m_old: the old mesh object
79
% m_new: the new mesh object
10+
% type (optional): 'approx' to use ourKNNsearch ANN wrapper
11+
% 'precise' to use built in MATLAB knnsearch
812
%
913
% Output
1014
% The indices of points from m_old to m_new
1115
%
1216
% Example
1317
% Transfer bathymetry data from one grid to a new one.
1418
% m_new.b = m_old.b(ind)
15-
ind = ourKNNsearch(m_old.p',m_new.p',1);
19+
20+
% checking type input
21+
if nargin < 3 || isempty(type)
22+
type = 'approx';
23+
end
24+
% check that knnsearch built-in exists if precise selection,
25+
% otherwise use approx
26+
if strcmp(type,'precise') && ~exist('knnsearch')
27+
type = 'approx';
28+
end
29+
30+
if strcmp(type,'approx')
31+
ind = ourKNNsearch(m_old.p',m_new.p',1);
32+
elseif strcmp(type,'precise')
33+
ind = knnsearch(m_old.p,m_new.p);
34+
else
35+
error(['Unknown selection for type: ' type])
36+
end
37+
1638
end

0 commit comments

Comments
 (0)