function [ Bmap1D ] = readfieldmap( filename, nskiplines )
%READFIELDMAP read fieldmap from the file
%   Detailed explanation goes here

if nargin < 2
    nskiplines =   29; % default for the on-axis field scans from ANL
end

% this doesn't work with variable space delimiters found in some of the SLAC scans
%  map = readtable(filename,'HeaderLines',nskiplines,'Delimiter',' ','ReadVariableNames',false);

%  % this is much slower but works with arbitrary delimiters; only only works with matlab >2016
%  opts = detectImportOptions(filename);
%  display(opts)
%  map = readtable(filename,opts);

% Zhen's script works well and is much faster
map = read_scan_data(filename);

% detect which format (SLAC or ANL)
%  tr = textread(filename,'%s'); tr = tr(1); tr = tr{1};
fid = fopen(filename);
cellread = textscan(fid,'%s',1);
strread = cellread{1}{1};
fclose(fid);

%  zs = table2array(map(:,1));
%  Bxs = -table2array(map(:,6));
%  Bys = table2array(map(:,7));

if( strcmp(strread, 'APS') )

    zs = map(:,1);
    Bxs = -map(:,6);
    Bys = map(:,7);
    
else

    zs = map(:,1);
    Bxs = -map(:,7);
    Bys = map(:,6);
    
end

medfilt_width = 7;
Bxs = medfilt1(Bxs, medfilt_width);
Bys = medfilt1(Bys, medfilt_width);

%  lpfilt_cuttoff = 1000;
%  Bxs = lpfilter(Bxs, lpfilt_cuttoff);
%  Bys = lpfilter(Bys, lpfilt_cuttoff);


Bmap1D = [ zs, Bxs, Bys ];

end

