When used together this set of modules gets PurpleAir PA-II sensor historical data for sensors located within a bounding box for a given date range, plots the data on a map and generates a timelapse video of the readings.
Some of the modules may also be used independently as modules in other programs or directly from the command line.
- pa_map_vis.py: The main program that executes the other modules to get the data and make the timelapse video.
- pa_get_df.py: Gets historical sensor data and returns a Pandas data frame.
- pa_map_plot.py: create the image frames.
- get_map.py: Gets a map image for the provided bounding box from Mapbox.
- pa_map_vid.py: create h.264 encoded mp4 video from image frames.
- Create a folder on your computer and clone the repo into it ( git clone https://github.com/wawzat/pa-map.git )
- It is recommended to create a virtual environment with virtualenv (or conda if you are using Anaconda or miniconda)
- Run python -m pip install -r requirements.txt to install the required Python packages.
- Request a key for the PurpleAir REST API from api@purpleair.com.
- Register an account with https://www.mapbox.com. You will need Public and Secret Access tokens which will be obtained from your account page.
- Create a root folder path and subfolders for images, data and video. Name the folders consistently with the names used in config.py.
- Rename the config_template.py file to config.py.
- Edit config.py with the PurpleAir keys, Mapbox tokens and file directory paths in the appropriate sections of the config.py template.
- Run python pa_map_vis.py -h for help with the command line arguments.
- Data are obtained in two stages.
- A sensor request is sent to the PurpleAir API and a list of the metadata for each sensor installed within the provied bounding box is retrieved.
- The list of sensor metadata is iterated over and historical data is retrived from the ThingSpeak API for each sensor.
- There are two data retrieval modes that may be selected with the -d argument.
- -d TS obtains historical data from ThinkSpeak. Historical data are saved in the Data folder as csv files.
- -d CSV provides a list the of csv files that exist in the Data folder to choose from. The intent here is if you want to adjust to a shorter date range, or modify some of the plotting/video parameters you can recreate the time lapse without re-downloading the historical data.
- The metadata obtained during step 1.2 above are saved as .txt files in the metadata folder. The --md argument will allow you to choose to use one of these files in-lieu of obtaining it from the PurpleAir API.
- If you are experiencing errors due to API rate limiting you may try increasing the sleep() time in pa_get_df.py get_sensor_ids() or get_ts_data() functions for PurpleAir or ThingSpeak respectively.
- Regions with a large number of sensors may take several hours to complete collecting the data.
- colorcet
- matplotlib
- numpy (should install automatically with Pandas)
- opencv-python (cv2)
- pandas
- Pillow (PIL) (should install automatically with matplotlib)
- requests