%% Instructions

% This script is used to produce plots of LAMP data for all HERO WEC test
% cases. To use this script, first load workspace to be analyzed. Once this
% script is run, the info structure saved within the workspace is used to
% determine which plots are applicable to the test case. The visualization
% settings can be adjusted to plot data over the entire recorded timespan,
% the timespan of LAMP's motion profile, or a user selected timespan (see
% visualization settings in line 119). These settings apply to all plots
% produced by this script.

%
%% Sensor Descriptions

% Data Arrays:

% Time              [s] DAQ time starting with MODAQ initialization
% PRESS-ON-1001     [psi] RO Pre Filter Water Pressure
% PRESS-ON-1002     [psi] RO Post Filter Water Pressure
% PRESS-OS-1001     [psi] WEC Pump Water Pressure
% PRESS-OS-2002     [psi] WEC Air Tank Pressure

% FLOW-ON-1001      [gpm] RO Inlet Flow Rate (Pre-Filter)
% FLOW-ON-1002      [gpm] RO Inlet Flow Rate (Post-Filter)
% FLOW-ON-1003      [gpm] Brine Discharge Flow Rate
% FLOW-ON-1004      [gpm] Permeate Flow Rate
% FLOW-OS-1001      [gpm] WEC Pump Flow Rate

% CND-ON-1001       [micro-Siemens] RO Input Conductivity
% CND-ON-1002       [micro-Siemens] RO Permeate Conductivity

% POS-OS-1001       [deg] Encoder Position (located on first stage of spring return)
% LC-ST-1001        [lbf] Anchor Load

% PT-ON-1001        [V] Generator phase 1 voltage
% PT-ON-1002        [V] Generator phase 2 voltage
% PT-ON-1003        [V] Generator phase 3 voltage
% PT-ON-2001        [V] Post-rectifier voltage
% PT-ON-2002        [V] Charge controller input voltage
% PT-ON-2003        [V] Charge controller output voltage
% PT-ON-2004        [V] Battery voltage
% PT-ON-2005        [V] Pump controller input voltage
% PT-ON-2006        [V] Pump 1 voltage
% PT-ON-2007        [V] Pump 2 voltage

% CT-ON-1001        [A] Generator phase 1 current
% CT-ON-2001        [A] Post-rectifier current
% CT-ON-2002        [A] Charge controller input current
% CT-ON-2003        [A] Charge controller output current
% CT-ON-2004        [A] Battery current
% CT-ON-2005        [A] Pump controller input current
% CT-ON-2006        [A] Pump 1 current
% CT-ON-2007        [A] Pump 2 current

% Calculated Arrays:

% POS_OS_1001_S             [deg] Smoothed encoder data used for winch velocity calculations
% Calc_POS_Winch            [deg] Winch position calculated from encoder data and first stage of spring return ratio
% Diff_Time                 [s] Time array used to calculate velocity and power arrays (shortened by 1 data apoint to reflect forward difference method used to calculate velocity)
% Calc_V_Encoder            [rpm] Encoder velocity
% Calc_V_Winch              [rpm] Winch velocity
% Calc_V_Gearbox            [rpm] Gearbox velocity
% Calc_P_abs                [kW] Instantaneous absorbed power
% Calc_P_WEC_Electric_Out   [kW] Instantaneous power output from generator
% Calc_Eta_WEC_Electric     [%] Drivetrain efficiency during test (winch to generator)
% Calc_P_Electric_1         [kW] Instantaneous post-rectifier power
% Calc_Eta_Rectifier_Instantaneous      [%] Instantaneous rectifier efficiency
% Calc_P_Electric_2         [kW] Instantaneous charge controller input power
% Calc_P_Electric_3         [kW] Instantaneous charge controller output power
% Calc_Eta_CC_Instantaneous [%] Instantaneous charge controller efficiency
% Calc_P_Electric_4         [kW] Instantaneous power into batteries
% Calc_P_Electric_5         [kW] Instantaneous power supplied to pump controllers
% Calc_P_Electric_P1        [kW] Instantaneous power supplied to pump 1
% Calc_P_Electric_P2        [kW] Instantaneous power supplied to pump 2
% Calc_Filter_DP            [psi] Pressure differential across RO pre-filter
% Calc_ACC_Flow             [gpm] Flow rate into onshore accumulators
% Calc_P_Hydraulic_1        [kW] Hydraulic power at pump outlet
% Calc_P_Hydraulic_2        [kW] Hydraulic power at RO system inlet
% Calc_P_Hydraulic_3        [kW] Hydraulic power at Clark pump inlet
% Calc_Recovery_Ratio       [-] RO recovery ratio during test
% Sim_LC_ST_1001            [lbf] Instantaneous winch line tension calculated using Simulink

