% Dopplercorr coorects data for Doppler shift
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% DESCRIPTION: dopplercorr coorects time series data for Doppler shift.
%              When the optical path difference (OPD) in the LTP interferometer
%              is scanned fast, a doppler shift adds up to the data.
%              The Doppler shift depends on the velocity with wich the OPD is scanned.
%              This function calculates the Doppler shift% and
%              subtracts it from the data to correct the 'Doppler error'.
%              In the variable 'delta' the Doppler shift is stored and from
%              their the 'Doppler error' stored in 'g' is calculated and finally
%              subtracted from the input data. 
% CALL:        b = dopplercorr(a,pl)
%
% INPUTS:      a  - analysis object(s) (time series)
%              pl - parameter list(s) - bin
%
% OUTPUTS:     b  - analysis object  (time series)
%              
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'dopplercorr')">Parameters Description</a>
%
% VERSION:     $Id: dopplercorr.m,v 1.19 2011/04/08 08:56:16 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = dopplercorr(varargin)

  %%% Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  %%% Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  %%% Collect all AOs
  [as, ao_invars,  rest] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  [pli, pl_invars, rest] = utils.helper.collect_objects(rest, 'plist', in_names);

  %%% Decide on a deep copy or a modify
  %%% REMARK: If you create a new AO (call the constructor) then
  %%%         it is not necessay to copy the input-AOs !!!!!!!!!!!!!!!!!!!!!!!!!
  bs = copy(as, nargout);
  
  %%% Combine plists
  pl = parse(pli, getDefaultPlist);
  k = find(pl,'bin');  
  if isempty(k)
    error('### Please specify the bin parameter ''bin''');
  end
  %%% go through analysis objects
  for kk = 1:numel(bs)
    %%%%%%%%%%   some calculations   %%%%%%%%%%
    ydata = bs(kk).data.getY;
    leng = length(ydata);
    delta = zeros(leng,1);
    delta(1) = (ydata(2)-ydata(1))/(2*pi);
    delta(leng) = (ydata(leng)-ydata(leng-1))/(2*pi);
        
    for i=2:(leng-1)
      delta(i) = (ydata(i+1)-ydata(i-1))/(4*pi);
    end
    
    g = -1./(2*k).*delta.*sin(2.*ydata)...
      + 1./(4*k*k).*delta.^2.*sin(2.*ydata) ... 
      + 1./(8*k*k).*delta.^2.*sin(4.*ydata);
    ydata_new = ydata + g;
    
    %%create new ao
    bs(kk).data.setY(ydata_new);
    
    %% Set Name
    bs(kk).name = 'new name';

    %%% Add History
    bs(kk).addHistory(getInfo('None'), pl, ao_invars(kk), bs(kk).hist);

  end

  % Clear the errors since they don't make sense anymore
  clearErrors(bs);  
  
  % Set output
  if nargout == numel(bs)
    % List of outputs
    for ii = 1:numel(bs)
      varargout{ii} = bs(ii);
    end
  else
    % Single output
    varargout{1} = bs;
  end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                               Local Functions                               %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getInfo
%
% DESCRIPTION: Get Info Object
%
% HISTORY:     11-07-07 M Hewitson
%                Creation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: dopplercorr.m,v 1.19 2011/04/08 08:56:16 hewitson Exp $', sets, pl);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    getDefaultPlist
%
% DESCRIPTION: Get Default Plist
%
% HISTORY:     11-07-07 M Hewitson
%                Creation.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
pl = plist();
  p = param('bin', 50, 'grid size for the doppler correction');
  pl.append(p);
end


