% Function fsmps_interp
% Interpolate size dist to a new diameter sequence.
% This script does not extrapolate!
% Size distributions can have NaNs. The function uses the non-NaN values for the calculations.
%
% INPUT: diam (diameter)
% diaminf (lower limit of the first diam channel, usually 10nm)
% sizedistr (rows: dN/dlogDp(time); columns: dN/dlogDp(Dp))
% remneg (logical for replacing negative dN/dlogDp by zero)
%
function [newsmps]=fsmps_interp(newdiam,diam,sizedistr,timeline)

newsmps=nan(size(sizedistr,1),size(newdiam,2));
for i=1:size(sizedistr,1)
if(mod(i,100)==0)
disp(i)
end
%figure,
%semilogx(diam,sizedistr(i,:),'-ro'),title(num2str(i)),axis tight
%set(gca,'XTick',[20 50 100 200]); hold on
% interpolate
sd=sizedistr(i,~isnan(sizedistr(i,:))); % desconsider NaNs in the size dist
if(diam(length(sd))>=0.9*newdiam(length(newdiam)))
newsmps(i,:)=interp1(diam(1:length(sd)),sd,newdiam,'pchip','extrap');
else
aux=find(newdiam<=1.1*diam(length(sd)));
newsmps(i,1:length(aux))=interp1(diam(1:length(sd)),sd,newdiam(aux),'pchip','extrap');
end
%semilogx(newdiam,newsmps(i,:),'-k*','LineWidth',2),axis tight
%set(gca,'XTick',[20 50 100 200]), hold off
%pause(0.7)
clear aux sd
end
newsmps(newsmps<-10)=NaN;
newsmps=cat(2,timeline,newsmps);