Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ output_states:
discharge: true # Save river discharge
soil_capillary: true # Save capillary water (Wc)
soil_gravitational: true # Save gravitational water (Wg)
soil_saturation: false # Save effective soil saturation (Se)
soil_plant: true # Save plant/canopy water (Wp)
soil_surface: true # Save surface water (Ws)
aquifer_head: false # Save groundwater head (h) when Linear groundwater is active
Expand Down
4 changes: 4 additions & 0 deletions examples/02-daily-balance-Arno-basin/Arno.daily.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ output_states:
# [boolean]
soil_gravitational: true

# OPTIONAL: Save the effective soil saturation Se = (Wc+Wg)/(Wc0+Wg0), non dimensional. Default: false
# [boolean]
soil_saturation: true

# OPTIONAL: Save states of plant/canopy water. Default: true
# [boolean]
soil_plant: true
Expand Down
4 changes: 4 additions & 0 deletions examples/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ output_states:
# [boolean]
soil_gravitational: true

# OPTIONAL: Save the effective soil saturation Se = (Wc+Wg)/(Wc0+Wg0), non dimensional. Default: false
# [boolean]
soil_saturation: false

# OPTIONAL: Save states of plant/canopy water. Default: true
# [boolean]
soil_plant: true
Expand Down
4 changes: 4 additions & 0 deletions mobidic/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ class OutputStates(BaseModel):
soil_gravitational: Optional[bool] = Field(
True, description="Option to save states of soil large pores for results analysis"
)
soil_saturation: Optional[bool] = Field(
False,
description=("Option to save the effective soil saturation (Wc+Wg)/(Wc0+Wg0)"),
)
soil_plant: Optional[bool] = Field(
True, description="Option to save states of plant/canopy water for results analysis"
)
Expand Down
1 change: 1 addition & 0 deletions mobidic/core/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ def run(
"paramset_id": self.config.basin.paramset_id,
},
reservoir_size=reservoir_size,
soil_capacity=wtot0,
)
logger.info(f"State output enabled: {state_settings.output_states}")
else:
Expand Down
40 changes: 40 additions & 0 deletions mobidic/io/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ class StateWriter:
flushing: Flush interval (positive int = every N steps, -1 = only at end)
max_file_size: Maximum file size in MB before creating a new chunk (default: 500)
add_metadata: Additional global attributes (optional)
reservoir_size: Number of reservoirs (0 = no reservoir output)
soil_capacity: Total soil water capacity Wc0 + Wg0 [m] on the model grid, after the
calibration multipliers and the Wg_Wc_tr transition have been applied. It is required
when ``output_states.soil_saturation`` is enabled

Examples:
>>> # With automatic chunking at 500 MB
Expand All @@ -343,6 +347,7 @@ def __init__(
max_file_size: float = 500.0,
add_metadata: dict | None = None,
reservoir_size: int = 0,
soil_capacity: np.ndarray | None = None,
):
"""Initialize the state writer and create the NetCDF file."""
self.base_output_path = Path(output_path)
Expand All @@ -352,6 +357,23 @@ def __init__(
self.network_size = network_size
self.reservoir_size = reservoir_size
self.output_states = output_states

# Total soil capacity for the effective saturation output
self.soil_capacity = None
if getattr(output_states, "soil_saturation", False):
if soil_capacity is None:
logger.warning(
"output_states.soil_saturation is enabled but no soil capacity was supplied to "
"StateWriter; the effective soil saturation will not be saved"
)
else:
soil_capacity = np.asarray(soil_capacity, dtype=float)
if soil_capacity.shape != tuple(grid_metadata["shape"]):
raise ValueError(
f"soil_capacity shape {soil_capacity.shape} does not match grid shape "
f"{tuple(grid_metadata['shape'])}"
)
self.soil_capacity = soil_capacity.copy()
self.flushing = flushing
self.max_file_size_bytes = max_file_size * 1024 * 1024 # Convert MB to bytes
self.add_metadata = add_metadata or {}
Expand Down Expand Up @@ -577,6 +599,11 @@ def flush(self) -> None:
wg_data = np.array([s.wg for s in self.buffer])
data_vars["Wg"] = (["time", "y", "x"], wg_data)

if self.soil_capacity is not None:
# Effective saturation: (Wc + Wg) / (Wc0 + Wg0).
se_data = np.array([(s.wc + s.wg) / self.soil_capacity for s in self.buffer])
data_vars["Se"] = (["time", "y", "x"], se_data)

if self.output_states.soil_plant:
# Check if any state has wp (plant water)
has_wp = any(s.wp is not None for s in self.buffer)
Expand Down Expand Up @@ -696,6 +723,19 @@ def flush(self) -> None:
"grid_mapping": "crs",
}

if "Se" in flush_ds:
flush_ds["Se"].attrs = {
"long_name": "Effective Soil Saturation",
"units": "1",
"description": (
"Ratio of the soil water content to the total soil water capacity, (Wc+Wg)/(Wc0+Wg0); "
"equivalent to (theta - theta_wp)/(theta_s - theta_wp), i.e., relative to the wilting point"
),
"valid_min": 0.0,
"valid_max": 1.0,
"grid_mapping": "crs",
}

if "Wp" in flush_ds:
flush_ds["Wp"].attrs = {
"long_name": "Plant/Canopy Water Content",
Expand Down
Loading