Tip

For an interactive online version click here: Binder badge

Working with forcing conditions#

[1]:
import os
import sys
import matplotlib.pyplot as plt
from hydromt.log import setuplog
from hydromt_sfincs import SfincsModel
[2]:
# Initialize SfincsModel with the artifact data catalog which contains data for North Italy
sf = SfincsModel(
    data_libs=["artifact_data"],
    root="tmp_example",
    mode="w+",
    logger=setuplog("", log_level=20),
)
2023-04-17 16:04:38,512 - root - log - INFO - HydroMT version: 0.7.1
2023-04-17 16:04:38,561 - root - data_catalog - INFO - Reading data catalog artifact_data v0.0.8 from archive
2023-04-17 16:04:38,562 - root - data_catalog - INFO - Parsing data catalog from /home/runner/.hydromt_data/artifact_data/v0.0.8/data_catalog.yml
2023-04-17 16:04:38,632 - root - model_api - INFO - Initializing sfincs model from hydromt_sfincs (v1.0.0).
[3]:
sf.setup_grid(
    x0=268650,
    y0=5018550,
    dx=150.0,
    dy=150.0,
    nmax=272,
    mmax=425,
    rotation=0,
    epsg=32633,
)
[4]:
sf.setup_config(
    tref="20100201 000000",
    tstart="20100201 000000",
    tstop="20100210 000000",
)
[5]:
# 2d precip
sf.setup_precip_forcing_from_grid(
    precip="era5_hourly",
    aggregate=True,
)
sf.write_forcing()
2023-04-17 16:04:38,665 - root - data_catalog - INFO - DataCatalog: Getting era5_hourly RasterDataset netcdf data from /home/runner/.hydromt_data/artifact_data/v0.0.8/era5_hourly.nc
2023-04-17 16:04:38,915 - root - sfincs - INFO - Write forcing files
[6]:
# 1d uniform precip
sf.setup_precip_forcing_from_grid(
    precip="era5_hourly",
    aggregate=True,
)
2023-04-17 16:04:38,936 - root - data_catalog - INFO - DataCatalog: Getting era5_hourly RasterDataset netcdf data from /home/runner/.hydromt_data/artifact_data/v0.0.8/era5_hourly.nc
2023-04-17 16:04:39,137 - root - model_api - WARNING - Replacing forcing: precip
[7]:
sf.forcing["precip"].to_pandas().to_csv("precip.csv")
sf.forcing.pop("precip", None)  # reset
sf.setup_precip_forcing(
    timeseries="precip.csv",
)
sf.plot_forcing()
2023-04-17 16:04:39,149 - root - data_catalog - INFO - DataCatalog: Getting precip DataFrame csv data from /home/runner/work/hydromt_sfincs/hydromt_sfincs/docs/_examples/precip.csv
2023-04-17 16:04:39,150 - root - dataframe - INFO - DataFrame: Read csv data.
[7]:
(<Figure size 600x300 with 1 Axes>,
 [<AxesSubplot:title={'center':'SFINCS precipitation forcing (precip)'}, ylabel='precipitation\n[mm.hr-1]'>])
../_images/_examples_example_forcing_7_2.png
[8]:
import pandas as pd
import geopandas as gpd
import numpy as np
from hydromt_sfincs import utils

df = utils.read_timeseries("sfincs_compound//sfincs.bzs", tref=sf.config["tref"])
gdf = utils.read_xy("sfincs_compound//sfincs.bnd", crs=sf.crs)

sf.forcing.pop("bzs", None)  # reset
sf.forcing.pop("precip", None)  # reset

# add timeseries and locations
sf.setup_waterlevel_forcing(
    timeseries=df,
    locations=gdf,
    merge=True,
)
sf.plot_forcing()
[8]:
(<Figure size 600x300 with 1 Axes>,
 [<AxesSubplot:title={'center':'SFINCS waterlevel forcing (bzs)'}, ylabel='waterlevel\n[m+ref]'>])
