function [transectStruct] = lidar2transect(filename, rawFileArray)
% Read a lidar transect file
% The file looks like this:

% -124.397664  42.549208 -245.71     860.00  1.383 22170 385248.451 4711703.622    NaN
% -124.393804  42.548330 -577.34     860.01 32.038 22170 385563.768 4711600.906    NaN
% -124.395897  42.548806 -397.52     860.02  6.684 22170 385392.793 4711656.591    NaN 
%
% Column description:
%
% 1.  longitude of data point
% 2.  latitude of data point
% 3.  Distance along profile line
% 4.  distance from segment origin (you shouldn't need this)
% 5.  height
% 6.  profile number
% 7.  UTM easting
% 8.  UTM northing
% 9.  linear regression (if applicable, otherwise NaN)

% This mfile loads the cut profile data (many data points per profile).
% It creates the variables:
% transect.lon                      longitude of each lidar data point
% transect.lat                      latiitude of each lidar data point
% transect.alititude                height of each lidar data point
% transect.crossShoreCoordinate     dist. from data point to profile origin
% transect.id                       profile number
% transect.easting                  easting of each lidar data point
% transect.northing                 northing of each lidar data point

[pathstr, name, ext, versn] = fileparts(filename);

data =  load(filename); 
trans = unique(data(:,6));

transectStruct = createlidartransectstruct();
transectStruct = repmat(transectStruct, 1, 1500);

% create a new structure
transect = createlidartransectstruct();
transect.areaCode = str2num(name(18:19));
transect.time = datenum(str2num(['20' name(1:2)]), str2num(name(3:4)), str2num(name(5:6))) - datenum(1970,1,1); % days since 01/01/1970
transect.areaName = name(16:17);

for i = 1: length(trans)

    id = find(data(:,6) == trans(i));                     %  find data linked to selected transect

    selection = data(id,:);                               % select the data that is linked to the selected transect
    [dummy id2] = sort(selection(:,3));                   % find the right sorting sequence
    selection = selection(id2,:);                         % make this the sequence  
    
    if size(selection,2) > 6
        transect.x = selection(:,7);                          % take from file
        transect.y = selection(:,8);
    else
        [transect.x,transect.y] = convertCoordinates(selection(:,1),selection(:,2),'CS1.code',4326,'CS2.code',32610); % compute from lon lat
    end
    
    most_seaward_point = find(selection(:,3) == min(selection(:,3)),1,'first');
    most_landward_point = find(selection(:,3) == max(selection(:,3)),1,'first');
    transect.contour = [transect.x(most_seaward_point), transect.x(most_landward_point),transect.y(most_seaward_point), transect.y(most_landward_point)];

    transect.id = selection(1,6);
    transect.origin = [];
    transect.crossShoreCoordinate = selection(:,3);
    
    if sum([rawFileArray.bytes]) < 1E9 % save memory mode
        transect.altitude = selection(:,5);
        transect.lon = selection(:,1);
        transect.lat = selection(:,2);
        if size(selection,2) > 6
            transect.regression = selection(:,9);
        else
            transect.regression = repmat(nan,length(selection),1); % otherwise regression information is not in datafile
        end
    end
       
    % store the results in the big array
    transectStruct(i) = transect;
   
end

 transectStruct(i+1:end) = [];