Py.Cafe

Sindhup24/

solara-river-forecast-0

River Forecast Visualization

DocsPricing
  • altair_points_with_river_numbers.csv
  • 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 solara
import pandas as pd
import altair as alt
import boto3
import zarr
import xarray as xr
import s3fs

# Define the specific RiverNumber and date
river_number = 110123714
date = '2024040100'

# S3 bucket and key setup
s3_bucket = 'geoglows-v2-forecasts'
s3_path = f's3://{s3_bucket}/{date}.zarr/'

# Function to fetch data from S3 Zarr store
def fetch_data_from_s3_zarr(s3_path, river_number):
    # Create a file system object
    fs = s3fs.S3FileSystem(anon=True)
    
    # Open the Zarr store
    store = fs.get_mapper(s3_path)
    
    # Load the data using xarray
    ds = xr.open_zarr(store, consolidated=True)
    
    # Extract the data for the specific RiverNumber
    forecast_data = ds.sel(river_number=river_number).to_dataframe().reset_index()
    return forecast_data

# Load CSV file with river numbers (for reference and selection)
csv_file_path = 'altair_points_with_river_numbers.csv'
data = pd.read_csv(csv_file_path)

@solara.component
def Page():
    selected_river_number = solara.reactive(river_number)
    selected_date = solara.reactive(date)
    forecast_data = solara.reactive(pd.DataFrame())

    def update_forecast_data(river_number, date):
        s3_path = f's3://{s3_bucket}/{date}.zarr/'
        forecast_data.value = fetch_data_from_s3_zarr(s3_path, river_number)
    
    # Watch for changes in selected_river_number and selected_date and update forecast_data accordingly
    solara.use_effect(lambda: update_forecast_data(selected_river_number.value, selected_date.value), [selected_river_number, selected_date])

    # Altair chart
    chart = alt.Chart(forecast_data.value).mark_line().encode(
        x='time:T',  # Assuming 'time' is the datetime dimension in the dataset
        y='value:Q'  # Assuming 'value' is the forecast value in the dataset
    ).properties(
        title=f'Forecast for RiverNumber {selected_river_number.value} on {selected_date.value}'
    )

    with solara.VBox():
        solara.Text(f"Selected RiverNumber: {selected_river_number.value}")
        solara.Text(f"Selected Date: {selected_date.value}")
        solara.DataFrame(data)
        solara.Figure(chart)