../_images/_examples_example_forcing_8_1.png
[9]:
# merge (overwrite) existing timeseries with different time resoltiuon
# and add offset
sf.setup_waterlevel_forcing(
    timeseries=df.iloc[::5, [0]],
    locations=gdf.iloc[[0]],
    offset="dtu10mdt",
    merge=True,
)
sf.plot_forcing()
2023-04-17 16:04:39,786 - root - data_catalog - INFO - DataCatalog: Getting dtu10mdt RasterDataset raster data from /home/runner/.hydromt_data/artifact_data/v0.0.8/dtu10mdt.tif
2023-04-17 16:04:39,862 - root - model_api - WARNING - Replacing forcing: bzs
[9]:
(<Figure size 600x300 with 1 Axes>,
 [<AxesSubplot:title={'center':'SFINCS waterlevel forcing (bzs)'}, ylabel='waterlevel\n[m+ref]'>])
../_images/_examples_example_forcing_9_2.png
[10]:
# update timeseries from csv
df.to_csv("waterlevel.csv")
sf.setup_waterlevel_forcing(
    timeseries="waterlevel.csv",
    merge=True,
)
sf.plot_forcing()
2023-04-17 16:04:40,035 - root - data_catalog - INFO - DataCatalog: Getting waterlevel DataFrame csv data from /home/runner/work/hydromt_sfincs/hydromt_sfincs/docs/_examples/waterlevel.csv
2023-04-17 16:04:40,036 - root - dataframe - INFO - DataFrame: Read csv data.
2023-04-17 16:04:40,060 - root - model_api - WARNING - Replacing forcing: bzs
[10]:
(<Figure size 600x300 with 1 Axes>,
 [<AxesSubplot:title={'center':'SFINCS waterlevel forcing (bzs)'}, ylabel='waterlevel\n[m+ref]'>])
../_images/_examples_example_forcing_10_2.png
[11]:
# overwrite forcing from geodataset (netcdf file)
sf.setup_waterlevel_forcing(
    geodataset="gtsmv3_eu_era5",
    offset="dtu10mdt",
    merge=False,
)
sf.plot_forcing()
2023-04-17 16:04:40,233 - root - data_catalog - INFO - DataCatalog: Getting gtsmv3_eu_era5 GeoDataset netcdf data from /home/runner/.hydromt_data/artifact_data/v0.0.8/gtsmv3_eu_era5.nc
2023-04-17 16:04:40,235 - root - geodataset - INFO - GeoDataset: Read netcdf data and clip to geom (epsg:32633) [263650.000, 5013550.000, 337400.000, 5064350.000].
2023-04-17 16:04:40,361 - root - data_catalog - INFO - DataCatalog: Getting dtu10mdt RasterDataset raster data from /home/runner/.hydromt_data/artifact_data/v0.0.8/dtu10mdt.tif
2023-04-17 16:04:40,420 - root - model_api - WARNING - Replacing forcing: bzs
[11]:
(<Figure size 600x300 with 1 Axes>,
 [<AxesSubplot:title={'center':'SFINCS waterlevel forcing (bzs)'}, ylabel='waterlevel\n[m+ref]'>])
../_images/_examples_example_forcing_11_2.png
[12]:
sf.write_forcing()
sf.write_config()
2023-04-17 16:04:40,705 - root - sfincs - INFO - Write forcing files
2023-04-17 16:04:40,725 - root - sfincs - INFO - Write vector file(s) for forcing.bzs to 'gis' subfolder
[13]:
# note that index number cannot be saved in ascii timeseries format
# and are stored as attributes of the geojson file
sf1 = SfincsModel(sf.root, mode="r")  # read mode
sf1.read_forcing()
sf1.plot_forcing()
2023-04-17 16:04:40,772 - hydromt_sfincs.sfincs - model_api - INFO - Initializing sfincs model from hydromt_sfincs (v1.0.0).
2023-04-17 16:04:40,822 - hydromt_sfincs.sfincs - model_api - WARNING - Replacing forcing: precip
[13]:
(<Figure size 600x600 with 2 Axes>,
 array([<AxesSubplot:title={'center':'SFINCS waterlevel forcing (bzs)'}, ylabel='waterlevel\n[m+ref]'>,
        <AxesSubplot:title={'center':'SFINCS precipitation forcing (precip)'}, ylabel='precipitation\n[mm.hr-1]'>],
       dtype=object))
../_images/_examples_example_forcing_13_2.png