function [tpwsData, tpwsData2] = tpws(p,T,varargin)
% Description:      Calculates the properties of water and steam based on
%                   the IAPWS IF-97 release as a function of temperature
%                   and pressure.  Thermodynamic properties are returned in
%                   a data structure and displayed in a table (optional).
%                   The units used by this application must be stored in
%                   tpws_SetUnit.m.
% -------------------------------------------------------------------------
% Syntax:           [tpwsData, tpwsData2] = tpws(p,T,dispTable)
%                   p           Pressure [MPa]
%                   T           Temperature [K]
%                   dispTable   Optional Parameter to disable the table 
%                               display of thermodynamic properties. By 
%                               default, the table feature is enabled.
%                                   0 = Disable table display
%                                   Note: No action necessary to enable 
%                                   table display
% -------------------------------------------------------------------------
% Output:           tpwsData    Data structure for the properties of water
%                   tpwsData2   Data structure for the vapor phase 
%                               when the water is saturated (otherwise, no
%                               data is stored in this parameter)
% -------------------------------------------------------------------------
% Parameters:       unitSet     Name of desired unit set. Must be defined
%                               tpws_SetUnit.m for functionality.
% -------------------------------------------------------------------------
% Example:          [DataA, DataB] = tpws(5,100);
%                   Calculates the properties of water at a pressure of 5
%                   and temperature of 100 (units are defined in the
%                   variable tpwsUnits). Thermodynamic properties are
%                   stored in the structures DataA and Data B.  Results are
%                   also displayed in a table.
%
%                   [DataA, DataB] = tpws(5,100,0);
%                   Performs the same operation as before but disables the
%                   table display

% Name of desired unit set
unitSet = 'iapws';

% Initialize the data structure
tpwsData = tpws_IntData;
tpwsData2 = 'No Data';
satData = 'No';

% Set the units
tpws_SetUnit(unitSet);

% Insert the user parameters into the data structure
tpwsData.p = p;
tpwsData.T = T;

% Convert the input data into IAPWS units
tpwsStandard = tpws_ConvuStand(tpwsData);

% Compute the properties of water
switch tpws_RegionSelect(tpwsStandard.p, tpwsStandard.T)
    case {1}
        tpwsStandard = region1_calc(tpwsStandard.p, tpwsStandard.T);
    case {2}
        tpwsStandard = region2_calc(tpwsStandard.p, tpwsStandard.T);
    case {3}
        % Iterate to determine rho at p
        rho = bisection('region3_calc(x,y)', tpwsStandard.T, 'p', tpwsStandard.p, 0, 1500, 0.000001, 10000);        
        tpwsStandard = region3_calc(rho, tpwsStandard.T);
    case {4}
        satData = 'Yes';
        [tpwsStandard, tpwsStandard2] = region4_calc(tpwsStandard.p, tpwsStandard.T);
    case {5}
        tpwsStandard = region5_calc(tpwsStandard.p, tpwsStandard.T);
    otherwise
        display('***ERROR: No Valid Data is available for these conditions!***')
end

% Convert the water data into the chosen units
switch satData
    case {'No'} 
        tpwsData = tpws_Convu(tpwsStandard);
    otherwise
        tpwsData = tpws_Convu(tpwsStandard);
        tpwsData2 = tpws_Convu(tpwsStandard2);
end

% Display the properties of water and steam in table format
if (length(varargin) == 0) 
    switch satData
        case {'No'} 
            tpws_Display(tpwsData);
        otherwise
            tpws_Display(tpwsData,tpwsData2);
    end
end