function M = symmetryoperation(varargin)
%SYMMETRYOPERATION delivers matrices of symmetry operation(s)
% M=SYMMETRYOPERATION(value,type,cs,info)
% 
% Coordinate Systems (s. Borchardt-Ott, S. 237)
% a - triklin (anorthic)
% m - monoklin
% o - orthorhombisch
% t - tetragonal
% h - hexagonal/trigonal
% r - rhomboedrisch (not used)
% c - kubisch
%
% M - Matrix
% tn - Trivialname
% it - International Tables
% cs - Coordinate System

% The following licence is added according to the email stored as
%   agreement.eml.

% Christian Borchert, Copyright 2009, 
%   Max Planck Institute for Dynamics of Complex Technical Systems, 
%   Magdeburg, Germany
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

if nargin<5
    if nargin==4
        value = varargin{1};
        type = varargin{2}; % Trivialname oder International Tables
        cs = varargin{3}; % Coordinate System
        info = varargin{4}; % Information to be retrieved
    elseif nargin==3
        value = varargin{1};
        type = varargin{2}; % Trivialname oder International Tables
        cs = varargin{3}; % Coordinate System
        info = 'all'; % Information to be Retrieved
    elseif nargin==2
        error('MATLAB:symmetryoperation:NotEnoughInputs',...
              'Wrong number of input arguments. See SYMMETRYOPERATION.')
    elseif nargin==1
        if strcmp(varargin{1},'all')
            type = varargin{1};
        else
            error('MATLAB:symmetryoperation:NotEnoughInputs',...
                  'Wrong number of input arguments. See SYMMETRYOPERATION.')
        end
    else
        type = 'all';
    end
else
    error('MATLAB:symmetryoperation:TooManyInputs',...
          'Too many input arguments. See SYMMETRYOPERATION.');
end

[M_all eq] = allsymmetryoperations;

if strcmp(type,'all');
    M = M_all;
else
    sep_value = strread(value,'%s');
    for j=1:length(sep_value);
        value = sep_value{j};
        M_cell = struct2cell(M_all);
        
        if strcmp(type,'TN')
            TN_IT = 1;
        else
            TN_IT = 2;
        end
        
        TN_IT_string = squeeze(M_cell(TN_IT,:,:));
        ind_match = strmatch(value,TN_IT_string,'exact');
        
        if length(ind_match)<1
            ind_equiv = strmatch(value,eq(:,1));
            if length(ind_equiv)<1
                error(['Symmetry operation ',value,' not found']);
            end
            value = eq(ind_equiv,2);
            ind_match = strmatch(value,TN_IT_string);
        end
        
        if length(ind_match)>1 % choose right crystal system
            cs_string = squeeze(M_cell(3,:,ind_match));
            [k,trash] = find(cell2mat(cs_string)==cs);
            if length(k)>1
                error(['Table of symmetry operations ambigous ',value]);
            end
            k = ind_match(k);
        else
            k = ind_match;
        end
        
        if strcmp(info,'all')
            M(j) = M_all(k);
        elseif strcmp(info,'M')
            M{j} = M_all(k).M;
        elseif strcmp(info,'M_inv')
            M{j} = M_all(k).M_inv;
        else
            error('MATLAB:symmetryoperation:UnknownArgument',...
                  'Unknown info argument. See SYMMETRYOPERATION.');
        end
            
    end
end


