% Analyze Water Flow
Load_rtplt(2); % Full Magnet
Load_rtplt(3); % Full Magnet
Load_rtplt(4); % Top Right Coil
Load_rtplt(5); % Top Right Coil
Load_rtplt(6); % Top Left Coil
Load_rtplt(7); % Bottom Left Coil
Load_rtplt(8); % Bottom Left Coil
Load_rtplt(9); % Bottom Right Coil
Load_rtplt(10); % Bottom Right Coil
Load_rtplt(13); % Top Right Coil After Flushing 
Load_rtplt(14); % Bottom Left After Flushing 
Load_rtplt(15); % Full Magnet. After flushing

figure('units','normalized','outerposition',[0 0 0.9 0.9]);
plot(RT2.Flow, mm(1));
hold
plot(RT15.Flow, mm(2));
xlabel('Measurement #');
ylabel('Flow Rate (gpm)')
title('Full Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6');
hline(mean(RT2.Flow), 'b--',['Before Flushing Flow = ', num2str(mean(RT2.Flow)), ' gpm']);
hline(mean(RT2.Flow) + std(RT2.Flow),'b', 'mean + std');
hline(mean(RT2.Flow) - std(RT2.Flow),'b','mean - std');

hline(mean(RT15.Flow), 'r :', ['After Flushing Flow = ', num2str(mean(RT15.Flow)), ' gpm']);
hline(mean(RT15.Flow) + std(RT15.Flow),'r--', 'mean + std');
hline(mean(RT15.Flow) - std(RT15.Flow),'r--','mean - std');

legend('Before', 'After')

saveas(gcf,'Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6.png');

figure('units','normalized','outerposition',[0 0 0.9 0.9]);
plot(RT13.Flow, mm(1));
hold
plot(RT6.Flow, mm(2));
plot(RT14.Flow, mm(3));
plot(RT9.Flow, mm(4));
xlabel('Measurement #');
ylabel('Flow Rate (gpm)')
title('Individual Coil Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6');
legend('Top Right', 'Top Left', 'Bottom Left', 'Bottom Right')
saveas(gcf,'Individual Coil Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6.png');


sum_of_coils = sum([mean([RT4.Flow;RT5.Flow]), mean(RT6.Flow), mean([RT7.Flow;RT8.Flow]), mean([RT9.Flow;RT10.Flow])]);
sum_of_coils_err = sum([std([RT4.Flow;RT5.Flow]), std(RT6.Flow), std([RT7.Flow;RT8.Flow]), std([RT9.Flow;RT10.Flow])]);

sum_of_coils_flush = sum([mean(RT13.Flow), mean(RT6.Flow), mean(RT14.Flow), mean([RT9.Flow;RT10.Flow])]);
sum_of_coils_err_flush = sum([std(RT13.Flow), std(RT6.Flow), std(RT14.Flow), std([RT9.Flow;RT10.Flow])]);


% Write summary file with the full manget and individual flows. 
   
  data_file = 'Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6.txt';   
    f_title = '                      Magnet Flow Rate of LCLS-II Quad 4215 - QDOG6 \n\n' ;
    
fidC = fopen(data_file,'w');
fprintf(fidC,f_title);

fprintf(fidC,'Full Magnet   Top Right Coil  Top Left Coil   Bottom Left Coil   Bottom Right Coil   Sum of Coils \n');
fprintf(fidC,'  (gpm)          (gpm)           (gpm)            (gpm)               (gpm)              (gpm) \n');
fprintf(fidC,'  %5.3f          %5.3f           %5.3f            %5.3f               %5.3f              %5.3f\n', mean(RT2.Flow), mean([RT4.Flow;RT5.Flow]), mean(RT6.Flow), mean([RT7.Flow;RT8.Flow]), mean([RT9.Flow;RT10.Flow]), sum_of_coils);                
fprintf(fidC,'  %5.3f          %5.3f           %5.3f            %5.3f               %5.3f              %5.3f \n', std(RT2.Flow), std([RT4.Flow;RT5.Flow]), std(RT6.Flow), std([RT7.Flow;RT8.Flow]), std([RT9.Flow;RT10.Flow]), sum_of_coils_err);                
fprintf(fidC,'                                            After Flushing \n');
fprintf(fidC,'  %5.3f          %5.3f                            %5.3f                                  %5.3f \n', mean(RT15.Flow), mean(RT13.Flow), mean(RT14.Flow), sum_of_coils_flush);                
fprintf(fidC,'  %5.3f          %5.3f                            %5.3f                                  %5.3f ', std(RT15.Flow), std(RT13.Flow), std(RT14.Flow), sum_of_coils_err_flush);                
fclose(fidC);
