function region = tpws_RegionSelect(p,T)
% Description:      Determines the region of the data point at p and T
%                   based upon the IAPWS IF-97 Release
% -------------------------------------------------------------------------
% Syntax:           region = tpws_RegionSelect(p,T)
%                   p           Pressure [MPa]
%                   T           Temperature [K]
% -------------------------------------------------------------------------
% Output:           region      Corresponding Region of the (p,T) point
% -------------------------------------------------------------------------
% Parameters:       satTol      Maximum allowable difference between the
%                               user input pressure and the corresponding
%                               saturation pressure (at T) to determine if
%                               the system is in Region 4 [MPa]

% Saturaton Tolerance
satTol = 0.0001;

if (T >= 1073.15) & (T <= 2273.15) & (p > 0) & (p <= 10) 
    region = 5;             
elseif (T > 273.15) & (T <= 1073.15) & (p > 0) & (p <= 100)
    if (T <= 623.15)
        if (abs(p - region4_pcalc(T)) < satTol)
            region = 4;     
        elseif (region4_pcalc(T) < p)
            region = 1;         
        else
            region = 2;     
        end
    else
        if (p > region23_pcalc(T))
            region = 3;
            if (T <= 647.096)
                if (abs(p - region4_pcalc(T)) < satTol)
                    region = 4;
                end
            end
        else
            region = 2;
        end                  
    end   
else
    region = NaN;
end