Assignment 08

Reading Properties and Interpolation

Assignment

Using the steam saturation properties table available at https://opentext.lib.vt.edu/thermodynamics/tables/  create a function that allows the user to pass in a temperature and gives an output report for

Temperature (C):  Temp Input by User
Pressure (MPa):
Volume (m^3/kg):
Energy (kJ/kg):
Enthalpy (kJ/kg):
Entropy (kJ/Kg K):

use vf, uf, hf, and sf values from the table for all property outputs.

Your function for output the correct value for each property interpolating when necessary and should also output a warning if the entered Temperature is outside the range of the table values.

You will demonstrate the output by using showing the answers for 75.5 C (interpolation required) in your report. 

The link to the tables is below. 

Information

The table can be read directly from the html by the use of the pandas function read_html with the URL of the page. Use of the header property in the read_html function will allow you to minimize the amount of work necessary to import the table correctly.

The return value from the read_html function will be a list that will include all tables that are in the page. In this case there should be only one table. Documentation for read_html is  at

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_html.html?highlight=pandas%20read_html#pandas.read_html 

Note: Now that you can read about every type of data source you have real power.

About Dataframes

The read_html function in pandas will return the tables it finds on a web page as a list of dataframes. It returns a list because there might be more than one table on the page, and it does return them all. A dataframe is a pandas object capable of holding the entirety of an html table. This is great, but often tables contain headers and other information outside of the data that we want. Here is a function that will go to a url, and return the first table on the page as a dataframe.

import pandas 

def get_data_from_webpage(url):
    # This returns a list of dataframes
    # if there are multiple tables in html, we would need multiple dataframes
    # If there is one table in html, we only need one
    t_table = pandas.read_html(url, header=0)
    # Element 0 is the DataFrame we want
    return t_table[0]

This is great – but the returned dataframe may not be terribly useful to you in calculations until you convert it to an array of numbers. Here is a function that will take a dataframe (df), take the column specified by col_name, remove the first value (which in our example is a text string of the units), and return it as an array of floats. The code here could be very useful to you later on – so you should get an understanding of the code.

def get_float_array_from_dataframe(df, col_name):
    # First we will get the column in col_name
    col = df[col_name]
    # Next we have to strip off the first row of text
    col = col.drop(col.index[0])
    # We can then return it as an array -
    # col.values is an array of strings, the following code
    # converts all of the values to float
    return [float(x) for x in col.values]

With these 2 functions, you can now read the table from a web page and easily bring back any column (by name) as a numeric array. I will leave it up to you to be able to interpolate the values of the other columns given a Temperature.

PS – I especially like the last line of code [float(x) for x in col.values] – it returns the values in col.values as an array of float. The [ ] brackets ensure that the return value is an array. The internal conversion float(x) does the actual conversion. The for x in col.values parses through each value in col.values assigning the value to x, then the float(x) converts to float. It is a pretty elegant conversion of all values in an array (or any ienumerable) to an array of a fixed type.

If you have any questions about how any of the lines of code actually work, simply ask on the discussion boards.

Steam Tables

I was able to access the Steam Tables (10/31/2023) at

https://pressbooks.pub/thermo/

you will need to get the links from the table of contents.