Solution: 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 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(0,20,2))
sst = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Print out the wind_speed and sst variables to check yourself.


In [2]:
print(wind_speed)
sst


[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Out[2]:
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

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 [3]:
def calc_heat_flux(u_atm, t_sea, rho=1.2, c_p=1004.5, c_h=1.2e-3, u_sea=1, t_atm=17):
    q = rho * c_p * c_h * (u_atm - u_sea) * (t_sea - t_atm)
    return q

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 [4]:
heat_flux = []
for u, t in zip(wind_speed, sst):
    q = calc_heat_flux(u, t)
    heat_flux.append(q)

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


In [5]:
heat_flux


Out[5]:
[17.35776,
 -15.911279999999998,
 -43.3944,
 -65.09159999999999,
 -81.00287999999999,
 -91.12823999999998,
 -95.46767999999999,
 -94.02119999999998,
 -86.78879999999998,
 -73.77047999999999]

4. Open a new text file for writing

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


In [6]:
# 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 [7]:
# with open('heat_flux_data.txt', 'w') as f:
#     for h in heat_flux:
#         f.write('{:3.1f}\n'.format(h))

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


In [8]:
# !cat heat_flux_data.txt