import requests
import folium
import folium.plugins
from folium import Map, TileLayer
from pystac_client import Client
import pandas as pd
import matplotlib.pyplot as plt
OCO-2 MIP Top-Down CO₂ Budgets
Approach
- Identify available dates and temporal frequency of observations for the given collection using the GHGC API
/stac
endpoint. The collection processed in this notebook is the OCO-2 MIP Top-Down CO₂ Budgets data product. - Pass the STAC item into the raster API
/stac/tilejson.json
endpoint. - Using
folium.plugins.DualMap
, we will visualize two tiles (side-by-side), allowing us to compare time points. - After the visualization, we will perform zonal statistics for a given polygon.
About the Data
The Committee on Earth Observation Satellites (CEOS) Atmospheric Composition - Virtual Constellation (AC-VC) Greenhouse Gas (GHG) team has generated the CEOS CO₂ Budgets dataset, which provides annual top-down carbon dioxide (CO2) emissions and removals from 2015 - 2020 gridded globally at 1° resolution, and as national totals. Data is provided in units of grams of carbon dioxide per square meter per year (g CO2/m2/yr). Only a subset of the full dataset is displayed in the GHG Center explore view.
For more information regarding this dataset, please visit the OCO-2 MIP Top-Down CO₂ Budgets data overview page.
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
Querying the STAC API
First, we are going to import the required libraries. Once imported, they allow better executing a query in the GHG Center Spatio Temporal Asset Catalog (STAC) Application Programming Interface (API) where the granules for this collection are stored.
# 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.
= "http://ghg.center/api/stac"
STAC_API_URL
# The RASTER API is used to fetch collections for visualization
= "https://ghg.center/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 CEOS National Top-Down CO₂ Budgets dataset
= "oco2-mip-co2budget-yeargrid-v1" collection_name
# Fetch the collection from the STAC API using the appropriate endpoint
# The 'requests' library allows a HTTP request possible
= requests.get(f"{STAC_API_URL}/collections/{collection_name}").json()
collection
# Print the properties of the collection to the console
collection
Examining the contents of our collection
under the temporal
variable, we see that the data is available from January 2015 to December 2020. By looking at the dashboard:time density
, we observe that the periodic frequency of these observations is yearly.
# Create a function that would search for a data collection in the US GHG Center STAC API
# First, we need to define the function
# The name of the function = "get_item_count"
# The argument that will be passed through the defined function = "collection_id"
def get_item_count(collection_id):
# Set a counter for the number of items existing in the collection
= 0
count
# Define the path to retrieve the granules (items) of the collection of interest in the STAC API
= f"{STAC_API_URL}/collections/{collection_id}/items"
items_url
# Run a while loop to make HTTP requests until there are no more URLs associated with the collection in the STAC API
while True:
# Retrieve information about the granules by sending a "get" request to the STAC API using the defined collection path
= requests.get(items_url)
response
# If the items do not exist, print an error message and quit the loop
if not response.ok:
print("error getting items")
exit()
# Return the results of the HTTP response as JSON
= response.json()
stac
# Increase the "count" by the number of items (granules) returned in the response
+= int(stac["context"].get("returned", 0))
count
# Retrieve information about the next URL associated with the collection in the STAC API (if applicable)
next = [link for link in stac["links"] if link["rel"] == "next"]
# Exit the loop if there are no other URLs
if not next:
break
# Ensure the information gathered by other STAC API links associated with the collection are added to the original path
# "href" is the identifier for each of the tiles stored in the STAC API
= next[0]["href"]
items_url
# Return the information about the total number of granules found associated with the collection
return count
# Apply the function created above "get_item_count" to the data collection
= get_item_count(collection_name)
number_of_items
# Get the information about the number of granules found in the collection
= requests.get(f"{STAC_API_URL}/collections/{collection_name}/items?limit={number_of_items}").json()["features"]
items
# Print the total number of items (granules) found
print(f"Found {len(items)} items")
Found 6 items
# 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[
Exploring Changes in CO₂ Levels Using the Raster API
In this notebook, we will explore the global changes of CO₂ budgets over time in urban regions. We will visualize the outputs on a map using folium
.
# Now we create a dictionary where the start datetime values for each granule is queried more explicitly by year and month (e.g., 2020-02)
= {item["properties"]["start_datetime"]: item for item in items}
items
# Next, we need to specify the asset name for this collection
# The asset name is referring to the raster band containing the pixel values for the parameter of interest
# For the case of the OCO-2 MIP Top-Down CO₂ Budgets collection, the parameter of interest is “ff”
= "ff" #fossil fuel asset_name
Below, we are entering the minimum and maximum values to provide our upper and lower bounds in the rescale_values
.
# Fetching the min and max values for a specific item
= {"max":items[list(items.keys())[0]]["assets"][asset_name]["raster:bands"][0]["histogram"]["max"], "min":items[list(items.keys())[0]]["assets"][asset_name]["raster:bands"][0]["histogram"]["min"]}
rescale_values
# Hardcoding the min and max values to match the scale in the GHG Center dashboard
= {"max": 450, "min": 0} rescale_values
Now, we will pass the item id, collection name, asset name, and the rescaling factor
to the Raster API
endpoint. We will do this twice, once for 2020 and again for 2019, so that we can visualize each event independently.
# Choose a color map for displaying the first observation (event)
# Please refer to matplotlib library if you'd prefer choosing a different color ramp.
# For more information on Colormaps in Matplotlib, please visit https://matplotlib.org/stable/users/explain/colors/colormaps.html
= "purd"
color_map
# Make a GET request to retrieve information for the 2020 tile which is the 1st item in the collection
# To retrieve the first item in the collection we use "0" in the "(items.keys())[0]" statement
# 2020
= requests.get(
co2_flux_1
# Pass the collection name, the item number in the list, and its ID
f"{RASTER_API_URL}/stac/tilejson.json?collection={items[list(items.keys())[0]]['collection']}&item={items[list(items.keys())[0]]['id']}"
# Pass the asset name
f"&assets={asset_name}"
# Pass the color formula and colormap for custom visualization
f"&color_formula=gamma+r+1.05&colormap_name={color_map}"
# Pass the minimum and maximum values for rescaling
f"&rescale={rescale_values['min']},{rescale_values['max']}",
# Return the response in JSON format
).json()
# Print the properties of the retrieved granule to the console
co2_flux_1
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://ghg.center/api/raster/stac/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?collection=oco2-mip-co2budget-yeargrid-v1&item=oco2-mip-co2budget-yeargrid-v1-2020&assets=ff&color_formula=gamma+r+1.05&colormap_name=purd&rescale=0%2C450'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
# Make a GET request to retrieve information for the 2019 tile which is the 2st item in the collection
# To retrieve the second item in the collection we use "1" in the "(items.keys())[1]" statement
# 2019
= requests.get(
co2_flux_2
# Pass the collection name, the item number in the list, and its ID
f"{RASTER_API_URL}/stac/tilejson.json?collection={items[list(items.keys())[1]]['collection']}&item={items[list(items.keys())[1]]['id']}"
# Pass the asset name
f"&assets={asset_name}"
# Pass the color formula and colormap for custom visualization
f"&color_formula=gamma+r+1.05&colormap_name={color_map}"
# Pass the minimum and maximum values for rescaling
f"&rescale={rescale_values['min']},{rescale_values['max']}",
# Return the response in JSON format
).json()
# Print the properties of the retrieved granule to the console
co2_flux_2
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://ghg.center/api/raster/stac/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?collection=oco2-mip-co2budget-yeargrid-v1&item=oco2-mip-co2budget-yeargrid-v1-2019&assets=ff&color_formula=gamma+r+1.05&colormap_name=purd&rescale=0%2C450'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
Visualizing CO₂ Emissions
# For this study we are going to compare the CO2 budget in 2020 and 2019 along the coast of California
# To change the location, you can simply insert the latitude and longitude of the area of your interest in the "location=(LAT, LONG)" statement
# Set the initial zoom level and center of map for both tiles
# 'folium.plugins' allows mapping side-by-side
= folium.plugins.DualMap(location=(34, -118), zoom_start=6)
map_
# Define the first map layer (2020)
= TileLayer(
map_layer_2020 =co2_flux_1["tiles"][0], # Path to retrieve the tile
tiles="GHG", # Set the attribution
attr=0.5, # Adjust the transparency of the layer
opacity
)
# Add the first layer to the Dual Map
map_layer_2020.add_to(map_.m1)
# Define the second map layer (2019)
= TileLayer(
map_layer_2019 =co2_flux_2["tiles"][0], # Path to retrieve the tile
tiles="GHG", # Set the attribution
attr=0.5, # Adjust the transparency of the layer
opacity
)
# Add the second layer to the Dual Map
map_layer_2019.add_to(map_.m2)
# Visualize the Dual Map
map_
Calculating Zonal Statistics
To perform zonal statistics, first we need to create a polygon. In this use case we are creating a polygon in Texas (USA).
# Create a polygon for the area of interest (aoi)
= {
texas_aoi "type": "Feature", # Create a feature object
"properties": {},
"geometry": { # Set the bounding coordinates for the polygon
"coordinates": [
[-95, 29], # South-east bounding coordinate
[-95, 33], # North-east bounding coordinate
[-104,33], # North-west bounding coordinate
[-104,29], # South-west bounding coordinate
[-95, 29] # South-east bounding coordinate (closing the polygon)
[
]
],"type": "Polygon",
}, }
# Create a new map to display the generated polygon
# We'll plug in the coordinates for a location
# central to the study area and a reasonable zoom level
= Map(
aoi_map
# Base map is set to OpenStreetMap
="OpenStreetMap",
tiles
# Define the spatial properties for the map
=[
location30,-100
],
# Set the zoom value
=6,
zoom_start
)
# Insert the polygon to the map
="Texas, USA").add_to(aoi_map)
folium.GeoJson(texas_aoi, name
# Visualize the map
aoi_map
# Check total number of items available within the collection
= requests.get(
items f"{STAC_API_URL}/collections/{collection_name}/items?limit=600"
"features"]
).json()[
# Print the total number of items (granules) found
print(f"Found {len(items)} items")
Found 6 items
# Examine the first item in the collection
0] items[
Now that we created the polygon for the area of interest, we need to develop a function that runs through the data collection and generates the statistics for a specific item (granule) within the boundaries of the AOI polygon.
# The bounding box should be passed to the geojson param as a geojson Feature or FeatureCollection
# Create a function that retrieves information regarding a specific granule using its asset name and raster identifier and generates the statistics for it
# The function takes an item (granule) and a JSON (polygon) as input parameters
def generate_stats(item, geojson):
# A POST request is made to submit the data associated with the item of interest (specific observation) within the boundaries of the polygon to compute its statistics
= requests.post(
result
# Raster API Endpoint for computing statistics
f"{RASTER_API_URL}/cog/statistics",
# Pass the URL to the item, asset name, and raster identifier as parameters
={"url": item["assets"][asset_name]["href"]},
params
# Send the GeoJSON object (polygon) along with the request
=geojson,
json
# Return the response in JSON format
).json()
# Print the result
print(result)
# Return a dictionary containing the computed statistics along with the item's datetime information.
return {
**result["properties"],
"datetime": item["properties"]["start_datetime"],
}
# Generate a for loop that iterates over all the existing items in the collection
for item in items:
# The loop will then retrieve the information for the start datetime of each item in the list
print(item["properties"]["start_datetime"])
# Exit the loop after printing the start datetime for the first item in the collection
break
2020-01-01T00:00:00+00:00
With the function above we can generate the statistics for the AOI.
%%time
# %%time = Wall time (execution time) for running the code below
# Generate statistics using the created function "generate_stats" within the bounding box defined by the polygon
= [generate_stats(item, texas_aoi) for item in items] stats
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.03978176414966583, 'max': 7348.5, 'mean': 710.6574534496499, 'count': 36.0, 'sum': 25583.668324187398, 'std': 1398.048920955156, 'median': 92.79795837402344, 'majority': 0.03978176414966583, 'minority': 0.03978176414966583, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.03978176414966583, 734.8858035877347, 1469.7318254113197, 2204.5778472349048, 2939.42386905849, 3674.269890882075, 4409.11591270566, 5143.961934529245, 5878.80795635283, 6613.653978176415, 7348.5]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.7717349633574486, 'percentile_98': 4147.699072265612}}}}
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.09139306098222733, 'max': 8174.17724609375, 'mean': 795.2997507835842, 'count': 36.0, 'sum': 28630.79102820903, 'std': 1557.5783137149344, 'median': 108.24344635009766, 'majority': 0.09139306098222733, 'minority': 0.09139306098222733, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.09139306098222733, 817.499978364259, 1634.9085636675359, 2452.317148970813, 3269.7257342740895, 4087.134319577366, 4904.542904880644, 5721.95149018392, 6539.360075487197, 7356.768660790473, 8174.17724609375]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.9499634824693204, 'percentile_98': 4609.885083007798}}}}
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.09151247888803482, 'max': 8392.841796875, 'mean': 816.3306965242243, 'count': 36.0, 'sum': 29387.905074872077, 'std': 1599.1110746714692, 'median': 110.89262771606445, 'majority': 0.09151247888803482, 'minority': 0.09151247888803482, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.09151247888803482, 839.3665409184993, 1678.6415693581105, 2517.916597797722, 3357.191626237333, 4196.466654676944, 5035.741683116556, 5875.016711556166, 6714.291739995778, 7553.566768435389, 8392.841796875]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.9720504634082319, 'percentile_98': 4733.363476562485}}}}
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.0910222977399826, 'max': 8138.96484375, 'mean': 791.9455965670446, 'count': 36.0, 'sum': 28510.041476413608, 'std': 1550.8237787859084, 'median': 107.77805709838867, 'majority': 0.0910222977399826, 'minority': 0.0910222977399826, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.0910222977399826, 813.9784044429659, 1627.865786588192, 2441.7531687334176, 3255.640550878644, 4069.52793302387, 4883.415315169095, 5697.302697314321, 6511.190079459548, 7325.077461604774, 8138.96484375]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.9726840510964394, 'percentile_98': 4589.991333007798}}}}
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.08842560648918152, 'max': 8243.259765625, 'mean': 801.7388236944875, 'count': 36.0, 'sum': 28862.597653001547, 'std': 1570.4892862189242, 'median': 108.75741958618164, 'majority': 0.08842560648918152, 'minority': 0.08842560648918152, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.08842560648918152, 824.4055596083402, 1648.7226936101913, 2473.0398276120422, 3297.3569616138934, 4121.674095615745, 4945.991229617595, 5770.308363619447, 6594.625497621298, 7418.942631623148, 8243.259765625]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.987190940976143, 'percentile_98': 4649.222045898423}}}}
{'type': 'Feature', 'geometry': {'type': 'Polygon', 'coordinates': [[[-95.0, 29.0], [-95.0, 33.0], [-104.0, 33.0], [-104.0, 29.0], [-95.0, 29.0]]]}, 'properties': {'statistics': {'b1': {'min': 0.08379923552274704, 'max': 8397.8779296875, 'mean': 816.0609874193453, 'count': 36.0, 'sum': 29378.19554709643, 'std': 1599.6065142777418, 'median': 110.1262092590332, 'majority': 0.08379923552274704, 'minority': 0.08379923552274704, 'unique': 36.0, 'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.08379923552274704, 839.8632122807205, 1679.6426253259183, 2519.4220383711163, 3359.201451416314, 4198.980864461511, 5038.76027750671, 5878.539690551907, 6718.319103597105, 7558.0985166423025, 8397.8779296875]], 'valid_percent': 100.0, 'masked_pixels': 0.0, 'valid_pixels': 36.0, 'percentile_2': 0.9802812077105046, 'percentile_98': 4736.898437499985}}}}
CPU times: user 115 ms, sys: 2.32 ms, total: 117 ms
Wall time: 1.35 s
# Print the stats for the first item in the collection
0] stats[
{'statistics': {'b1': {'min': 0.03978176414966583,
'max': 7348.5,
'mean': 710.6574534496499,
'count': 36.0,
'sum': 25583.668324187398,
'std': 1398.048920955156,
'median': 92.79795837402344,
'majority': 0.03978176414966583,
'minority': 0.03978176414966583,
'unique': 36.0,
'histogram': [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.03978176414966583,
734.8858035877347,
1469.7318254113197,
2204.5778472349048,
2939.42386905849,
3674.269890882075,
4409.11591270566,
5143.961934529245,
5878.80795635283,
6613.653978176415,
7348.5]],
'valid_percent': 100.0,
'masked_pixels': 0.0,
'valid_pixels': 36.0,
'percentile_2': 0.7717349633574486,
'percentile_98': 4147.699072265612}},
'datetime': '2020-01-01T00:00:00+00:00'}
Create a function that goes through every single item in the collection and populates their properties - including the minimum, maximum, and sum of their values - in a table.
# Create a function that converts statistics in JSON format into a pandas DataFrame
def clean_stats(stats_json) -> pd.DataFrame:
# Normalize the JSON data
= pd.json_normalize(stats_json)
df
# Replace the naming "statistics.b1" in the columns
= [col.replace("statistics.b1.", "") for col in df.columns]
df.columns
# Set the datetime format
"date"] = pd.to_datetime(df["datetime"])
df[
# Return the cleaned format
return df
# Apply the generated function on the stats data
= clean_stats(stats)
df
# Display the stats for the first 5 granules in the collection in the table
# Change the value in the parenthesis to show more or a smaller number of rows in the table
5) df.head(
datetime | min | max | mean | count | sum | std | median | majority | minority | unique | histogram | valid_percent | masked_pixels | valid_pixels | percentile_2 | percentile_98 | date | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2020-01-01T00:00:00+00:00 | 0.039782 | 7348.500000 | 710.657453 | 36.0 | 25583.668324 | 1398.048921 | 92.797958 | 0.039782 | 0.039782 | 36.0 | [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0... | 100.0 | 0.0 | 36.0 | 0.771735 | 4147.699072 | 2020-01-01 00:00:00+00:00 |
1 | 2019-01-01T00:00:00+00:00 | 0.091393 | 8174.177246 | 795.299751 | 36.0 | 28630.791028 | 1557.578314 | 108.243446 | 0.091393 | 0.091393 | 36.0 | [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0... | 100.0 | 0.0 | 36.0 | 0.949963 | 4609.885083 | 2019-01-01 00:00:00+00:00 |
2 | 2018-01-01T00:00:00+00:00 | 0.091512 | 8392.841797 | 816.330697 | 36.0 | 29387.905075 | 1599.111075 | 110.892628 | 0.091512 | 0.091512 | 36.0 | [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0... | 100.0 | 0.0 | 36.0 | 0.972050 | 4733.363477 | 2018-01-01 00:00:00+00:00 |
3 | 2017-01-01T00:00:00+00:00 | 0.091022 | 8138.964844 | 791.945597 | 36.0 | 28510.041476 | 1550.823779 | 107.778057 | 0.091022 | 0.091022 | 36.0 | [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0... | 100.0 | 0.0 | 36.0 | 0.972684 | 4589.991333 | 2017-01-01 00:00:00+00:00 |
4 | 2016-01-01T00:00:00+00:00 | 0.088426 | 8243.259766 | 801.738824 | 36.0 | 28862.597653 | 1570.489286 | 108.757420 | 0.088426 | 0.088426 | 36.0 | [[28.0, 2.0, 1.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0... | 100.0 | 0.0 | 36.0 | 0.987191 | 4649.222046 | 2016-01-01 00:00:00+00:00 |
Visualizing the Data as a Time Series
We can now explore the fossil fuel emission time series (January 2015 -December 2020) available for the Dallas, Texas area of the U.S. We can plot the data set using the code below:
# Figure size: 20 representing the width, 10 representing the height
= plt.figure(figsize=(20, 10))
fig
plt.plot("datetime"], # X-axis: sorted datetime
df["max"], # Y-axis: maximum CO₂ emission
df[="red", # Line color
color="-", # Line style
linestyle=0.5, # Line width
linewidth="CO2 emissions", # Legend label
label
)
# Display legend
plt.legend()
# Insert label for the X-axis
"Years")
plt.xlabel(
# Insert label for the Y-axis
"CO2 emissions gC/m2/year1")
plt.ylabel(
# Insert title for the plot
"CO2 emission Values for Texas, Dallas (2015-2020)")
plt.title(
# Add data citation
plt.text("datetime"].iloc[0], # X-coordinate of the text
df["max"].min(), # Y-coordinate of the text
df[
# Text to be displayed
"Source: NASA/NOAA OCO-2 MIP Top-Down CO₂ Budgets",
=12, # Font size
fontsize="left", # Horizontal alignment
horizontalalignment="top", # Vertical alignment
verticalalignment="blue", # Text color
color
)
# Plot the time series
plt.show()
Text(0.5, 1.0, 'CO2 emission Values for Texas, Dallas (2015-2020)')
# The 2018-01-01 observation is the 3rd item in the list.
# Considering that a list starts with "0", we need to insert "2" in the "items[2]" statement
print(items[2]["properties"]["start_datetime"])
2018-01-01T00:00:00+00:00
# A GET request is made for the 2018-01-01 tile
= requests.get(
co2_flux_3
# Pass the collection name, the item number in the list, and its ID
f"{RASTER_API_URL}/stac/tilejson.json?collection={items[2]['collection']}&item={items[2]['id']}"
# Pass the asset name
f"&assets={asset_name}"
# Pass the color formula and colormap for custom visualization
f"&color_formula=gamma+r+1.05&colormap_name={color_map}"
# Pass the minimum and maximum values for rescaling
f"&rescale={rescale_values['min']},{rescale_values['max']}",
# Return the response in JSON format
).json()
# Print the properties of the retrieved granule to the console
co2_flux_3
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://ghg.center/api/raster/stac/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?collection=oco2-mip-co2budget-yeargrid-v1&item=oco2-mip-co2budget-yeargrid-v1-2018&assets=ff&color_formula=gamma+r+1.05&colormap_name=purd&rescale=0%2C450'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
# Create a new map to display the 2018-01-01 tile
= Map(
aoi_map_bbox
# Base map is set to OpenStreetMap
="OpenStreetMap",
tiles
# Set the center of the map
=[
location30,-100
],
# Set the zoom value
=6.8,
zoom_start
)
# Define the map layer
= TileLayer(
map_layer
# Path to retrieve the tile
=co2_flux_3["tiles"][0],
tiles
# Set the attribution and adjust the transparency of the layer
="GHG", opacity = 0.7
attr
)
# Add the layer to the map
map_layer.add_to(aoi_map_bbox)
# Visualize the map
aoi_map_bbox
Summary
In this notebook we have successfully explored, analyzed, and visualized the STAC collection for OCO-2 MIP Top-Down CO₂ Budgets.
- 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
- Visualizing CO₂ Emissions for two distinctive months/years
- Generate zonal statistics for a specified region
- Generate a time-series graph
If you have any questions regarding this user notebook, please contact us using the feedback form.