% fig_time_line_diagnosis

%% Select and load data
path = 'P:\EnPro\Paper\02_paper_rule_based_diagnosis\Data';
addpath(genpath('P:\EnPro\Paper\02_paper_rule_based_diagnosis\Code\'));
%Select any scenario
file_name = uigetfile(path, 'MultiSelect','on')
%Fixed scenario
% file_name = '20240920-120510_Scenarios_33_pump_wear_yellow_800_Q_low.mat_processed.mat';

addpath(genpath(path));
load (file_name);       %loaded as data

%% Helping parameters
help = helper_structs();

modules = help.modules;
q_modules = [help.q_pump help.q_mixer];
% module = input('Which module is faulty? (1=cyan, 2=magenta, 3=yellow, 4=water, 5=sugar, 6=mixer1, 7=mixer2');
module = 3;
if module >= 4
    mixer = 6;
else
    mixer = 7;
end
% fault_input = 1;
% 
% while fault_input == 1
%     n_fault = input('Which fault was emulated in the chosen files?');
%     if n_fault >= 1 & n_fault <= 43
%         fault_input = 0;
%     end
% end
n_fault = 15;

% Determine fault type diagnosis
 val = []; ind = [];
for i = 1:length(data.measurement_TIME_VECTOR.value)
    [val(i) ind(i)] = max(data.results.prognosis(i,:));
end

%% Plot figure with subplots

%% Plots without uncertainty

% Plot of bypass opening and volume flow
fig_time = figure;
tiledlayout(2,1);
nexttile;
% bypass opening
plot(data.measurement_TIME_VECTOR.value, data.yellow_sensor_bypass.value);
set(gca, 'XTickLabel',[]);
yl2 = ylabel('BYPASS in °');
ylim([0 20]);
% [fig_time, IDs] = PlotID.TagPlot(fig_time,'Location', 'custom', 'Position', [1 0.1], 'Rotation', 90);
pimpPlot;

% volumeflow
nexttile;
plot(data.measurement_TIME_VECTOR.value, data.yellow_sensor_Q.value);
yl3 = ylabel('VOLUME FLOW in l/min');
ylim([0 3.5]);
xlabel('TIME in s');
%Align ylabels
pos2=get(yl2,'Pos');
pos3=get(yl3,'Pos');
set(yl3,'Pos',[pos2(1) pos3(2) pos3(3)]);
pimpPlot;
setFigureSize('Processes12_12');

script = ['fig_time_series','.m']; % This creates a path to the script this line is  called from
rdata =  ['P:\EnPro\Paper\02_paper_rule_based_diagnosis\Data\' file_name] ; % a single path does not need a cell array
% PlotID.Publish(rdata, script, fig_time);



%% Plots with uncertainty

% Plot of observer values and residuals for worn module

fig_uncert_obs = figure;
tiledlayout(2,1);
nexttile;
%observers at dosing module
errorbar(data.measurement_TIME_VECTOR.value, data.observer.(modules{module}).Q_pump.value,...
    (-1)*data.observer.(modules{module}).Q_pump.uncertainty,data.observer.(modules{module}).Q_pump.uncertainty,...
    'Color',[.25 .25 .25]);
hold on
errorbar(data.measurement_TIME_VECTOR.value, data.observer.(modules{module}).Q_plant.value,...
    (-1)*data.observer.(modules{module}).Q_plant.uncertainty,data.observer.(modules{module}).Q_plant.uncertainty,...
    'Color',[.5 .5 .5]);
errorbar(data.measurement_TIME_VECTOR.value, data.observer.(modules{module}).Q_power.value,...
    (-1)*data.observer.(modules{module}).Q_power.uncertainty,data.observer.(modules{module}).Q_power.uncertainty,...
    'Color',[.75 .75 .75]);
errorbar(data.measurement_TIME_VECTOR.value, data.(q_modules{module}).value,...
    (-1)*data.(q_modules{module}).uncertainty,data.(q_modules{module}).uncertainty,...
    'Color','k');
yl1 = ylabel('VOLUME FLOW in l/min');
ylim([0 4]);
set(gca, 'XTickLabel',[]);
% [fig_uncert_obs, IDs] = PlotID.TagPlot(fig_uncert_obs,'Location', 'custom', 'Position', [1 0.1], 'Rotation', 90);
% legend('Q pump','Q plant','Q power','Q meas');
pimpPlot;

% residuals
nexttile;
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_pump_plant.value,...
    (-1)*data.residuals.(modules{module}).R_pump_plant.uncertainty,data.residuals.(modules{module}).R_pump_plant.uncertainty,...
    'Color',[.25 .25 .25]);
hold on
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_power_plant.value,...
    (-1)*data.residuals.(modules{module}).R_power_plant.uncertainty,data.residuals.(modules{module}).R_power_plant.uncertainty,...
     'Color',[.5 .5 .5]);
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_power_pump.value,...
    (-1)*data.residuals.(modules{module}).R_power_pump.uncertainty,data.residuals.(modules{module}).R_power_pump.uncertainty,...
     'Color',[.75 .75 .75]);
