function varargout = sensorArrayAnalyzer(varargin)
%sensorArrayAnalyzer Analyze beam pattern of 2D and 3D arrays with
%different antennas, microphone and SONAR elements
%
%   sensorArrayAnalyzer is an interactive app for the analysis and
%   visualization of phased arrays. It allows the user to select an array
%   from a list, change its parameters and interact with several updated
%   plots, such as array geometry, array response 2D and 3D patterns. The
%   app also calculates several array characteristics like array gain,
%   array span, the total number of elements, HPBW, FNBW and SLL in azimuth
%   and elevation plane.  The user can generate MATLAB code, take the array
%   designed to MATLAB workspace or to any folder of choice. The user can
%   save the designed array and import any valid array, element object or
%   app session.

%   Copyright 2012-2019 The MathWorks, Inc.

isMOTW = phased.internal.PSTCustomSettings.isMOTW;
if ~isMOTW
    narginchk(0,1);

    if nargin
        % Command Line API with arguments
        % Valid arugments  - app session .MAT file and element or array
        % objects
        try
            if ischar(varargin{1}) || isstring(varargin{1})
                % If varargin{1} is a file name
                % To catch any error while loading
                tempData = load(varargin{1});
                
                % Error if the .mat file is not a valid sensor array app
                % session
                if isfield(tempData,'arrayAppSession')
                    data = tempData.arrayAppSession;
                else
                    error(message('phased:apps:arrayapp:invalidsession'))
                end
            else
                % If the data is an object
                data = varargin{1};
            end
            
            % Validate the loaded data
            if ~phased.apps.internal.SensorArrayApp.isValidSensorArray(data)
                error(message('phased:apps:arrayapp:invalidimport'))
            end
            
            % Do cross checks
            phased.apps.internal.SensorArrayApp.crossValidation(data);
        catch me
            % Catch any error reported
            error(me.identifier,me.message)
        end
    end
    
    out = phased.apps.internal.SensorArrayApp(varargin{:});
    
    if nargout
        varargout{1} = out;
    end
else
    % Old App does not allow output
    nargoutchk(0,0);
    phased.apps.internal.SensorArrayViewer.SensorArrayViewer;
end

