function [z,y] = Measurement_MESC_Ver1(root,y0,p0,d_nom,d,u,p,par,N,K_S,K_M)

% This function calculates the measurements and the aux z = [Tin1; Tin2; 
% Tin3; TexHEx; cin1; cin2; cin3; u; cSset; cM]; for MESC
%
% The output is given by
%       y           Measurements of the systems
%       z           Auxiliary vector z = [Tin1; Tin2; Tin3; TexHEx; cin1; 
%                   cin2; cin3; u; cSset; cM];
%
% The required input of this function is given as
%       root        Dynamic state vector of the system
%       y0          Initial values for the vector of measurements
%       d_nom       Vector containing our inlet variables to the system
%       u           Manipulated variable of the system (split ratios)
%       p0          Initial values for parameter vector
%       p           Parameter vector
%       par         Parameters of the model
%       N           Total number of descritization steps
%       KS:         Slave controller tuning parameters (not needed)
%       KM:         Master controller tuning parameters (not needed)
%       H:          Structure containing the different H matrices (not
%                   needed)
%       H_Ver:      Integer for the selected H matrix (not needed)

% Definition of feed variables
in = d_nom+d;          % Inlet values (m, p, T, c, H2/N2)

if exist('K_S','var')
    % Assignment of the differential and algebraic states
    T     = root(1:N);
    eSint = root(N+1:N+3);
    wNH3  = root(N+4:2*N+3);
    
    % Calculation of the controller action
    u = max(p0(1:3) + K_S.I.*eSint,0.0001);
    
    % SOC Master controller is used
    if exist('K_M','var')
        % Assignment of the differential and algebraic states
        eMint = root(N+4:N+6);
        wNH3  = root(N+7:2*N+6);
        cM    = p(1:3);
    end

else
    % Redefinition of the temperatures
    T    = root(1:N);
    wNH3 = root(N+1:2*N);
end

% Redefinition of massflows
mS1 = u(1)*in(1);
mS2 = u(2)*in(1);
mS3 = u(3)*in(1);
mSHEx = (1-u(1)-u(2)-u(3))*in(1);

% Recalculation of the descritizations per bed
N1 = N/3;
N2 = N/3;
N3 = N/3;

% Calculation of the Heat Exchanger outlet temperature
CStar = (1-u(1)-u(2)-u(3));
NTU   = par.UA/(mSHEx*par.Cp);
eps   = (1-exp(-NTU*(1-CStar)))/(1-CStar*exp(-NTU*(1-CStar)));
TexHEx = (eps*T(N)+(1-eps)*in(3));

% Reactor 1 inlet temperature
m1 = mS1+mSHEx;
Tin1 = (mSHEx*TexHEx + mS1*in(3))/m1;
cin1 = in(4);

% Reactor 2 inlet temperature
m2 = mS2+m1;
Tin2 = (m1*T(N1) + mS2*in(3))/m2;
cin2 = (m1*wNH3(N1) + mS2*in(4))/m2;

% Reactor 3 inlet temperature
m3 = in(1);
Tin3 = (m2*T(N1+N2) + mS3*in(3))/m3;
cin3 = (m2*wNH3(N1+N2) + mS3*in(4))/m3;

if exist('K_S','var')
    % Definition of the measurement vector   
    z = [Tin1; Tin2; Tin3; TexHEx; cin1; cin2; cin3; u];
    y = [z(4); z(1); T(N1); z(2); T(N1+N2); z(3); T(N)]; 

    % SOC Master controller is used
    if exist('K_M','var')      
        cMset = [0;0;0];
        
        % Calculation of the integral error
        deM_intdt = cMset-cM;

        % Calculation of the controller action
        cSset = (y0(2:4) + K_M.p .*deM_intdt + K_M.I .*eMint);
        
        z = [Tin1; Tin2; Tin3; TexHEx; cin1; cin2; cin3; u; cSset; cM];
        y = [z(4); z(1); T(N1); z(2); T(N1+N2); z(3); T(N)]; 
    end
    
else
    % Definition of the measurements
    z = [Tin1; Tin2; Tin3; TexHEx; cin1; cin2; cin3];
    y = [z(4); z(1); T(N1); z(2); T(N1+N2); z(3); T(N)];
end
