DEV Community

Gareth M.
Gareth M.

Posted on

Displaying air quality data in the terminal

Intro

My local county reports data on the level of several pollutants throughout the area. However, the only way to see this data is via an online dashboard. My project air-monitor is a Python script that uses the backend APIs of the dashboard to display the same data in the terminal.

Obtaining the data

The dashboard makes separate requests for each pollutant type, which are specified in the request parameter where, along with the desired time span.
The service measures the following pollutant types:
- sulfur dioxide
- carbon monoxide
- Particulate Matter (10 and 2.5)
- ozone
- nitrogen oxide

The data is returned in an array of JSON objects, each of which contains a value as well as the date and time the data was collected

The are also several sensors placed throughout the county's area, which each generate their own sets of data points. In the web dashboard, these are shown in different colors/lines. In the future, ANSI codes could be used to show multiple data sources. For now, I've opted to use an averaging function that combines pollutants with multiple data sources, which is handled by the chart library.

Graphing the Data

To graph the data, chart.py uses a numpy matrix that is allocated based on the possible values ranges of a pollutant, since each pollutant is measured with different unit types (ppb, ppm, ug/m^3). Each pollutant type has it's own array size and conversion factors to display. For example, here are the values for CO.

  "CO":(40, 2, -0.01, "ppm", 100, 100, "{:.2f}", 100), # ppm
Enter fullscreen mode Exit fullscreen mode

The first value is used as the size for the outer array (the columns), and each of the sub-arrays is allocated according to the number of hours the data covers. The rest of the values are used to round and display the numbers. Then, the values are printed for each interval where a data point exists.
In order to average data from several locations, the data is first arranged into a dictionary with each time slot as a key. Then, any duplicates are averaged together and re-packaged with the same format as the original JSON data.

Image description

Challenges

The main challenge with this project is getting the data to fit inside the terminal, while also making it legible enough. My goal was for the user to visually be able to match the shape of the graph to the one in the web dashboard, but also fit on smaller displays/terminals with fewer columns. I wanted to make an initial version of this project that just used regular terminal output, without depending on any special ANSI codes. Future updates would include adding colors to the output and adding connecting lines/dots to better show the curve of the graph.

Project repo: https://github.com/gbafana25/air-monitor
VODs of coding streams: https://youtube.com/@gbafana25

Top comments (0)