clear all
clc
%% Instructions

% This script is used to plot data from the 2024 HERO WEC electric
% configuration deployment. The plotting time interval and subsystems to be
% plotted are user defined. To use this script, set the Input.Directory
% variable to the location of the MHKDR Submission 551 download as a string
% array. Next, select subsystems from which data will be plotted and input
% start and end times in UTC date string format ('DD-MMM-YYYY HH:mm:ss).
% The Visualization structure can be used to define the plotting range,
% instructions on using this can be found in the Visualization Settings
% section of this script.
%% Inputs

% Set Input.Directory to the location of a single folder used to store all
% data folders downloaded from MHKDR submission 551.
Input.Directory = "C:\Users\jpanzare\Documents\FY24 Deployment\Electric Deployment\Parquet_to_Workspace";
addpath(Input.Directory);

% Select subsystems ('YES', or 'NO')
Input.WEC = 'YES';       % On WEC sensors and anchor load cell
Input.Gen = 'YES';       % WEC generator output sensors (PowRaw group)
Input.PE = 'YES';        % Onshore power electronics sensors
Input.RO = 'YES';        % RO subsystem sensors

Input.Start_Time = '18-Apr-2024 23:00:00';
%% Visualization Settings

% Visualization structure can be adjusted to display a select time frame on
% all plots. Two options are available:
% 'Default' - displays full 30-minute data set
% 'Custom' - enables user selected plot range

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

if strcmp(Visualization.Range,'Custom') == 1
    % Enter custom start and end times in UTC date string format for plots here
    Visualization.Start = '18-Apr-2024 23:25:00';   
    Visualization.End = '18-Apr-2024 23:27:00';     
end
%% Import Data

Month_Number = num2str(month(Input.Start_Time));
if strlength(Month_Number) < 2
    Month_Number = strcat('0',Month_Number);
end

WEC_File = Input.Directory + '/WEC Data Workspaces/' + Input.Start_Time(8:11) +'-' + Month_Number + '-' + Input.Start_Time(1:2) + '_' + Input.Start_Time(13:14) + '-' + Input.Start_Time(16:17) + '_WEC_Subsystem';
Gen_File = Input.Directory + '/Gen Data Workspaces/' + Input.Start_Time(8:11) +'-' + Month_Number + '-' + Input.Start_Time(1:2) + '_' + Input.Start_Time(13:14) + '-' + Input.Start_Time(16:17) + '_Gen_Subsystem';
PE_File = Input.Directory + '/PE Data Workspaces/' + Input.Start_Time(8:11) +'-' + Month_Number + '-' + Input.Start_Time(1:2) + '_' + Input.Start_Time(13:14) + '-' + Input.Start_Time(16:17) + '_PE_Subsystem';
RO_File = Input.Directory + '/RO Data Workspaces/' + Input.Start_Time(8:11) +'-' + Month_Number + '-' + Input.Start_Time(1:2) + '_' + Input.Start_Time(13:14) + '-' + Input.Start_Time(16:17) + '_RO_Subsystem';

if strcmp(Input.WEC,'YES') == 1
    WEC_Data = load(WEC_File);
end
if strcmp(Input.Gen,'YES') == 1
    Gen_Data = load(Gen_File);
end
if strcmp(Input.PE,'YES') == 1
    PE_Data = load(PE_File);
end
if strcmp(Input.RO,'YES') == 1
    RO_Data = load(RO_File);
end

clear Month_Number WEC_File Gen_File PE_File RO_File

%% Complete Visualization Settings

if strcmp(Visualization.Range,'Custom') == 1
    fprintf('Visualization range set by user')

    Visualization.Start = datetime(Visualization.Start,TimeZone = 'UTC');
    Visualization.End = datetime(Visualization.End,TimeZone = 'UTC');
    
    if Visualization.End > (datetime(Input.Start_Time,TimeZone="UTC") + minutes(30))
        warning('User defined visualization end time not contained in selected data file(s). Visualization end time has been set to end of selected data file(s).')
        Visualization.End = datetime(Input.Start_Time,TimeZone="UTC") + minutes(30);
    end
    if Visualization.Start < datetime(Input.Start_Time,TimeZone="UTC")
        warning('User defined start time not contained in selected data file(s). Visualization start time has been set to start of selected data file(s).')
    end
elseif strcmp(Visualization.Range,'Default') == 1
    Visualization.Start = datetime(Input.Start_Time,TimeZone="UTC");
    Visualization.End = datetime(Input.Start_Time,TimeZone="UTC") + minutes(30);
end
%% WEC Plotting

if strcmp(Input.WEC,'YES') == 1
    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.LC_MOOR_1001)
    xlabel('Time')
    ylabel('Load [Lbf]')
    title('Anchor Load Cell')
    xlim([Visualization.Start Visualization.End])

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.vap_POS_OS_1001_UW_S)
    xlabel('Time')
    ylabel('Position [Degrees]')
    title('Encoder Position')
    xlim([Visualization.Start Visualization.End])

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.calc_POS_OS_1001_UW_S_V)
    xlabel('Time')
    ylabel('Velocity [rpm]')
    title('Encoder Velocity')
    xlim([Visualization.Start Visualization.End])

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.calc_Winch_V)
    xlabel('Time')
    ylabel('Velocity [rpm]')
    title('Winch Velocity')
    xlim([Visualization.Start Visualization.End]) 

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.calc_Gearbox_V)
    xlabel('Time')
    ylabel('Velocity [rpm]')
    title('Gearbox Output Velocity')
    xlim([Visualization.Start Visualization.End])

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.PRESS_OS_2002)
    xlabel('Time')
    ylabel('Pressure [psi]')
    title('Air Tank Pressure')
    xlim([Visualization.Start Visualization.End])

    figure; plot(WEC_Data.WEC_Datetime,WEC_Data.calc_P_abs)       
    xlabel('Time')
    ylabel('Power [kW]')
    title('Calculated Instantaneous Absorbed Power')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])
