-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalignTile.m
267 lines (175 loc) · 5.55 KB
/
alignTile.m
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
function [outFlag,outname]=alignTile(f)
% alignTile Aligns unregistered tile to registered neigbors
%
% [outFlag,outname]=alignTile(f) where f is a cellstr of path/files with
% f{1} the floating file to be aligned and f{2:end} the registered files to
% align it to. outFlag returns true if successful, with output file name.
% If fail, outFlag will be false and outname will be empty.
% set out flag
outFlag=false;
outname=[];
%% first test if input args are either valid filenames or mat file handles
nfiles=length(f);
for i=1:nfiles
if ischar(f{i}) % it's a string, might be a filename
if exist(f{i},'file') % yup its a file
m = matfile(f{i}); % load it
else
error('File does not exist');
end
elseif isvalid(f{i}) % not a string, is it a valid file handle?
m = f{i}; % yes, so set m to f and get the filename for f
f{i} = m.Properties.Source;
else
error('input arg must be a filename or valid matfile handle')
end
eval(['f',num2str(i-1),'=f{i};']);
eval(['m',num2str(i-1),'=m;']);
clear m
end
info0=whos(m0,'z');
sz0=info0.size;
dtrans=cell(1,nfiles-1);
rmse=cell(1,nfiles-1);
%% Coregister to each neighbor
for i=2:nfiles
eval(['m=m',num2str(i-1),';']);
% crop reference to buffer
c0 = m0.x >= min(m.x) & m0.x <= max(m.x);
r0 = m0.y >= min(m.y) & m0.y <= max(m.y);
c0 = [find(c0,1,'first'),find(c0,1,'last')];
r0 = [find(r0,1,'first'),find(r0,1,'last')];
if isempty(c0) || isempty(r0); error('no overlap between tiles'); end
% crop floating tile to buffer
c1 = m.x >= min(m0.x) & m.x <= max(m0.x);
r1 = m.y >= min(m0.y) & m.y <= max(m0.y);
c1 = [find(c1,1,'first'),find(c1,1,'last')];
r1 = [find(r1,1,'first'),find(r1,1,'last')];
% Coregistration cluster array
C0=m0.C(r0(1):r0(2),c0(1):c0(2));
coregClusters=max(C0(:))-1;
if coregClusters == -1; fprintf('no overlapping data in %s\n',f{i}); continue; end
if coregClusters == 0; error('Grid appears to be already registered'); end
%% pull buffer data and align
x0 = m0.x(1,c0(1):c0(2));
y0 = m0.y(r0(1):r0(2),1);
z0 = m0.z(r0(1):r0(2),c0(1):c0(2));
mt0= m0.mt(r0(1):r0(2),c0(1):c0(2));
x1 = m.x(1,c1(1):c1(2));
y1 = m.y(r1(1):r1(2),1);
z1 = m.z(r1(1):r1(2),c1(1):c1(2));
%mt1= m.mt(r0(1):r0(2),c0(1):c0(2));
% cluster coregistraton loop
for j=1:coregClusters
fprintf('Registering coregistered cluster %d of %d\n',j,coregClusters);
% make a mask of this cluster
mt0c= mt0 & (C0 == j+1);
% co-register floating tile to reference tile
[~,dtrans{j,i-1},rmse{j,i-1}] = coregisterdems(x0, y0, z0, x1, y1, z1, mt0c);
end
clear m r0 c0 r1 c1 C0 x0 y0 z0 mt0 x1 y1 z1 mt1 mt0c
end
% check to see if no overlap = all empty dtrans cells
emptyCheck=cellfun(@isempty,dtrans);
if ~any(~emptyCheck)
fprintf('unable to align: no overlap \n');
return
end
% take average dtrans for each cluster
for i=1:size(dtrans,1)
dtrans{i,1} = nanmean([dtrans{i,:}],2);
rmse{i,1} = nanmean([rmse{i,:}],2);
end
dtrans=dtrans(:,1);
rmse=rmse(:,1);
% reorganize dtrans and rmse for consistency with gcp reg files
dtrans=dtrans';
dtrans = [dtrans{:}];
rmse=rmse';
rmse = [rmse{:}];
if ~any(~isnan(rmse))
fprintf('coregistration failure\n');
return
end
%% create output file
outname=strrep(m0.Properties.Source,'_dem.mat','_reg_dem.mat');
clear m1
m1 = matfile(outname,'Writable',true);
fprintf('Setting x and y\n');
m1.x=m0.x;
m1.y=m0.y;
m1.dtrans = dtrans;
m1.rmse = rmse;
%% Regridding
% get cluster array
C=m0.C;
% Apply registration to z grid variable
fprintf('Regridding z\n');
z= nan(sz0); % initialize output
for i=1:size(dtrans,2)
if any(isnan(dtrans(:,i))); continue; end
% make a mask of this cluster
N = C == i+1;
% apply registration
ztemp = applyRegistration(dtrans(:,i),m0,N);
clear N
n=isnan(z) & ~isnan(ztemp);
z(n) = ztemp(n);
clear ztemp n
end
m1.z=z;
clear z
% Apply registration to mt grid variable
fprintf('Regridding mt\n');
% cluster coregistraton loop
mt = false(sz0); % initialize output
for i=1:size(dtrans,2)
if any(isnan(dtrans(:,i))); continue; end
% make a mask of this cluster
N = C == i+1;
% Apply registration
mttemp = applyRegistration(dtrans(:,i),m0,N,'mt');
clear N
% add in any matches
mt = mt | mttemp;
clear mttemp
end
m1.mt=mt;
clear mt
%% Apply registration to or grid variable
fprintf('Regridding or\n');
% cluster coregistraton loop
or = zeros(sz0,'int16'); % initialize output
for i=1:size(dtrans,2)
if any(isnan(dtrans(:,i))); continue; end
% make a mask of this cluster
N = C == i+1;
% Apply registration
ortemp = applyRegistration(dtrans(:,i),m0,N,'or');
clear N
n= or==0 & ortemp ~= 0;
or(n) = ortemp(n);
clear ortemp n
end
m1.or=or;
clear or
%% Apply registration to dy grid variable
fprintf('Regridding dy\n');
% cluster coregistraton loop
dy= zeros(sz0,'int16'); % initialize output
for i=1:size(dtrans,2)
if any(isnan(dtrans(:,i))); continue; end
% make a mask of this cluster
N = C == i+1;
% Apply registration
dytemp = applyRegistration(dtrans(:,i),m0,N,'dy');
clear N
n= dy==0 & dytemp ~= 0;
dy(n) = dytemp(n);
clear dytemp n
end
m1.dy=dy;
clear dy
fprintf('Building metadata\n');
tileRegMeta(m1,{'Neighbor Align'})
outFlag=true;