yl3 = ylabel('RESIDUALS in l/min');
xlabel('TIME in s');
ylim([-0.5 3.5]);
% legend('pupl','popl','popu', 'pure');
pimpPlot;
%Align ylabels
pos1=get(yl1,'Pos');
pos3=get(yl3,'Pos');
set(yl3,'Pos',[pos1(1) pos3(2) pos3(3)]);
setFigureSize('Processes12_12');

script = ['fig_time_series','.m']; % This creates a path to the script this line is  called from
rdata =  ['P:\EnPro\Paper\02_paper_rule_based_diagnosis\Data\' file_name] ; % a single path does not need a cell array
% PlotID.Publish(rdata, script, fig_uncert_obs);


% Plot of residuals for parallel and downstream module

fig_uncert_para = figure;
tiledlayout(2,1);
nexttile;
module = 1;
%residuals of parallel module
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_pump_plant.value,...
    (-1)*data.residuals.(modules{module}).R_pump_plant.uncertainty,data.residuals.(modules{module}).R_pump_plant.uncertainty,...
    'Color',[.25 .25 .25]);
hold on
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_power_plant.value,...
    (-1)*data.residuals.(modules{module}).R_power_plant.uncertainty,data.residuals.(modules{module}).R_power_plant.uncertainty,...
     'Color',[.5 .5 .5]);
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{module}).R_power_pump.value,...
    (-1)*data.residuals.(modules{module}).R_power_pump.uncertainty,data.residuals.(modules{module}).R_power_pump.uncertainty,...
     'Color',[.75 .75 .75]);
yl1 = ylabel('RESIDUALS in l/min');
ylim([-0.5 0.5]);
title('PARALLEL MODULE A');
set(gca, 'XTickLabel',[]);
% [fig_uncert_para, IDs] = PlotID.TagPlot(fig_uncert_para,'Location', 'custom', 'Position', [1 0.1], 'Rotation', 90);
% legend('Q pump','Q plant','Q power','Q meas');
pimpPlot;

% residuals of downstream module
nexttile;
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{mixer}).R_pump_resis.value,...
    (-1)*data.residuals.(modules{mixer}).R_pump_resis.uncertainty,data.residuals.(modules{mixer}).R_pump_resis.uncertainty,...
    'Color',[.5 .5 .5]);
hold on
errorbar(data.measurement_TIME_VECTOR.value, data.residuals.(modules{mixer}).R_power_resis.value,...
    (-1)*data.residuals.(modules{mixer}).R_power_resis.uncertainty,data.residuals.(modules{mixer}).R_power_resis.uncertainty,...
     'Color','k');
yl3 = ylabel('RESIDUALS in l/min');
xlabel('TIME in s');
title('DOWNSTREAM MIXER 2');
ylim([-0.5 3.5]);
% legend('pupl','popl','popu', 'pure');
pimpPlot;
%Align ylabels
pos1=get(yl1,'Pos');
pos3=get(yl3,'Pos');
set(yl3,'Pos',[pos1(1) pos3(2) pos3(3)]);
setFigureSize('Processes12_12');

script = ['fig_time_series','.m']; % This creates a path to the script this line is  called from
rdata =  ['P:\EnPro\Paper\02_paper_rule_based_diagnosis\Data\' file_name] ; % a single path does not need a cell array
% PlotID.Publish(rdata, script, fig_uncert_para);


% Fault Type Diagnosis
fig_uncert_diagnosis = figure;
tiledlayout(2,1);
nexttile;
plot(data.measurement_TIME_VECTOR.value, data.results.fault_input(:,n_fault),'Color','k','LineStyle','-');
yl1 = ylabel('FAULT INPUT');
ylim([-0.1 1]);
% [fig_uncert_diagnosis, IDs] = PlotID.TagPlot(fig_uncert_diagnosis,'Location', 'custom', 'Position', [1 0.1], 'Rotation', 90);
pimpPlot;

% Fault Degree Diagnosis
nexttile;
errorbar(data.measurement_TIME_VECTOR.value, data.results.prognosis(:,n_fault),...
    (data.results.prognosis(:,n_fault)-data.results.uncert_prognosis_low(:,n_fault)),...
    (data.results.prognosis(:,n_fault)-data.results.uncert_prognosis_high(:,n_fault)),...
    'Color',[.3 .3 .3],'LineStyle','-');
yl2 = ylabel('DIAGNOSIS');
xlabel('TIME in s');
ylim([-0.1 1]);

pimpPlot;
% legend('INPUT', 'DIAGNOSE');
%Align ylabels
pos1=get(yl1,'Pos');
pos2=get(yl2,'Pos');
set(yl1,'Pos',[pos2(1) pos1(2) pos1(3)]);
setFigureSize('Processes12_12');

script = ['fig_time_series','.m']; % This creates a path to the script this line is  called from
rdata =  ['P:\EnPro\Paper\02_paper_rule_based_diagnosis\Data\' file_name] ; % a single path does not need a cell array
% PlotID.Publish(rdata, script, fig_uncert_diagnosis);

