function CTU_Codes_Loaded=loadexistingcodes()

% This function:
%               1- loads existing default code using internal function.
%               2- loads codes from the existing database stored in the
%                  file:  \CTU_Codes_Database\CTU_Codes_Loaded.mat
%               3- if the database does not exist, the function creates a
%                  new database and fills it for the first time by the default
%                  codes
%
%      Outputs: The default or the updated database code.


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

currentFolder = pwd;
mymatlab_root = matlabroot;
cd(mymatlab_root);

cd ('\');
if ~ (exist('CTU_Codes_Database','dir')==7)
    mkdir CTU_Codes_Database;
    disp('A directory (CTU_Codes_Database)is created in your local disk to save CTU codes in the file : CTU_Codes_Loaded.mat');    
end
cd CTU_Codes_Database;

if exist('CTU_Codes_Loaded.mat','file')
    load('CTU_Codes_Loaded.mat');
    
else % load initial default codes.
    CTU_Codes_Loaded=internal_ExistingCodes();
    save CTU_Codes_Loaded.mat CTU_Codes_Loaded;
end
cd (currentFolder);
end



        % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        %                 Internal functions 
        % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CTU_Codes_Loaded=internal_ExistingCodes()
% Spectral resolution (M=2)
CTU_Codes_Loaded(1).SpectralRes=2;
CTU_Codes_Loaded(1).SpatialRes=-1;
CTU_Codes_Loaded(1).HistResol=-1;
CTU_Codes_Loaded(1).Codes=[0 1];


% Spectral resolution (M=3)
CTU_Codes_Loaded(2).SpectralRes=3;
CTU_Codes_Loaded(2).SpatialRes=[ 4  6  8  12 16  20  24  28  32  36  40  44    48    52    56    60    64    68    72    76   80    84   88    92    96    100   104 ];
CTU_Codes_Loaded(2).HistResol=[15 28 45 91 153 231 325 435 561 703 861 1035  1225  1431  1653  1891  2154  2415  2701  300  3321  3655 4005  4370  4753  5151  5565];
CTU_Codes_Loaded(2).Codes=[1 2 6; 1 2 8; 1 2 10; 1 2 14; 1 2 18; 1 2 22; 1 2 26; 1 2 30; 1 2 34; 1 2 38; 1 2 42; 1 2 46; 1 2 50;...
    1 2 54; 1 2 58; 1 2 62; 1 2 66; 1 2 70; 1 2 74; 1 2 78; 1 2 82; 1 2 86; 1 2 90; 1 2 94; 1 2 98; 1 2 102; 1 2 106];


% Spectral resolution (M=5)
CTU_Codes_Loaded(3).SpectralRes=5;
CTU_Codes_Loaded(3).SpatialRes=[4 6 8 12 16 24];
CTU_Codes_Loaded(3).HistResol=[70 210 495 1820 4845 20475];
CTU_Codes_Loaded(3).Codes=[1	2	6	22      56;    1	2	8	44      155;   1	2	10	74      397;   1	2	14	158     1028;
    1	2	18	274     2330; 1	2	26	3996	4866];


% Spectral resolution (M=7)
CTU_Codes_Loaded(4).SpectralRes=7;
CTU_Codes_Loaded(4).SpatialRes=[4 6 8 12 16];
CTU_Codes_Loaded(4).HistResol=[210 924 3003 18564 74613];
CTU_Codes_Loaded(4).Codes=[1	2	6	22	56      154     369;   1	2	8	44	155     723     1917;  1	2	10	74	397     1821	7807;
    1	2	14	158	1028	24716	34848; 1	2	18	274	2330	84489	144205];


% Spectral resolution (M=9)
CTU_Codes_Loaded(5).SpectralRes=9;
CTU_Codes_Loaded(5).SpatialRes=[4 6 8];
CTU_Codes_Loaded(5).HistResol=[495 3003 12870];
CTU_Codes_Loaded(5).Codes=[1	2	6	22	56	154	  369	857     1425;  1	2	8	44	155	723	  1917	6277	15638;
    1	2	10	74	397	1821  7807	62441	88895];
end