2. Tempscat Files

Let’s imagine an institution called Tempscat intended to collect data about whether conditions all around Catalonia. Tempscat composed of a central station (aka TCentral) connected to a network of Tempscat Municipal Stations (aka TMuni) located in many towns in Catalonia that send information about the whether variables in that spot.

Let’s focus on the temperature variable. Each TMuni is constantly measuring the temperature and computes the average temperature for every hour. This information is sent in lots of several hours to the TCentral. Therefore TCentral receives a number of messages from each TMuni throughout the day, collects them all and produces a daily file with all the measures received that day. This is a csv file (Comma Separated Values) whose format is as follows: each line has the town name followed by several float numbers all separated by ','. Each town usually appears in several lines since the TCentral in that town send severals lots throughout the day.

For example the file temp_11-1-2026.csv contains information about the temperature from three towns collected in lots of 6 hours:

Barcelona,11.5,10.0,9.5,9.0,8.5,8.0
La Cellera de Ter,2.0,1.5,1.0,0.0,1.0,2.5
Sort,0.0,1.0,1.7,3.1,2.0,0.5
La Cellera de Ter,1.0,1.5,2.2,3.4,6.5,9.0
Sort,0.0,-2.0,-2.5,-3.0,3.0,7.5
Barcelona,8.1,8.5,8.8,9.2,10.5,12.0
Sort,9.0,11.5,1E.0,12.0,8.5,7.0
La Cellera de Ter,10.3,11.0,12.0,12.5,11.5,9.0
Barcelona,12.5,13.0,13.5,13.7,13.5,12.5
Barcelona,11.5,11.0,10.8,10.5,10.2,10.0
Sort,4.5,4.0,2.0,0.5,1.0,o.0
La Cellera de Ter,6.0,5.8,5.2,5.0,A.2,3.5

Since the communication system is not perfect, some temperature values might be corrupted and include characters that cannot be interpreted as a numeric value. For instance, notice in the above example that line 7 contains and character 'E', ine 11 contains the character 'o' and line 12 contains the caracter 'A'. Anything that cannot be interpreted as numeric value (either float or integer) should be disregarded.

Given a file like this, we wish to compile its information to produce a file with the daily minimum and maximum temperatures for each town. This ought to be a CSV file with the following format:

  1. the town name (str)

  2. the minimum temperature (float)

  3. the maximum temperature (float)

  4. the number of valid temperature measures (int)

  5. the valid temperature measures (sequence of float)

For example, the output expected from the above file would be the file tempminmax_11-1-2026.csv:

Barcelona,8.0,13.7,24,11.5,10.0,9.5,9.0,8.5,8.0,8.1,8.5,8.8,9.2,10.5,12.0,12.5,13.0,13.5,13.7,13.5,12.5,11.5,11.0,10.8,10.5,10.2,10.0
La Cellera de Ter,0.0,12.5,23,2.0,1.5,1.0,0.0,1.0,2.5,1.0,1.5,2.2,3.4,6.5,9.0,10.3,11.0,12.0,12.5,11.5,9.0,6.0,5.8,5.2,5.0,3.5
Sort,-3.0,12.0,22,0.0,1.0,1.7,3.1,2.0,0.5,0.0,-2.0,-2.5,-3.0,3.0,7.5,9.0,11.5,12.0,8.5,7.0,4.5,4.0,2.0,0.5,1.0

Notice that Barcelona has 24 valid measures whereas La Cellera de Ter has 23 and Sort has only 22, due to the invalid characters found in the input file.

To this purpose you are asked to deliver the following Python function in the module tempscat (file tempscat.py):

temp_join(in_filename, out_filename)

such that

given

  • in_filename, a str with the filename of a file with the temperatures of one day as described above

  • out_filename, a str with the name of the output file as described above

returns an int with the number of municipalites processed

produces a file named out_filename with the format and compiled info from the info in_filename as described above.

Note

The implement this function here you have a code snippet and a recommendation:

  • To chech whether a value read from the file is a valid numeric value you can used the file is_float.py. You can download it and either import or copy into your code file.

  • It is recommended to store the temperatures for each town coming from several lines of in_filename into a dictionary where the key is the town name and the value is the list of processed, numeric temperatures for that town name.

Doctests are available in the file temp_join.test. The file to pass these doctests are available at: