%**************************************************************************
%This function calculated the average undulator period.
%
%Input:
%z_zero, Z positions of the field zeros (m)
%
%Output:
%lambda_u, average undulator period (m)
%
%Zachary Wolf
%10/23/06

function [lambda_u] = field_calc_lambda_u(z_zero)

%Check
if length(z_zero) < 5
    lambda_u = 0;
    return;
end

%Find the distance between the field zeros
delta_z_zero = diff(z_zero);

%Calculate the average distance between the field zeros
delta_z_zero_ave = mean(delta_z_zero);

%Calculate the rms distance between the field zeros
delta_z_zero_rms = std(delta_z_zero);

%Calculate the undulator period
% if delta_z_zero_rms / delta_z_zero_ave > 0.1
%     lambda_u = 0;
% else
%     lambda_u = 2 * delta_z_zero_ave;
% end
lambda_u = 2 * delta_z_zero_ave;

%Done

