GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — setsm_postprocessing/QCStripsByTile.m at master · ihowat/setsm_postprocessing

Expand file tree

/

QCStripsByTile.m

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
494 lines (339 loc) · 14.2 KB
/

QCStripsByTile.m

Copy path

File metadata and controls

494 lines (339 loc) · 14.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
function QCStripsByTile(regionNum,varargin)
%% Argins
%regionNum='02'; % region number
tilefile = 'PGC_Imagery_Mosaic_Tiles_Antarctica.mat'; %PGC/NGA Tile definition file, required
arcdemfile= 'rema_tiles.mat'; % lists which tiles go to which regions, required
dbasefile = 'rema_strips_8m_wqc_cs2bias.mat'; % database file
changePath='/Users/ihowat'; %if set, will change the path to the REMA directory from what's in the database file. set to [] if none.
% if an older set of mosaic files already exist, we can speed things up by
% check to see if they already have 100% coverage - will skip if do. Leave
% empty if none.
tileDir= dir(['/data4/REMA/region_',regionNum,'*']);
if ~isempty(tileDir)
tileDir= ['/data4/REMA/',tileDir(1).name,'/mosaic_reg_qc_feather/40m/'];
end
%% defaults
startfrom = 1;
minN = 500;
minArea = 500;
% parse inputs
for i=1:2:length(varargin)
eval([varargin{i},'=',num2str(varargin{i+1}),';']);
end
%% Find tiles in this region and load meta
% Get tile Defs
tiles=load(tilefile);
a=load(arcdemfile);
%remove duplicated tile entries keeping just first occurence
[~,n] = unique(a.tileName,'stable');
a = structfun(@(x) ( x(n) ), a, 'UniformOutput', false);
% get index of this region number
n=a.regionNum==str2num(regionNum);
% check to make sure we find sum
if ~(any(n)); error('no tiles matched for this region number'); end
% match region number to overlapping tiles
[~,n]=intersect(tiles.I,a.tileName(n));
% crop tile structure to overlapping tiles
tiles = structfun(@(x) ( x(n) ), tiles, 'UniformOutput', false);
% load database structure
meta=load(dbasefile);
% alter paths in database if set
if ~isempty(changePath)
meta.f = strrep(meta.f,'/data4',changePath);
meta.region = strrep(meta.region,'/data4',changePath);
end
%check meta file for required fields
if ~isfield(meta,'avg_rmse'); error('meta stucture missing avg_rmse field '); end
if ~isfield(meta,'xmax'); error('meta stucture missing xmax field '); end
if ~isfield(meta,'ymax'); error('meta stucture missing ymax field '); end
if ~isfield(meta,'xmin'); error('meta stucture missing ymin field '); end
if ~isfield(meta,'ymin'); error('meta stucture missing ymin field '); end
if ~isfield(meta,'x'); error('meta stucture missing x field '); end
if ~isfield(meta,'y'); error('meta stucture missing y field '); end
if ~isfield(meta,'f'); error('meta stucture missing f field '); end
% select the whichever registration has the better sigma_bias (all or 1 yr)
if isfield(meta,'sigma_all') && isfield(meta,'sigma_1yr') && ~isfield(meta,'sigma')
meta.sigma = nanmin([meta.sigma_all(:)';meta.sigma_1yr(:)'])';
end
% if no ground control error field, just set to nan
if ~isfield(meta,'sigma')
meta.sigma = nan(size(meta.f));
end
% if ground control error > 1, set to NaN
meta.sigma(meta.sigma > 1) = NaN;
% set 0 RMSE (single scenes) to NaN
meta.avg_rmse(meta.avg_rmse == 0) = NaN;
% tile loop
for i=startfrom:length(tiles.I)
fprintf(' \n')
fprintf('Working tile %d of %d: %s \n',i,length(tiles.I),tiles.I{i});
tile = structfun(@(x) ( x(i) ), tiles, 'UniformOutput', false);
% check existing tile for coverage
if ~isempty(tileDir)
if exist([tileDir,tile.I{1},'_40m_dem.mat'],'file')
load([tileDir,tile.I{1},'_40m_dem.mat'],'N');
if sum(N(:))./numel(N) == 1
fprintf('tile coverage complete, skipping\n')
clear N
continue
end
end
end
qctile(tile,meta,minN,minArea,changePath);
end
function qctile(tiles,meta,minN,minArea,changePath)
%% Spatial coverage search
% make tile boundary polygon
tilevx = [tiles.x0;tiles.x0;tiles.x1;tiles.x1;tiles.x0];
tilevy = [tiles.y0;tiles.y1;tiles.y1;tiles.y0;tiles.y0];
% quick search: find strips within range of this tile. This does not
% account for background area of around strips but just pairs them down to
% speed the poly intersection loop
n = meta.xmax > tiles.x0 & meta.xmin < tiles.x1 & ...
meta.ymax > tiles.y0 & meta.ymin < tiles.y1;
if ~any(n); fprintf('no strip overlap\n'); return; end
% get polys
meta = structfun(@(x) ( x(n,:) ), meta, 'UniformOutput', false);
% search for all strips overlapping this tile
in=zeros(size(meta.f));
for i=1:length(in)
in(i) = any(inpolygon(meta.x{i},meta.y{i},tilevx,tilevy)) | ...
any(inpolygon(tilevx,tilevy,meta.x{i},meta.y{i}));
end
if ~any(in); fprintf('no strip overlap\n'); return; end
% crop meta data struct to only overlapping strips
meta = structfun(@(x) ( x(logical(in),:) ), meta, 'UniformOutput', false);
fprintf('%d files overlapping this tile, ',sum(in));
% add existing qc data
meta = addQC2Meta(meta,changePath);
fprintf('%d files with existing qc\n',sum(meta.qc ~= 0));
% remove already added entries from metadata
if any(meta.qc == 5)
fprintf('removing %d files with qc flag=5\n',sum(meta.qc == 5));
meta = structfun(@(x) ( x(meta.qc ~= 5 ,:) ), meta, 'UniformOutput', false);
if isempty(meta.f); fprintf('all strips removed, returning \n'); return; end
end
% build grid
res = 40;
buff = 0;
x = tiles.x0-buff*res: res:tiles.x1+buff*res;
y = tiles.y1+buff*res:-res:tiles.y0-buff*res;
y = y(:);
N= zeros(length(y),length(x),'uint8');
% apply coastline
if isfield(tiles,'coastline')
fprintf('applying coastline, ');
A = false(size(N));
i=1;
for i=1:length(tiles.coastline{1})
if isempty(tiles.coastline{1}{i}); continue; end
A(roipoly(x,y,N,tiles.coastline{1}{i}(1,:),...
tiles.coastline{1}{i}(2,:))) = true;
end
percent_filled = 100*sum(~A(:))./numel(A);
if percent_filled > 0.2
N(~A) = 1;
else
percent_filled =0;
end
clear A
fprintf('%.2f%% filled as water\n',percent_filled);
if percent_filled == 100; fprintf('returning \n'); return; end
end
%% Build Grid Point Index Field
% make a cell for each file containing the col-wise indices of the data
% points on the tile grid. This is used search for new or existing data on
% the grid within each search iteration.
fprintf('calculating tile pixel coverage for each strip, ');
% initialize output field
meta.gridPointInd=cell(size(meta.f));
% can be slow, so we'll use a counter
count = 0;
% already qc'd files to add to N
add2N = meta.qc > 0 & meta.qc < 4;
fprintf('%d already qc-passed files will be added \n',sum(add2N))
if ~any(~add2N); fprintf('all files already passed qc, returning \n'); return; end
% file loop
for i=1:length(meta.f)
% counter
if i>1 for p=1:count fprintf('\b'); end; %delete line before
count = fprintf('strip %d of %d',i,size(meta.f,1));
end
% locate grid pixels within footprint polygon
BW = roipoly(x, y, N, meta.x{i}, meta.y{i});
%if mask data exists, apply it
if meta.qc(i) == 3
j=1;
for j=1:length(meta.maskPolyx{i})
BW(roipoly(x,y,BW,...
meta.maskPolyx{i}{j},meta.maskPolyy{i}{j}))=0;
end
end
% add if already qc'd
if add2N(i); N(BW) = 1; continue; end
% convert BW mask to col-wise indices and save to cell
meta.gridPointInd{i}=find(BW);
% get rid of mask
clear BW
end
% clear counter line
for p=1:count fprintf('\b'); end;
% check if filled
percent_filled = 100*sum(N(:))./numel(N);
fprintf('%.2f%% filled\n',percent_filled);
if percent_filled == 100; fprintf('returning \n'); return; end
% remove already added entries from metadata
meta = structfun(@(x) ( x(~add2N ,:) ), meta, 'UniformOutput', false);
% make another field with the number of data points
meta.gridPointN=cellfun(@length,meta.gridPointInd);
%remove strips below a minimum size
stripArea = nan(size(meta.f));
for i=1:length(meta.f)
stripArea(i) = polyarea(meta.x{i}, meta.y{i})./1000^2;
end
fprintf('removing %d strips smaller than %.1f km^2\n',sum(stripArea < minArea),minArea);
meta = structfun(@(x) ( x(stripArea >= minArea ,:) ), meta, 'UniformOutput', false);
%% coverage test loop
if ~isfield(meta,'rmse'); meta.rmse=nan(size(meta.f));end
if ~isfield(meta,'dtrans'); meta.dtrans=zeros(length(meta.f),3); end
if ~isfield(meta,'overlap'); meta.overlap=zeros(size(meta.f)); end % number existing data points overlapping file
%%
recountFlag=true;
skipn = 0;
while length(meta.f) >= 1
percent_filled = 100*sum(N(:))./numel(N);
if percent_filled == 100; fprintf('100%% filled returning \n',percent_filled); return; end
fprintf('%.2f%% filled\n', percent_filled);
% loop through files in boundary and find # of new/existing points
if recountFlag
for i=1:length(meta.f)
% subset of N at data points in this file
Nsub=N(meta.gridPointInd{i});
% count existing data points
meta.overlap(i)=sum(Nsub);
% count new data points
meta.gridPointN(i) = sum(~Nsub);
end
end
recountFlag=false;
% remove rendundant files (with already 100% coverage)
redundantFlag = meta.gridPointN < minN;
if any(redundantFlag)
% remove redundant files from lists
meta = structfun(@(x) ( x(~redundantFlag,:) ), meta, 'UniformOutput', false);
fprintf('%d redundant files (N < %d) removed\n',sum(redundantFlag),minN);
if isempty(meta.f); fprintf('all strips removed, returning \n'); return; end
end
A = nansum([100.*meta.gridPointN./numel(N),1./(meta.avg_rmse.^2)],2);
[~,n]=sort(A,'descend');
if skipn < 0; skipn = length(n)-1; end
% skip if skipped on last iteration
if length(n) >= 1+skipn
n = n(1+skipn);
else
n=n(1);
skipn = 0;
end
fprintf('%d of %d strips remaining\n',skipn+1,length(meta.f));
fileName=strrep(meta.f{n},'meta.txt','dem_browse.tif');
fprintf('%s\n',fileName);
fprintf('%d new pointsm, gcp sigma=%.2f, mean coreg RMSE=%.2f, max coreg RMSE=%.2f \n',...
meta.gridPointN(n),meta.sigma(n),meta.avg_rmse(n), meta.max_rmse(n));
% localfileName = strrep(fileName,'/data4/REMA','~/rema8mStripBrowse');
localfileName = fileName;
I=readGeotiff(localfileName);
Ni=interp2(x,y(:),N,I.x,I.y(:),'*nearest');
imagesc(I.x,I.y,I.z,'alphadata',single(I.z ~= 0))
set(gca,'color','r')
axis xy equal tight;
colormap gray;
hold on;
imagesc(I.x,I.y,Ni,'alphadata',single(Ni).*.5)
if isfield(tiles,'coastline')
i=1;
for i=1:length(tiles.coastline{1})
if isempty(tiles.coastline{1}{i}); continue; end
plot(tiles.coastline{1}{i}(1,:),...
tiles.coastline{1}{i}(2,:),'b','linewidth',1)
end
end
plot([tiles.x0,tiles.x0,tiles.x1,tiles.x1,tiles.x0], [tiles.y0,tiles.y1,tiles.y1,tiles.y0,tiles.y0],'w','linewidth',2)
set(gca,'xlim',[min(I.x)-500 max(I.x)+500],'ylim',[min(I.y)-500 max(I.y)+500]);
%set(gcf,'units','normalized');
%set(gcf,'position',[0.01,0.01,.35,.9])
qc=load([fileparts(fileName),'/qc.mat']);
fileNames = qc.fileNames;
% alter paths in database if set
if ~isempty(changePath)
fileNames = strrep(fileNames,'/data4',changePath);
end
[~,IA]=intersect(fileNames, fileName);
if isempty(IA); error('this file name not matched in the qc.mat, probably need to upadate it.'); end
if qc.flag(IA) ~= 4 && qc.flag(IA) ~= 0
fprintf('flag previoulsy changed to %d, applying\n',qc.flag(IA))
else
j=1;
while j
try
flag=input('Enter quality flag: 0=skip,9=back, 1=good, 2=partial, 3=manual edit, 4=poor, 5=unuseable, 6=quit\n');
if ~isempty(flag)
if isnumeric(flag)
if flag == 0 || flag == 9 || flag == 1 || flag == 2 || flag == 3 || flag == 4 || flag == 5 || flag == 6
break;
end
end
end
catch
fprintf('%d not recogized, try again\n',flag);
end
if iscell(flag); flag=flag{1}; end
fprintf('%d not recogized, try again\n',flag);
end
if flag == 6; clf; return; end
if flag == 0; skipn = skipn+1; clf; continue; end
if flag == 9; skipn = skipn-1; clf; continue; end
qc.flag(IA)=flag;
if flag == 1 || flag == 2
qc.x{IA}=cell(1); qc.y{IA}=cell(1);
end
if flag == 3
fprintf('entering manual edit mode\n')
j=1;
while j
[~,qc.x{IA}{j},qc.y{IA}{j}] = roipoly;
plot(qc.x{IA}{j},qc.y{IA}{j},'g','linewidth',2)
while j
s=input('continue editing this image? (y/n)\n','s');
if ~strcmpi(s,'y') && ~strcmpi(s,'n')
fprintf('%s not recogized, try again\n',s);
else
break
end
end
if strcmpi(s,'n'); break; end
j=j+1;
end
end
save([fileparts(fileName),'/qc.mat'],'-struct','qc');
end
clf
if qc.flag(IA) > 0 && qc.flag(IA) < 4
M = I.z ~=0;
j=1;
for j=1:length(qc.x{IA})
M(roipoly(I.x,I.y,M,qc.x{IA}{j},qc.y{IA}{j}))=0;
end
M=interp2(I.x,I.y(:),single(M),x,y(:),'*nearest');
N(M == 1) = 1;
recountFlag=true;
% remove this file from the meta struct
in=1:length(meta.f); in(n)=[];
meta = structfun(@(x) ( x(in,:) ), meta, 'UniformOutput', false);
skipn = 0;
elseif qc.flag(IA) == 4 || qc.flag(IA) == 5
% remove this file from the meta struct
in=1:length(meta.f); in(n)=[];
meta = structfun(@(x) ( x(in,:) ), meta, 'UniformOutput', false);
end
end
%
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.