# Import the following libraries
# For fetching from the Raster API
import requests
# For making maps
import folium
import folium.plugins
from folium import Map, TileLayer
# For talking to the STAC API
from pystac_client import Client
# For working with data
import pandas as pd
# For making time series
import matplotlib.pyplot as plt
# For formatting date/time data
import datetime
# Custom functions for working with GHGC data via the API
import ghgc_utils
ODIAC Fossil Fuel CO₂ Emissions
Access this Notebook
You can launch this notebook in the US GHG Center JupyterHub by clicking the link below. If you are a new user, you should first sign up for the hub by filling out this request form and providing the required information.
Access the ODIAC Fossil Fuel CO₂ Emissions notebook in the US GHG Center JupyterHub.
Table of Contents
Data Summary and Application
- Spatial coverage: Global
- Spatial resolution: 1 km x 1 km
- Temporal extent: January 2000 - December 2022
- Temporal resolution: Monthly
- Unit: Metric tonnes of carbon per 1 km x 1 km cell per month
- Utility: Climate Research
For more, visit the ODIAC Fossil Fuel CO₂ Emissions data overview page.
Approach
- Identify available dates and temporal frequency of observations for the given collection using the GHGC API
/stac
endpoint. Collection processed in this notebook is ODIAC CO₂ emissions version 2023. - Pass the STAC item into raster API
/collections/{collection_id}/items/{item_id}/tilejson.json
endpoint - We’ll visualize two tiles (side-by-side) allowing for comparison of each of the time points using
folium.plugins.DualMap
- After the visualization, we’ll perform zonal statistics for a given polygon.
About the Data
ODIAC Fossil Fuel CO₂ Emissions
The Open-Data Inventory for Anthropogenic Carbon dioxide (ODIAC) is a high-spatial resolution global emission data product of CO₂ emissions from fossil fuel combustion (Oda and Maksyutov, 2011). ODIAC pioneered the combined use of space-based nighttime light data and individual power plant emission/location profiles to estimate the global spatial extent of fossil fuel CO₂ emissions. With the innovative emission modeling approach, ODIAC achieved the fine picture of global fossil fuel CO₂ emissions at a 1x1km.
For more information regarding this dataset, please visit the ODIAC Fossil Fuel CO₂ Emissions data overview page.
Terminology
Navigating data via the GHGC API, you will encounter terminology that is different from browsing in a typical filesystem. We’ll define some terms here which are used throughout this notebook. - catalog
: All datasets available at the /stac
endpoint - collection
: A specific dataset, e.g. ODIAC Fossil Fuel CO2 Emissions - item
: One granule in the dataset, e.g. one monthly file of CO2 emissions - asset
: A variable available within the granule, e.g. fossil fuel CO2 emisisons - STAC API
: SpatioTemporal Asset Catalogs - Endpoint for fetching metadata about available datasets - Raster API
: Endpoint for fetching data itself, for imagery and statistics
Install the Required Libraries
Required libraries are pre-installed on the GHG Center Hub. If you need to run this notebook elsewhere, please install them with this line in a code cell:
%pip install requests folium rasterstats pystac_client pandas matplotlib –quiet
Query the STAC API
STAC API Collection Names
Now, you must fetch the dataset from the STAC API by defining its associated STAC API collection ID as a variable. The collection ID, also known as the collection name, for the ODIAC Fossil Fuel CO2 Emissions dataset is odiac-ffco2-monthgrid-v2023.*
**You can find the collection name of any dataset on the GHGC data portal by navigating to the dataset landing page within the data catalog. The collection name is the last portion of the dataset landing page’s URL, and is also listed in the pop-up box after clicking “ACCESS DATA.”*
# Provide the STAC and RASTER API endpoints
# The endpoint is referring to a location within the API that executes a request on a data collection nesting on the server.
# The STAC API is a catalog of all the existing data collections that are stored in the GHG Center.
= "https://earth.gov/ghgcenter/api/stac"
STAC_API_URL
# The RASTER API is used to fetch collections for visualization
= "https://earth.gov/ghgcenter/api/raster"
RASTER_API_URL
# The collection name is used to fetch the dataset from the STAC API. First, we define the collection name as a variable
# Name of the collection for ODIAC dataset
= "odiac-ffco2-monthgrid-v2023" collection_name
# Using PySTAC client
# Fetch the collection from the STAC API using the appropriate endpoint
# The 'pystac' library allows a HTTP request possible
= Client.open(STAC_API_URL)
catalog = catalog.get_collection(collection_name)
collection
# Print the properties of the collection to the console
collection
- type "Collection"
- id "odiac-ffco2-monthgrid-v2023"
- stac_version "1.0.0"
- description "The Open-source Data Inventory for Anthropogenic CO₂ (ODIAC) data product is a monthly high-resolution global data product of modeled fossil fuel carbon dioxide (CO₂) emissions. A complex model incorporates and combines space-based nighttime light data and individual power plant emission/location profiles from the latest country fossil fuel CO₂ estimates (2000-2019) made by the Carbon Dioxide Information Analysis Center (CDIAC) team at the Appalachian State University (CDIAC at AppState, Gilfillan et al. 2021, Hefner et al. 2022). The ODIAC estimated global spatial extent of fossil fuel CO₂ emissions is produced on a 1 km by 1 km grid that details variations in urban regions where emissions are most intense. The ODIAC CO₂ emission data is widely used by the international research community for applications such as CO₂ flux inversion, urban emission estimation, and observing system design experiments. The ODIAC product was first created in 2009 by Dr. Tomohiro Oda with support from the National Institute for Environmental Studies (NIES) GOSAT project. The ODIAC team is now supported by NASA Goddard Space Flight Center, NASA Carbon Monitoring System program, the NASA Orbiting Carbon Observatory mission and NIES. The US GHG Center displays the ODIAC 2023 version containing monthly data from January 2000 to December 2022 that replaces all previous versions. The source dataset can be found at https://doi.org/10.17595/20170411.001"
links[] 5 items
0
- rel "items"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023/items"
- type "application/geo+json"
1
- rel "parent"
- href "https://earth.gov/ghgcenter/api/stac/"
- type "application/json"
2
- rel "root"
- href "https://earth.gov/ghgcenter/api/stac"
- type "application/json"
- title "US GHG Center STAC API"
3
- rel "self"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023"
- type "application/json"
4
- rel "http://www.opengis.net/def/rel/ogc/1.0/queryables"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023/queryables"
- type "application/schema+json"
- title "Queryables"
stac_extensions[] 2 items
- 0 "https://stac-extensions.github.io/render/v1.0.0/schema.json"
- 1 "https://stac-extensions.github.io/item-assets/v1.0.0/schema.json"
renders
dashboard
assets[] 1 items
- 0 "co2-emissions"
rescale[] 1 items
0[] 2 items
- 0 -10
- 1 60
- colormap_name "jet"
co2-emissions
assets[] 1 items
- 0 "co2-emissions"
rescale[] 1 items
0[] 2 items
- 0 -10
- 1 60
- colormap_name "jet"
item_assets
co2-emissions
- type "image/tiff; application=geotiff; profile=cloud-optimized"
roles[] 2 items
- 0 "data"
- 1 "layer"
- title "Fossil Fuel CO₂ Emissions"
- description "Model-estimated monthly, 1 km resolution CO₂ emissions from fossil fuel combustion, cement production and gas flaring created using space-based nighttime light data and individual power plant emission/location profiles."
- dashboard:is_periodic True
- dashboard:time_density "month"
- title "ODIAC Fossil Fuel CO₂ Emissions v2023"
extent
spatial
bbox[] 1 items
0[] 4 items
- 0 -180
- 1 -90
- 2 180
- 3 90
temporal
interval[] 1 items
0[] 2 items
- 0 "2000-01-01T00:00:00Z"
- 1 "2022-12-31T00:00:00Z"
- license "CC-BY-4.0"
providers[] 1 items
0
- name "National Institute for Environmental Studies"
roles[] 2 items
- 0 "producer"
- 1 "licensor"
- url "https://www.nies.go.jp"
summaries
datetime[] 2 items
- 0 "2000-01-01T00:00:00Z"
- 1 "2022-12-31T00:00:00Z"
Examining the contents of our collection
under summaries
we see that the data is available from January 2000 to December 2022. By looking at the dashboard:time density
we observe that the periodic frequency of these observations is monthly.
#items = list(collection.get_items()) # Convert the iterator to a list
#print(f"Found {len(items)} items")
# The search function lets you search for items within a specific date/time range
= catalog.search(
search =collection_name,
collections=['2010-01-01T00:00:00Z','2015-12-31T00:00:00Z']
datetime
)= search.item_collection()
items # Take a look at the items we found
for item in items:
print(item)
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201512>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201511>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201510>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201509>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201508>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201507>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201506>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201505>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201504>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201503>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201502>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201501>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201412>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201411>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201410>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201409>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201408>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201407>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201406>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201405>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201404>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201403>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201402>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201401>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201312>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201311>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201310>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201309>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201308>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201307>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201306>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201305>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201304>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201303>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201302>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201301>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201212>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201211>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201210>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201209>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201208>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201207>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201206>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201205>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201204>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201203>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201202>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201201>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201112>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201111>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201110>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201109>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201108>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201107>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201106>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201105>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201104>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201103>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201102>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201101>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201012>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201011>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201010>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201009>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201008>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201007>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201006>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201005>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201004>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201003>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201002>
<Item id=odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201001>
# Examine the first item in the collection
# Keep in mind that a list starts from 0, 1, 2... therefore items[0] is referring to the first item in the list/collection
0] items[
- type "Feature"
- stac_version "1.0.0"
stac_extensions[] 2 items
- 0 "https://stac-extensions.github.io/raster/v1.1.0/schema.json"
- 1 "https://stac-extensions.github.io/projection/v1.1.0/schema.json"
- id "odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201512"
geometry
- type "Polygon"
coordinates[] 1 items
0[] 5 items
0[] 2 items
- 0 -180
- 1 -90
1[] 2 items
- 0 180
- 1 -90
2[] 2 items
- 0 180
- 1 90
3[] 2 items
- 0 -180
- 1 90
4[] 2 items
- 0 -180
- 1 -90
bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
properties
- end_datetime "2015-12-31T00:00:00+00:00"
- start_datetime "2015-12-01T00:00:00+00:00"
- datetime None
links[] 5 items
0
- rel "collection"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023"
- type "application/json"
1
- rel "parent"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023"
- type "application/json"
2
- rel "root"
- href "https://earth.gov/ghgcenter/api/stac"
- type "application/json"
- title "US GHG Center STAC API"
3
- rel "self"
- href "https://earth.gov/ghgcenter/api/stac/collections/odiac-ffco2-monthgrid-v2023/items/odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201512"
- type "application/geo+json"
4
- rel "preview"
- href "https://earth.gov/ghgcenter/api/raster/collections/odiac-ffco2-monthgrid-v2023/items/odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201512/map?assets=co2-emissions&rescale=-10%2C60&colormap_name=jet"
- type "text/html"
- title "Map of Item"
assets
co2-emissions
- href "s3://ghgc-data-store/odiac-ffco2-monthgrid-v2023/odiac2023_1km_excl_intl_201512.tif"
- type "image/tiff; application=geotiff"
- title "Fossil Fuel CO₂ Emissions"
- description "Model-estimated monthly, 1 km resolution CO₂ emissions from fossil fuel combustion, cement production and gas flaring created using space-based nighttime light data and individual power plant emission/location profiles."
proj:bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
- proj:epsg 4326
- proj:wkt2 "GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]"
proj:shape[] 2 items
- 0 21600
- 1 43200
raster:bands[] 1 items
0
- scale 1.0
- nodata -9999.0
- offset 0.0
- sampling "area"
- data_type "float32"
histogram
- max 41811.08203125
- min -824.8943481445312
- count 11
buckets[] 10 items
- 0 14879
- 1 6
- 2 0
- 3 1
- 4 0
- 5 0
- 6 0
- 7 0
- 8 0
- 9 1
statistics
- mean 37.9515726808625
- stddev 386.7686107112524
- maximum 41811.08203125
- minimum -824.8943481445312
- valid_percent 2.8394699096679688
proj:geometry
- type "Polygon"
coordinates[] 1 items
0[] 5 items
0[] 2 items
- 0 -180.0
- 1 -90.0
1[] 2 items
- 0 180.0
- 1 -90.0
2[] 2 items
- 0 180.0
- 1 90.0
3[] 2 items
- 0 -180.0
- 1 90.0
4[] 2 items
- 0 -180.0
- 1 -90.0
proj:projjson
id
- code 4326
- authority "EPSG"
- name "WGS 84"
- type "GeographicCRS"
datum
- name "World Geodetic System 1984"
- type "GeodeticReferenceFrame"
ellipsoid
- name "WGS 84"
- semi_major_axis 6378137
- inverse_flattening 298.257223563
- $schema "https://proj.org/schemas/v0.7/projjson.schema.json"
coordinate_system
axis[] 2 items
0
- name "Geodetic latitude"
- unit "degree"
- direction "north"
- abbreviation "Lat"
1
- name "Geodetic longitude"
- unit "degree"
- direction "east"
- abbreviation "Lon"
- subtype "ellipsoidal"
proj:transform[] 9 items
- 0 0.008333333333333333
- 1 0.0
- 2 -180.0
- 3 0.0
- 4 -0.008333333333333333
- 5 90.0
- 6 0.0
- 7 0.0
- 8 1.0
roles[] 2 items
- 0 "data"
- 1 "layer"
rendered_preview
- href "https://earth.gov/ghgcenter/api/raster/collections/odiac-ffco2-monthgrid-v2023/items/odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201512/preview.png?assets=co2-emissions&rescale=-10%2C60&colormap_name=jet"
- type "image/png"
- title "Rendered preview"
- rel "preview"
roles[] 1 items
- 0 "overview"
- collection "odiac-ffco2-monthgrid-v2023"
# Examine the first item in the collection
# Keep in mind that a list starts from 0, 1, 2... therefore items[0] is referring to the first item in the list/collection
= {item.properties["start_datetime"][:7]: item for item in items} items_dict
# Before we go further, let's pick which asset to focus on for the remainder of the notebook.
# This dataset only has one asset:
= "co2-emissions" asset_name
Creating Maps Using Folium
We will explore changes in fossil fuel emissions in urban regions, visualizing the data on a map using folium
.
Fetch Imagery Using Raster API
Here we get information from the Raster API
which we will add to our map in the next section.
# Specify two date/times that you would like to visualize, using the format of items_dict.keys()
= ["2015-01","2015-04"] dates
Below, we use some statistics of the raster data to set upper and lower limits for our color bar. These are saved as the rescale_values
, and will be passed to the Raster API in the following step(s).
# Extract collection name and item ID for the first date
= items_dict[dates[0]]
first_date = first_date.collection_id
collection_id_1 = first_date.id
item_id_1 # Select relevant asset
object = first_date.assets[asset_name]
= object.extra_fields.get("raster:bands", [{}])
raster_bands # Print raster bands' information
raster_bands
[{'scale': 1.0,
'nodata': -9999.0,
'offset': 0.0,
'sampling': 'area',
'data_type': 'float32',
'histogram': {'max': 38934.10546875,
'min': -768.0534057617188,
'count': 11,
'buckets': [14879, 6, 0, 1, 0, 0, 0, 0, 0, 1]},
'statistics': {'mean': 35.59434825686841,
'stddev': 360.9807471537631,
'maximum': 38934.10546875,
'minimum': -768.0534057617188,
'valid_percent': 2.8394699096679688}}]
# Use statistics to generate an appropriate colorbar range
= {
rescale_values "max": raster_bands[0]["statistics"]["mean"] + 2*raster_bands[0]["statistics"]["stddev"],
"min": 0,
}
print(rescale_values)
{'max': 757.5558425643947, 'min': 0}
Now, you will pass the item id
, collection name
, asset name
, and the rescale values
to the Raster API endpoint, along with a colormap. This step is done twice, one for each date/time you will visualize, and tells the Raster API which collection, item, and asset you want to view, specifying the colormap and colorbar ranges to use for visualization. The API returns a JSON with information about the requested image. Each image will be referred to as a tile.
# Choose a colormap for displaying the data
# Make sure to capitalize per Matplotlib standard colormap names
# For more information on Colormaps in Matplotlib, please visit https://matplotlib.org/stable/users/explain/colors/colormaps.html
= "Spectral_r" color_map
# Make a GET request to retrieve information for the date mentioned below
= requests.get(
tile1 f"{RASTER_API_URL}/collections/{collection_id_1}/items/{item_id_1}/tilejson.json?"
f"&assets={asset_name}"
f"&color_formula=gamma+r+1.05&colormap_name={color_map.lower()}"
f"&rescale={rescale_values['min']},{rescale_values['max']}"
).json()
# Print the properties of the retrieved granule to the console
tile1
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://earth.gov/ghgcenter/api/raster/collections/odiac-ffco2-monthgrid-v2023/items/odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201501/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?assets=co2-emissions&color_formula=gamma+r+1.05&colormap_name=spectral_r&rescale=0%2C757.5558425643947'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
# Repeat the above for your second date/time
# Note that we do not calculate new rescale_values for this tile
# We want date tiles 1 and 2 to have the same colorbar range for visual comparison.
= items_dict[dates[1]]
second_date # Extract collection name and item ID
= second_date.collection_id
collection_id_2 = second_date.id
item_id_2
= requests.get(
tile2 f"{RASTER_API_URL}/collections/{collection_id_2}/items/{item_id_2}/tilejson.json?"
f"&assets={asset_name}"
f"&color_formula=gamma+r+1.05&colormap_name={color_map.lower()}"
f"&rescale={rescale_values['min']},{rescale_values['max']}"
).json()
# Print the properties of the retrieved granule to the console
tile2
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://earth.gov/ghgcenter/api/raster/collections/odiac-ffco2-monthgrid-v2023/items/odiac-ffco2-monthgrid-v2023-odiac2023_1km_excl_intl_201504/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?assets=co2-emissions&color_formula=gamma+r+1.05&colormap_name=spectral_r&rescale=0%2C757.5558425643947'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
Generate Map
# Initialize the map, specifying the center of the map and the starting zoom level.
# 'folium.plugins' allows mapping side-by-side via 'DualMap'
# Map is centered on the position specified by "location=(lat,lon)"
= folium.plugins.DualMap(location=(41.8, -87.6), zoom_start=9)
map_
# Define the first map layer using the tile fetched for the first date
# The TileLayer library helps in manipulating and displaying raster layers on a map
= TileLayer(
map_layer_1 "tiles"][0], # Path to retrieve the tile
tile1[="GHG", # Set the attribution
attr=0.8, # Adjust the transparency of the layer
opacity=True,
overlay=f"{collection.title}, {dates[0]}"
name
)
# Add the first layer to the Dual Map
map_layer_1.add_to(map_.m1)
# Define the second map layer using the tile fetched for the second date
= TileLayer(
map_layer_2 "tiles"][0], # Path to retrieve the tile
tile2[="GHG", # Set the attribution
attr=0.8, # Adjust the transparency of the layer
opacity=True,
overlay=f"{collection.title}, {dates[1]}"
name
)
# Add the second layer to the Dual Map
map_layer_2.add_to(map_.m2)
# Add a layer control to switch between map layers
=False).add_to(map_)
folium.LayerControl(collapsed
# Add colorbar
# We can use 'generate_html_colorbar' from the 'ghgc_utils' module
# to create an HTML colorbar representation.
= ghgc_utils.generate_html_colorbar(color_map,rescale_values,label=f'{items[0].assets[asset_name].title} (tonne C/km2/month)')
legend_html
# Add colorbar to the map
map_.get_root().html.add_child(folium.Element(legend_html))
# Visualize the Dual Map
map_
Observe that fossil fuel CO2 emissions are much higher in January than in April in Chicago. This is due in large part to the use of central heating, which often relies on burning oil or natural gas, during the cold Midwestern months.
Calculating Zonal Statistics
To perform zonal statistics, first we need to create a polygon.
# We'll start with an AOI over Chicago. Give it a name for use later
= "Chicago, USA"
aoi_name # Define the AOI as a GeoJSON
= {
aoi "type": "Feature", # Create a feature object
"properties": {},
"geometry": { # Set the bounding coordinates for the polygon
"coordinates": [
[-88.5, 42.2], # South-east bounding coordinate
[-88.5, 41.4], # North-east bounding coordinate
[-87.5,41.4], # North-west bounding coordinate
[-87.5,42.2], # South-west bounding coordinate
[-88.5, 42.2] # South-east bounding coordinate (closing the polygon)
[
]
],"type": "Polygon",
}, }
# Quick Folium map to visualize this AOI
= folium.Map(location=(41.8, -88), zoom_start=8)
map_ # Add AOI to map
=aoi_name,style_function=lambda feature:{"fillColor":"none","color":"#E467E8"}).add_to(map_)
folium.GeoJson(aoi, name# Add data layer to visualize number of grid cells within AOI
map_layer_1.add_to(map_)# Add a colorbar
= ghgc_utils.generate_html_colorbar(color_map,rescale_values,label='tonne CO2/km2/month',dark=True)
legend_html
map_.get_root().html.add_child(folium.Element(legend_html)) map_
We can generate the statistics for the AOI using a function from the ghgc_utils
module, which fetches the data and its statistics from the Raster API.
%%time
# %%time = Wall time (execution time) for running the code below
# Statistics will be returned as a Pandas DataFrame
= ghgc_utils.generate_stats(items,aoi,url=RASTER_API_URL,asset=asset_name)
df # Print the first 5 rows of our statistics DataFrame
5) df.head(
Generating stats...
Done!
CPU times: user 212 ms, sys: 34.5 ms, total: 247 ms
Wall time: 32.3 s
datetime | min | max | mean | count | sum | std | median | majority | minority | unique | histogram | valid_percent | masked_pixels | valid_pixels | percentile_2 | percentile_98 | date | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2015-12-01T00:00:00+00:00 | 4.67938470840454101562 | 167770.23437500000000000000 | 243.48350524902343750000 | 10410.00000000000000000000 | 2534663.25000000000000000000 | 2459.67121786632287694374 | 165.95074462890625000000 | 581.76385498046875000000 | 4.67938470840454101562 | 9074.00000000000000000000 | [[10403, 1, 2, 0, 1, 1, 0, 0, 1, 1], [4.679384... | 90.35999999999999943157 | 1110.00000000000000000000 | 10410.00000000000000000000 | 7.64125776290893554688 | 693.57031250000000000000 | 2015-12-01 00:00:00+00:00 |
1 | 2015-11-01T00:00:00+00:00 | 4.16928482055664062500 | 149296.93750000000000000000 | 218.57122802734375000000 | 10410.00000000000000000000 | 2275326.50000000000000000000 | 2188.83553516475967626320 | 149.50103759765625000000 | 518.34576416015625000000 | 4.16928482055664062500 | 9024.00000000000000000000 | [[10403, 1, 2, 0, 1, 1, 0, 0, 1, 1], [4.169284... | 90.35999999999999943157 | 1110.00000000000000000000 | 10410.00000000000000000000 | 6.80828428268432617188 | 617.96423339843750000000 | 2015-11-01 00:00:00+00:00 |
2 | 2015-10-01T00:00:00+00:00 | 4.12834739685058593750 | 147730.53125000000000000000 | 216.55181884765625000000 | 10410.00000000000000000000 | 2254304.50000000000000000000 | 2165.87615066051284884452 | 148.14408874511718750000 | 513.25622558593750000000 | 4.12834739685058593750 | 8954.00000000000000000000 | [[10403, 1, 2, 0, 1, 1, 0, 0, 1, 1], [4.128347... | 90.35999999999999943157 | 1110.00000000000000000000 | 10410.00000000000000000000 | 6.74143505096435546875 | 611.89654541015625000000 | 2015-10-01 00:00:00+00:00 |
3 | 2015-09-01T00:00:00+00:00 | 4.02172708511352539062 | 143930.67187500000000000000 | 211.35943603515625000000 | 10410.00000000000000000000 | 2200251.75000000000000000000 | 2110.16598873169232319924 | 144.87901306152343750000 | 500.00067138671875000000 | 4.02172708511352539062 | 9109.00000000000000000000 | [[10403, 1, 2, 0, 1, 1, 0, 0, 1, 1], [4.021727... | 90.35999999999999943157 | 1110.00000000000000000000 | 10410.00000000000000000000 | 6.56732845306396484375 | 596.09350585937500000000 | 2015-09-01 00:00:00+00:00 |
4 | 2015-08-01T00:00:00+00:00 | 4.37767410278320312500 | 156797.85937500000000000000 | 228.73756408691406250000 | 10410.00000000000000000000 | 2381158.00000000000000000000 | 2298.80653818454220527201 | 156.43536376953125000000 | 544.25378417968750000000 | 4.37767410278320312500 | 8924.00000000000000000000 | [[10403, 1, 2, 0, 1, 1, 0, 0, 1, 1], [4.377674... | 90.35999999999999943157 | 1110.00000000000000000000 | 10410.00000000000000000000 | 7.14857625961303710938 | 648.85137939453125000000 | 2015-08-01 00:00:00+00:00 |
Time-Series Analysis
We can now explore the ODIAC fossil fuel emission time series available for our AOI. We can plot the data set using the code below.
# Figure size: 20 representing the width, 10 representing the height
= plt.figure(figsize=(10,5))
fig
# Use which_stat to pick which statisticm from our DataFrame to visualize
= "mean"
which_stat
plt.plot("date"], # X-axis: sorted datetime
df[# Y-axis: maximum CO₂ level
df[which_stat], ="red", # Line color
color="-", # Line style
linestyle=2, # Line width
linewidth=f"{which_stat.capitalize()} {items[0].assets[asset_name].title}" # Legend label
label
)
# Display legend
plt.legend()
# Insert label for the X-axis
"Years")
plt.xlabel(
# Insert label for the Y-axis
"tonne CO$_2$/km2/month")
plt.ylabel(
# Insert title for the plot
f"{which_stat.capitalize()} {items[0].assets[asset_name].title} for {aoi_name}")
plt.title(
###
# Add data citation
plt.text("date"].iloc[40], # X-coordinate of the text
df[min(), # Y-coordinate of the text
df[which_stat].# Text to be displayed
f"Source: {collection.title}",
=9, # Font size
fontsize="blue", # Text color
color
)
# Plot the time series
plt.show()
You can see emissions peak in winter in this time series, similarly to the Dual Map in the previous section!
Summary
In this notebook we have successfully explored, analysed and visualized STAC collecetion for ODIAC C02 fossisl fuel emission (2023).
- Install and import the necessary libraries
- Fetch the collection from STAC collections using the appropriate endpoints
- Count the number of existing granules within the collection
- Map and compare the CO₂ levels for two distinctive months/years
- Generate zonal statistics for the area of interest (AOI)
- Visualizing the Data as a Time Series
If you have any questions regarding this user notebook, please contact us using the feedback form.