function update_ctu_code_database(NewCode)

% This function add a new record (CTU codes) to the database.
%
% Input:
%          NewCode: Structure containing New code
%
% Copyright 2015 Abdelmounaime Safia
% Last Revision January, 4, 2015

CTU_Codes_Loaded=loadexistingcodes();

temp_CTU_Codes_Loaded=CTU_Codes_Loaded;
% Check if the found code exists
base=NewCode.SpectralRes;
NBposition=NewCode.SpatialRes;
Tempcode=get_exact_code(base,NBposition);
if ~isstruct(Tempcode) % the newe code should be added
    [~, nb_spec_res]=size(CTU_Codes_Loaded);
    SpecRes_pos=-1;
    for i=1:nb_spec_res
        if CTU_Codes_Loaded(i).SpectralRes==NewCode.SpectralRes
            SpecRes_pos=i;
            break;
        end
    end
    
    if SpecRes_pos>0 % the spectral resolution of the case to be added exists
        % SpatRes_pos=-1;
        [~, SpatRes_pos]=find(CTU_Codes_Loaded(SpecRes_pos).SpatialRes<NewCode.SpatialRes);
        if isempty(SpatRes_pos) % update will be at the begening of the vector
            temp_beginning_SpatialRes=[];
            temp_beginning_HistResol=[];
            temp_beginning_Codes=[];
            %---------------------
            temp_end_SpatialRes=temp_CTU_Codes_Loaded(SpecRes_pos).SpatialRes;
            temp_end_HistResol=temp_CTU_Codes_Loaded(SpecRes_pos).HistResol;
            temp_end_Codes=temp_CTU_Codes_Loaded(SpecRes_pos).Codes;
            
            
            % Update will be at the end of the vector
        elseif length(SpatRes_pos)==length(CTU_Codes_Loaded(SpecRes_pos).SpatialRes);
            temp_beginning_SpatialRes=temp_CTU_Codes_Loaded(SpecRes_pos).SpatialRes;
            temp_beginning_HistResol=temp_CTU_Codes_Loaded(SpecRes_pos).HistResol;
            temp_beginning_Codes=temp_CTU_Codes_Loaded(SpecRes_pos).Codes;
            %---------------------
            temp_end_SpatialRes=[];
            temp_end_HistResol=[];
            temp_end_Codes=[];
            
            % update will be within elements of the vector
        elseif length(SpatRes_pos)<length(CTU_Codes_Loaded(SpecRes_pos).SpatialRes)
            temp_beginning_SpatialRes=temp_CTU_Codes_Loaded(SpecRes_pos).SpatialRes(1:SpecRes_pos);
            temp_beginning_HistResol=temp_CTU_Codes_Loaded(SpecRes_pos).HistResol(1:SpecRes_pos);
            temp_beginning_Codes=temp_CTU_Codes_Loaded(SpecRes_pos).Codes(1:SpecRes_pos,:);
            %---------------------
            temp_end_SpatialRes=temp_CTU_Codes_Loaded(SpecRes_pos).SpatialRes(SpecRes_pos+1:end);
            temp_end_HistResol=temp_CTU_Codes_Loaded(SpecRes_pos).HistResol(SpecRes_pos+1:end);
            temp_end_Codes=temp_CTU_Codes_Loaded(SpecRes_pos).Codes(SpecRes_pos+1:end,:);
            
        end
        % Adding new record
        CTU_Codes_Loaded(SpecRes_pos).SpatialRes=horzcat(temp_beginning_SpatialRes,NewCode.SpatialRes,temp_end_SpatialRes);
        CTU_Codes_Loaded(SpecRes_pos).HistResol=horzcat(temp_beginning_HistResol,NewCode.HistResol,temp_end_HistResol);
        CTU_Codes_Loaded(SpecRes_pos).Codes=vertcat(temp_beginning_Codes,NewCode.code,temp_end_Codes);
        
    else % a new case with a new spectral resolution should be added.
        NB_SpectralRes=length(CTU_Codes_Loaded);
        CTU_Codes_Loaded(NB_SpectralRes+1).SpectralRes=NewCode.SpectralRes;
        CTU_Codes_Loaded(NB_SpectralRes+1).SpatialRes=NewCode.SpatialRes;
        CTU_Codes_Loaded(NB_SpectralRes+1).HistResol=NewCode.HistResol;
        CTU_Codes_Loaded(NB_SpectralRes+1).Codes=NewCode.code;
    end
    
    currentFolder=pwd;
    mymatlab_root=matlabroot;
    cd(mymatlab_root);
    cd ('\');
    cd ('CTU_Codes_Database');
    save CTU_Codes_Loaded.mat CTU_Codes_Loaded;
    cd (currentFolder);
    fprintf('__________________________________________________________');
    fprintf('\n New codes for the case of M=%d and N=%d have been found and added to the local database  \n',NewCode.SpectralRes,NewCode.SpatialRes);
    disp(NewCode)
    fprintf('__________________________________________________________\n');
end
end
