function RunExperiment(Name,flag,varargin)
clc
close all
home=cd;
EXP=struct;
disp(gcp)
addAttachedFiles(gcp, {'RunExperiment.m','Run_jktebopID.m','Run_jktebopID_RR.m','Run_jktebopID_T9.m','Run_jktebopID_RR_T9.m','Homomorfic.m'});
disp(gcp)

switch flag
    case 'N'
        narginchk(2,2);
        EXP=ADD(EXP,{[Name '.mat']},{'N'});
    case 'N9'
        narginchk(2,2);
        EXP=ADD(EXP,{[Name '.mat']},{'N9'});
    case 'H'
        narginchk(4,4);
        EXP=ADD(EXP,{[Name '.mat']},{'H'},varargin{1},varargin{2});  
    case 'H9'
        narginchk(4,4);
        EXP=ADD(EXP,{[Name '.mat']},{'H9'},varargin{1},varargin{2});
    case 'HRR'
        narginchk(4,4);
        EXP=ADD(EXP,{[Name '.mat']},{'N'});
        PRIORITIZE(1)=length(EXP);
        EXP=ADD(EXP,{[Name '.mat']},{'HRR'},varargin{1},varargin{2});
    case 'HRR9'
        narginchk(4,4);
        EXP=ADD(EXP,{[Name '.mat']},{'N'});
        PRIORITIZE(1)=length(EXP);
        EXP=ADD(EXP,{[Name '.mat']},{'HRR9'},varargin{1},varargin{2});          
        
end







%save([home '/EXP_list.mat'],'EXP')






% EXECUTE THE EXPERIMENTS OF LISTS AT THE DIFFERENT LEVEL OF PRIORITY
if exist('PRIORITIZE','var')
    save([home '/EXP_list.mat'],'PRIORITIZE','-append')
    for j=1:length(PRIORITIZE)
        parfor i=1:PRIORITIZE(j)
        %for i=1:PRIORITIZE(j)
            experimento(EXP(i),i,home)
        end
        figure
        title('EXPERIMENTS JKTEBOT')
        text(.1,.9,['Priority ' num2str(j) ' Experiments Completed'])
        text(.1,.8,['Number of Experiments : ' num2str(PRIORITIZE(j))])
        pause(1);
        
    end
end
% EXECUTE PRIORITY EXPERIMENTS
parfor i=1:length(EXP)
    experimento(EXP(i),i,home)
end
figure
title('EXPERIMENTS JKTEBOT')
text(.1,.9,'Finished non-priority experiments')
text(.1,.8,['Number of Experiments : ' num2str(length(EXP))])

end
%% FUNCTIONS
function EXP=ADD(EXP,files,filter,varargin)
for i1=files
    for i2=filter
        switch i2{1}
            case 'R'
                N=length(EXP)+1;
                EXP(N).fileIN=i1{1};
                EXP(N).fileOUT='Ref.mat';
                EXP(N).filter=i2{1};
            case 'R9'
                N=length(EXP)+1;
                EXP(N).fileIN=i1{1};
                EXP(N).fileOUT='Ref_T9.mat';
                EXP(N).filter=i2{1};
            case 'N'
                N=length(EXP)+1;
                EXP(N).fileIN=i1{1};
                EXP(N).fileOUT='Noise.mat';
                EXP(N).filter=i2{1};
            case 'N9'
                N=length(EXP)+1;
                EXP(N).fileIN=i1{1};
                EXP(N).fileOUT='Noise_T9.mat';
                EXP(N).filter=i2{1};
            case 'H'
                for i3=varargin{1}
                    for i4=varargin{2}
                        N=length(EXP)+1;
                        EXP(N).fileIN=i1{1};
                        EXP(N).fileOUT=['H_' num2str(i3) '_' num2str(i4) 'db.mat'];
                        EXP(N).filter=i2{1};
                        EXP(N).Par=[i3,i4];
                    end
                end
            case 'H9'
                for i3=varargin{1}
                    for i4=varargin{2}
                        N=length(EXP)+1;
                        EXP(N).fileIN=i1{1};
                        EXP(N).fileOUT=['H_T9_' num2str(i3) '_' num2str(i4) 'db.mat'];
                        EXP(N).filter=i2{1};
                        EXP(N).Par=[i3,i4];
                    end
                end
            case 'HRR'
                for i3=varargin{1}
                    for i4=varargin{2}
                        N=length(EXP)+1;
                        EXP(N).fileIN=i1{1};
                        EXP(N).fileOUT=['HRR_' num2str(i3) '_' num2str(i4) 'db.mat'];
                        EXP(N).filter=i2{1};
                        EXP(N).Par=[i3,i4];
                    end
                end
            case 'HRR9'
                for i3=varargin{1}
                    for i4=varargin{2}
                        N=length(EXP)+1;
                        EXP(N).fileIN=i1{1};
                        EXP(N).fileOUT=['HRR_T9_' num2str(i3) '_' num2str(i4) 'db.mat'];
                        EXP(N).filter=i2{1};
                        EXP(N).Par=[i3,i4];
                    end
                end
            case 'HPB'
                for i4=varargin{1}
                    for i3=0:(i4-1)
                        for i5=varargin{2}
                            N=length(EXP)+1;
                            EXP(N).fileIN=i1{1};
                            EXP(N).fileOUT=['HPB_' num2str(i3) '_' num2str(i4) '_' num2str(i5) 'db.mat'];
                            EXP(N).filter=i2{1};
                            EXP(N).Par=[i3,i4,i5];
                        end
                    end
                end
            case 'HRB'
                for i4=varargin{1}
                    for i3=0:(i4-1)
                        for i5=varargin{2}
                            N=length(EXP)+1;
                            EXP(N).fileIN=i1{1};
                            EXP(N).fileOUT=['HRB_' num2str(i3) '_' num2str(i4) '_' num2str(i5) 'db.mat'];
                            EXP(N).filter=i2{1};
                            EXP(N).Par=[i3,i4,i5];
                        end
                    end
                end
            case 'HP'
                for i3=varargin{1}
                    for i4=varargin{2}
                        if i3<i4 
                            for i5=varargin{3}
                                N=length(EXP)+1;
                                EXP(N).fileIN=i1{1};
                                EXP(N).fileOUT=['HP_' num2str(i3) '_' num2str(i4) '_' num2str(i5) 'db.mat'];
                                EXP(N).filter=i2{1};
                                EXP(N).Par=[i3,i4,i5];
                            end
                        end
                    end
                end
            case 'F'
                for i3=varargin{1}
                    for i4=varargin{2}
                        N=length(EXP)+1;
                        EXP(N).fileIN=i1{1};
                        EXP(N).fileOUT=['F_' num2str(i3) '_' num2str(i4) 'db.mat'];
                        EXP(N).filter=i2{1};
                        EXP(N).Par=[i3,i4];
                    end
                end
        end
    end
