function [ Bmap2D ] = read2Dfieldmap( filespath )
%read2fieldmaps read fieldmaps from the files and stack them at a distance
%   filespath is the directory containing the scans

thresh = 0.01; % field threshold fraction (for locating the edge of the und)

Bmap2D = [];

filenames = {{'001gap000.000x+01.00y+00.00',
'002gap000.000x+00.80y+00.00',
'003gap000.000x+00.60y+00.00',
'004gap000.000x+00.40y+00.00',
'005gap000.000x+00.20y+00.00',
'006gap000.000x+00.00y+00.00', % file naming ambiguity so try both
'006gap000.000x+00.00y+00.00', 
'007gap000.000x-00.20y+00.00',
'008gap000.000x-00.40y+00.00',
'009gap000.000x-00.60y+00.00',
'010gap000.000x-00.80y+00.00',
'011gap000.000x-01.00y+00.00'},
{'001gap+000.000x+01.00y+00.00',
'002gap+000.000x+00.80y+00.00',
'003gap+000.000x+00.60y+00.00',
'004gap+000.000x+00.40y+00.00',
'005gap+000.000x+00.20y+00.00',
'006gap+000.000x+00.00y+00.00', % file naming ambiguity so try both
'006gap+000.000x-00.00y+00.00', 
'007gap+000.000x-00.20y+00.00',
'008gap+000.000x-00.40y+00.00',
'009gap+000.000x-00.60y+00.00',
'010gap+000.000x-00.80y+00.00',
'011gap+000.000x-01.00y+00.00'}};

fns = ['/zscan.dat';'/Zscan.dat'];

xs = 1e-3*sort([linspace(-1,1,11), 0]);


for i = 1:numel(filenames{1})
    for j = 1:2
        filepath = [filespath '/' filenames{j}{i} fns(j,:)];
        try
            Bm = readfieldmap( filepath, 47 );
        %      Bm(:, 4) = Bm(:,3); % rearrange
        %      Bm(:, 3) = Bm(:,2); % to
        %      Bm(:, 2) = ys(i); % fit y in
            Bm(:, 4) = xs(i); % fit y in
            Bmap2D = [Bmap2D; Bm];
            break
        catch
            if(j==2000)
                fprintf(['NOTE: could not read ' filepath '\n']);
    %                  fprintf('(It''s normal to have 1 and only 1 of these messages.)\n');
            end
        end
    end
end


end

