function Ret = pare_varpro_hook (what, vn, OPTIONS, INC);
%PARE_VARPRO_HOOK       Hook for varpro ellipse fit  
%
% Ret = pare_varpro_hook (what, vn, OPTIONS, INC);
% Hook for the 'varpro' routine, returns data depending on 'what'.
% given data is Y = [x-coord(1:m,1); y-coord(1:m,1)]
% estimation function is 
% a*Phi(1) + b*Phi(2) + z(1)*Phi(3) + z(2)*Phi(4)
%
% what: kind of data needed (see 'varpro')
% vn: non-linear parameters
% OPTIONS: same as options passed to 'varpro' routine
% INC: incidence matrix, for what == 'DPhi_Inc' only
%
% Ret: see 'varpro'

  m = size(vn,1) - 1;
  if (strcmp(what,'Phi') | strcmp(what,'DPhi') | ...
      strcmp(what,'DPhi_Inc')),
    C = cos (vn(1:m));
    S = sin (vn(1:m));
    c = cos (vn(m+1));
    s = sin (vn(m+1));
  end  
    
  if    (strcmp(what, 'Phi')),
    Ret = [ c*C, -s*S,  ones(m,1), zeros(m,1);
            s*C,  c*S, zeros(m,1),  ones(m,1)];
  elseif (strcmp(what, 'DPhi')),
    Ret = [];
    for i = 1:m,
      J = zeros(2*m,4);
      J(i,1)   = -c*S(i);
      J(i+m,1) = -s*S(i);
      J(i,2)   = -s*C(i);
      J(i+m,2) =  c*C(i);
      Ret = [Ret, J];
    end
    Ret = [Ret, [-s*C, -c*S, zeros(m,2); c*C, -s*S, zeros(m,2)]];
  elseif (strcmp(what, 'DPhi_Inc')),
    %% INC(i,j) if column phi(i)/dalpha(j) should be added.
    p   = ones(1,4)*INC*ones(m+1,1);
    Ret = zeros(2*m,p);
    p   = 0;
    for i = 1:m,
      if (INC(1,i)), p = p+1; Ret(i,p) = -c*S(i); 
                              Ret(i+m,p) = -s*S(i); 
      end;
      if (INC(2,i)), p = p+1; Ret(i,p) = -s*C(i); 
                              Ret(i+m,p) =  c*C(i); 
      end;
      if (INC(3,i)), p = p+1; end;
      if (INC(4,i)), p = p+1; end;
    end
    i = m+1;
    if (INC(1,i)), p = p+1; Ret(:,p) = [-s*C;  c*C]; end;
    if (INC(2,i)), p = p+1; Ret(:,p) = [-c*S; -s*S]; end;
    if (INC(3,i)), p = p+1; end;
    if (INC(4,i)), p = p+1; end;
  elseif (strcmp(what, 'Inc')),
    Ret = [ones(2,m+1); zeros(2,m+1)];
  else
    str = sprintf ('unknowm: command %s.', what);
    error (str);
  end

end % pare_varpro_hook
