Skip to content

Python wrapper for the National Grid carbon intensity API

License

Notifications You must be signed in to change notification settings

OSUKED/CIDataPortal

Repository files navigation

National Grid Data Portal API Wrapper

DOI


Overview

About the Portal

The National Grid ESO Carbon Intensity API provides an interface to data on the Carbon Intensity of the UK electricity system at both a national and regional (DNO) level. It was developed as a collaboration between WFF, Environmental Defense Fund, NG ESO & Oxford University.

This Python wrapper makes it easier to query data from the API and receive back Panda's DataFrames ready for further analysis, as well as simplify the querying procedure itself. If you have any ideas for the module please feel free to contribute!


The package can be installed using:

pip install CIDataPortal


Module Usage

Getting Started

The module's Wrapper class is the main interface with the API, it can be imported as follows:

from CIDataPortal import Wrapper

To make a query you must first initialise the Wrapper class. You can then use the .query_API() to (by default) retrieve data for todays obsrrved and forecasted carbon intensity. The response is then automatically parsed into a Panda's DataFrame.

wrapper = Wrapper()

df = wrapper.query_API()

df.head()
forecast actual
2020-06-17 00:00:00+00:00 263 265.0
2020-06-17 00:30:00+00:00 259 263.0
2020-06-17 01:00:00+00:00 259 262.0
2020-06-17 01:30:00+00:00 259 262.0
2020-06-17 02:00:00+00:00 256 264.0

It is then trivial to then plot and carry out further analysis with the data, e.g:

wrapper.query_API().plot()
plt.ylabel('gCO2/kWh')


Advanced Usage

We can also specify the data stream, spatial aggregation level and date range to be returned from the API. Whilst the API limits requests to a maximum of 2-weeks, the Python wrapper automatically handles the splitting of queries and collation of returned data.

wrapper = Wrapper()

df = wrapper.query_API('2020-01-01',
                       '2020-06-01',
                       level='national', 
                       data_stream='generation')

df.head()
biomass coal imports gas nuclear other hydro solar wind
2020-01-01 00:00:00+00:00 8.7 2.5 9.5 29.5 25.8 0.5 2.5 0.0 21.0
2020-01-01 00:30:00+00:00 8.6 2.4 9.3 30.8 25.3 0.4 2.4 0.0 20.8
2020-01-01 01:00:00+00:00 8.9 2.5 9.6 29.1 26.2 0.5 2.5 0.0 20.7
2020-01-01 01:30:00+00:00 9.0 2.6 9.8 28.5 26.7 0.5 2.3 0.0 20.6
2020-01-01 02:00:00+00:00 9.2 2.6 10.0 27.5 27.3 0.5 2.1 0.0 20.8