function [m,netdata] = CheckNetworkData(netdata,flag,desc)
%CHECKNETWORKDATA Check the network data and return M, the number of
% network parameter data points.
%
% [M,NETDATA] = CHECKNETWORKDATA(NETDATA,FLAG,DESC) checks that NETDATA
% contains valid network parameter data. The type of check is determined by
% FLAG. The function returns NETDATA, the original data, and M, the number
% of network parameter data points. If the input NETDATA is a vector, the
% function reshapes it.
%
% FLAG can be an integer or a string. If FLAG is an integer, N, the
% function checks if NETDATA is an N-by-N-by-M array. If FLAG is a string,
% the function checks if NETDATA is N-by-N-by-M, 2N-by-2N-by-M, or
% 4N-by-4N-by-M, where N is not specified explicitly. The string must be
% 'N', '2N', or '4N'.
%
% The DESC input argument is used for error message description.
%
%   % EXAMPLE: Create an amplifier object and check its S-parameters and
%   %          Z-parameters
%       amp = rfckt.amplifier;
%       sparams = extract(amp,'S_Parameters'); 
%       zparams = extract(amp,'Z_Parameters'); 
%       [m_s,sparams] = CheckNetworkData(sparams,2,'S_Params');   
%       [m_z,zparams] = CheckNetworkData(zparams,'2N','Z_Params'); 
% 
%   See also CHECKZ

%   Copyright 2007-2021 The MathWorks, Inc.

narginchk(1,3);

if nargin < 2;  flag = 'N';  end
if nargin < 3;  desc = 'NetworkData';  end

throw_error = 0;
% Must be numeric
if ~isnumeric(netdata) || isempty(netdata)
    error(message('rflib:CheckNetworkData:NotNumeric', desc))
end

% NaN is not allowed
if anynan(netdata)
    error(message('rflib:CheckNetworkData:IsNaN', desc))
end

if isvector(netdata)
    netdata = reshape(netdata, [1 1 length(netdata)]);
end
[n1,n2,m,d4] = size(netdata);

% There will be two switch cases. First will check whether class of input 
% flag is char or double. If the class is char, the second switch case will 
% check whether input flag is the specified dimension.

switch class(flag)
    case 'char'     % First class check
        switch flag
            % if NxN
            case 'N'
                if (n1 ~= n2) || (d4 ~= 1)
                    throw_error = 1;
                end 
            % if 2Nx2N
            case '2N'
                if (mod(n1,2)) || (n1 ~= n2) || (d4 ~= 1)
                    throw_error = 1;
                end
            % if 4Nx4N
            case '4N'
                if (mod(n1,4)) || (n1 ~= n2) || (d4 ~= 1)
                    throw_error = 1;
                end
            otherwise
                error(message('rflib:CheckNetworkData:InvalidChar'));
        end
    case 'double'	% Second class check
        if (n1 ~= flag) || (n1 ~= n2) || (d4 ~= 1)
            throw_error = 1;
        end        
    otherwise       % Should not go into this.
        error(message('rflib:CheckNetworkData:InvalidClass'));
end

% If throw_error is set to 1, error message will be triggered.
if throw_error
    if isnumeric(flag)
        flag = num2str(flag);
    end
    error(message('rflib:CheckNetworkData:NotDimByDim', desc, flag, flag));
end
