% Copyright 2017 The Johns Hopkins University Applied Physics Laboratory.
% Licensed under the MIT License. See LICENSE.txt in the project root for full license information.

% Calculates error distribution given certain point percentiles and pre-set error level

% Version History
%   2017/03/28  Marc Bosch       Initial version
function [errorKprofile] = errorprofiling(data,ThsmeshCUMUL,ThsZPERC)
errorKprofile=zeros(2,max(length(ThsmeshCUMUL),length(ThsZPERC)));
for ij=1:length(ThsmeshCUMUL)
    errorCUMUL(ij) = length(find(data<ThsmeshCUMUL(ij)))/ ...
        length(data);
end
errorKprofile(1,1:length(ThsmeshCUMUL)) = errorCUMUL;
sorted_errorK=sort(data,'ascend');
for ij=1:length(ThsZPERC)
    pos = find(sorted_errorK<=ThsZPERC(ij));
    if isempty(pos)==1
        errorPERC(ij)=NaN;
    else
        errorPERC(ij) = sorted_errorK(round(ThsZPERC(ij)*length(sorted_errorK)));
    end
end
errorKprofile(2,1:length(ThsZPERC)) = errorPERC;
