function[transect] = calcTransectBufferWidth2(SamplePoints, sampleDistance, startingPoint,thresholds)

%this FUNCTION will run through each transect and determine how wide it is based upon either the average height or fc value within a square sample area.
%SamplePoints are the xyz locations plus the calculated height and FC for
%each sample.

% sampleDistance is the sampling distance (ie every 1 meter, every 2
% meters, etc)

% startingPoint is the starting location of the stream


%-------BEGIN stuff to comment out when i reinstate the function------
%{
clear
clc
sampleDistance=25;
%load sampleBuffer2
%sampleDistance= perp_seg;
load startingPoint2
startingPoint=startingPoint2;
load BufferSample_neg BufferSample_neg
SamplePoints = (BufferSample_neg);

%}
%------END stuff to comment out when i reinstate the function -----

%%
%preallocate for memory savings...
sampleBuffer=zeros(length(SamplePoints(:,1)),3);

%calculate matrix with 0 and 1 representing nonforest (0) and forest (1)
for sample = 1:length(SamplePoints(:,1)) %this creates a matrix for both positive and negative values       
       % for pos=1:((length(SamplePoints(:,1))))
            
         %change this to calculate based upon the fractional cover for a 5x5m cell.
         
         if SamplePoints(sample,6) >= thresholds(1) %taller than x m vegetation
             sampleBuffer(sample,1:2)=[SamplePoints(sample,1) 1];
         else
             sampleBuffer(sample,1:2)=[SamplePoints(sample,1) 0];
         end
         
         % forest cover based upon FC values
         if SamplePoints(sample,7) >= thresholds(2)%greater than or equal to 30% cover
             sampleBuffer(sample,3)=(1);
         else
             sampleBuffer(sample,3)=(0);
         end
        %end
end

%calculate width 
%%

transectLength = (length(sampleBuffer(:,1)));

%%
% define the gap size (the size at which forest is no longer forest), 5
% means a gap wider than 5 meters (more than5 pixels with a value of non forest in a
% row) will define the edge of the buffer.
%forestThresh is the percent cover of forest used to define "forest". this
%number is used by the UNCC to define forest (20-30%).
gapSize=5;
forestThresh=.3;
windowSize = 5;

%nf = not forest
i=0;
nf=0;
trees=0;
totalForest=0;

%calculate positive width (sampleBuffer(:,2) is based upon height, (:,3) is
%based upon FC values in column 3

for sample2 = 1:transectLength   
    if (sampleBuffer(sample2,3))==1 && (nf <= gapSize)
        i = i + nf + 1; 
        trees=trees+1;
        nf=0;
    elseif  (sampleBuffer(sample2,3))==0 && (nf <= gapSize)
        nf=nf+1;
    else
        break
    end

   pctCover = (trees/sample2);
   totalForest=i;
       
end

%subtract 2 from transect forest width to allow for 2 m from the stream
%centerline (a 6' buffer)... check to see if this is ultimately enough...
    transectForestWidth = (totalForest * sampleDistance);
%     if transectForestWidth == 0
%     else
%         transectForestWidth = transectForestWidth-2;
%     end

% need to adjust this because if total forest width < window size there is
% an issue ------------------------------------------------------------

V = sampleBuffer(:,2);

%slider window (function, window size, matrix, window type). calculate
%percent forest along entire transect, 
percentForest = slidefun(@mean, windowSize, V,'backward');
% make sure the length of forest is dense enough

if totalForest < windowSize
    if totalForest < 1
        transectDensity=0;
    else    
        transectDensity = trees/totalForest;  
    end
        
else
checkSamples = (totalForest-windowSize)+1;
transectDensity=mean(percentForest(1:checkSamples));
end

%i could ultimately add some more code to this to refigure out how wide the
%transect should be but for now i'll just make sure that the entire
%transect is generally greater than 30%

% disp('total trees')
% disp(trees);
% 
% disp('total length')
% disp(totalForest);
% disp('finished calculating transect widthdone!')
%i can't add an x and y here as there's no end point value...
%{
xEnd=startingPoint(1); %this should be the stream starting point
yEnd=startingPoint(2);
%}
%set all to 150 width to look at data

%{
    xEnd=SamplePoints(150,4); %this number is being pulled from buffersamplepos and neg
    yEnd=SamplePoints(150,5);
    transectForestWidth=150;
%}

sampleBeginxy=(SamplePoints(1,4:5));

if totalForest<1
    xEnd= sampleBeginxy(1); %changedt to be the stream EDGE  starting point
    yEnd= sampleBeginxy(2); %this should be the stream starting point
else
   xEnd=SamplePoints(totalForest,4); %this number is being pulled from buffersamplepos and neg
   yEnd=SamplePoints(totalForest,5);
end


%}
   
% add a x and y value to this matrix
transect=[startingPoint(1) startingPoint(2) xEnd yEnd transectForestWidth transectDensity sampleBeginxy(1:2) ];