% Calculated Values:

% Calc_Tension_AVG          [lbf] Average winch line tension during test
% Calc_Tension_Max          [lbf] Maximum winch line tension during test
% Start.Index               Index of first data point in LAMP test profile where motion initiates
% Start.Time                [s] Time value of Start.Index
% End.Index                 Index of last data point in LAMP test profile
% End.Time                  [s] Time value of End.Index
% Calc_P_abs_AVG            [kW] Average absorbed power during test
% Calc_Q_Pump_AVG           [gpm] Average flow rate supplied by hydraulic pump during test
% Calc_P_WEC_Electric_Out_AVG   [kW] Average power output of generator during test
% Calc_P_Electric_1_AVG     [kW] Average post-rectifier electric power during test
% Calc_Eta_Rectifier        [%] Rectifier efficiency during test
% Calc_P_Electric_2_AVG     [kW] Average charge controller input power during test
% Calc_P_Electric_3_AVG     [kW] Average charge controller output power during test
% Calc_Eta_CC               [%] Charge controller efficiency during test
% Calc_Eta_Electric_Overall [%] Overall electrical system efficiency during test
% Calc_P_Electric_4_AVG     [kW] Average power into batteries during test
% Calc_Battery_Charge       [kWh] Calculated energy input to battery during test
% Calc_P_Electric_5_AVG     [kW] Average power supplied to pump controllers during test
% Calc_P_Electric_P1_AVG    [kW] Average power supplied to pump 1 during test
% Calc_P_Electric_P2_AVG    [kW] Average power supplied to pump 2 during test
% Calc_Loss_Conversion      [%] Conversion loss at electric pumps
% Calc_Q_Feed_AVG           [gpm] Average flow rate into RO system during test
% Calc_Volume_Pump          [Gallons] Total volume of water moved by hydraulic pump
% Calc_Volume_RO            [Gallons] Total volume of water recieved at RO
% Calc_Volume_PRV           [Gallons] Total Volume of water discharged by pressure relief valves
% Calc_Volume_Permeate      [Gallons] Total volume of permeate produced during test
% Calc_PRV_Percent          [%] Percent of water pumped which is discharged by pressure relief valves
% Calc_P_Hydraulic_1_AVG    [kW] Average hydraulic power at pump otlet during test
% Calc_P_Hydraulic_2_AVG    [kW] Average hydraulic power at RO system inlet during test
% Calc_P_Hydraulic_3_AVG    [kW] Average hydraulic power at Clark pump inlet during test
% Calc_Loss_Filter          [%] Percent of energy loss across RO pre-filter
% Calc_Eta_Overall          [%] Overall system efficiency during test (winch to Clark pump)
% Calc_Q_Permeate_AVG       [gpm] Average permeate flow rate during test
% Calc_Loss_Transfer        [%] Percent of energy lost across hydraulic transfer hose
% Sim_Tension_AVG           [lbf] Average winch line tension calculated using Simulink
% Sim_Tension_Max           [lbf] Maximum winch line tension calculated using Simulink
% Run.Time                  [s] Duration of test
% Calc_Eta_WEC_Hydraulic    [%] Drivetrain efficiency during test (winch to pump outlet)

Test_Case = strrep(info.Run,"_"," ");

%
%% Visualization Settings

% Visualization structure can be adjusted to display a select time frame on
% all plots. Three options are available:
% 'Default' - displays all data recorded includes LAMP hold phases in all
% cases and LAMP initialization or settling in some cases. 
% 'Test' - displays only data recorded during the prescribed LAMP motion
% profile (this is the range which all performance metrics were calculated
% over)
% 'Custom' - enables user selected plot range

Visualization.Range = 'Default';         % 'Default', 'Test', or 'Custom'

if strcmp(Visualization.Range,'Default') == 1
    Visualization.Start = Time(1);
    Visualization.End = Time(end);
elseif strcmp(Visualization.Range,'Test') == 1
    Visualization.Start = Start.Time;
    Visualization.End = End.Time;
    fprintf('Only data during LAMP motion profile is displayed')
elseif strcmp(Visualization.Range,'Custom') == 1
    % Enter custom start and end time for plots here
    Visualization.Start = 200;     % [s]
    Visualization.End = 300;     % [s]
    fprintf('Visualization range set by user')
end

%
%% Anchor Load Cell

if info.Data_Collection.LC == 1
    figure; plot(Time,LC_ST_1001)
    xlabel('Time [s]')
    ylabel('Load [Lbf]')
    title(Test_Case + " Anchor Load Cell")
    xlim([Visualization.Start Visualization.End])
