
%% relocate vaklodingen raw files that are shifted
%  before proceeding with processing

D               = dir2('..\raw\grid','file_incl','(\.asc$)');
files           = find(~[D.isdir]);
fullfilenames   = strcat({D(files).pathname}, {D(files).name})';
mkpath('..\..\raw\shifted\');

multiWaitbar('Processing asc files',0)

for ii = 1:length(fullfilenames);
    
    %% 
    
    [path, fname, extension] = fileparts(fullfilenames{ii});
    if ~strcmp(extension,lower(extension))
        movefile(fullfilenames{ii},fullfile(path,[fname lower(extension)]));
        [path, fname, extension] = fileparts(fullfilenames{ii});
    end
    
    %% read header
    
    fid          = fopen(fullfilenames{ii});
    s            = textscan(fid,'%s %f',6);
    ncols        = s{2}(strcmpi(s{1},'ncols'        ));
    nrows        = s{2}(strcmpi(s{1},'nrows'        ));
    xllcorner    = s{2}(strcmpi(s{1},'xllcorner'    ));
    yllcorner    = s{2}(strcmpi(s{1},'yllcorner'    ));
    cellsize     = s{2}(strcmpi(s{1},'cellsize'     ));
    fclose(fid);
    
    %% check location: corners should be located at mesh defined by cellsize
    
    if mod(xllcorner,cellsize) >0
        disp(fullfilenames{ii});
        disp(mod(xllcorner,cellsize));
        movefile(fullfilenames{ii},fullfile(path,'..','shifted',[fname extension]));
    elseif mod(yllcorner,cellsize) >0
        disp(fullfilenames{ii});
        disp(mod(yllcorner,cellsize));
        movefile(fullfilenames{ii},fullfile(path,'..','shifted',[fname extension]));
    end
    
    multiWaitbar('Processing asc files',ii/length(fullfilenames))
    
end
