% Seting up File Name Variables
FileNameBase = 'Data_file-XX.dat';
stFile = 1;
endFile = 299; 

dateSt = datetime(2022,08,16,16,20,0);  %start date and time in UTC

matrixLength = 30000*(endFile-stFile+1)
ShortFiles = [];
sampleRate = 50; %Hz

PowData = zeros(matrixLength,7);
Time = zeros(matrixLength,1);
aa = 1;
for FileIndx = stFile:endFile
    FileIndx
    OpenFileName = strrep(FileNameBase,'XX',num2str(FileIndx));
    tempData = csvread(OpenFileName,4,1);
    tempDataLen=length(tempData);
    ShortFiles = [ShortFiles FileIndx];
    PowData(aa:aa+tempDataLen-1,:) = tempData;
    Time(aa:aa+tempDataLen-1) = (0:(tempDataLen-1))/sampleRate + 600*(FileIndx-1); 
    
    aa = aa + tempDataLen;
end

waveData = csvread('WaveData_CSIJennettes_Sofar.csv',1,0);
waveTime = datetime(waveData(:,4), 'convertfrom', 'posixtime', 'Format', 'MM/dd/yy HH:mm:ss.SSS');

PowData = PowData(1:aa-30000,:);
Time = Time(1:aa-30000,:);
dateNet = dateSt+Time/3600/24;

clearvars FileIndx FileNameBase OpenFileName aa endFile matrixLength stFile tempData tempDataLen;

DesalData.Time = Time;
DesalData.Date = dateNet;
DesalData.CTGen = PowData(:,2);
DesalData.CTDC = PowData(:,3);%-1.8;
DesalData.Flow = PowData(:,4)-0.027;
DesalData.PTGen = PowData(:,5);
DesalData.PTDC = PowData(:,6);
DesalData.Press = PowData(:,7);


figure(1); 
    dcAx(1) = subplot(2,1,1); 
    plot(DesalData.Date,DesalData.CTDC);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[0,120],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[0,120],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[0,120],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[0,120],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[0,120],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[0,120],'linewidth',1,'color','m');
    ylabel('DC Current (A)');
    
    dcAx(2) = subplot(2,1,2); 
    plot(DesalData.Date,DesalData.PTDC);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[0,70],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[0,70],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[0,70],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[0,70],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[0,70],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[0,70],'linewidth',1,'color','m');
    ylabel('DC Voltage (V)');
   
    linkaxes(dcAx, 'x');
    xlabel('Date and Time (UTC)')

figure(2); 
    watAx(1) = subplot(2,1,1); 
    plot(DesalData.Date,DesalData.Press);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[-20,160],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[-20,160],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[-20,160],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[-20,160],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[-20,160],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[-20,160],'linewidth',1,'color','m');
    ylabel('Pressure (psi)'); 
    
    watAx(2)= subplot(2,1,2); 
    plot(DesalData.Date,DesalData.Flow);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[-2,10],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[-2,10],'linewidth',1,'color','r')
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[-2,10],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[-2,10],'linewidth',1,'color','g'); 
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[-2,10],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[-2,10],'linewidth',1,'color','m');
    ylabel('Flow (gal/min)');
    
    linkaxes(watAx, 'x');
    xlabel('Date and Time (UTC)')

figure(3); 
    genAx(1) = subplot(2,1,1); 
    plot(DesalData.Date,DesalData.CTGen);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[-40,40],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[-40,40],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[-40,40],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[-40,40],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[-40,40],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[-40,40],'linewidth',1,'color','m');
    ylabel('AC Generator Current (A)'); 
    
    genAx(2)=subplot(2,1,2);
    plot(DesalData.Date,DesalData.PTGen);
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[-150,150],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[-150,150],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[-150,150],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[-150,150],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[-150,150],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[-150,150],'linewidth',1,'color','m');
    ylabel('AC Generator Voltage (V)');
    
    linkaxes(genAx, 'x');
    xlabel('Date and Time (UTC)')

DesalData.watProd = cumsum(DesalData.Flow./sampleRate);
DesalData.CTDC(find(isnan(DesalData.CTDC))) = 0;
DesalData.dcPow = DesalData.CTDC.*DesalData.PTDC;
DesalData.netDCEng = cumsum(DesalData.dcPow./sampleRate)/3600;

figure(4); 
    netProd(1) = subplot(4,1,1); 
    plot(DesalData.Date,DesalData.dcPow);grid;
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[0,4000],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[0,4000],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[0,4000],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[0,4000],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[0,4000],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[0,4000],'linewidth',1,'color','m');
    line([datetime(2022,08,17,19,20,0), datetime(2022,08,17,19,56,0)],[3900,3900],'linewidth',8,'color','r');
    line([datetime(2022,08,17,19,56,0), datetime(2022,08,17,20,48,0)],[3900,3900],'linewidth',8,'color','g');
    line([datetime(2022,08,17,20,48,0), datetime(2022,08,18,20,48,0)],[3900,3900],'linewidth',8,'color','b');
    
    ylabel('DC Power (W)')
    
    netProd(2) = subplot(4,1,2);
    plot(DesalData.Date,DesalData.netDCEng);grid;
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[0,3000],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[0,3000],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[0,3000],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[0,3000],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[0,3000],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[0,3000],'linewidth',1,'color','m');
    ylabel('DC Energy (Wh)')
    
    netProd(3) = subplot(4,1,3);
    plot(DesalData.Date,DesalData.watProd);grid;
    line([DesalData.Date(78705*50),DesalData.Date(78705*50)],[0,30000],'linewidth',1,'color','r');
    line([DesalData.Date(87747*50),DesalData.Date(87747*50)],[0,30000],'linewidth',1,'color','r');
    line([DesalData.Date(105019*50),DesalData.Date(105019*50)],[0,30000],'linewidth',1,'color','g');
    line([DesalData.Date(112096*50),DesalData.Date(112096*50)],[0,30000],'linewidth',1,'color','g');
    line([DesalData.Date(153578*50),DesalData.Date(153578*50)],[0,30000],'linewidth',1,'color','m');
    line([DesalData.Date(159943*50),DesalData.Date(159943*50)],[0,30000],'linewidth',1,'color','m');
    ylabel('Cumulative Water Flow (gal)')
    
    netProd(4) = subplot(4,1,4);
    yyaxis left
    plot(waveTime,waveData(:,5));grid;
    ylabel('Significant Wave Height (m)')
    
    yyaxis right
    plot(waveTime,waveData(:,6));grid;
    ylabel('Peak Period (s)')
    
    linkaxes(netProd,'x');
    xlabel('Date and Time (UTC)')




