clear
[year_original year_duplicate] = deal(1992, 1994);
pattern = '^KB112_4746_(%i|%i).*'; % regular expression for the asc files of concern
boundaries = 'shp'
% path of asc files
ascdir = 'd:\heijer\sources\raw_vaklodingen\grid\';
% path of ldb files with zeeland measurement tiles
ldbdir = 'd:\heijer\sources\DienstZeeland\polygons\';
% path of shp files with zero differences between particular years
if diff(sort([year_original year_duplicate])) == 2
    shapedir = 'd:\heijer\Work\Deltares\Projects\KPP\BenO_kust\2013\data_op_orde\verschil0\Verschil_polygons2_CLEAN\';
else
    shapedir = 'd:\heijer\Work\Deltares\Projects\KPP\BenO_kust\2013\data_op_orde\verschil0\Verschil_polygons_CLEAN\';
end

if strcmp(boundaries, 'ldb')
    D = dir2(ldbdir,...
        'file_incl', '^(13|14)_.*\.ldb',...
        'no_dirs', true);
    ldb = cellfun(@(fname) readldb(fullfile(ldbdir, fname)), {D.name},...
        'uniformoutput', false);
    fieldfun = @lower;
elseif strcmp(boundaries, 'shp')
    D = dir2(shapedir,...
        'file_incl', sprintf('_%imin%i.shp$', sort([year_original year_duplicate], 'descend')),...
        'no_dirs', true);
    ldbtmp = cellfun(@(fname) shaperead(fullfile(shapedir, fname(1:end-4))), {D.name},...
        'uniformoutput', false);
    ldb{1} = ldbtmp{1}(1);
    for i = 2:length(ldbtmp{1})
        ldb{i} = ldbtmp{1}(i);
    end
    fieldfun = @upper;
else
    error
end
% find asc files that match the predefined pattern
D = dir2(ascdir,...
    'file_incl', sprintf(pattern, year_original, year_duplicate),...
    'no_dirs', true);

% construct the paths to the duplicate resp. original file
idx1 = ~cellfun(@isempty, regexp({D.name}, sprintf('_%i%s', year_duplicate, '\d{4}'), 'once'));
fname1 = fullfile(ascdir, D(idx1).name)
% idx2 = ~cellfun(@isempty, regexp({D.name}, sprintf('_%i%s', year_original, '\d{4}'), 'once'));
fname2 = false;%fullfile(ascdir, D(idx2).name)

% modify or delete the duplicate file based on zero difference within the
% polygon(s)
asc_cleanup_and_plot_difference(ldb, fname1, fname2, 'fieldfun', fieldfun)