function tpwsData = region1_calc(p,T)
% Description:      Calculates the thermodynamic properties of water based
%                   on the IAPWS IF-97 Release for Region 1
% -------------------------------------------------------------------------
% Syntax:           tpwsData = region1_calc(p,T)
%                   p           Pressure [MPa]
%                   T           Temperature [K]
% -------------------------------------------------------------------------
% Output:           tpwsData    Structure containing all relevant
%                               properties

Pi = p / 16.53;
tau = 1386 / T;
R = 0.461526;

[I,J,n] = region1_gfe;

gamma = 0;
gamma_pi = 0;
gamma_pi_pi = 0;
gamma_tau = 0;
gamma_tau_tau = 0;
gamma_pi_tau = 0;

for k = 1:34
    gamma = gamma + n(k) * (7.1 - Pi)^(I(k)) * (tau - 1.222)^(J(k));
    gamma_pi = gamma_pi + -n(k) * I(k) * (7.1 - Pi)^(I(k) - 1) * (tau - 1.222)^(J(k));
    gamma_pi_pi = gamma_pi_pi + n(k) * I(k) * (I(k) - 1) * (7.1 - Pi)^(I(k) - 2) * (tau - 1.222)^(J(k));
    gamma_tau = gamma_tau + n(k) * (7.1 - Pi)^(I(k)) * J(k) * (tau - 1.222)^(J(k) - 1);
    gamma_tau_tau = gamma_tau_tau + n(k) * (7.1 - Pi)^(I(k)) * J(k) * (J(k) - 1) * (tau - 1.222)^(J(k) - 2);
    gamma_pi_tau = gamma_pi_tau + -n(k) * I(k) * (7.1 - Pi)^(I(k) - 1) * J(k) * (tau - 1.222)^(J(k) - 1);
end

tpwsData.T = T;
tpwsData.p = p;
tpwsData.v = Pi * gamma_pi * R * T / p / 1000;
tpwsData.u = (tau * gamma_tau - Pi * gamma_pi) * R * T;
tpwsData.s = (tau * gamma_tau - gamma) * R;
tpwsData.h = tau * gamma_tau * R * T;
tpwsData.c_p = -tau * tau * gamma_tau_tau * R;
tpwsData.c_v = (-tau * tau * gamma_tau_tau + (gamma_pi - tau * gamma_pi_tau)^2 / gamma_pi_pi) * R;
tpwsData.state = 'Liquid';