diff --git a/docs/reference/state.md b/docs/reference/state.md index 2c09df6..bfa0d92 100644 --- a/docs/reference/state.md +++ b/docs/reference/state.md @@ -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 diff --git a/examples/02-daily-balance-Arno-basin/Arno.daily.yaml b/examples/02-daily-balance-Arno-basin/Arno.daily.yaml index 9e61f19..9190856 100644 --- a/examples/02-daily-balance-Arno-basin/Arno.daily.yaml +++ b/examples/02-daily-balance-Arno-basin/Arno.daily.yaml @@ -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 diff --git a/examples/sample_config.yaml b/examples/sample_config.yaml index f33f841..c03710b 100644 --- a/examples/sample_config.yaml +++ b/examples/sample_config.yaml @@ -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 diff --git a/mobidic/config/schema.py b/mobidic/config/schema.py index 23f03df..4b1cdf8 100644 --- a/mobidic/config/schema.py +++ b/mobidic/config/schema.py @@ -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" ) diff --git a/mobidic/core/simulation.py b/mobidic/core/simulation.py index 4a07280..1bdd244 100644 --- a/mobidic/core/simulation.py +++ b/mobidic/core/simulation.py @@ -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: diff --git a/mobidic/io/state.py b/mobidic/io/state.py index 6a25d69..55d3cc4 100644 --- a/mobidic/io/state.py +++ b/mobidic/io/state.py @@ -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 @@ -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) @@ -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 {} @@ -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) @@ -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",