import requests
import pandas as pd
import matplotlib.pyplot as plt
import plotly.graph_objs as go
from io import StringIO
Carbon Dioxide and Methane Concentrations from the Indianapolis Flux Experiment (INFLUX)
Approach
- Identify available dates and temporal frequency of observations for the given data. The collection processed in this notebook is the Atmospheric concentrations of carbon dioxide (CO₂) and methane (CH₄) collected at NIST Urban Test Bed tower sites in the Northeastern U.S.
- Visualize the time series data
About the Data
NIST is engaged in research to improve measurement of greenhouse gas emissions in areas containing multiple emission sources and sinks, such as cities. NIST’s objective is to develop measurement tools supporting independent means to increase the accuracy of greenhouse gas emissions data at urban and regional geospatial scales. NIST has established three test beds in U.S. cities to develop and evaluate the performance of advanced measurement capabilities for emissions independent of their origin. Located in Indianapolis, Indiana, the Los Angeles air basin of California, and the U.S. Northeast corridor (beginning with the Baltimore/Washington D.C. region), the test beds have been selected for their varying meteorology, terrain and emissions characteristics. These test beds will serve as a means to independently diagnose the accuracy of emissions data obtained directly from emission or uptake sources.
For more information regarding this dataset, please visit the Carbon Dioxide and Methane Concentrations from the Indianapolis Flux Experiment (INFLUX) 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
Importing required libraries
Querying the Feature Vector API
First, we are going to import the required libraries. Once imported, they allow better executing a query in the GHG Center Feature Vector Application Programming Interface (API) where the items for this collection are stored.
="https://earth.gov/ghgcenter/api/features" FEATURE_API_URL
# Function to fetch CSV data for a station with a limit parameter
def get_station_data_csv(station_code, gas_type, frequency, elevation_m, limit=100000):
# Use the ?f=csv and limit query to get more rows
= f"https://earth.gov/ghgcenter/api/features/collections/public.nist_flux_in_{station_code}_{gas_type}_{frequency}_concentrations/items?f=csv&elevation_m={elevation_m}&limit={limit}"
url print(url)
try:
= requests.get(url)
response
# Check if the response is successful
if response.status_code != 200:
print(f"Failed to fetch data for {station_code}. Status code: {response.status_code}")
return pd.DataFrame()
# Check if the content type is CSV
= response.headers.get('Content-Type')
content_type if 'text/csv' not in content_type:
print(f"Unexpected content type for {station_code}: {content_type}")
print("Response content:", response.text)
return pd.DataFrame()
# Read the CSV content into a pandas DataFrame
= StringIO(response.text)
csv_data return pd.read_csv(csv_data)
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return pd.DataFrame()
Visualizing the CH₄ data for two NEC stations
# Get station name and elevation from metdata dataframe
# Fetch data for site01 (elevation 256) and site09 (elevation 277), using limit=10000
# ch4/co2 select the ghg
= get_station_data_csv('site01', 'ch4', 'hourly', 256,limit=10000)
site01_data = get_station_data_csv('site09', 'ch4', 'hourly', 277,limit=10000)
site09_data
# Check if data was successfully retrieved before proceeding
if site01_data.empty or site09_data.empty:
print("No data available for one or both stations. Exiting.")
else:
# Convert the 'datetime' column to datetime for plotting
'datetime'] = pd.to_datetime(site01_data['datetime'], format='%Y-%m-%dT%H:%M:%SZ')
site01_data['datetime'] = pd.to_datetime(site09_data['datetime'], format='%Y-%m-%dT%H:%M:%SZ')
site09_data[
# Plot the data
=(10, 6))
plt.figure(figsize'datetime'], site01_data['value'], label='site01 (256m)', color='blue', marker='o')
plt.plot(site01_data['datetime'], site09_data['value'], label='site09 (277m)', color='green', marker='o')
plt.plot(site09_data[
'Methane (CH₄) Hourly Concentrations Over Time for site01 and site09 Stations')
plt.title('Time')
plt.xlabel('CH₄ Concentration (ppb)')
plt.ylabel(
plt.legend()True)
plt.grid(
# Show plot
plt.show()
https://earth.gov/ghgcenter/api/features/collections/public.nist_flux_in_site01_ch4_hourly_concentrations/items?f=csv&elevation_m=256&limit=10000
https://earth.gov/ghgcenter/api/features/collections/public.nist_flux_in_site09_ch4_hourly_concentrations/items?f=csv&elevation_m=277&limit=10000