A lightweight Python wrapper for EPA AirNow Air Quality API
Project description
pyairnow: a thin Python wrapper for the AirNow API
pyairnow
is a simple, tested, thin client library for interacting with the
AirNow United States EPA Air Quality Index API.
Python Versions
pyairnow
is currently supported and tested on:
- Python 3.8
- Python 3.9
- Python 3.10
- Python 3.11
Installation
pip install pyairnow
API Key
You can get an AirNow API key from the AirNow API site. Ensure you read and understand the expectations and limitations of API usage, which can be found at the AirNow FAQ.
Usage
import asyncio
import datetime
from pyairnow import WebServiceAPI
async def main() -> None:
client = WebServiceAPI('your-api-key')
# Get current observational data based on a zip code
data = await client.observations.zipCode(
'90001',
# if there are no observation stations in this zip code, optionally
# provide a radius to search (in miles)
distance=50,
)
# Get current observational data based on a latitude and longitude
data = await client.observations.latLong(
34.053718, -118.244842,
# if there are no observation stations at this location, optionally
# provide a radius to search (in miles)
distance=50,
)
# Get forecast data based on a zip code
data = await client.forecast.zipCode(
'90001',
# to get a forecast for a certain day, provide a date in yyyy-mm-dd,
# if not specified the current day will be used
date='2020-09-01',
# if there are no observation stations in this zip code, optionally
# provide a radius to search (in miles)
distance=50,
)
# Get forecast data based on a latitude and longitude
data = await client.forecast.latLong(
# Lat/Long may be strings or floats
'34.053718', '-118.244842',
# forecast dates may also be datetime.date or datetime.datetime objects
date=datetime.date(2020, 9, 1),
# if there are no observation stations in this zip code, optionally
# provide a radius to search (in miles)
distance=50,
)
asyncio.run(main())
By default, the library creates a new connection to AirNow with each coroutine.
If you are calling a large number of coroutines (or merely want to squeeze out
every second of runtime savings possible), an
aiohttp
ClientSession
can be used
for connection pooling:
import asyncio
import datetime
from aiohttp import ClientSession
from pyairnow import WebServiceAPI
async def main() -> None:
async with ClientSession() as session:
client = WebServiceAPI('your-api-key', session=session)
# ...
asyncio.run(main())
The library provides two convenience functions to convert between AQI and pollutant concentrations. See this EPA document for more details.
from pyairnow.conv import aqi_to_concentration, concentration_to_aqi
# Supported Pollutants
# --------------------
# Ozone ('O3'): ppm
# pm2.5 ('PM2.5'): ug/m^3
# pm10 ('PM10'): ug/m^3
# Carbon Monoxide ('CO'): ppm
# Sulfur Dioxide ('SO2'): ppm
# Nitrogen Dioxide ('NO2'): ppm
# Returns AQI = 144 for pm2.5 of 53.0 ug/m^3
aqi_to_concentration(144, 'PM2.5')
# Returns Cp = 53.0 ug/m^3
concentration_to_aqi(53.0, 'PM2.5')
Contributing
- Check for open features/bugs or start a discussion on one.
- Fork the repository.
- Install Poetry and set up the development workspace:
poetry install
- Code your new feature or bug fix.
- Write tests that cover your new functionality.
- Run tests and ensure 100% code coverage:
make test
- Run the linter to ensure 100% code style correctness:
make lint
- Update
README.md
with any new documentation. - Add yourself to
AUTHORS.md
. - Submit a pull request!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyairnow-1.2.2.tar.gz
.
File metadata
- Download URL: pyairnow-1.2.2.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 643d0de6e1e7d18b6547410bf2f39a3bb3c12c0d7e24e6bf15c5f4ddad650071 |
|
MD5 | cd22b4acb6fdc0b5104c0293cd6536f4 |
|
BLAKE2b-256 | f56270a2db01fe8c929ed68412761688ecca8a3bb295c10244c9a56dc4b25cd9 |
File details
Details for the file pyairnow-1.2.2-py3-none-any.whl
.
File metadata
- Download URL: pyairnow-1.2.2-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1e8d8bee778efa1730d2bc24f0fe7967025cbfb196a77f9396d742130c63092 |
|
MD5 | 213723203c9bd06a8068ad5dbdf5e201 |
|
BLAKE2b-256 | 1edf279023a34b1f1cd4929309bb4988607cb8513abe3452a401df6918eb9ef6 |