Skip to content

Commit

Permalink
update frac_sw_used
Browse files Browse the repository at this point in the history
  • Loading branch information
hboisgon committed Oct 16, 2024
1 parent 78a602e commit a6182f8
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 85 deletions.
35 changes: 32 additions & 3 deletions examples/update_model_water_demand.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"source": [
"Here, you see we will be adding the required information to simulate water demand and allocation in wflow, by running three different setup functions:\n",
"- **setup_allocation_areas**: Adds a map that defines the different regions that will be used to allocate water. By default this is a mix between administrative boundaries and catchment boundaries\n",
"- **setup_allocation_surfacewaterfrac**: prepare the fraction of surface water used for allocation (can be reduced if groundwater or non conventional water sources are also present in the basin).\n",
"- **setup_lulcmaps_with_paddy** (or **setup_lulcmaps**): update the landuse to include new parameters (crop coefficient kc and soil water pressure heads h) and add the paddies (rice fields). To allow for water to pool on the surface of the rice fields, soil parameters will also be updated to include an additional thin layer with limited vertical conductivity.\n",
"- **setup_domestic_demand**: Add domestic water demands (gross and net) from gridded data and downscaled using high resolution population map.\n",
"- **setup_other_demand**: Adds maps to the wflow schematization that describe how much water is demanded (gross and net amounts) by different sources: domestic (dom), industry (ind), and livestock (lsk). In our case, as we downscale domestic with population, we will here add industry and livestock.\n",
Expand Down Expand Up @@ -73,7 +74,7 @@
"outputs": [],
"source": [
"# NOTE: copy this line (without !) to your shell for more direct feedback\n",
"! hydromt build wflow \"./wflow_piave_water_demand\" -r \"{'basin': [12.2051, 45.8331], 'bounds': [11.70, 45.35, 12.95, 46.70]}\" -i wflow_build.yml -d \"artifact_data==v0.0.6\" -vv"
"! hydromt build wflow \"./wflow_piave_water_demand\" -r \"{'basin': [12.2051, 45.8331], 'bounds': [11.70, 45.35, 12.95, 46.70]}\" -i wflow_build.yml -d artifact_data -vv"
]
},
{
Expand All @@ -89,7 +90,7 @@
"metadata": {},
"outputs": [],
"source": [
"! hydromt update wflow wflow_piave_water_demand -i wflow_update_water_demand.yml -d artifact_data -d \"https://github.com/Deltares/hydromt_wflow/releases/download/v0.5.0/wflow_artifacts.yml\" -v"
"! hydromt update wflow wflow_piave_water_demand -i wflow_update_water_demand.yml -d artifact_data -d \"https://github.com/Deltares/hydromt_wflow/releases/download/v0.5.0/wflow_artifacts.yml\" -d ../tests/data/demand/data_catalog.yml -v"
]
},
{
Expand All @@ -110,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -397,6 +398,34 @@
"source": [
"Here we see a couple of distinct administrative boundaries (black lines), some of which already follow the catchment boundaries. When the catchment crosses an administrative boundary, that region receives a different but unique identifier. Note that we are using level 2 boundaries here, which might not be the most realistic for a region of this size."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Surface water frac used for water allocation\n",
"\n",
"By default, Wflow will allocate all water demands with water from the surface water (frac_sw_used=1). However, if in a certain areas, groundwater or other non conventional sources can be used, the frac_sw_used of each allocation area can be reduced and prepared using the **setup_allocation_surfacewaterfrac** method.\n",
"\n",
"Here we used global data from GLOFAS (Lisflood) for the fraction of grounwater used, presence of groundwater bodies and non conventional sources (0 is Piave). The water allocations are the ones we prepared in the previous step in order to match better the wflow model basin delineation.\n",
"\n",
"Let's have a look at the resulting map:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1)\n",
"\n",
"ax.set_title(\"Allocation surface water frac used\")\n",
"\n",
"mod.grid[\"frac_sw_used\"].raster.mask_nodata().plot(ax=ax, add_labels=False)\n",
"mod.geoms[\"basins\"].plot(ax=ax, facecolor=\"none\", edgecolor=\"black\")\n",
"mod.geoms[\"rivers\"].plot(ax=ax)"
]
}
],
"metadata": {
Expand Down
5 changes: 5 additions & 0 deletions examples/wflow_update_water_demand.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ setup_allocation_areas:
min_area: 30
admin_bounds_fn: gadm_level2

setup_allocation_surfacewaterfrac:
gwfrac_fn: lisflood_gwfrac
gwbodies_fn: lisflood_gwbodies
ncfrac_fn: lisflood_ncfrac

# Update model with GLCNMO landuse data in order to add the paddies
setup_lulcmaps_with_paddy:
lulc_fn: glcnmo
Expand Down
95 changes: 59 additions & 36 deletions hydromt_wflow/wflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3381,47 +3381,57 @@ def setup_allocation_areas(
self.set_grid(alloc, name="allocation_areas")

# Update the settings toml
self.set_config("input.vertical.waterallocation.areas", "allocation_areas")
self.set_config("input.vertical.allocation.areas", "allocation_areas")

# Add alloc to geoms
self.set_geoms(alloc.raster.vectorize(), name="allocation_areas")

def setup_surfacewaterfrac(
def setup_allocation_surfacewaterfrac(
self,
gwfrac_fn: Union[str, xr.DataArray],
gwbodies_fn: Union[str, xr.DataArray],
ncfrac_fn: Union[str, xr.DataArray],
waterareas_fn: Union[str, xr.DataArray],
waterareas_fn: Optional[Union[str, xr.DataArray]] = None,
gwbodies_fn: Optional[Union[str, xr.DataArray]] = None,
ncfrac_fn: Optional[Union[str, xr.DataArray]] = None,
interpolate_nodata: bool = False,
):
"""Create water demand surface water consumption fraction.
"""Create the fraction of water allocated from surface water.
This fraction entails the division of the water demand between surface water
and ground water.
This fraction entails the division of the water demand between surface water,
ground water (aquifers) and non conventional sources (e.g. desalination plants).
The surface water fraction is based on the raw groundwater fraction, if
groudwater bodies are present (these are absent in e.g. mountainous regions),
groundwater bodies are present (these are absent in e.g. mountainous regions),
a fraction of water consumed that is obtained by non-conventional means and the
water source areas.
Non-conventional water could e.g. be water acquired by desalination of ocean or
other brackish water.
Adds model layer:
* **frac_sw_used**: fraction of water allocated from surface water [0-1]
Parameters
----------
gwfrac_fn : Union[str, xr.DataArray]
The raw groundwater fraction per grid cell. The values of these cells need
to be between 0 and 1.
gwbodies_fn : Union[str, xr.DataArray]
The presence of groundwater bodies per grid cell. The values are ought to
be binary (either 0 or 1).
ncfrac_fn : Union[str, xr.DataArray]
The non-conventional fraction. Same types of values apply as for
`gwfrac_fn`.
waterareas_fn : Union[str, xr.DataArray]
The areas over which the water has to be distributed. This may either be
a global (or more local map) or it may be defined as `allocation_areas`.
When `allocation_areas` is provided, the source areas created by the
`setup_allocation_areas` will be used for this setup method.
a global (or more local map). If not provided, the source areas created by
the `setup_allocation_areas` will be used.
gwbodies_fn : Union[str, xr.DataArray], optional
The presence of groundwater bodies per grid cell. The values are ought to
be binary (either 0 or 1). If they are not provided, we assume groundwater
bodies are present where gwfrac is more than 0.
ncfrac_fn : Union[str, xr.DataArray], optional
The non-conventional fraction. Same types of values apply as for
`gwfrac_fn`. If not provided, we assume no non-conventional sources are
used.
interpolate_nodata : bool, optional
If True, nodata values in the resulting frac_sw_used map will be linearly
interpolated. Else a default value of 1 will be used for nodata values
(default).
"""
self.logger.info("Preparing surface water fraction map.")
# Load the data
Expand All @@ -3430,21 +3440,33 @@ def setup_surfacewaterfrac(
geom=self.region,
buffer=2,
)
gwbodies = self.data_catalog.get_rasterdataset(
gwbodies_fn,
geom=self.region,
buffer=2,
)
ncfrac = self.data_catalog.get_rasterdataset(
ncfrac_fn,
geom=self.region,
buffer=2,
)
if gwbodies_fn is not None:
gwbodies = self.data_catalog.get_rasterdataset(
gwbodies_fn,
geom=self.region,
buffer=2,
)
else:
gwbodies = None
if ncfrac_fn is not None:
ncfrac = self.data_catalog.get_rasterdataset(
ncfrac_fn,
geom=self.region,
buffer=2,
)
else:
ncfrac = None

# check wether to use the models own allocation areas
if waterareas_fn == "allocation_areas":
if waterareas_fn is None:
self.logger.info("Using wflow model allocation areas.")
waterareas = self.grid[waterareas_fn]
if "allocation_areas" not in self.grid:
self.logger.error(
"No allocation areas found. Run setup_allocation_areas first "
"or provide a waterareas_fn."
)
return
waterareas = self.grid["allocation_areas"]
else:
waterareas = self.data_catalog.get_rasterdataset(
waterareas_fn,
Expand All @@ -3453,22 +3475,23 @@ def setup_surfacewaterfrac(
)

# Call the workflow
w_frac = workflows.demand.surfacewaterfrac(
self.grid["wflow_dem"],
w_frac = workflows.demand.surfacewaterfrac_used(
gwfrac_raw=gwfrac_raw,
da_like=self.grid["wflow_dem"],
waterareas=waterareas,
gwbodies=gwbodies,
ncfrac=ncfrac,
waterareas=waterareas,
interpolate=interpolate_nodata,
)

# Update the settings toml
self.set_config(
"input.vertical.waterallocation.frac_sw_used",
"SurfaceWaterFrac",
"input.vertical.allocation.frac_sw_used",
"frac_sw_used",
)

# Set the dataarray to the wflow grid
self.set_grid(w_frac, name="SurfaceWaterFrac")
self.set_grid(w_frac, name="frac_sw_used")

def setup_domestic_demand(
self,
Expand Down
Loading

0 comments on commit a6182f8

Please sign in to comment.