function Curent_CTU_code = main_ctu_code_load_search (M,N,Search_Step)

% This is the main function that loads (or finds) Compact Texture Unit 
% codes for texture codification based on the concept of Compact Texture
% Unit proposed by Abdelmounaime Safia and Dong-Chen He in :
%
% Reference:
% ----------
% [1]	A. Safia and D. He, “Improving the Texture Spectrum Model Using a Compact Texture  Unit Descriptor,” J. Commun. Comput., vol. 10, no. 2, pp. 1–18, 2013.
% [2]	A. Safia and D.-C. He, “Multiband compact texture unit descriptor for intra-band and inter-band texture analysis,” ISPRS J. Photogramm. Remote Sens., 2014. 
%
% 
% 
% Inputs: 
%           M                  : Spectral resolution
%           N                  : Spatial resolution
%           Search_Step        : Search interval (optional)
%
% Output:
%          Curent_CTU_code    : Structure containing CTU codes. If no code
%          was found, the function returns Curent_CTU_code = NaN.
%
%__________________________________________________________________________
%
%
%          --------------------------------------------------------
%         |    List of functions used by the search algorithm      |
%          --------------------------------------------------------
%
% CodeValidity = find_new_CTU_code(Initial_CTU_Code,target_base,Search_Step)
% CTU_Codes_Loaded=loadexistingcodes()
% Curent_CTU_code=get_exact_code(base,NBposition)
% initial_TU_Codes=get_valid_code_for_maxi_base(base,NBposition)
% start_stop=get_optimum_serach_range_for_exact_base(base,NBposition)
% update_ctu_code_database(NewCode)
% HistResol=tu_and_ctu_histogram_resolution(base,NBposition,flag)
% database_ctu_code_integrity()
% exit_message_CTU_serach(flag)
%
%
%
% Copyright 2015 Abdelmounaime Safia
% Last Revision January , 4, 2015



%% ------------------------------------------------------------------------
%%
% Inputs integrity check !
if nargin == 3
    if (N < 0) || (mod(M,1) ~= 0)
        fprintf('\n _________________________________________________________________________________________________________________\n');
        error('Input arguments error ! N should be natural number');
    end
    
    if (Search_Step <= 0) || (mod(Search_Step,1) ~= 0)
        fprintf('\n _________________________________________________________________________________________________________________\n');
        error('Input arguments error ! Search_Step should be natural number higher than zero');
    end
elseif nargin == 2
    Search_Step=2000;
else
    fprintf('\n _________________________________________________________________________________________________________________\n');
    error('Input arguments error');
end
if (M>2) && (rem(M,2)==0)||(M<2) || (mod(M,1) ~= 0)
    fprintf('\n _________________________________________________________________________________________________________________\n');
    error (' The spectral resolution ''base'' should be equal to 2 or anny odd natural digit higher than 2');
end


%% ------------------------------------------------------------------------
%%

% Check if an exact code exists
Curent_CTU_code=get_exact_code(M,N);
if ~ isstruct(Curent_CTU_code) % An intial code should be found

% Find new CTU code
Initial_CTU_Code = get_valid_code_for_maxi_base(M,N);
Curent_CTU_code = find_new_CTU_code (Initial_CTU_Code,M,Search_Step);
end