%% Header
% Purpose:  Calculation and graphical representation of SIMC 
%           tuning performance
% Author:   Axel Lodemel Holene
% E-mail:   axel.holene@gmail.com
% Date:     13. February 2013
% About:    ctrl = [Kc I D]

%% Initialising
clear all
close all
clc

% Adding commonFiles to search path
mDir   = fullfile(...
        '~/Documents/NTNU/Master_thesis/matlab/newWorldOrder/') ;
shared = fullfile(mDir,'commonFiles') ;
m2tkz  = fullfile(mDir,'matlab2tikz/src') ;
addpath(shared)
addpath(m2tkz)

titleName = {'Case 1', 'Case 2', 'Case 3', 'Case 4', ...
             'Case 5', 'Case 6', 'Case 7', 'Case 8', ...
             'Case 9', 'Case 10', 'Case 11', 'Case 12', ...
             'Case 13'} ;
modelName = {'case1', 'case2', 'case3', 'case4', 'case5', ...
             'case6', 'case7', 'case8', 'case9', 'case10', ...
             'case11', 'case12', 'case13'} ;
cTypes  = {'po', 'simc', 'smith'} ;
ctrlrs  = {'pid', 'pi'} ;
cParm   = {'Chriss', 'Parallel', 'AltParallel', 'Cascade'} ;

for modelId = 1:1 %length(modelName)
    i = 1 ;                                             % Counter
    J = zeros(80,24) ;
    Ms = J ;
    Mt = J ;
    captions = {};
    for cType = 1:length(cTypes)
        for ctrlr = 1:length(ctrlrs)
            for cNum = 1:length(cParm)
                innerLoop = true ;
                fprintf('Attempting to load %s \t\t', ...
                        strcat( upper(cTypes{cType}), ':', ...
                                upper(ctrlrs{ctrlr}), ':', ...
                                cParm{cNum}))
                try
                    load([mDir, '/results/', ...
                        num2str(cTypes{cType}), '/', ...
                        num2str(ctrlrs{ctrlr}), '/', ...
                        num2str(cParm{cNum}),   '/', ...
                        num2str(modelName{modelId}),'.mat'],...
                        'res')
                catch
                    fprintf('Failed. \n')
                    innerLoop = false ;
                end
                if innerLoop
                    caption = strcat(upper(cTypes{cType}),':', ...
                                    upper(ctrlrs{ctrlr}), ':', ...
                                    cParm{cNum}) ;
                    captions{i} = caption ;
                    this = getfield(res, ...
                                    strcat(upper(cTypes{cType}),...
                                    upper(ctrlrs{ctrlr}))) ;
                    j = this.J ;
                    ms = this.Msc ;
                    mt = this.Mt ;
                    J(1:length(j),i) = j ;
                    Ms(1:length(ms),i) = ms ;
                    Mt(1:length(mt),i) = mt ;
                    fprintf('Success!\n') ;
                end
                i = i + 1 ;
            end
        end
    end
    % Removing zeros
    J(J==0)     = NaN ;
    Ms(Ms == 0) = NaN ;
    Mt(Mt == 0) = NaN ;

    % Plotting routine
    figure(modelId)
    clf
    subplot(2,1,1), plot(Ms, J, 'LineWidth', 1) ;
    axis([0 4 0 4])
    xlabel('Robustness, $M_s$', ...
           'interpreter','latex','FontSize',12)
    ylabel('Performance, $J$', ...
            'interpreter','latex','FontSize',12)
    title(titleName{modelId}, ...
            'interpreter','latex','FontSize',12)
    legend(captions, 'Location','NorthEast')

    subplot(2,1,2), plot(Ms, Mt, 'LineWidth', 1)
    axis([0 3 0 3])
    xlabel('$M_s$', 'interpreter', 'latex', 'FontSize', 12)
    ylabel('$M_t$', 'interpreter', 'latex', 'FontSize', 12)
    title(titleName{modelId},'interpreter','latex','FontSize',12)
    legend(captions, 'Location','NorthEast')

    save([mDir, 'results/merged/structs/', ...
        num2str(modelName{modelId}),'.mat'])
    saveas(gcf,['results/merged/figures/',...
           num2str(modelName{modelId}),'.fig'])
    loc = strcat('results/tex/',num2str(modelName{modelId}),'.tex');
    matlab2tikz(loc, 'width', '\textwidth') ;
end                                             % Ending for-loop
restoredefaultpath