end

%% Generator Plotting

if strcmp(Input.Gen,'YES') == 1

    figure; plot(Gen_Data.Gen_Datetime,Gen_Data.PT_ON_1001)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(Gen_Data.Gen_Datetime,Gen_Data.CT_ON_1001)
    ylabel('Current [A]')
    title('Generator Phase 1')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(Gen_Data.Gen_Datetime,Gen_Data.PT_ON_1002)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(Gen_Data.Gen_Datetime,Gen_Data.CT_ON_1002)
    ylabel('Current [A]')
    title('Generator Phase 2')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(Gen_Data.Gen_Datetime,Gen_Data.PT_ON_1003)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(Gen_Data.Gen_Datetime,Gen_Data.CT_ON_1003)
    ylabel('Current [A]')
    title('Generator Phase 3')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(Gen_Data.Gen_Datetime,Gen_Data.calc_P_WEC_Electric_Out)
    xlabel('Time')
    ylabel('Power [W]')
    title('Generator Instantaneous Power')
    xlim([Visualization.Start Visualization.End])

end

%% Power Electronics Plotting

if strcmp(Input.PE,'YES') == 1    

    figure; plot(PE_Data.PE_Datetime,PE_Data.PT_ON_2001)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(PE_Data.PE_Datetime,PE_Data.CT_ON_2001)
    ylabel('Current [A]')
    title('Rectifier Output')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_1)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Rectifier Output Power')
    xlim([Visualization.Start Visualization.End])

    figure; plot(PE_Data.PE_Datetime,PE_Data.PT_ON_2002)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(PE_Data.PE_Datetime,PE_Data.CT_ON_2002)
    ylabel('Current [A]')
    title('Charge Controller Input')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_2)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Charge Controller Input Power')
    xlim([Visualization.Start Visualization.End])

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

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_3)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Charge Controller Output Power')
    xlim([Visualization.Start Visualization.End])

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_Eta_CC_I)
    xlabel('Time')
    ylabel('Efficiency [-]')
    title('Instantaneous Charge Controller Efficiency')
    xlim([Visualization.Start Visualization.End])

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

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_4)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Battery Input Power')
    xlim([Visualization.Start Visualization.End])

    figure; plot(PE_Data.PE_Datetime,PE_Data.PT_ON_2005)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(PE_Data.PE_Datetime,PE_Data.CT_ON_2005)
    ylabel('Current [A]')
    title('Pump Controller Input')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_5)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Pump Controller Input Power')
    xlim([Visualization.Start Visualization.End])

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

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_P1)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Pump 1 Input Power')
    xlim([Visualization.Start Visualization.End])

    figure; plot(PE_Data.PE_Datetime,PE_Data.PT_ON_2007)
    hold on
    xlabel('Time')
    ylabel('Voltage')
    yyaxis right
    plot(PE_Data.PE_Datetime,PE_Data.CT_ON_2007)
    ylabel('Current [A]')
    title('Pump 1 Input')
    legend('Voltage','Current')
    xlim([Visualization.Start Visualization.End])
    hold off

    figure; plot(PE_Data.PE_Datetime,PE_Data.calc_P_Electric_P2)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Pump 2 Input Power')
    xlim([Visualization.Start Visualization.End])

    if strcmp(Input.WEC,'YES') == 1 && strcmp(Input.Gen,'YES') == 1
        figure; plot(WEC_Data.WEC_Datetime,WEC_Data.calc_P_abs,Gen_Data.Gen_Datetime,Gen_Data.calc_P_WEC_Electric_Out,PE_Data.PE_Datetime,PE_Data.calc_P_Electric_1,PE_Data.PE_Datetime,PE_Data.calc_P_Electric_2,PE_Data.PE_Datetime,PE_Data.calc_P_Electric_3)
        xlabel('Time')
        ylabel('Power [kW]')
        title('HERO WEC Power')
        legend('Absorbed Power','Generator AC Power','Rectifier Output','Charge Controller Input','Charge Controller Output')
        xlim([Visualization.Start Visualization.End])
    end
