function start_stop=get_optimum_serach_range_for_exact_base(base,NBposition)

% This function retrieves the optimum range (interval) in which the search
% for a new CTU codes for an non existing M,N texture analysis
% scenario does not exist


% Copyright 2015 Abdelmounaime Safia
% Lasts Revision January, 4, 2015


if (base>2) && (rem(base,2)==0)||(base<2)
    fprintf('\n _________________________________________________________________________________________________________________');
    fprintf('\n \n The spectral resolution ''base'' should be equal to 2 or anny odd natural digit higher than 2');
    fprintf('\n _________________________________________________________________________________________________________________\n');
    error('The spectral resolution ''base'' should be equal to 2 or anny odd natural digit higher than 2');
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Initialisation
CTU_Codes_Loaded=loadexistingcodes();
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check if an exact code exists
Curent_CTU_code=get_exact_code(base,NBposition);
if isstruct(Curent_CTU_code)
    fprintf('\n__________________________________________________________');
    fprintf('\n\n Exact CTU codes already exists in your database\n');
    disp(Curent_CTU_code);
    fprintf('__________________________________________________________\n');
    start_stop=Curent_CTU_code;
else % An intial code should be found
    
    
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    % Search in the code database for codes corresponding to the same spectral
    % resolution than the requested one.
    [~, nb_spec_res]=size(CTU_Codes_Loaded);
    
    j=0;
    i=1;
    while i<=nb_spec_res
        if CTU_Codes_Loaded(i).SpectralRes==base
            j=i;
        end
        i=i+1;
    end
    if j~=0
        [~, temp_pos]=find(CTU_Codes_Loaded(j).SpatialRes<NBposition);
        if ~isempty (temp_pos)
            start_stop (1,1)=CTU_Codes_Loaded(j).Codes(temp_pos(end),base-1);
        else
            start_stop (1,1)=NaN;
        end
        [~, temp_pos]=find(CTU_Codes_Loaded(j).SpatialRes>NBposition);
        if ~isempty (temp_pos)
            start_stop (1,2)=CTU_Codes_Loaded(j).Codes(temp_pos(1),base);
        else
            start_stop (1,2)=NaN;
        end
    else
        start_stop (1,1)=NaN;
        start_stop (1,2)=NaN;
    end
end


