function [iError,arAxes] = wecBode(zModel,stMode,varargin)
% wecBode
%   iError = wecBode(zModel,stMode,varargin)
%   This function plots the Bode plot of the model given as input. The
%   model should be made using the model geenration files supplied as part
%   of IMPACT. The two modes are:
%   'mp' -> it plots the numerical values of magnitude and phase
%   'tf' -> it plots the fitted transfer function
%
%
%   Project: IMPACT
%   Title of Code: wecBode
%   Relevant additional documentation: 6.18.09091.SCL.R.001 Deliverable 4
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with SgurrControl
%   and acquiring permission. SgurrControl is not liable for any
%   damage caused as a result of unauthorised changes to this
%   document.

% Initialise
iError = 0;
arAxes = [];

% Perform different functions based on mode selected - find the relevant
% phase and magnitude and plot a Bode plot based on them.  If an incorrect
% mode is supplied then give an error

if strcmpi(stMode,'mp')
    arMagnitude = zModel.arMagnitude;
    arPhase = zModel.arPhase;
    arFrequency = zModel.arFreq;
    if ~all(size(arFrequency)==size(arMagnitude))
        arFrequency = arFrequency';
    end
    
else
    if strcmpi(stMode,'tf')
        arFrequencyRange = zModel.arFreq;
        [arMagnitude, arPhase, arFrequency] =...
            getMagPhase(zModel.tfModel,arFrequencyRange);
        arMagnitude = arMagnitude';
        if arPhase(1)>180
            while arPhase(1)>180
                arPhase = arPhase-360;
            end
        else
            if arPhase(1)<-180
                while arPhase(1)<-180
                    arPhase = arPhase+360;
                end
            end
        end
    else
        rError = 1;
        error('Wrong option')
    end
end

% If the third input is used then plot the relevant displacement, velocity
% or acceleration plot.  If the third input is nonsense then set an error.
% If there is no third input default to position.
if nargin>2
    if strcmp(varargin{1},'disp')
        plotMagNPhase(20*log10(arMagnitude),arPhase,arFrequency);
    else
        if strcmp(varargin{1},'vel')
            plotMagNPhase(20*log10(arMagnitude.*arFrequency),arPhase+90,arFrequency);
        else if strcmp(varargin{1},'acc')
                plotMagNPhase(20*log10(arMagnitude.*arFrequency.^2),arPhase+180,arFrequency);
            else
                rError = 2;
                error('Wrong option')
            end
        end
    end
else
    arAxes = plotMagNPhase(20*log10(arMagnitude),arPhase,arFrequency);
end
