function [data,tel_xx,tel_yy] = read_sed_flux(varargin)
%skip_header skips the header lines for CMS sediment flux output files
%   Input options:
%   no input - use ui to select file manually.
%   one input - input file name (as a string). If path is not specified it will be
%               assumed to be the present working directory.
%
%   Output:
%   data - a double in which each column represents a column from the file.
%% parse input
if nargin == 0
    [file,path] = uigetfile('*.*','Select the File to Read');
    fname = [path,'\',file];
else
    fname = varargin{1};
end

%% read file
fid = fopen(fname);                 % open file
point_keyword = 'NUMBER_POINTS';    % set keyword for column number specification
xy_keyword = 'XY_BEGIN';            % set keyword for x, y coordinates
keyword = 'VECTOR_TS_BEGIN';        % set keyword to indicate the last header line
in_header = 1;                      % set header flag

while in_header && ~feof(fid)
    pres_str = fgetl(fid);    
    if contains(pres_str,keyword)
        in_header = 0;
    elseif contains(pres_str,point_keyword)
        pres_words = strsplit(pres_str);
        pres_num = str2double(pres_words);
        pres_num(isnan(pres_num))=[];
        if ~isempty(pres_num)
            num_points = pres_num(1);
            num_col = 1+2*num_points;
        end
    elseif contains(pres_str,xy_keyword)
        for ii = 1:num_points
%            xy_cord = fscanf(fid,'%f %f',2);
           xy_str=fgetl(fid);
           xy_words=strsplit(xy_str);
           xy_num=str2double(xy_words);
           xy_num(isnan(xy_num))=[];
           tel_xx(ii) = xy_num(1);
           tel_yy(ii) = xy_num(2);
        end
    end
end

col_str = repmat('%f ',[1,num_col]);
data_cells = textscan(fid,col_str);
fclose(fid);

cell_len = length(data_cells);
ts_len = length(data_cells{1});
data = nan(ts_len,cell_len);

for cell_i = 1:cell_len
    data(:,cell_i) = data_cells{cell_i};
end
end

