function[StreamLength] = calcStreamLength(SampleDistance, streamData)

%this FUNCTION will  calculate each stream distance ahead of time (for preallocation
%purposes)

% read in stream points... Columns based upon split stream code:
% OriginalStreamName, StreamName, VertexNumber, X, Y

fprintf('assign Stream data, \n\r')
%{
%------------BEGIN for testing only *****************************
clear
clc
%streamData = dlmread('data\streamPoints_allClean4.csv',',',1,0);
load SplitStreams
streamData = SplitStreams;
SampleDistance = 100;
%------------END for testing only *****************************
%}
%calculate total number of streams for the watershed
totalStreams= length(find(unique(streamData(:,2))));
%totalStreams = length(UniqueStreams);
StreamLength=zeros(totalStreams,5);
%Loop through each stream and calculate total length and number of vertices
%per stream

fprintf('Begin calculate stream length & number of vertices for the watershed...\n\r')
for Name = 1:totalStreams
    
    
    % find verticies for each stream segment within each stream    
    A = find(streamData(:,2) == Name);

    % number of verticies in stream segment
    L = length(A);

    originalStreamName = (streamData(A(1),1));
    
    % Order of points (order located in column 6 of the stream data)
    Order = streamData(A(1,1):A(end,1),3);

    % Find stream vertex coordinates
    Stream_temp = [streamData(Order,4)  streamData(Order,5)]; %/ check this - it was screwing things up in the other file

    % removed the sort command - not needed?
    %Stream_coord = Stream_temp(Order,:);
    Stream_coord = Stream_temp;
        
    % preallocate stream segment for L (total stream vertices) and x and Y)
    p1_stream = zeros((L-1),2);
    p2_stream = zeros((L-1),2);
    dist_ft = zeros((L-1),1);
    dist_sum = zeros(L,1);
  
    % Solve for the distance between stream verticies

    for j = 1:L-1

    p1_stream(j,:) = [Stream_coord(j,1) Stream_coord(j,2)]; %first stream vertext coordinate
    p2_stream(j,:) = [Stream_coord(j+1,1) Stream_coord(j+1,2)]; %second stream vertext coordinate

    % distance between 2 points
    dist_ft(j) = sqrt((p2_stream(j,1)-p1_stream(j,1))^2+(p2_stream(j,2)-p1_stream(j,2))^2);

    % convert length from ft to meters
    dist = 0.3048*dist_ft;
    end

    % Calculate cummulative stream length starting at first point given in file
    for k = 2:L-1
    dist_sum(1) = 0;
    dist_sum(2) = dist(1); % pull the distance between the vertices (dist matrix)
    dist_sum(k+1) = dist_sum(k)+dist(k);
    end
    
    Length_temp=(max(dist_sum));
    %adjust this to account for the first transect being run Xm away from
    %the starting vertex of the stream.
    
    %compensate for the fact that sometimes the end of the stream will be
    %at 999.999999 frome the starting vertex - and thus the number of transects will be 9 instead of 10 when you round down
    % so add 1 additional transect by assigning length to 1000 if the
    % actual value is 999.9999 - but why is it 999.9999
%     if Length_temp > 999.99990 && Length_temp < 1000.0000
%         Length_temp = round(Length_temp);
%     end
        
    NumberTransects= floor(Length_temp/SampleDistance);
    
    %record stream length (Stream Name, Length, Number of Vertices, Number of Transects)
    StreamLength(Name,:)= [originalStreamName, Name, Length_temp, L, NumberTransects];
end

fprintf('Done calculating length of each stream for the entire watershed...\n\r')
