Py.Cafe

nitinmagima/

rainfall-hindcast-visualization

Rainfall Hindcast Visualization

DocsPricing
  • data/
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
import pandas as pd
import os
import plotly.express as px

# πŸ“‚ Set the correct data folder path
DATA_FOLDER = "data/"

# πŸ› οΈ Function to load CSV files dynamically
@st.cache_data
def load_data(folder):
    data_dict = {}
    
    for file in os.listdir(folder):
        if file.endswith(".csv"):
            file_path = os.path.join(folder, file)
            place_name = file.replace("chadjasmean_data.csv", "").replace(".csv", "").replace("_", " ").strip()
            df = pd.read_csv(file_path, header=None)  # Load CSV without column names
            
            # 🟒 Rename columns correctly
            df.columns = ["Year", "Rainfall"]

            # 🟒 Convert "Year" values: 1 β†’ 1991, ..., 35 β†’ 2025
            df["Year"] = pd.to_numeric(df["Year"], errors="coerce")  # Convert safely
            df.dropna(subset=["Year"], inplace=True)  # Remove invalid values
            df["Year"] = df["Year"].astype(int) + 1990  # Shift to correct years

            # 🟒 Ensure Rainfall values are numeric
            df["Rainfall"] = pd.to_numeric(df["Rainfall"], errors="coerce")

            data_dict[place_name] = df  # Store cleaned data

    return data_dict

# πŸ“Š Load datasets dynamically
data_dict = load_data(DATA_FOLDER)

# 🎨 Streamlit UI
st.title("πŸ“ˆ Rainfall Hindcast Interactive Dashboard")
st.write("Select a region to visualize rainfall trends:")

# πŸ”½ Dropdown for region selection
region = st.selectbox("Select Region", list(data_dict.keys()))

# πŸ“‰ Plot the data using Plotly
if region:
    df = data_dict[region]

    # πŸ“Š Create a Plotly line chart
    fig = px.line(df, x="Year", y="Rainfall", markers=True, 
                  title=f"Rainfall Trends for {region}")

    # πŸ“Œ Customize the layout
    fig.update_layout(
        xaxis_title="Year",
        yaxis_title="Rainfall (mm)",
        hovermode="x",
        template="plotly_white"
    )

    # πŸ–ΌοΈ Show the interactive chart
    st.plotly_chart(fig, use_container_width=True)