-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinDownGlobalMap.m
More file actions
executable file
·49 lines (49 loc) · 1.82 KB
/
binDownGlobalMap.m
File metadata and controls
executable file
·49 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
% find for each index of input grid the nearest index in output grid
% nkpmn@web.de
function [idxlin,idxY,idxX] = binDownGlobalMap(inLat,inLon,outLat,outLon)
inLalo = wrapTo360(inLon(:)) + 1i*inLat(:);
outLalo = wrapTo360(outLon(:)) + 1i*outLat(:);
idxlin = nan(size(inLalo));
lims = thread_distro(12,numel(inLalo));
%%
spmd
T = disp_progress('init','allocating indices to nearest bin');
for ii = lims(labindex,1):lims(labindex,2)
T = disp_progress('show',T,diff(lims(labindex,:)),1000);
[minA,minAidx] = min(abs(inLalo(ii)-outLalo)) ;
[minB,minBidx] = min(abs(inLalo(ii)-outLalo + 360)) ;
if minA < minB
idxlin(ii) = minAidx;
else
idxlin(ii) = minBidx; % crossing 360 meridian
end
end
idxx = gop(@horzcat,idxlin,1);
end
idxx = idxx{1};
idxlin = nansum(idxx,2);
[idxY,idxX] = raise_1d_to_2d(size(inLat,1),idxlin);
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% split number of loop iterations into chunks for spmd
% from-to array = thread_distro(threads,steps)
function lims = thread_distro(threads,steps)
lims = nan(threads,2);
distemp = round(linspace(1,steps+1,threads+1));
lims(:,1) = distemp(1:end-1);
lims(:,2) = lims(:,1)+diff(distemp)'-1;
if threads>steps
lims = thread_distro(steps,steps);
lims(end+1:threads,:) = repmat([1 0],threads-steps,1);
end
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get 2d-coordinate pair from 1d-coordinate.
function [y,x] = raise_1d_to_2d(Y,x1d)
if isempty(x1d)
y=[];x=[];return
end
x=(ceil((double(x1d))/double(Y)));
y=(x1d - (x-1)*Y);
end
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%