function dummy = addpz(Editor)
%ADDPZ  Adds pole or zero to editor.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $  $Date: 2000/06/14 20:19:39 $

% REVISIT
dummy = [];

% Pointers
HG = Editor.HG;
CompData = Editor.LoopData.Compensator;

% Gather info about added root
AddType =  Editor.ModeData;  % {Type,ID}

% Acquire new pole/zero position 
CP = get(HG.PlotAxes,'CurrentPoint');
if CP{1}(1,3)==1,
   % Dropped pole in mag axes
   CP = CP{1};
elseif CP{2}(1,3)==1,
   % Dropped pole in phase axes
   CP = CP{2};
end
X = unitconv(CP(1,1),Editor.FrequencyUnits,'rad/sec');

% Determine root value based on pole/zero type
[Zeros,Poles,Type,Status,Action] = ...
    LocalGetRootValue(X,AddType{:},CompData.Ts);

% Start transaction
T = handle.transaction(get(findpackage('sisodata'),'DefaultDatabase'));
set(T,'Name',Action,'OperationStore','on','InverseOperationStore','on');

% Add new pole/zero group to database
addpz(CompData,Type,Zeros,Poles);

% Register transaction 
register(Editor.LoopData,T);

% Notify peers of data change
dataevent(Editor.LoopData,'all');

% Confirm operation in status bar and update history
Editor.up.send('StatusChanged',...
    sisodata.dataevent(Editor.up,'StatusChanged',{Status,Status}));


%----------------- Local functions -----------------

%%%%%%%%%%%%%%%%%%%%%
% LocalGetRootValue %
%%%%%%%%%%%%%%%%%%%%%
function [Zeros,Poles,Type,Status,Action] = LocalGetRootValue(X,Type,ID,Ts)
% Infers specified root value from mouse location

% RE: * Uses only the natural frequency info (X = Wn)
%     * X is in rad/sec
if Ts
    DomainVar = 'z';
else
    DomainVar = 's';
end

switch Type
case 'Real'
    % Real pole/zero. RE: Assume stability
    R = LocalRootValue(-X,Ts);
    if strcmpi(ID,'Zero')
        Zeros = R;  Poles = zeros(0,1);
    else
        Poles = R;  Zeros = zeros(0,1);
    end
    Status = sprintf('Added real compensator %s at %s = %.3g',lower(ID),DomainVar,R);
    Action = sprintf('Add %s',ID);
    
case 'Complex'
    % Complex pole zero: assume stability + damping = 1.0
    R = LocalRootValue(-X,Ts);
    if strcmpi(ID,'Zero')
        Zeros = [R;R];  Poles = zeros(0,1);
    else
        Poles = [R;R];  Zeros = zeros(0,1);
    end
    Status = sprintf('Added complex pair of compensator %ss at %s = %.3g %s %.3gi',...
        lower(ID),DomainVar,real(R),char(177),0);
    Action = sprintf('Add %s',ID);
    
case 'Lead'
    % Lead network (s+tau1)/(s+tau2)  tau1<tau2
    Zeros = LocalRootValue(-X/1.5,Ts);
    Poles = LocalRootValue(-X,Ts);
    Type = 'LeadLag';
    Status = sprintf('Added lead network with zero at %s = %.3g and pole at %s = %.3g',...
        DomainVar,Zeros,DomainVar,Poles);
    Action = 'Add Lead';
    
case 'Lag'
    % Lag network (s+tau1)/(s+tau2)  tau1>tau2
    Zeros = LocalRootValue(-1.5*X,Ts);
    Poles = LocalRootValue(-X,Ts);
    Type = 'LeadLag';
    Status = sprintf('Added lag network with zero at %s = %.3g and pole at %s = %.3g',...
        DomainVar,Zeros,DomainVar,Poles);
    Action = 'Add Lag';

case 'Notch'
    % Notch filter: default is zeta1=0.05,zeta2=0.5 (1/2 max width and 20dB depth)
    z1 = 0.05;   z2 = 0.5;
    r1 = X * (-z1 + 1i*sqrt(1-z1^2));
    r2 = X * (-z2 + 1i*sqrt(1-z2^2));
    Zeros = LocalRootValue([r1;conj(r1)],Ts);
    Poles = LocalRootValue([r2;conj(r2)],Ts);
    Status = sprintf('Added notch filter with zeros at %s = %.3g %s %.3gi and poles at %s = %.3g %s %.3gi',...
        DomainVar,real(Zeros(1)),char(177),abs(imag(Zeros(1))),...
        DomainVar,real(Poles(1)),char(177),abs(imag(Poles(1))));
    Action = 'Add Notch';

end


%%%%%%%%%%%%%%%%%%
% LocalRootValue %
%%%%%%%%%%%%%%%%%%
function R = LocalRootValue(R,Ts)
% Convert to discrete time values if necessary
if Ts,
    R = exp(Ts*R);
end


