function temp_for_salinity_calculation=thermallagcorrection(temp,alpha,tau,nyquist)


%%% algorithm to correct for thermal lag in conductivity.
%%% based on morison et al (1994)
%%%
%%%
%%% usage: temp_for_salinity_calculation=thermallagcorrection(temp,alpha,tau,nyquist)
%%% you must specify alpha, tau and the nyquist.
%%%
%%% the output "temp_for_salinity_calculation" can then be used with conductivity values
%%% for new salinity calculations


a=(4*nyquist*alpha*tau)/(1+(4*nyquist*tau)); %a and b are parameters for the correction
b=1-(2*a)/alpha;


%initialize so that it dosnt grow inside the loop. save a lot of time:
temp_correction=zeros(length(temp),1); 



for i=2:length(temp) %the main loop
temp_correction(i)=-b*temp_correction(i-1)+a*(temp(i)-temp(i-1));
end

%set the first equal to the second, so our series stays the same length
temp_correction(1)=temp_correction(2); 


%take the correction term from the original series:
temp_for_salinity_calculation=temp-temp_correction; 