%
% Create a CDF graph
%
% DataSortArray - data to plot, must be sorted
% NameArray     - Name for each data set, used to create the legend
% LegendArray   - If present used to augment NameArray when creating the legend
% colorArray    - used to define the colour for each data set
% xAxisMax      - Define maximum value for x-axis
% yAxisMin      - Define minimum value for y-axis
% xLab          - Label for the x-axis
% graphTitle    - title for the graph
% fileRoot      - root file name 
% addPercentiles - add 68% and 95% lines
% saveFig       - 1 save a fig file, 0 don't
%
%
function createCDF(DataSortArray,NameArray, LegendArray, colorArray, xAxisMax, yAxisMin, xLab, graphTitle, fileRoot, saveFig, addPercentiles)

index = 1;
figure

% The name string is pass in for each data element, however if the
% caller was not able to generate data for the last element in
% DataSortArray unless we reduce the loop below we'll crash. If there is
% missing data in the middle we process through with no issues.
[m,n] = size(DataSortArray);
if(n < length(NameArray))
  NumElements = n;
else
  NumElements = length(NameArray);
end

for k=1:NumElements
  if(length(DataSortArray{k}) > 0)
    if(iscell(LegendArray))
      LegendAll{index} = [NameArray{k}, LegendArray{k}];
    else
      LegendAll{index} = NameArray{k};
    end

    plot(DataSortArray{k}',...
        (1:length(DataSortArray{k}')) ...
            ./length(DataSortArray{k}'), ...
        'color', colorArray(k,:), ...
        'linewidth',3);

    if (index == 1)
      hold on
    end

    index = index + 1;
  end
end

if(exist('LegendAll') ~= 0)
  grid

  A = axis;
  A(1) = 0;
  A(2) = xAxisMax;
  A(3) = yAxisMin;
  axis(A);
  
  if( (yAxisMin < 0.5) & (addPercentiles == 1) )
    X(1) = 0;
    X(2) = xAxisMax;

    Y(1) = 0.68;
    Y(2) = 0.68;
    plot(X,Y,'-.','color',[1 165/255 0],'linewidth',3);

    Y(1) = 0.95;
    Y(2) = 0.95;
    plot(X,Y,'-.','color',[1 0 0],'linewidth',3);
    legend([LegendAll,'68%','95%'],'Location','Best');
  else
    legend(LegendAll,'Location','Best');
  end


  xlabel(xLab);
  ylabel('CDF');
  title(graphTitle);
  TempStr = [fileRoot, 'png'];
  savePlot(TempStr,saveFig);
end

