close all
clear all
clc

%% Parameters
file_name  = 'MORPHO_FOD7.h5';
data_type  = 'Active';   % IMP, FBG, Impact, MTS, Active
load_cycle = '8kN_200';  % Not for impedance and impact. Avalaible loads: 4kN_200, 4kN_400, 8kN_200, 8kN_400, 12kN_200, 12kN_400, 14kN_200, 14kN_400, 16kN_200, 16kN_400, 18kN_200, 18kN_400, 20kN_100, 20kN_200, 20kN_300, 20kN_400, 22kN_100, 22kN_200, 22kN_300, 22kN_400, 24kN_100
sensor_id  = 2 ;         % 1 to 4 for active | 1 to 16 for FBGs | not for MTS

%% Load data

file_info = h5info(file_name); % Get HDF5 file informations

switch data_type
    case 'IMP'
        % Load impedance data 
        imp_folder = '/1_Impedance/';
        imp_cases  = h5info(file_name,imp_folder);
        sel_case   = imp_cases.Groups(1).Name ;
        if (sensor_id>4)&&(sensor_id<30)
            Z          = h5read(file_name,[sel_case '/PZT_' num2str(sensor_id) '/Z']) ; 
            phase      = h5read(file_name,[sel_case '/PZT_' num2str(sensor_id) '/phase']) ; 
            freq       = h5read(file_name,[sel_case '/PZT_' num2str(sensor_id) '/freq']) ; 
            f_start    = h5readatt(file_name,imp_folder,'f_start');
            f_stop     = h5readatt(file_name,imp_folder,'f_stop');
            % Plot impedance data
            a = figure(); set(a,'windowstyle','docked');
            subplot(2,1,1); semilogx(freq,Z); xlabel('Frequency (Hz)'); ylabel('Impedance modulus (Ohm)'); grid on; xlim([f_start f_stop])
            subplot(2,1,2); semilogx(freq,phase); xlabel('Frequency (Hz)'); ylabel('Impedance phase (°)'); grid on; xlim([f_start f_stop])
            sgtitle(sel_case,'interpreter','none')
        else
            disp('No impedance data available for this sensor')
        end

    case 'FBG'
        % Load FBG data 
        FBG_folder = '/2_FBG/';
        FBG_data   = h5read(file_name,[FBG_folder '/' load_cycle '/FBG_' num2str(sensor_id) '/FBG_data']) ; 
        time       = h5read(file_name,[FBG_folder '/' load_cycle '/FBG_' num2str(sensor_id) '/time']) ; 
        % Plot FBG data
        a = figure(); set(a,'windowstyle','docked');
        plot(time,FBG_data); xlabel('Time (s)'); ylabel('Strain (\mum/m)');
        title([load_cycle '/FBG_' num2str(sensor_id)],'interpreter','none')

    case 'Impact'
        % Load impact data
        Impact_folder = '/3_Impact/';
        PZT_data      = h5read(file_name,[Impact_folder 'PZT_' num2str(sensor_id) '/PZT_data']) ; 
        f_s           = h5readatt(file_name,Impact_folder,'f_s');
        time          = (0:length(PZT_data)-1)/f_s;
        % Plot Impact data
        a = figure(); set(a,'windowstyle','docked');
        plot(time,PZT_data); xlabel('Time (s)'); ylabel('Amplitude (V)');
        title([Impact_folder 'PZT_' num2str(sensor_id)],'interpreter','none')

    case 'MTS'
        % Load MTS data
        MTS_folder = '/4_MTS/';
        disp       = h5read(file_name,[MTS_folder '/' load_cycle  '/displacement']) ; 
        time       = h5read(file_name,[MTS_folder '/' load_cycle  '/time']) ; 
        load       = h5read(file_name,[MTS_folder '/' load_cycle  '/load']) ; 
        % Plot MTS data
        a = figure(); set(a,'windowstyle','docked');
        subplot(2,1,1); plot(time,disp); xlabel('Time (s)'); ylabel('Displacement (mm)');
        subplot(2,1,2); plot(time,load); xlabel('Time (s)'); ylabel('Displacement (kN)');
        sgtitle(load_cycle,'interpreter','none')

    case 'Active'
        if sensor_id<5
            % Load active data
            Active_folder = '/5_Active';
            freq_cases  = h5info(file_name,[Active_folder '/' load_cycle]);
            sel_freq = freq_cases.Groups(1).Name;
            PZT_data      = h5read(file_name,[sel_freq '/Actionneur' num2str(sensor_id) '/measured_data_rep_1.mat']) ; 
            time = PZT_data(:,1)-PZT_data(1,1);
            pzt_records(:,[sensor_id setdiff(1:size(PZT_data,2)-1,sensor_id)]) = PZT_data(:,2:end);
            % Plot Active data
            for n_fig = 1:5
                a = figure(); set(a,'windowstyle','docked');
                for n_pzt =1:5
                    curr_pzt = (n_fig-1)*5+n_pzt ;
                    subplot(5,1,n_pzt); plot(time,pzt_records(:,curr_pzt))
                    xlabel('Time (s)'); ylabel('Amp (V)'); title(['PZT ' num2str(curr_pzt)])
                end
                sgtitle(sel_freq,'interpreter','none')
            end
        else
            disp('No active data available for that PZT')
        end
end