# 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
CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes
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 CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes notebook in the US GHG Center JupyterHub.
Table of Contents
Data Summary and Application
- Spatial coverage: Global
- Spatial resolution: 1° x 1°
- Temporal extent: January 1998 - December 2021
- Temporal resolution: Monthly
- Unit: Grams of methane per square meter per year
- Utility: Climate Research
For more, visit the CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes data overview page.
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 CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes Data product. - Pass the STAC item into the raster API
/collections/{collection_id}/items/{item_id}/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
CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes
Surface methane (CH₄) emissions are derived from atmospheric measurements of methane and its ¹³C carbon isotope content. Different sources of methane contain different ratios of the two stable isotopologues, ¹²CH₄ and ¹³CH₄. This makes normally indistinguishable collocated sources of methane, say from agriculture and oil and gas exploration, distinguishable. The National Oceanic and Atmospheric Administration (NOAA) collects whole air samples from its global cooperative network of flasks (https://gml.noaa.gov/ccgg/about.html), which are then analyzed for methane and other trace gasses. A subset of those flasks are also analyzed for ¹³C of methane in collaboration with the Institute of Arctic and Alpine Research at the University of Colorado Boulder. Scientists at the National Aeronautics and Space Administration (NASA) and NOAA used those measurements of methane and ¹³C of methane in conjunction with a model of atmospheric circulation to estimate emissions of methane separated by three source types, microbial, fossil and pyrogenic.
For more information regarding this dataset, please visit the CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes 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. CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes - item
: One granule in the dataset, e.g. one monthly file of methane inverse fluxes - asset
: A variable available within the granule, e.g. microbial, fossil, or pyrogenic methane fluxes - 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 CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes dataset is ct-ch4-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 CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes
= "ct-ch4-monthgrid-v2023" collection_name
# Fetch the collection from the STAC API using the appropriate endpoint
# The 'pystac_client' library enables an HTTP request
= 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 "ct-ch4-monthgrid-v2023"
- stac_version "1.0.0"
- description "Surface methane (CH₄) emissions are derived from atmospheric measurements of methane and its ¹³C carbon isotope content. Different sources of methane contain different ratios of the two stable isotopologues, ¹²CH₄ and ¹³CH₄. This makes normally indistinguishable collocated sources of methane, say from agriculture and oil and gas exploration, distinguishable. The National Oceanic and Atmospheric Administration (NOAA) collects whole air samples from its global cooperative network of flasks (https://gml.noaa.gov/ccgg/about.html), which are then analyzed for methane and other trace gases. A subset of those flasks are also analyzed for ¹³C of methane in collaboration with the Institute of Arctic and Alpine Research at the University of Colorado Boulder. Scientists at NOAA and the National Aeronautics and Space Administration (NASA) used those measurements of methane and ¹³C of methane in conjunction with a model of atmospheric circulation to estimate emissions of methane separated by three source types: microbial, fossil and pyrogenic. This dataset presents monthly methane emissions in units of grams of methane per square meter per year (g CH₄/m²/year) from microbial, fossil and pyrogenic sources, along with a layer of total methane emissions from all three sources combined, at 1° resolution from 1998 to 2021. The source data can be found at https://doi.org/10.25925/40jt-qd67"
links[] 5 items
0
- rel "items"
- href "https://earth.gov/ghgcenter/api/stac/collections/ct-ch4-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/ct-ch4-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/ct-ch4-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
total
assets[] 1 items
- 0 "total"
rescale[] 1 items
0[] 2 items
- 0 0
- 1 50
- colormap_name "purd"
fossil
assets[] 1 items
- 0 "fossil"
rescale[] 1 items
0[] 2 items
- 0 0
- 1 50
- colormap_name "purd"
dashboard
assets[] 1 items
- 0 "total"
rescale[] 1 items
0[] 2 items
- 0 0
- 1 50
- colormap_name "purd"
microbial
assets[] 1 items
- 0 "microbial"
rescale[] 1 items
0[] 2 items
- 0 0
- 1 30
- colormap_name "purd"
pyrogenic
assets[] 1 items
- 0 "pyrogenic"
rescale[] 1 items
0[] 2 items
- 0 0
- 1 8
- colormap_name "purd"
item_assets
total
- type "image/tiff; application=geotiff; profile=cloud-optimized"
roles[] 2 items
- 0 "data"
- 1 "layer"
- title "Total CH₄ Emission"
- description "Total methane emission from microbial, fossil, and pyrogenic sources."
fossil
- type "image/tiff; application=geotiff; profile=cloud-optimized"
roles[] 2 items
- 0 "data"
- 1 "layer"
- title "Fossil CH₄ Emission"
- description "Emission of methane from all fossil sources, such as oil and gas activities and coal mining."
microbial
- type "image/tiff; application=geotiff; profile=cloud-optimized"
roles[] 2 items
- 0 "data"
- 1 "layer"
- title "Microbial CH₄ Emission"
- description "Emission of methane from all microbial sources, such as wetlands, ruminants, agriculture, and termites."
pyrogenic
- type "image/tiff; application=geotiff; profile=cloud-optimized"
roles[] 2 items
- 0 "data"
- 1 "layer"
- title "Pyrogenic CH₄ Emission"
- description "Emission of methane from all sources of biomass burning, such as wildfires and crop residue burning."
- dashboard:is_periodic True
- dashboard:time_density "month"
- title "CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes 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 "1998-01-01T00:00:00Z"
- 1 "2021-12-31T00:00:00Z"
- license "CC0-1.0"
summaries
datetime[] 2 items
- 0 "1998-01-01T00:00:00Z"
- 1 "2021-12-31T00:00:00Z"
Examining the contents of our collection
under the temporal
variable, we see that the data is available from January 1998 to December 2021. By looking at the dashboard:time density
, we observe that the data is periodic with monthly time density.
#%%time
#items = list(collection.get_items()) # Convert the iterator to a list
#print(f"Found {len(items)} items")
>>>>>>> remote <modified: >
# 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','2010-12-31T00:00:00Z']
datetime
)# Take a look at the items we found
for item in search.item_collection():
print(item)
<Item id=ct-ch4-monthgrid-v2023-201012>
<Item id=ct-ch4-monthgrid-v2023-201011>
<Item id=ct-ch4-monthgrid-v2023-201010>
<Item id=ct-ch4-monthgrid-v2023-201009>
<Item id=ct-ch4-monthgrid-v2023-201008>
<Item id=ct-ch4-monthgrid-v2023-201007>
<Item id=ct-ch4-monthgrid-v2023-201006>
<Item id=ct-ch4-monthgrid-v2023-201005>
<Item id=ct-ch4-monthgrid-v2023-201004>
<Item id=ct-ch4-monthgrid-v2023-201003>
<Item id=ct-ch4-monthgrid-v2023-201002>
<Item id=ct-ch4-monthgrid-v2023-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
= search.item_collection()
items 0] items[
- type "Feature"
- stac_version "1.0.0"
- id "ct-ch4-monthgrid-v2023-201012"
properties
- end_datetime "2010-12-31T00:00:00+00:00"
- start_datetime "2010-12-01T00:00:00+00:00"
- datetime None
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
links[] 5 items
0
- rel "collection"
- href "https://earth.gov/ghgcenter/api/stac/collections/ct-ch4-monthgrid-v2023"
- type "application/json"
1
- rel "parent"
- href "https://earth.gov/ghgcenter/api/stac/collections/ct-ch4-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/ct-ch4-monthgrid-v2023/items/ct-ch4-monthgrid-v2023-201012"
- type "application/geo+json"
4
- rel "preview"
- href "https://earth.gov/ghgcenter/api/raster/collections/ct-ch4-monthgrid-v2023/items/ct-ch4-monthgrid-v2023-201012/map?assets=total&rescale=0%2C50&colormap_name=purd"
- type "text/html"
- title "Map of Item"
assets
total
- href "s3://ghgc-data-store/ct-ch4-monthgrid-v2023/CTCH4_methane_total_emis_201012.tif"
- type "image/tiff; application=geotiff"
- title "Total CH₄ Emission"
- description "Total methane emission from microbial, fossil and pyrogenic sources."
proj:bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
- 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 180
- 1 360
raster:bands[] 1 items
0
- scale 1.0
- nodata -9999.0
- offset 0.0
- sampling "area"
- data_type "float64"
histogram
- max 311.60150516544485
- min 0.0
- count 11
buckets[] 10 items
- 0 64636
- 1 137
- 2 15
- 3 5
- 4 3
- 5 1
- 6 1
- 7 0
- 8 0
- 9 2
statistics
- mean 0.7568005339278185
- stddev 4.004427035389141
- maximum 311.60150516544485
- minimum 0.0
- valid_percent 100.0
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:transform[] 9 items
- 0 1.0
- 1 0.0
- 2 -180.0
- 3 0.0
- 4 -1.0
- 5 90.0
- 6 0.0
- 7 0.0
- 8 1.0
roles[] 2 items
- 0 "data"
- 1 "layer"
fossil
- href "s3://ghgc-data-store/ct-ch4-monthgrid-v2023/CTCH4_methane_fossil_emis_201012.tif"
- type "image/tiff; application=geotiff"
- title "Fossil CH₄ Emission"
- description "Emission of methane from all fossil sources, such as oil and gas activities and coal mining."
proj:bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
- 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 180
- 1 360
raster:bands[] 1 items
0
- scale 1.0
- nodata -9999.0
- offset 0.0
- sampling "area"
- data_type "float64"
histogram
- max 299.56836603082485
- min 0.0
- count 11
buckets[] 10 items
- 0 64742
- 1 50
- 2 4
- 3 2
- 4 0
- 5 1
- 6 0
- 7 0
- 8 0
- 9 1
statistics
- mean 0.21987304157168486
- stddev 2.240681690258249
- maximum 299.56836603082485
- minimum 0.0
- valid_percent 100.0
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:transform[] 9 items
- 0 1.0
- 1 0.0
- 2 -180.0
- 3 0.0
- 4 -1.0
- 5 90.0
- 6 0.0
- 7 0.0
- 8 1.0
roles[] 2 items
- 0 "data"
- 1 "layer"
microbial
- href "s3://ghgc-data-store/ct-ch4-monthgrid-v2023/CTCH4_methane_microbial_emis_201012.tif"
- type "image/tiff; application=geotiff"
- title "Microbial CH₄ Emission"
- description "Emission of methane from all microbial sources, such as wetlands, agriculture and termites."
proj:bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
- 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 180
- 1 360
raster:bands[] 1 items
0
- scale 1.0
- nodata -9999.0
- offset 0.0
- sampling "area"
- data_type "float64"
histogram
- max 311.45261281643224
- min 0.0
- count 11
buckets[] 10 items
- 0 64723
- 1 60
- 2 10
- 3 2
- 4 3
- 5 0
- 6 1
- 7 0
- 8 0
- 9 1
statistics
- mean 0.5075071869923118
- stddev 3.0330395077807752
- maximum 311.45261281643224
- minimum 0.0
- valid_percent 100.0
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:transform[] 9 items
- 0 1.0
- 1 0.0
- 2 -180.0
- 3 0.0
- 4 -1.0
- 5 90.0
- 6 0.0
- 7 0.0
- 8 1.0
roles[] 2 items
- 0 "data"
- 1 "layer"
pyrogenic
- href "s3://ghgc-data-store/ct-ch4-monthgrid-v2023/CTCH4_methane_pyrogenic_emis_201012.tif"
- type "image/tiff; application=geotiff"
- title "Pyrogenic CH₄ Emission"
- description "Emission of methane from all sources of biomass burning, such as wildfires and crop residue burning."
proj:bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.0
- 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 180
- 1 360
raster:bands[] 1 items
0
- scale 1.0
- nodata -9999.0
- offset 0.0
- sampling "area"
- data_type "float64"
histogram
- max 9.585022854679874
- min 0.0
- count 11
buckets[] 10 items
- 0 64299
- 1 302
- 2 105
- 3 49
- 4 19
- 5 12
- 6 4
- 7 5
- 8 1
- 9 4
statistics
- mean 0.029420305363821946
- stddev 0.22957600452706245
- maximum 9.585022854679874
- minimum 0.0
- valid_percent 100.0
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:transform[] 9 items
- 0 1.0
- 1 0.0
- 2 -180.0
- 3 0.0
- 4 -1.0
- 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/ct-ch4-monthgrid-v2023/items/ct-ch4-monthgrid-v2023-201012/preview.png?assets=total&rescale=0%2C50&colormap_name=purd"
- type "image/png"
- title "Rendered preview"
- rel "preview"
roles[] 1 items
- 0 "overview"
bbox[] 4 items
- 0 -180.0
- 1 -90.0
- 2 180.0
- 3 90.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"
- collection "ct-ch4-monthgrid-v2023"
# Restructure our items into a dictionary where keys are the datetime items
# Then we can query more easily by date/time, e.g. "2020"
= {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.
# We'll focus on microbial sources of CH4 fluxes, so our asset of interest is:
= "microbial" asset_name
Creating Maps using Folium
You will now explore changes in the microbial CH₄ flux for two different dates/times. You will visualize the outputs on a map using folium
.
Fetch Imagery from 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()
= ["2010-07","2010-01"] 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]]
observation_date_1 = observation_date_1.collection_id
collection_id = observation_date_1.id
item_id # Select relevant asset (microbial CH4 emissions)
object = observation_date_1.assets[asset_name]
= object.extra_fields.get("raster:bands", [{}])
raster_bands # Print the raster bands' information
raster_bands
[{'scale': 1.0,
'nodata': -9999.0,
'offset': 0.0,
'sampling': 'area',
'data_type': 'float64',
'histogram': {'max': 352.1198378793238,
'min': 0.0,
'count': 11,
'buckets': [64535, 213, 31, 13, 3, 2, 1, 1, 0, 1]},
'statistics': {'mean': 0.9793962770298661,
'stddev': 5.309472357044403,
'maximum': 352.1198378793238,
'minimum': 0.0,
'valid_percent': 100.0}}]
# Use mean, scaled stddev, and minimum to generate an appropriate color bar range.
= {
rescale_values "max": raster_bands[0]['statistics']['mean'] + 2.5*raster_bands[0]['statistics']['stddev'],
"min": raster_bands[0]['statistics']['minimum'],
}
print(rescale_values)
{'max': 14.253077169640875, 'min': 0.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
= "PuRd" color_map
# Make a GET request to retrieve information for your first date/time
= requests.get(
observation_date_1_tile f"{RASTER_API_URL}/collections/{collection_id}/items/{item_id}/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
observation_date_1_tile
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://earth.gov/ghgcenter/api/raster/collections/ct-ch4-monthgrid-v2023/items/ct-ch4-monthgrid-v2023-201007/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?assets=microbial&color_formula=gamma+r+1.05&colormap_name=purd&rescale=0.0%2C14.253077169640875'],
'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, because we want date tiles 1 and 2 to have the same colorbar range for best visual comparison.
= items_dict[dates[1]]
observation_date_2 # Extract collection name and item ID
= observation_date_2.collection_id
collection_id = observation_date_2.id
item_id
= requests.get(
observation_date_2_tile f"{RASTER_API_URL}/collections/{collection_id}/items/{item_id}/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
observation_date_2_tile
{'tilejson': '2.2.0',
'version': '1.0.0',
'scheme': 'xyz',
'tiles': ['https://earth.gov/ghgcenter/api/raster/collections/ct-ch4-monthgrid-v2023/items/ct-ch4-monthgrid-v2023-201001/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?assets=microbial&color_formula=gamma+r+1.05&colormap_name=purd&rescale=0.0%2C14.253077169640875'],
'minzoom': 0,
'maxzoom': 24,
'bounds': [-180.0, -90.0, 180.0, 90.0],
'center': [0.0, 0.0, 0]}
Generate Map
First, we’ll define the Area of Interest (AOI) as a GEOJSON. This will be visualized as a filled polygon on the map.
# The AOI is currently set to Eastern Canada, North America.
= {
aoi "type": "FeatureCollection",
"features": [
{"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[# [longitude, latitude]
-106.81091327586626,58.13717287115446], # Southwest Bounding Coordinate
[-106.81091327586626,46.689085955377266], # Southeast Bounding Coordinate
[-84.5565048510494,46.689085955377266], # Northeast Bounding Coordinate
[-84.5565048510494,58.13717287115446], # Northwest Bounding Coordinate
[-106.81091327586626,58.13717287115446] # Closing the polygon at the Southwest Bounding Coordinate
[
]
],"type": "Polygon"
}
}
] }
We will use the DualMap
format from folium
to visualize our two dates side-by-side for the area of interest. Below we add the desired layers to our map and add markers to identify the date/times shown.
# 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=(52, -95.3), zoom_start=4)
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_observation_date_1 =observation_date_1_tile["tiles"][0], # Path to retrieve the tile
tiles="GHG", # Set the attribution
attr=0.8, # Adjust the transparency of the layer
opacity=f"{dates[0]} {items[0].assets['microbial'].title}",
name=True,
overlay
)# Add the first layer to the Dual Map
# This will appear on the left side, specified by 'm1'
map_layer_observation_date_1.add_to(map_.m1)
# Define the second map layer using the tile fetched for the second date
= TileLayer(
map_layer_observation_date_2 =observation_date_2_tile["tiles"][0], # Path to retrieve the tile
tiles="GHG", # Set the attribution
attr=0.8, # Adjust the transparency of the layer
opacity=f"{dates[1]} {items[0].assets['microbial'].title}",
name=True,
overlay
)# Add the second layer to the Dual Map
# This will appear on the right side, specified by 'm2'
map_layer_observation_date_2.add_to(map_.m2)
# Display AOI on both maps
="Eastern Canada, North America").add_to(map_)
folium.GeoJson(aoi, name
# 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='g CH4/m2/year',dark=True)
legend_html
# Add colorbar to the map
map_.get_root().html.add_child(folium.Element(legend_html))
# Visualize the Dual Map
map_
This visualization effectively illustrates the difference in microbial activity in warm vs. cold temperatures, which vary most widely at high latitudes.
Calculate Zonal Statistics
To perform zonal statistics, first we need to create a polygon. In this use case we are creating a polygon in Eastern Canada, North America.
# Give your AOI a name to be used in your time series plot later on.
='Eastern Canada'
aoi_name# This AOI is defined as a GEOJSON.
= {
aoi "type": "FeatureCollection",
"features": [
{"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[# [longitude, latitude]
-106.81091327586626,58.13717287115446], # Southwest Bounding Coordinate
[-106.81091327586626,46.689085955377266], # Southeast Bounding Coordinate
[-84.5565048510494,46.689085955377266], # Northeast Bounding Coordinate
[-84.5565048510494,58.13717287115446], # Northwest Bounding Coordinate
[-106.81091327586626,58.13717287115446] # Closing the polygon at the Southwest Bounding Coordinate
[
]
],"type": "Polygon"
}
}
] }
# Quick Folium map to visualize this AOI
= folium.Map(location=(53, -96.5), zoom_start=3)
map_ # Add AOI to map
=aoi_name).add_to(map_)
folium.GeoJson(aoi, name# Add data layer to visualize number of grid cells within AOI
map_layer_observation_date_1.add_to(map_)# Add a quick colorbar
= ghgc_utils.generate_html_colorbar(color_map,rescale_values,label='g CH4/m2/year',dark=True)
legend_html
map_.get_root().html.add_child(folium.Element(legend_html)) map_
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
# Statistics will be returned as a Pandas DataFrame
= ghgc_utils.generate_stats(items,aoi,url=RASTER_API_URL,asset=asset_name) df
Generating stats...
Done!
CPU times: user 100 ms, sys: 17.6 ms, total: 118 ms
Wall time: 7.66 s
# Take a look at the first 5 rows of our statistics DataFrame
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 | 2010-12-01T00:00:00+00:00 | 0.00000000000000000000 | 6.82134329057177346556 | 0.57121562215132426488 | 259.83999633789062500000 | 148.42466516794601716356 | 0.87483299220655952322 | 0.15977512022801718117 | 0.00000000000000000000 | 0.00000000036842792514 | 284.00000000000000000000 | [[219, 28, 36, 10, 2, 1, 1, 1, 0, 1], [0.0, 0.... | 100.00000000000000000000 | 0.00000000000000000000 | 299.00000000000000000000 | 0.00000000000000000000 | 2.77342200516181325298 | 2010-12-01 00:00:00+00:00 |
1 | 2010-11-01T00:00:00+00:00 | 0.00000000000000000000 | 9.18063230321920364929 | 0.48526060207306331851 | 259.83999633789062500000 | 126.09011306558737430805 | 0.85094422073117337302 | 0.15373060116246106688 | 0.00000000000000000000 | 0.00000000035909511554 | 284.00000000000000000000 | [[245, 39, 7, 2, 3, 1, 0, 0, 0, 2], [0.0, 0.91... | 100.00000000000000000000 | 0.00000000000000000000 | 299.00000000000000000000 | 0.00000000000000000000 | 2.95262395920586362408 | 2010-11-01 00:00:00+00:00 |
2 | 2010-10-01T00:00:00+00:00 | 0.00000000000000000000 | 20.97541986485276765961 | 1.30173648352961701669 | 259.83999633789062500000 | 338.24320311323430132688 | 2.93905303511565607621 | 0.25056334584628725537 | 0.00000000000000000000 | 0.00000000035415695592 | 284.00000000000000000000 | [[260, 17, 9, 2, 3, 2, 2, 2, 0, 2], [0.0, 2.09... | 100.00000000000000000000 | 0.00000000000000000000 | 299.00000000000000000000 | 0.00000000000000000000 | 12.13802520760731518124 | 2010-10-01 00:00:00+00:00 |
3 | 2010-09-01T00:00:00+00:00 | 0.00000000000000000000 | 66.99361532484425652001 | 5.67575875359129611297 | 259.83999633789062500000 | 1474.78913374791295609612 | 10.38348145192236593459 | 1.19664200489307481767 | 0.00000000000000000000 | 0.00000000033616082800 | 284.00000000000000000000 | [[234, 23, 19, 6, 5, 3, 3, 2, 2, 2], [0.0, 6.6... | 100.00000000000000000000 | 0.00000000000000000000 | 299.00000000000000000000 | 0.00000000000000000000 | 41.57290663092389593203 | 2010-09-01 00:00:00+00:00 |
4 | 2010-08-01T00:00:00+00:00 | 0.00000000000000000000 | 97.63207641317480067755 | 10.64292497187927821756 | 259.83999633789062500000 | 2765.45758571755641241907 | 16.33012417584687270278 | 1.61103687945000451087 | 0.00000000000000000000 | 0.00000000029630847828 | 284.00000000000000000000 | [[193, 45, 33, 7, 5, 7, 4, 2, 0, 3], [0.0, 9.7... | 100.00000000000000000000 | 0.00000000000000000000 | 299.00000000000000000000 | 0.00000000000000000000 | 64.69942926410929828762 | 2010-08-01 00:00:00+00:00 |
Time-Series Analysis
Let’s look at the maximum methane flux within our AOI over the full time range of the dataset. The code below generates a time series plot from the table above.
# Figure size: 20 representing the width, 10 representing the height
= df.sort_values(by="datetime")
df = plt.figure(figsize=(10,5))
fig
# Change 'which_stat' below if you would rather look at a different statistic, like minimum or mean.
= "mean"
which_stat
plt.plot(0:7] for d in df["datetime"]], # X-axis: sorted datetime
[d[# Y-axis: maximum CH4 flux
df[which_stat], ="red", # Line color
color="-", # Line style
linestyle=2, # Line width
linewidth=f"{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
"g CH₄/m²/year")
plt.ylabel(= 90)
plt.xticks(rotation
# Show once-annual tick marks
#interval = 12
#ticks = range(0, len(df), interval) # Tick positions
#plt.xticks(ticks, [d[0:7] for d in df["datetime"]][list(ticks)]) # Use the corresponding datetime values
0:7] for d in df['datetime'][:]][::2])
plt.xticks([d[
# Insert title for the plot
f"{which_stat.capitalize()} Monthly Flux from {items[0].assets[asset_name].title} sources within {aoi_name} AOI (1998-2021)")
plt.title(
# Add data citation
plt.text(0:7] for d in df['datetime']][0], # X-coordinate of the text
[d[min(), # Y-coordinate of the text
df[which_stat].
# Text to be displayed
f"Source: {collection.title}",
=8, # Font size
fontsize="left", # Horizontal alignment
horizontalalignment="top", # Vertical alignment
verticalalignment="blue", # Text color
color
)
# Plot the time series
plt.show()
This is a visual representation of the seasonal cycle of microbial methane emissions in Eastern Canada over the span of 22 years!
Summary
In this notebook we have successfully completed the following steps for the STAC collection for the CarbonTracker-CH₄ Isotopic Methane Inverse Fluxes dataset: 1. Install and import the necessary libraries 2. Fetch the collection from STAC collections using the appropriate endpoints 3. Count the number of existing granules within the collection 4. Map and compare the CH₄ inverse fluxes over an area of interest for two distinctive dates/times 5. Create a table that displays the minimum, maximum, and sum of the CH₄ inverse fluxes for a specified region 6. Generate a time-series graph of the CH₄ inverse fluxes for a specified region
If you have any questions regarding this user notebook, please contact us using the feedback form.