%Function for loading data and plotting. 
%To be run using make_plot.m

%Inputs: 
%     file: string contanining filename
%     starttime: time where plotting should be started
%     endtime: time where plotting should be finished
%     par: containing parameters for span of y-axis
%     opt: if opt == X, figure vertical size will be screensize/X
%       else == screensize
%     opt2: if opt2 == Y, figure horrisontal size will be screensize/Y
%       else == screensize
%
% Written by: Knut Åge Meland
% Email: knutagemeland@gmail.com
        
function [t, P1, P2, P3, P4, dp] = plotter(file, starttime, endtime, par, opt, opt2)

if nargin == 4;
    opt = 0;
    opt2 =0;
end
if nargin ==5;
    opt2=0;
end
var = load(file);
akser=[
        0, endtime-starttime, par.Air_span ;
        0, endtime-starttime, par.Water_span ; 
        0, endtime-starttime, par.P1_span ; 
        0, endtime-starttime, par.P2_span ;
        0, endtime-starttime, par.P3_span ;
        0, endtime-starttime, par.P4_span ; 
        0, endtime-starttime, par.dp_span ;   
        0, endtime-starttime, par.slug_span ;   
        0, endtime-starttime, par.sp_span ;   
        0, endtime-starttime, par.valve_span ; 
            ];
          
t=var(:,1);
temp=find(starttime<t& t<endtime);
t=t(temp,:)-starttime;

q_air=var(temp,3);
q_liq=var(temp,4);
P1=var(temp,5);
P2=var(temp,6);
P3=var(temp,7);
P4=var(temp,8);
dp=var(temp,9);
slug=var(temp,10);
sp=var(temp,11);
valve=var(temp,12);

%% Plot

% figure(1)
% plot(t,q_air)
% %title('Air flowrate')
% xlabel('Time[s]')
% ylabel('Flowrate[l/min]')
% axis(akser(1,:))
% 
% figure(2)
% plot(t,q_liq)
% title('Liquid flowrate')
% xlabel('Time[s]')
% ylabel('Flowrate[l/min]')
% axis(akser(2,:))
% 
% figure(3)
figur_setup(opt)
plot(t,P1)
% title('Pressure at the mixingpoint (P1)')
hold on
plot(t,sp,'--black')
hold off
legend('P1','SP')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')
axis(akser(3,:))
% 
% figure(4)
% plot(t,P2)
% title('Pressure topside (P2)')
% xlabel('Time[s]')
% ylabel('Gauge pressure[kPa]')
% axis(akser(4,:))
%
%figure(5)
% figur_setup(opt)
% plot(t,P3,'-g')
% %title('Pressure in the buffertank (P3)')
% xlabel('Time[s]')
% ylabel('Gauge pressure[kPa]')
% hold on
% plot(t,sp,'--black')
% axis(akser(5,:))
% legend('P3','SP')
% hold off

%figure(6)
% figur_setup(opt)
% plot(t,P4)
% % title('Pressure at the mixingpoint (P4)')
% hold on
% plot(t,sp,'--black')
% hold off
% legend('P4','SP')
% xlabel('Time[s]')
% ylabel('Gauge pressure[kPa]')
% axis(akser(6,:))


figure(7)
plot(t,dp)
title('DP-cell')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')
axis(akser(7,:))

figure(8)
plot(t,slug)
title('Slug sensor')
xlabel('Time[s]')
ylabel('Slug sensor signal [V]')
axis(akser(8,:))

figure(9)
plot(t,sp)
title('setpoint')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')
axis(akser(9,:))

% figure(10)
% plot(t,valve)
% title('Valve position')
% xlabel('Time[s]')
% ylabel('Valve position [%]')
% axis(akser(10,:))

%% subplott

figur_setup(opt)

%subplot(2,2,1:2)
plot(t,P1)
hold on
plot(t,P2,'--r')
plot(t,P3,':g')
%plot(t,P4, '-cyan')
% plot(t,dp,'-b')
plot(t,sp,'-.black')

axis(akser(3,:))
legend('P1','P2','P3','SP','Location','NorthWest')
xlabel('Time[s]')
ylabel('Gauge pressure[kPa]')
hold off

%subplot(2,2,3)
figur_setup(opt, opt2)
plot(t,q_liq,'-b')
hold on
plot(t,q_air,'--r')
hold off
legend('Water', 'Air','Location','NorthWest')
xlabel('Time[s]')
ylabel('Flowrate[l/min]')
axis(akser(2,:))

%subplot(2,2,4)
figur_setup(opt, opt2)
plot(t,valve)
xlabel('Time[s]')
ylabel('Valve position [%]')
axis(akser(10,:))



