Aanderaa Currents MATLAB Implementation

function [u, v, cspd, cdir] = compute_aa_currents ( cspd_counts, cspd_cals, cdir_counts, cdir_cals, magnetic_variation  )
% COMPUTE_AA_CURRENTS:  calculate current parameters from raw aanderaa engineering units
%
% USAGE:  [u, v, cspd, cdir] = compute_aa_currents ( cspd_counts, cspd_cals, cdir_counts, cdir_cals, mag_var );
%
% PARAMETERS:
% Input:
%    cspd_counts:
%        current speed raw engineering units as reported by Aanderaa Current Meter
%    cspd_cals:
%        current speed calibration coefficients.  See the Aanderaa manual.
%    cdir_counts:
%        current direction raw engineering units as reported by Aanderaa Current Meter
%    cdir_cals:
%        current direction calibration coefficients.  See the Aanderaa manual.
%    magnetic_variation:
%        Corrects direction for magnetic north.
% Output:
%    u, v:
%        East and north velocity components in cm/s
%    cspd, cdir:
%        speed and direction in cm/s, angular degrees (true north)


cspd = cspd_cals(1) ...
     + cspd_cals(2)*cspd_counts ...
     + cspd_cals(3)*cspd_counts.^2 ...
	 + cspd_cals(4)*cspd_counts.^3;

cdir = cdir_cals(1) ...
     + cdir_cals(2)*cdir_counts ...
     + cdir_cals(3)*cdir_counts.^2 ...
	 + cdir_cals(4)*cdir_counts.^3;

	
cdir=mod((cdir + magnetic_variation), 360);
        

%
% Calculate speed east and north from the current speed and direction
u = cspd .* cos ( (90 - cdir)*pi/180 );
v = cspd .* sin ( (90 - cdir)*pi/180 );
		

return;