Python software for USB Wireless WeatherStations

Template.py -- creates text data file based on a template

This is probably the most difficult to use module in the weather station software collection. It generates text files based on a "template" file plus the raw, hourly, daily & monthly weather station data. The template processing goes beyond simple substitution of values to include loops, jumps forwards or backwards in the data, processing of the data and substitution of missing values.

A template file can be any sort of text file (plain text, xml, html, etc.) to which "processing instructions" have been added. These processing instructions are delimited by hash ('#') characters. They are not copied to the output, but cause something else to happen: either a data value is inserted or one of a limited number of other actions is carried out.

Before writing your own template files, it might be useful to look at some of the examples in the example_templates directory.

Processing instructions

Example

Here is an example snippet showing basic and advanced use of the template features. It is part of the 6hrs.txt example template file, which generates an HTML table of 7 hourly readings (which should span 6 hours).

#hourly#
#jump -6#
#loop 7#
  <tr>
    <td>#idx "%Y/%m/%d" "" "[None, x][x.hour == 0 or loop_count == 7]"#</td>
    <td>#idx "%H%M %Z"#</td>
    <td>#temp_out "%.1f °C"#</td>
    <td>#hum_out "%d%%"#</td>
    <td>#wind_dir "%s" "-" "wind_dir_text[x]"#</td>
    <td>#wind_ave "%.0f mph" "" "x * 3.6 / 1.609344"#</td>
    <td>#wind_gust "%.0f mph" "" "x * 3.6 / 1.609344"#</td>
    <td>#rain "%0.1f mm"#</td>
    <td>#rel_pressure "%.0f hPa"#, #pressure_trend "%s" "" "pressure_trend_text(x)"#</td>
  </tr>
#jump 1#
#endloop#

The first three lines of this snippet do the following: select hourly data, jump back 6 hours, start a loop with a count of 7. A jump forward of one hour appears just before the end of the repeated segment. As this last jump (of one hour) happens each time round the loop, a sequence of 7 data readings will be output. The last line marks the end of the loop — everything between the #loop 7# and #endloop# lines is output 7 times.

The #temp_out ...#, #hum_out ...#, #rain ...# and #rel_pressure ...# instructions show basic data output. They each use a fmt_string to format the data appropriately. The #wind_ave ...# and #wind_gust ...# instructions show how to use a conversion expression to convert m/s to mph.

The #wind_dir ...# and #pressure_trend ...# instructions show use of the built-in array wind_dir_text and function pressure_trend_text to convert numerical values into English text.

Finally we get to datetime values. The #idx "%H%M"# instruction simply outputs the time (in HHMM format) of the data's index. The #idx "%Y/%m/%d" "" "[None, x][x.hour == 0 or loop_count == 7]"# instruction is a bit more complicated. It outputs the date, but only on the first line or if the date has changed. It does this by indexing the array [None, x] with a boolean expression that is true when loop_count is 7 (i.e. on the first pass through the loop) or x.hour is zero (i.e. this is the first hour of the day).


Python software for USB Wireless WeatherStations © Jim Easterbrook.