% Analyze Water Flow 4213 QDOG 3
Load_rtplt(3); % Full Magnet
Load_rtplt(4); % Top Right Magnet
Load_rtplt(5); % Top Left Magnet
Load_rtplt(6); % Bottom Left Magnet
Load_rtplt(7); % Bottom Right Magnet
% Load_rtplt(8); % Full Magnet

figure('units','normalized','outerposition',[0 0 0.9 0.9]);
plot(RT3.Flow, mm(1));
% hold
% plot(RT8.Flow, mm(2));
xlabel('Measurement #');
ylabel('Flow Rate (gpm)')
title('Full Magnet Flow Rate of LCLS-II Quad 4213 - QDOG3');
hline(mean(RT3.Flow), 'b--',['Beginning Flow = ', num2str(mean(RT3.Flow)), ' gpm']);
hline(mean(RT3.Flow) + std(RT3.Flow),'b', 'mean + std');
hline(mean(RT3.Flow) - std(RT3.Flow),'b','mean - std');

% hline(mean(RT8.Flow), 'r :', ['End Flow = ', num2str(mean(RT8.Flow)), ' gpm']);
% hline(mean(RT8.Flow) + std(RT8.Flow),'r--', 'mean + std');
% hline(mean(RT8.Flow) - std(RT8.Flow),'r--','mean - std');

% legend('Before', 'After')

figure('units','normalized','outerposition',[0 0 0.9 0.9]);
plot(RT4.Flow, mm(1));
hold
plot(RT5.Flow, mm(2));
plot(RT6.Flow, mm(3));
plot(RT7.Flow, mm(4));
xlabel('Measurement #');
ylabel('Flow Rate (gpm)')
title({'Individual Coil Magnet Flow Rate of LCLS-II Quad 4213 - QDOG3'});
legend('Top Right', 'Top Left', 'Bottom Left', 'Bottom Right')
saveas(gcf,'Individual Coil Magnet Flow Rate of LCLS-II Quad 4213 - QDOG3.png');

sum_of_coils = sum([mean(RT4.Flow), mean(RT5.Flow), mean(RT6.Flow), mean(RT7.Flow)]);
sum_of_coils_err = sum([std(RT4.Flow), std(RT5.Flow), std(RT6.Flow), std(RT7.Flow)]);

% Write summary file with the fuull manget and individual flows. 
   
  data_file = 'Magnet Flow Rate of LCLS-II Quad 4213 - QDOG3.txt';   
    f_title = '                      Magnet Flow Rate of LCLS-II Quad 4213 - QDOG3 \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(RT3.Flow), mean(RT4.Flow), mean(RT5.Flow), mean(RT6.Flow), mean(RT7.Flow), sum_of_coils);                
fprintf(fidC,'  %5.3f          %5.3f           %5.3f            %5.3f              %5.3f              %5.3f ', std(RT4.Flow), std(RT4.Flow), std(RT5.Flow), std(RT6.Flow), std(RT7.Flow), sum_of_coils_err);                
fclose(fidC);