% Function fsmps_param
% Calculates size dist parameters N, Dpg and sg
% 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)
% sizedist (rows: dN/dlogDp(time); columns: dN/dlogDp(Dp))
% remneg (logical for replacing negative dN/dlogDp by zero)
%
% OUTPUT: param (N(cm-3), Dpg(nm), sg, SurfaceArea(um2/cm3), Volume(um3/cm3), SMD(nm), VMD(nm))
function [param]=fsmps_param(diam,diaminf,sizedist,remneg)

if(remneg)
sizedist(sizedist<0)=0;
end
dlogDp=diff(log10([diaminf diam]));
logDp=log10(diam);
param=nan(length(sizedist),7);
for i=1:length(sizedist)
if(mod(i,100)==0)
disp(i)
end
naonan=find(~isnan(sizedist(i,:)));
if(length(naonan)==length(diam))
dN=sizedist(i,:);
else
dN=sizedist(i,naonan);
end
param(i,1)=dN*dlogDp(naonan)'; % N(cm-3)
param(i,2)=10^(sum(dN.*dlogDp(naonan).*logDp(naonan))/param(i,1)); % Dpg(nm)
param(i,3)=10^(sqrt(sum(dN.*dlogDp(naonan).*(logDp(naonan)-log10(param(i,2))).^2)/param(i,1))); % sg
param(i,4)=sum(1E-06*pi*dN.*dlogDp(naonan).*(diam(naonan).^2),2); % Surface Area (um2/cm3)
param(i,5)=sum(1E-09*(pi/6)*dN.*dlogDp(naonan).*(diam(naonan).^3),2); % Volume (um3/cm3)
param(i,6)=10^(sum(1E-06*pi*dN.*dlogDp(naonan).*(diam(naonan).^2).*logDp(naonan),2)/param(i,4)); % SFM (nm)
param(i,7)=10^(sum(1E-09*(pi/6)*dN.*dlogDp(naonan).*(diam(naonan).^3).*logDp(naonan),2)/param(i,5)); % VMD (nm)
end