There are some interesting data on our environment such as air quality factors that can be available from public datasets. This can be useful to determine if there are health risks in your local weather environments and can be programatically tracked by using the right approach in Python.
What Comprises Air Quality?
There is no one thing called ‘air quality’ but it is a combination of different factors that are combined together to form an index. There is also no universal air quality index, but there are different standards across geographies as each locale has different factors they want to include. It is a typically a ranking system which makes it easier to grasp the amount of pollutants or quality in the air to indicate to people how much risk they could be at in a given location.
Air Quality Index
A scale used to indicate how toxic the air is, along with the risks associated with each ranking, is an air quality index. For the appropriate amounts of major air contaminants, an AQI is determined using proven criteria based on medical science.
There are several values of air quality indexes:
- Informing the public in an understandable way about air quality so that they can take steps to protect their health
- To support countries in designing and reviewing strategies for improved air quality
- Ability to consolidate information across various factors to understand a single quantitative representation of air quality
- Ability to compare different locations and/or trends in air quality over time
Air Quality Data Sources
The World Air Quality Index website (waqi.info) is a modestly funded platform which helps to aggregate air quality data from sensors across the world. The site was created by researchers in Beijing that wanted to make it easier to see a world view of air quality data. They have so far managed to get about 15,000 sensors across 130 countries around the world that provide air quality data in real-time.
The data is updated on an hourly basis where, for example, a 10AM reading means the measurement was performed between 9am-10am. Each city is searchable and data can be accessed through an API from an API key.
The platform is useful in that you can get a good coverage of the world’s air quality data in the one place.
Getting Air Quality From API
The waqi.info website offers an API service through an API key where the data is provided in JSON format. You can call the API a maximum of 1000 times a second.
Data format will be as follows:
Ok, so first step is to get the API key. You can get the token from the following page: https://aqicn.org/api/ where you can get a data-platform token. You’ll need to enter your email address and name to get the API token key.
Once you provide your email address and click on the confirmation link on your confirmation email, you will get the token as shown here:
You will need the API key in order to call the API shortly. As you can see, if you call the url with the API and the city, you can then get the json data. The API can be called with:
https://api.waqi.info/feed/<city>/?token=<API Token key>
In the above example, when you go to the url with the city ‘beijing’ and your token, you will see the following:
Air Quality API Format
The API provides a wealth of information – here are some key fields:
Field | Description | Example |
data/aqi | Air quality index from 0 – +300. The quality is as follows: 0-50 = Good 51-100 = Moderate 101-150 = Unhealthy for moderate groups 151-200 = Unhealthy 201-300 = Very Unhealthy +300 = Hazardous | 71 |
data/time/s | Time in the local timezone where the station is located | “2021-02-20 19:00:00” |
data/time/tz | Timezone of the field data/time/s | “+08:00” |
data/city/name | City name | “Chi_sp, Illinois” |
data/city/geo | Longitude and latitude in an array | “41.913600”,”-87.723900″ |
If you are struggling to get a given city name, you can also provide the ID of the station. You can find the ID of a given station by searching for the “idx” variable in the source code for a given city. For example, if you wanted to find the ID for Chicago. You can do the following:
Step1: Go to the city search page at https://aiqcn.org/city/all
Step 2: Click on the city page – in this case chicago. Next go to view source
Step 3: Search for the keyword “idx”. You can see here the id is 7595
Step 4 : Call the API with the id.
https://api.waqi.info/feed/@<ID>/?token=<API Token key>
Calling the Air Quality API from Python
import requests
city = 'kanpur'
url = 'http://api.waqi.info/feed/' + city + '/?token='
api_key = '4b64972ed75f8008b440319494e7b01f7c5c248c'
main_url = url + api_key
r = requests.get(main_url)
data = r.json()['data']
aqi = data['aqi']
iaqi = data['iaqi']
dew = iaqi.get('dew','Nil')
no2 = iaqi.get('no2','Nil')
o3 = iaqi.get('o3','Nil')
so2 = iaqi.get('so2','Nil')
pm10 = iaqi.get('pm10','Nil')
pm25 = iaqi.get('pm25','Nil')
pollen = iaqi.get('pol','Nil')
print(f'{city} AQI :',aqi,'\n')
print('Individual Air quality')
print('Dew :',dew['v'])
print('no2 :',no2['v'])
print('Ozone :',o3['v'])
print('sulphur :',so2['v'])
print('pm10 :',so2['v'])
print('pm25 :',pm25['v'])
Here we are going to use Python3 for pulling the data from a free source weather forecast website using tokens and APIs.
Output:
In the above code we simply requested the given website to share its weather forecast data with us and we put forward the data in an organised way in our console using python3.
Get Notified Automatically Of New Articles
Error SendFox Connection: