function out = findclosest_n(X, IND, n)
% findclosest
% supposed to find the closest n values of X elements in search index IND
% inputs should be vectors (indexing X to IND)
%

% test values
% X = (0:1:100);
% IND = (0:1.2:100*1.1);
% n = 5;
% X = TCCON_other_time_1970;
% IND = TCCON_time_1970;
% max_dist = .5;
% IND = IND1(randperm(length(IND1)));
% X = X1(randperm(length(X1)));

% timer1 = cputime;

% data input check
if (size(X,1) ~=1 && size(X,2)~=1) || (size(IND,1)~=1 && size(IND,2)~=1) || length(n) ~=1;
    error('Mis-sized inputs. X and IND are vectors, n is a scalar')
end

[IND_ordered, IND_original_ind] = sort(IND); %puts IND into ascending order if it isn't already

n = min(n, length(IND)); %if n is larger than the size of the index, this scales n back
closest_indx = zeros(length(X), n); %preallocated for speed
closest_indx_value = zeros(length(X),n); %preallocated for speed
closest_indx_diff = zeros(length(X),n); %preallocated for speed


for i1=1:length(X)
    diff_vals = X(i1) - IND;
    [sortB, sortIND] = sort(abs(diff_vals));
    closest_indx_diff(i1,:) = diff_vals(sortIND(1:n));
    closest_indx(i1, :) = sortIND(1:n);
    closest_indx_value(i1, :) = IND(sortIND(1:n));
    
end
%     search_start = 1;
%     search_end = length(IND);
%     while search_start<=search_end
%         mid = round((search_start+search_end)/2); %indexes mid-point since last search
%         diff = X(i1) - IND_ordered(mid); %difference between mid-point value of index and of search values
%             if diff == 0; %if there is an exact match
%                 closest_indx(i1) = IND_original_ind(mid); %indexes where X(i) would be in original IND vector
%                 closest_indx_value(i1) = IND_ordered(mid); %indexes the nearest index value of X(i)
%                 closest_indx_diff(i1) = 0; %indexes difference to nearest index value of X(i)
%                 search_end = mid; %to break out of while loop
%                 search_start = mid+1; %to break out of while loop
%             elseif diff>0; %x value is greater than value at current midpoint
%                 search_start = mid + 1;
%             elseif diff<0; %X value is less than value at current midpoint
%                 search_end = mid-1;
%             end
%     end
%     if search_end<search_start && diff~=0; %didnt find exact match
%         search_end = search_start;
%         search_start = search_end-1;
%         if search_end>length(IND); %if data are outside of range on upper end
%             mid = length(IND);
%         elseif search_start<1; %if data are outside of range on lower end
%             mid = 1;
%         elseif abs(X(i1) - IND_ordered(search_end)) < abs(X(i1) - IND_ordered(search_start)) %decides which end the value (X(i)) is closest to
%             mid = search_end;
%         else
%             mid = search_start; %goes with lower number if the difference is the same
%         end
% 
%         closest_indx(i1) = IND_original_ind(mid); %indexes where X(i) would be in original IND vector
%         closest_indx_value(i1) = IND_ordered(mid); %indexes the nearest index value of X(i)
%         closest_indx_diff(i1) = X(i1) - IND_ordered(mid); %indexes difference to nearest index value of X(i)
% %         if abs(closest_indx_diff(i))>max_dist; %too far away for value to be meaningful
% %             closest_indx(i) = NaN; %indexes where X(i) would be in original IND vector
% %             closest_indx_value(i) = NaN; %indexes the nearest index value of X(i)
% %             closest_indx_diff(i) = NaN;
% %         end
%     end
% end

% A = [X', closest_indx', closest_indx_value', closest_indx_diff', IND'];
out.closest_indx = closest_indx;
out.closest_indx_value = closest_indx_value;
out.closest_indx_diff = closest_indx_diff;
       