%%plot_TempProfile
t_plot=size(t,1); %plot teperatureprofile at tend
%t_plot=108; %plot temperatureprofile at t_plot instead of tend
time=['time:' num2str(t(t_plot))];
%% Get Temperature profile
Temp_pre=get_Temperature(coldata.stages_pre,[1 t_plot],x);
Temp_main=get_Temperature(coldata.stages_main,[1 t_plot],x);

%% get position and value of Temperature measurepoints
%numbered from 1 to 40 in Prefrac and 
%from 1 to 80 in main column

%stages_temperature_short:
%row 1: section
%row 2: stage (within prefrac (1-40) or main column (1-80)
%row 3: start Temp
%row 4: end Temp
%row 5: Setpoint Start/nominal
%row 6: Setpoint End

stages_Temperature_short=zeros(6,size(stages_Temperature,2));

%get section and stage of each measurement point
for k=1:size(stages_Temperature,2), %Temp stage loop  
    if stages_Temperature(k)<=2*sum(coldata.Nt(1:2))
        for kk=1:2,  % pre section loop
            if 2*sum(coldata.Nt(1:kk))>=stages_Temperature(k)
                stages_Temperature_short(1,k)=kk;
                stages_Temperature_short(2,k)=stages_Temperature(k)-sum(coldata.Nt(1:kk))+coldata.Nt(kk);
                break
            end
        end
    elseif stages_Temperature(k)>2*sum(coldata.Nt(1:2))
        for kk=3:6,  % main section loop
            if 2*sum(coldata.Nt(1:kk))>=stages_Temperature(k)
                stages_Temperature_short(1,k)=kk;
                stages_Temperature_short(2,k)=stages_Temperature(k)-sum(coldata.Nt(1:kk-1))-sum(coldata.Nt(1:2));
                break
            end
        end
    else
        disp('Error');
    end
end
stages_Temperature_short(3:4,:)=get_Temperature(stages_Temperature,[1 t_plot],x);
stages_Temperature_short(5,:)=Temp_nom; %calculated in init file from x_nom
if size(UU,2)>15,
    stages_Temperature_short(6,:)=UU(end,[16 19 17 18 20 21]);
end

%% Plot

figure
%Prefrac
subplot(2,1,1)
hold all
plot(Temp_pre(1,:)) %Plot Profile
plot(Temp_pre(2,:)) %Plot Profile
old_limits = axis;
%Plot points
for k=1:6 % check all temp. measure points if they are in the prefrac
   if stages_Temperature_short(1,k)<=2
       plot(stages_Temperature_short(2,k),stages_Temperature_short(5,k),'bd','MarkerFaceColor','b'); %nominal Setpoint
       plot(stages_Temperature_short(2,k),stages_Temperature_short(6,k),'rd','MarkerFaceColor','r'); %latest Setpoint
       text(stages_Temperature_short(2,k),min(stages_Temperature_short(5:6,k))-3,stages_Temperature_name(k));
   end
end
axis([1, 40, old_limits(3), old_limits(4)]);
plot([20 20],[50 200],':k') %plot Sectionborder
legend('nominal','end','Location','Southeast')
xlabel('stage')
ylabel('Temperature [°C]')
title('prefractionator')

%Main
subplot(2,1,2)
hold all
plot(Temp_main(1,:)) %Plot Profile
plot(Temp_main(2,:)) %Plot Profile
old_limits = axis;
%Plot Points
for k=1:6 % check all temp. measure points if they are in the main column
   if stages_Temperature_short(1,k)>2
       plot(stages_Temperature_short(2,k),stages_Temperature_short(5,k),'bd','MarkerFaceColor','b');
       plot(stages_Temperature_short(2,k),stages_Temperature_short(6,k),'rd','MarkerFaceColor','r');
       text(stages_Temperature_short(2,k),min(stages_Temperature_short(5:6,k))-6,stages_Temperature_name(k));
   end
end
axis([1, 80, old_limits(3), old_limits(4)]);
%plot section border
plot([20 20],[50 200],':k')
plot([40 40],[50 200],':k')
plot([60 60],[50 200],':k')
legend('nominal','end','Location','Southeast')
xlabel('stage')
ylabel('Temperature [°C]')
title('main column')

%q=mtit(time,'fontsize',14,'xoff',0.3);
