In-class and homework assignment for Tuesday, Sept. 11

You are encouraged (but not required) to work with a partner, in which case the two of you will turn in one paper with both your names on it (and both of you get the same grade).

Surface Meteorological Airways Format

The weather services of many nations and satellite observing platforms make more than 3 billion measurements of the Earth's atmosphere every day. Given such a huge volume of data, standardized ways to represent it are imperative.

The so-called METAR format is used to encode certain weather observations that are taken at ground level. Tables of these observations are updated hourly and can be downloaded from any National Weather Service web site. Here are the METAR data for stations in and near Arizona taken between 3 pm and 5 pm on Sept. 10, 2007.

Exercise 1. Write a Python script, metar.py, that reads metar.dat and produces a list of the form

station time pressure temperature dewpoint
where the latter two values are given in Fahrenheit and the pressure is given in hectoPascals. No special formatting of the time field is necessary, but please format the pressure and temperatures nicely.

One little challenge is to format an entry like 32/M01, which means that the ambient temperature is 32 degrees Celsius and the dewpoint is -1 degree Celsius. Some automated measurements omit the dewpoint temperature, in which case the corresponding entry looks like 32/ and you should write M for the missing dewpoint.

Atmospheric pressure in the United States is commonly reported in inches of mercury (Hg), but the international standard is hectoPascals (hPa), also known as millibars (mb). This conversion chart gives equivalent (in,hPa) pairs. Your Python script should read it in, along with the METAR data, to supply the conversion. You may assume that pressure data is reported to the nearest hundredth of an inch (when inches are used) and that the values lie within the range given in the conversion chart. (Suggestion: build a hash table from the conversion data so that an expression of the form hPa[inHg] yields the hectoPascal equivalent for a given reading, inHg, in inches of mercury. If you experience difficulties due to rounding, then convert inches of mercury to the nearest 4-digit integer value before placing it in the hash table.)

You may hardwire the name of the conversion file in your Python script as barometric.dat. Otherwise, your script must be runnable in the form

python metar.py < metar.dat
(It's easy to imagine how such a script might be part of a larger one that reads a METAR file and automatically reformats it for a Web page intended to be accessed by the public.)

Integer division

If a and b are integer values, and b is nonzero, then we define the expression a/b as the quotient, q, obtained according to the following criteria:
  1. a = bq + r
  2. q has the same sign as ab
  3. 0 ≤ |r| < |b|.
These three criteria are not sufficient to yield a unique choice of q and r. For example, if a=27 and b=4, then
27 = 4×6 + 3 = 4×7 + (-1)
so we may define 27/4 as either 6 or 7.

To obtain a unique quotient and remainder, a fourth condition is required. There are two reasonable possibilities.

Exercise 2. Make a table of the values of q and r that each of the following expressions produces under Convention Q and under Convention R.

  1. 21/6
  2. -21/6
  3. 21/(-6)
  4. (-21)/(-6)

Exercise 3. Write a Python function, div_q(a,b) that returns the tuple (q,r) giving the quotient and remainder, respectively, according to Convention Q. (You may assume that a and b are integers and that b is nonzero; no error checking is required.)

Exercise 4. Write a Python function, div_r(a,b) that returns the tuple (q,r) giving the quotient and remainder, respectively, according to Convention R. (As above, no error checking is required.)

In the remaining problems, assume that a and b are integer variables and that b is not zero.

Exercise 5. Prove that Convention Q and Convention R produce the same values of q and r when a and b are both positive.

Exercise 6. True or false: Under Convention Q,

(-a)/b = -(a/b).
If the statement is true, then use the definition of q and r to prove that it is always true. Otherwise, you can prove that the statement is false by finding a single counterexample.

Exercise 7. True or false: Under Convention R, (-a)/b = -(a/b). As above, either write a proof or find a counterexample.

Submission instructions

You will create three files for this assignment, as follows:

Package these files into a tar archive, hw4.tar, using the tar command as follows:

tar -cf hw4.tar metar.py division.py remainder.txt
Then mail the tar archive to mat420hw at gmail.com with a subject line of the form
Your_name MAT 420 HW4
and include hw4.tar as an attachment. Assignments are due by 5 p.m., Tuesday, Sept. 18.

Copyright(c) 2007 by Eric J. Kostelich. All rights reserved.