end

%% RO Plotting

if strcmp(Input.RO,'YES') == 1

    figure; plot(RO_Data.RO_Datetime,RO_Data.FLOW_ON_1001)
    xlabel('Time')
    ylabel('Flow Rate [gpm]')
    title('RO Subsystem Feed Flow')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

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

    figure; plot(RO_Data.RO_Datetime,RO_Data.PRESS_ON_1001)
    xlabel('Time')
    ylabel('Pressure [psi]')
    title('RO Pre-Filter Pressure')
    xlim([Visualization.Start Visualization.End])

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

    figure; plot(RO_Data.RO_Datetime,RO_Data.calc_Filter_DP)
    xlabel('Time')
    ylabel('Pressure [psi]')
    title('Pressure Differential across Filter')
    xlim([Visualization.Start Visualization.End])

    figure; plot(RO_Data.RO_Datetime,RO_Data.calc_Acc_Flow)
    xlabel('Time')
    ylabel('Flow Rate [gpm]')
    title('Accumulator Flow')
    xlim([Visualization.Start Visualization.End])

    figure; plot(RO_Data.RO_Datetime,RO_Data.calc_P_Hydraulic_2,RO_Data.RO_Datetime,RO_Data.calc_P_Hydraulic_3)
    xlabel('Time')
    ylabel('Power [kW]')
    title('Hydraulic Power')
    legend('Hydraulic Power at RO Inlet','Hydraulic Power at Clark Pump Inlet')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

    figure; plot(RO_Data.RO_Datetime,RO_Data.FLOW_ON_1003)  
    xlabel('Time')
    ylabel('Flow Rate [gpm]')
    title('Brine Discharge Flow Rate')
    xlim([Visualization.Start Visualization.End])

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

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

    figure; plot(RO_Data.RO_Datetime,RO_Data.PRESS_ON_1002)
    hold on
    yyaxis left
    xlabel('Time')
    ylabel('Pressure [psi]')
    title('RO Feed Pressure and Permeate Production')
    yyaxis right
    plot(RO_Data.RO_Datetime,RO_Data.FLOW_ON_1004)
    ylabel('Flow Rate [gpm]')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])
    legend('Pressure at Clark Pump', 'Permeate Flow')
    hold off

    figure; plot(RO_Data.RO_Datetime,RO_Data.calc_Recovery_Ratio)
    xlabel('Time')
    ylabel('Recovery Ratio [-]')
    title('RO Recovery Ratio')
    xlim([Visualization.Start Visualization.End])
    ylim([0 inf])

    figure; plot(RO_Data.RO_Datetime,RO_Data.CND_ON_1001)
    xlabel('Time')
    ylabel('Conductivity [Micro-Siemens]')
    title('RO Inlet Conductivity')
    xlim([Visualization.Start Visualization.End])

    figure; plot(RO_Data.RO_Datetime,RO_Data.CND_ON_1002)
    xlabel('Time')
    ylabel('Conductivity [Micro-Siemens]')
    title('Permeate Conductivity')
    xlim([Visualization.Start Visualization.End])

end