14.Analyze the air-quality data of Kochi using the Open-Meteo Air Quality API. Retrieve hourly AQI, PM2.5, and PM10 data for Kochi. Perform statistical and time-series analysis and plot suitable charts to visualize changes in AQI, PM2.5, and PM10 levels over time. Visualize the results using suitable charts and develop a dashboard.

Input:https://air-quality-api.open-meteo.com/v1/air-quality?latitude=9.93&longitude=76.27&hourly=pm10,pm2_5,us_aqi&timezone=auto location?

pip install requests pandas plotly streamlit

streamlit

app.py:


import requests
import pandas as pd
import plotly.express as px
import streamlit as st

# Open-Meteo Air Quality API
URL = (
    "https://air-quality-api.open-meteo.com/v1/air-quality"
    "?latitude=9.93&longitude=76.27"
    "&hourly=pm10,pm2_5,us_aqi&timezone=auto"
)

st.set_page_config(
    page_title="Air Quality Dashboard",
    page_icon="🌍",
    layout="wide"
)

st.title("Open-Meteo Air Quality Dashboard")
st.write("Hourly air quality analysis for Kochi")

# Fetch data from API
@st.cache_data(ttl=900)
def get_air_quality():
    response = requests.get(URL, timeout=20)
    response.raise_for_status()
    return response.json()

try:
    data = get_air_quality()

    # Extract hourly data
    hourly = data["hourly"]

    df = pd.DataFrame({
        "Time": pd.to_datetime(hourly["time"]),
        "PM2.5": hourly["pm2_5"],
        "PM10": hourly["pm10"],
        "US AQI": hourly["us_aqi"]
    })

    # Display dataset
    st.subheader("Air Quality Dataset")
    st.dataframe(df, use_container_width=True)

    # Statistical analysis
    st.subheader("Statistical Analysis")

    stats = df[["PM2.5", "PM10", "US AQI"]].describe()
    st.dataframe(stats, use_container_width=True)

    # Key statistics
    col1, col2, col3 = st.columns(3)

    col1.metric("Average PM2.5", f"{df['PM2.5'].mean():.2f}")
    col2.metric("Average PM10", f"{df['PM10'].mean():.2f}")
    col3.metric("Average US AQI", f"{df['US AQI'].mean():.2f}")

    # Time-series chart for AQI
    st.subheader("AQI Trend")

    fig_aqi = px.line(
        df,
        x="Time",
        y="US AQI",
        title="Hourly US Air Quality Index",
        markers=True
    )

    st.plotly_chart(fig_aqi, use_container_width=True)

    # PM2.5 trend
    st.subheader("PM2.5 Trend")

    fig_pm25 = px.line(
        df,
        x="Time",
        y="PM2.5",
        title="Hourly PM2.5 Concentration",
        markers=True
    )

    st.plotly_chart(fig_pm25, use_container_width=True)

    # PM10 trend
    st.subheader("PM10 Trend")

    fig_pm10 = px.line(
        df,
        x="Time",
        y="PM10",
        title="Hourly PM10 Concentration",
        markers=True
    )

    st.plotly_chart(fig_pm10, use_container_width=True)

    # Compare pollutants
    st.subheader("Pollutant Comparison")

    comparison = df[["PM2.5", "PM10"]].mean().reset_index()
    comparison.columns = ["Pollutant", "Average Concentration"]

    fig_compare = px.bar(
        comparison,
        x="Pollutant",
        y="Average Concentration",
        title="Average PM2.5 vs PM10"
    )

    st.plotly_chart(fig_compare, use_container_width=True)

    # Download CSV
    csv = df.to_csv(index=False).encode("utf-8")

    st.download_button(
        "Download Air Quality Data",
        data=csv,
        file_name="air_quality_data.csv",
        mime="text/csv"
    )

except requests.exceptions.RequestException as e:
    st.error(f"API connection error: {e}")

except (KeyError, ValueError) as e:
    st.error(f"Error processing air quality data: {e}")
