function [lattice, crystal, table] = gtReadSpaceGroup(sg)
% GTREADSPACEGROUP  Read the spacegroup and give the hermann_mauguin symbol
%     [lattice, crystal, table] = gtReadSpaceGroup(sg)
%     ------------------------------------------------
%
%     INPUT:
%       sg = spacegroup to look at <double>
%
%     OUTPUT:
%       lattice = lattice system for this spacegroup
%       crystal = crystal system for this spacegroup
%       table   = mhashtable with 'spacegroup' : hermann_mauguin symbol
%                 
%     Version 003 14-05-2012 by LNervo
%       Add lattice system calculation

fid=fopen( fullfile( getenv('USER'), 'matlabDCT', 'zUtil_Cryst','spacegroups.dat' ),'r');
C=textscan(fid,'%s %d','delimiter','\t','commentstyle','matlab');
fclose(fid);

spacegroup=uint8(C{2});
hermann=C{1};
n=size(hermann,1);

table=mhashtable;
for i=1:n
    s=hermann{i};
    s(isspace(s))=[];
    table.put(num2str(spacegroup(i)),s);
end

table.remove('0');
herm=table.get(num2str(sg));

% Spacegroups

if between(sg,1,2)
    crystal = 'triclinic'; % a ~= b ~= c;  alpha ~= beta ~= gamma ~= 90
    lattice = 'triclinic';
end

if between(sg,3,15)
    crystal = 'monoclinic'; % a ~= b ~= c;  alpha = gamma = 90 ~= beta
    lattice = 'monoclinic';
end

if between(sg,16,74)
    crystal = 'orthorhombic'; % a ~= b ~= c;  alpha = beta = gamma = 90
    lattice = 'orthorhombic';
end

if between(sg,75,142)
    crystal = 'tetragonal'; % a = b ~= c;  alpha = beta = gamma = 90
    lattice = 'tetragonal';
end

if between(sg,143,167)
    crystal = 'trigonal';
    
    if ~isempty(strfind(herm,'P')) % hexagonal lattice 
        % a1 = a2 = a3 ~= c;  alpha = beta = 90; gamma = 120
        lattice = 'hexagonal';
    elseif ~isempty(strfind(herm,'R')) % rhombohedral lattice
        % a = b = c;  alpha = beta = gamma ~= 90
        lattice = 'rhombohedral';
    end
end

if between(sg,168,194) || sg==663 % snow case
    crystal = 'hexagonal'; % a1 = a2 = a3 ~= c;  alpha = beta = 90; gamma = 120
    lattice = 'hexagonal';
end

if between(sg,195,230)
    crystal = 'cubic'; % a = b = c;  alpha = beta = gamma = 90
    lattice = 'cubic';
end

        
        
end % end of function