% Purpose:  Graphical representation of max time delay
% Author:   Axel Lodemel Holene
% E-mail:   axel.holene@gmail.com
% Date:     Mai 2013
% About:    

%% Initialising
clear all
close all
clc

% Adding commonFiles to search path
mDir   = fileparts(pwd) ;
shared = fullfile(mDir,'commonFiles') ;     addpath(shared)
m2tkz  = fullfile(mDir,'matlab2tikz/src') ; addpath(m2tkz)
varclr = fullfile(mDir,'varycolor') ;       addpath(varclr)

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'} ;

Ms_s = 1.6 ; % Which Ms to plot from? [1.3, 1.6, 1.7, 1.9]

for modelId = 1:9
  i = 1 ;                                              % Counter
  J = zeros(150,24) ;
  d = J ;
  captions = {};
  for cType = 1:3
    for ctrlr = 1:2
      for cNum = 4
        innerLoop = true ;
        lPath = strcat(mDir,'/results/thetaMax/', ...
                cTypes{cType},'/', ctrlrs{ctrlr},'/', ...
                cParm{cNum},'/', modelName{modelId},'.mat') ;
        fprintf('Attempting to load %s \t\t',lPath)
        try
          load(lPath,'res')
        catch
            fprintf('Failed. \n')
            fprintf('Error: %s\n', lasterr)
            innerLoop = false ;
        end % try
        while innerLoop
          try
            this = getfield(res, ...
                   strcat('THETAMAX',upper(ctrlrs{ctrlr}))) ;
          catch
            fprintf('Failed at InnerLoop.\n')
            innerLoop = false ;
          end % try
          caption = strcat(upper(cTypes{cType}),...
                  ':', upper(ctrlrs{ctrlr}),':',...
                  cParm{cNum}) ;
          idx = find(this.Ms_span == Ms_s) ;
          n = length(this.d_span) ;
          d(1:n,i) = this.d_span ;
          J(1:n,i) = this.J(:,idx) ;
          captions{i} = caption ;
          innerLoop = false ;
        end % innerLoop
        i = i + 1 ;
      end % cNum
    end % ctrlr
  end % cType
  % Set final captions and plotting colors
  captions = captions(~cellfun('isempty',captions)) ;
  ColorSet = varycolor(length(captions)) ;
  % Removing zeros
  J(:,all(~any(J),1)) = []; % and columns
  d(:,all(~any(d),1)) = []; % and columns
  % [I,A] = find(J~=0);
  % J = J(:,1:max(A)) ;
  % [I,A] = find(d~=0);
  % d = d(:,1:max(A)) ;
  % J(J==0)   = NaN ;
  % d(d == 0) = NaN ;
  % Plotting routine
  figure(modelId)
  clf 
  set(gca, 'ColorOrder', ColorSet) ;
  plot(d,J) ;
  xlabel('Time-delay, $\theta$', 'interpreter', 'latex')
  ylabel('Cost, $J$', 'interpreter', 'latex') 
  legend(captions, 'Location', 'NorthEast')
  title(titleName{modelId},'interpreter','latex')

  save([mDir, '/results/merged/structs/distCost/', ...
        num2str(modelName{modelId}),'.mat'])
  saveas(gcf,[mDir, '/results/merged/figures/distCost/',...
        num2str(modelName{modelId}),'.fig'])
  loc = strcat(mDir, '/results/tex/distCost/',...
        num2str(modelName{modelId}),'.tex');
  matlab2tikz(loc,'showInfo', false) ;
    fprintf('\t Finished!\n\n')

end % modelId








