function [lib] = loadLibFun(libFileName)

%loadLib

%Read the lib file
FID                             = fopen(libFileName);
if FID == -1, error('Cannot open file'), end;
libFile                         = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');
fclose(FID);

all_indices_RESIDUE             = strfind(libFile{1,1}, 'RESIDUE');
indices_RESIDUE                 = find(~cellfun('isempty', all_indices_RESIDUE));

k = 1;
for j = indices_RESIDUE'
    ATOMS                       = [];
    HEADER                      = [];
    
    HEADER = textscan(libFile{1,1}{j}, '%s%s%d');
    
    lib(k).HEADER               = [HEADER{:}];
    lib(k).RESIDUE              = HEADER{2}{1};
    lib(k).NUMATOMS             = HEADER{3};
    
    for a = 1:lib(k).NUMATOMS
        ATOMS                   = textscan(libFile{1,1}{j+a}, '%d%s%s');
        lib(k).ATOMS(a)         = ATOMS{2};
        lib(k).ATOMTYPES(a)     = ATOMS{3};
        
    end
    
    k = k + 1;
    
end

all_indices_RESIDUE             = [];
indices_RESIDUE                 = [];

FID                             = fopen('numcode.lib');
if FID == -1, error('Cannot open file'), end;
libFileNUM                      = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');
fclose(FID);

all_indices_RESIDUE             = strfind(libFileNUM{1,1}, 'RESIDUE');
indices_RESIDUE                 = find(~cellfun('isempty', all_indices_RESIDUE));

for j = indices_RESIDUE'
    ATOMS                       = [];
    HEADER                      = [];
    
    HEADER = textscan(libFileNUM{1,1}{j}, '%s%s%d');
    
    for i = 1:length(lib)
     
        if strcmp(lib(i).RESIDUE , HEADER{2}{1}) && isequal(lib(i).NUMATOMS, HEADER{3})
               
            for a = 1:lib(i).NUMATOMS
                ATOMS                   = textscan(libFileNUM{1,1}{j+a}, '%d%d%s%s');
                lib(i).ATOMNUMCODES(a)  = ATOMS{2};
                lib(i).ATOMTMARKLEY(a)  = ATOMS{3};
        
            end 
        end
    end
    
end

clearvars libFile libFileNUM