function [prxs_n,pplat,pplon,parchv_tp] = rmv_repeats(prxs,plat,plon,...
    archv_tp,rtol)
%RMV_REPEATS  Remove repeated proxies (very highly correlated ones)

%------------------------------------------------
% AVERAGE REPEATED (OR NEARLY IDENTICAL) PROXIES
%------------------------------------------------
disp('Removing repeated proxies...')

% COMPUTE THE CORRELATION MATRIX and MSE
P=zeros(size(prxs,2));M=zeros(size(prxs,2));N=zeros(size(prxs,2));
for i=1:size(prxs,2)
    parfor j=1:size(prxs,2)
        P(i,j)=nancorr(prxs(:,i),prxs(:,j));
        M(i,j)=nanmse(prxs(:,i),prxs(:,j));
        N(i,j)=isequal(isnan(prxs(:,i)),isnan(prxs(:,j)));
    end
end



% Average identical proxies (highly positively correlated)
pp=P;pp(isnan(pp))=0;
mm=M;nn=N;
prxs_n=[];
prxsj=prxs;
pplat=[];
pplon=[];
parchv_tp=cell(1);

msetol=0.05; % MSE tolerance

rids=cell(1);

jj=1;
while size(pp,2)>0
    
    % Find high correlations, low MSE, same length and missing values
    dups=find(pp(:,1)>rtol & mm(:,1)<msetol & nn(:,1)==1);
    
    rids{jj}=dups+jj-1;
    
    % Select and average duplicates based on the correlation matrix
    prxs_n=cat(2,prxs_n,nanmean(prxsj(:,dups),2));
    
    % Set proxy values to first value (may not have identical lat/lon, but
    % how to decide which to choose?)
    pt=plat(dups);
    pplat(jj)=pt(1);
    pt=plon(dups);
    pplon(jj)=pt(1);
    
    pa=archv_tp(dups);
    parchv_tp(jj)=pa(1);
    
    % remove averaged proxies, including first column
    pp(:,dups)=[];pp(dups,:)=[];
    mm(:,dups)=[];mm(dups,:)=[];
    nn(:,dups)=[];nn(dups,:)=[];
    prxsj(:,dups)=[];
    plat(dups)=[];
    plon(dups)=[];
    archv_tp(dups)=[];
    
    
    jj=jj+1;
    
end

%pNum=length(parchv_tp);

%save('proxy_correlation_matrix.mat','P','M','N','rids')

end