else
    figure; plot(Time,Sim_LC_ST_1001)
    xlabel('Time [s]')
    ylabel('Load [Lbf]')
    title(Test_Case + " Simulated Anchor Load Cell")
    xlim([Visualization.Start Visualization.End])
end

%
%% Encoder

figure; plot(Time,POS_OS_1001_S)
xlabel('Time [s]')
ylabel('Position [Degrees]')
title(Test_Case + " Encoder Position")
xlim([Visualization.Start Visualization.End])

figure; plot(Time,Calc_POS_Winch);
xlabel('Time [s]')
ylabel('Position [Degrees]')
title(Test_Case + " Winch Position")
xlim([Visualization.Start Visualization.End])

figure; plot(Diff_Time,Calc_V_Encoder)
xlabel('Time [s]')
ylabel('Velocity [rpm]')
title(Test_Case + " Encoder Velocity")
xlim([Visualization.Start Visualization.End])

figure; plot(Diff_Time,Calc_V_Winch)
xlabel('Time [s]')
ylabel('Velocity [rpm]')
title(Test_Case + " Winch Velocity")
xlim([Visualization.Start Visualization.End]) 

figure; plot(Diff_Time,Calc_V_Gearbox)
xlabel('Time [s]')
ylabel('Velocity [rpm]')
title(Test_Case + " Gearbox Output Velocity")
xlim([Visualization.Start Visualization.End])

%
%% Hydraulic Pump

if strcmp(info.Configuration,'Hydraulic') == 1
    figure; plot(Time,FLOW_OS_1001)
    hold on
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " Pump Flow")
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])
    hold off
end

%
%% Electric Generator

if strcmp(info.Configuration,'Electric') == 1
    % 3 phase AC out of generator
    figure; plot(Time,PT_ON_1001)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_1001)
    ylabel('Current [A]')
    title(Test_Case + " Generator Phase 1")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(Time,PT_ON_1002)
    xlabel('Time [s]')
    ylabel('Voltage')
    title(Test_Case + " Generator Phase 2")
    xlim([Visualization.Start Visualization.End])

    figure; plot(Time,PT_ON_1003)
    xlabel('Time [s]')
    ylabel('Voltage')
    title(Test_Case + " Generator Phase 3")
    xlim([Visualization.Start Visualization.End])

    figure; plot(Time,Calc_P_WEC_Electric_Out)
    xlabel('Time [s]')
    ylabel('Power [W]')
    title(Test_Case + " Generator Instantaneous Power")
    xlim([Visualization.Start Visualization.End])

    figure; plot(Time,PT_ON_2001)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2001)
    ylabel('Current [A]')
    title(Test_Case + " Post-Rectifier")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off
       
    % DC into charge controller

    figure; plot(Time,PT_ON_2002)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2002)
    ylabel('Current [A]')
    title(Test_Case + " Charge Controller Input")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off
      
    % DC from charge controller

    figure; plot(Time,PT_ON_2003)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2003)
    ylabel('Current [A]')
    title(Test_Case + " Charge Controller Output")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    % DC at batteries

    figure; plot(Time,PT_ON_2004)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2004)
    ylabel('Current [A]')
    title(Test_Case + " Battery")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    % DC at pump controllers
    % In cases where the pump disconnect was engaged, but pumps wer turned
    % off using the MODAQ web system, a current draw of approximately 50 mA
    % exists at the pump controller input due to power use to operate the
    % pump controller.

    figure; plot(Time,PT_ON_2005)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2005)
    ylabel('Current [A]')
    title(Test_Case + " Pump Controller Input")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off
       
    % DC to each pump

    % Pump 1
    figure; plot(Time,PT_ON_2006)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2006)
    ylabel('Current [A]')
    title(Test_Case + " Pump 1 Input")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    % Pump 2
    figure; plot(Time,PT_ON_2007)
    hold on
    xlabel('Time [s]')
    ylabel('Voltage')
    yyaxis right
    plot(Time,CT_ON_2007)
    ylabel('Current [A]')
    title(Test_Case + " Pump 2 Input")
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off
end

%
%% Air Tank

figure; plot(Time,PRESS_OS_2002)
xlabel('Time [s]')
ylabel('Pressure [psi]')
title(Test_Case + " Air Tank Pressure")
xlim([Visualization.Start Visualization.End])

%
%% RO Inlet