end

end
%%
function experimento(EXP,ID,home)
disp([num2str(ID) ' ' EXP.fileIN ' ' EXP.fileOUT])
if isempty(EXP.fileIN)
    return
elseif exist([home '/' EXP.fileIN],'file')
    load([home '/' EXP.fileIN])
else
    disp(['Missing file: ' home '/' EXP.fileIN])
    return
end
if ~exist([home '/' E.NAME],'dir')
    mkdir([home '/' E.NAME])
end
if exist([home '/' E.NAME '/' EXP.fileOUT],'file')
    disp(['EXISTING EXPERIMENT ' E.NAME '/' EXP.fileOUT])
    return
end
Y=[];
RUN_EXPERIMENT=false;
USE_REFERENCES=false;
TASK_N=3;
switch EXP.filter
    case 'R'
        Y=E.Y;
        RUN_EXPERIMENT=true;
    case 'R9'
        Y=E.Y;
        RUN_EXPERIMENT=true;
        TASK_N=9;
    case 'N'
        Y=E.YN;
        RUN_EXPERIMENT=true;
    case 'N9'
        Y=E.YN;
        RUN_EXPERIMENT=true;
        TASK_N=9;
    case 'H'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=Homomorfic(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2));
        end
        RUN_EXPERIMENT=true;
    case 'H9'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=Homomorfic(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2));
        end
        RUN_EXPERIMENT=true;
        TASK_N=9;
    case 'HRR'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=Homomorfic(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2));
        end
        load([home '/' E.NAME '/Noise.mat'])
        if exist('R','var')
            RR=R;
            RUN_EXPERIMENT=true;
            USE_REFERENCES=true;
        end
    case 'HRR9'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=Homomorfic(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2));
        end
        load([home '/' E.NAME '/Noise.mat'])
        if exist('R','var')
            RR=R;
            RUN_EXPERIMENT=true;
            USE_REFERENCES=true;
            TASK_N=9;
        end
    case 'HPB'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=HPB(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2),EXP.Par(3));
        end
        RUN_EXPERIMENT=true;
    case 'HRB'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=HRB(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2),EXP.Par(3));
        end
        RUN_EXPERIMENT=true;
    case 'HP'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=HPendiente(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2),EXP.Par(3));
        end
        RUN_EXPERIMENT=true;
    case 'F'
        Y=E.YN;
        for j=1:E.N
            Y(j,:)=Filtro(Y(j,:),E.FFT_size,EXP.Par(1),EXP.Par(2));
        end
        RUN_EXPERIMENT=true;
end
%RUN_EXPERIMENT=false;

switch TASK_N
    case 3
        if RUN_EXPERIMENT
            if USE_REFERENCES
                R=Run_jktebopID_RR(ID,E,Y,RR);
                if ~isempty(R)
                    save([home '/' E.NAME '/' EXP.fileOUT],'R')
                end
            else
                R=Run_jktebopID(ID,E,Y);
                if ~isempty(R)
                    save([home '/' E.NAME '/' EXP.fileOUT],'R')
                end
            end
        end
    case 9
        if RUN_EXPERIMENT
            if USE_REFERENCES
                R=Run_jktebopID_RR_T9(ID,E,Y,RR);
                if ~isempty(R)
                    save([home '/' E.NAME '/' EXP.fileOUT],'R')
                end
            else
                R=Run_jktebopID_T9(ID,E,Y);
                if ~isempty(R)
                    save([home '/' E.NAME '/' EXP.fileOUT],'R')
                end
            end
        end
end

end



