Exercise: build a simple program (1 h)

By doing this exercise you will apply Python basics that we learned today: loops, lists, functions, strings. In addition, you will try to write data to a text file.

Synopsis

  • Create data of wind and sea surface temperature within some range
  • Functionalise the equation for the heat flux
  • In a loop calculate the heat flux with wind speed and temperature as inputs
  • Open a new text file for writing
  • Loop over the function output and write the data to the file
  • Hint: use string formatting to specify number of decimal places

Equation

The bulk formula for the sea-to-air heat flux (positive upwards) is

$Q = \rho c_p C_H (u_{atm} - u_{sea}) (T_{sea} - T_{atm}) $

where

  • $Q$ is the sensible heat flux,
  • $\rho$ = 1.2 $kg~m^{−3}$ is the density of air at sea level,
  • $c_p$ = 1004.5 $J kg^{-1} K^{-1} $ is the specific heat capacity,
  • $C_H$ = 1.2$\times 10^{-3}$ is the exchange coefficient for heat
  • $u_{sea}$ = 0.5 $m~s^{−1}$ is the ocean surface velocity,
  • $u_{atm}$ is the wind speed at 10 m above the sea surface,
  • $T_{sea}$ is the sea surface temperature (SST), and
  • $T_{atm}$ = 17 $^\circ C$ is the air temperature at 10 m above the sea surface.

1. Create data of within the following range

  • wind speed: 0-20 $m~s^{−1}$, every 2 $m~s^{−1}$
  • SST: 5-15 $^\circ C$, every 1 $^\circ C$

Hint: use range() function and wrap it in a list() function.

If you want to create lists of arbitrary values (e.g. non-integer), use my_list = [ ] notation.


In [1]:
## Your code
#wind_speed = list(range(    ))
#sst =

Print out the wind_speed and sst variables to check yourself.

2. Create a function to calculate the heat flux

  • Wind speed and temperature should be the required input arguments
  • Make the constants ($\rho$, $c_p$, etc) to be keyword (aka optional) arguments with default values
  • Use return statement to return the output

You've already forgotten it, but the formula is $Q = \rho c_p C_H (u_{atm} - u_{sea}) (T_{sea} - T_{atm}) $


In [2]:
# def calc_heat_flux():

3. In a loop, calculate the heat flux with wind speed and temperature as inputs

  • First, create an empty list heat_flux of $Q$ values
  • Then, write a loop
  • Every iteration: after $Q$ is computed, append it to the heat_flux list

In [3]:
#
# for ...
#     q = calc_heat_flux(...)
#

Print out heat_flux variable to check that the values are sensible (no pun intended).

4. Open a new text file for writing

Now, you need to open a file. Explore the built-in function open():


In [4]:
# open?

The recommended way of writing/reading files is using the context statement with:

# example
with open('super_descriptive_file_name', mode='r') as your_file:
    your_file.read()

Some commonly used I/O modes:

  • mode='w' means that we opened file for writing. This mode overwrites any existing data.
  • mode='r' - open a file for reading
  • mode='a' - open a file for appending data
  • mode='x' - open for exclusive creation, failing if the file already exists
  • mode='b' - binary mode

5. Loop over the function output and write the data to the file

  • *Open a file named heat_flux_data.txt for writing
  • Use with statement
  • Inside the with code block, write a loop to iterate through the heat_flux values and write each of them on a new line
  • Instead of read() as in the example above, use write() method
  • Note: write() method needs string type input
  • You can convert numeric values to string type using str() function or, even better, format() method
  • Add "\n" character to the string to indicate a line break

In [5]:
# Your code here...

Use a text editor of your choice (or Jupyter!) to check the contents of the file.