if strcmp(info.RO,'ON') == 1
    figure; plot(Time,FLOW_ON_1001)
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " RO Inlet Flow")
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

    figure; plot(Time,FLOW_ON_1002)
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " RO Post Filter Flow")
    xlim([Visualization.Start Visualization.End])

    if info.Data_Collection.VoltageAI == 1
        figure; plot(Time,PRESS_ON_1001)
        xlabel('Time [s]')
        ylabel('Pressure [psi]')
        title(Test_Case + " RO Pre-Filter Pressure")
        xlim([Visualization.Start Visualization.End])

        figure; plot(Time,PRESS_ON_1002)
        xlabel('Time [s]')
        ylabel('Pressure [psi]')
        title(Test_Case + " RO Post-Filter Pressure")
        xlim([Visualization.Start Visualization.End])

        figure; plot(Time,Calc_Filter_DP)
        xlabel('Time [s]')
        ylabel('Pressure [psi]')
        title(Test_Case + " Pressure Differential across Filter")
        xlim([Visualization.Start Visualization.End])
    end

    figure; plot(Time,Calc_ACC_Flow)
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " Accumulator Flow")
    xlim([Visualization.Start Visualization.End])

    % Hydraulic power in RO system
    if info.Data_Collection.VoltageAI == 1 && strcmp(info.Configuration,'Hydraulic') == 1
        figure; plot(Diff_Time,Calc_P_Hydraulic_1,Diff_Time,Calc_P_Hydraulic_2)
        xlabel('Time [s]')
        ylabel('Power [kW]')
        title(Test_Case + " Hydraulic Power")
        legend('Hydraulic Power at Pump','Hydraulic Power at RO Inlet')
        xlim([Visualization.Start Visualization.End])
        ylim([0 inf])
    
        if info.Data_Collection.LC == 1
            figure; plot(Diff_Time,Calc_P_abs,Diff_Time,Calc_P_Hydraulic_3);
            xlabel('Time [s]')
            ylabel('Power [kW]')
            title(Test_Case + " Calculated Power")
            legend('Absorbed Power at Winch','Hydraulic Power at Clark Pump')
            xlim([Visualization.Start Visualization.End])
            ylim([0 inf])
        end
    end
end

%
%% RO Outlet

if strcmp(info.RO,'ON') == 1
    figure; plot(Time,FLOW_ON_1003)  
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " Brine Discharge Flow Rate")
    xlim([Visualization.Start Visualization.End])

    figure; plot(Time,FLOW_ON_1004)
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " Permeate Flow Rate")
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

    figure; plot(Time,FLOW_ON_1002,Time,FLOW_ON_1004)
    xlabel('Time [s]')
    ylabel('Flow Rate [gpm]')
    title(Test_Case + " RO Feed Flow and Permeate Production")
    legend('Flow to Clark Pump','Permeate Flow')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

    if info.Data_Collection.VoltageAI == 1
        figure; plot(Time,PRESS_ON_1002)
        hold on
        yyaxis left
        xlabel('Time [s]')
        ylabel('Pressure [psi]')
        title(Test_Case + " RO Feed Pressure and Permeate Production")
        yyaxis right
        plot(Time,FLOW_ON_1004)
        ylabel('Flow Rate [gpm]')
        xlim([Visualization.Start Visualization.End])
        ylim([0 inf])
        legend('Pressure at Clark Pump', 'Permeate Flow')
        hold off
    end

    figure; plot(Time,Calc_Recovery_Ratio)
    xlabel('Time [s]')
    ylabel('Recovery Ratio [-]')
    title(Test_Case + " RO Recovery Ratio")
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])
end

%
%% Conductivity Sensors

if strcmp(info.RO,'ON')
    figure; plot(Time,CND_ON_1001)
    xlabel('Time [s]')
    ylabel('Conductivity [Micro-Siemens]')
    title(Test_Case + " RO Inlet Conductivity")
    xlim([Visualization.Start Visualization.End])

    figure; plot(Time,CND_ON_1002)
    xlabel('Time [s]')
    ylabel('Conductivity [Micro-Siemens]')
    title(Test_Case + " Permeate Conductivity")
    xlim([Visualization.Start Visualization.End])
end

%
%% Calculated Hydraulic Power and Efficiency

if strcmp(info.Configuration,'Hydraulic') == 1
    if info.Data_Collection.LC ==1
        figure; plot(Diff_Time,Calc_P_abs,Diff_Time,Calc_P_Hydraulic_1)       
        xlabel('Time [s]')
        ylabel('Power [kW]')
        title(Test_Case + " Calculated Power")
        legend('Absorbed Power at Winch','Hydraulic Power Out from Pump')
        xlim([Visualization.Start Visualization.End])
        ylim([0 inf])
    end
else        % Drivetrain cases
    figure; plot(Diff_Time,Calc_P_abs)       
    xlabel('Time [s]')
    ylabel('Power [kW]')
    title(Test_Case + " Calculated Absorbed Power")
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])
end