Contents
Exercise outline
The goal of this exercise is to create a simple volume model. In the end it should be possible to run the volume model and inspect some (dummy) spatio-temporal output results.
Create a new model class
Add a new folder to the plugin project named Models. In this folder, create a new class named VolumeModel.cs and add the following code:
using System.Linq; using DelftTools.Functions; using DelftTools.Functions.Generic; using DelftTools.Hydro; using DelftTools.Shell.Core.Workflow; using DelftTools.Shell.Core.Workflow.DataItems; using log4net; using NetTopologySuite.Extensions.Coverages; namespace DeltaShell.Plugin.DemoApp.Models { public class VolumeModel : ModelBase { private static readonly ILog Log = LogManager.GetLogger(typeof(VolumeModel)); // Handle for writing log messages private readonly DrainageBasin basin; private readonly TimeSeries precipitation; private readonly FeatureCoverage volume; /// <summary> /// Creates a volume model /// </summary> public VolumeModel() { // Create the input items of the volume model basin = new DrainageBasin(); precipitation = new TimeSeries { Components = { new Variable<double>("Precipitation") } }; // Create the output item of the volume model volume = new FeatureCoverage("Output data") { IsTimeDependent = true, Arguments = { new Variable<Catchment>("Catchment") { FixedSize = 0 } }, Components = { new Variable<double>("Volume") }, }; // Actually add the input/output items to the model after wrapping them with data items DataItems.Add(new DataItem(basin, "Basin", typeof(DrainageBasin), DataItemRole.Input, "BasinTag")); DataItems.Add(new DataItem(precipitation, "Precipitation", typeof(TimeSeries), DataItemRole.Input, "PrecipitationTag")); DataItems.Add(new DataItem(volume, "Volume", typeof(FeatureCoverage), DataItemRole.Output, "VolumeTag")); } /// <summary> /// The basin of the volume model /// </summary> public DrainageBasin Basin { get { return basin; } } /// <summary> /// The precipitation time series of the volume model /// </summary> public TimeSeries Precipitation { get { return precipitation; } } /// <summary> /// The initialization of model runs /// </summary> protected override void OnInitialize() { // Clear any previous output volume.Clear(); // Ensure the coordinate system of the volume output is the same as the catchments input (basin) volume.CoordinateSystem = basin.CoordinateSystem; // Check if at least one catchment is present if (!basin.Catchments.Any()) { Log.Error("At least one catchment should be present"); Status = ActivityStatus.Failed; // Cancels the model run } // Check if at least one precipitation value is present if (precipitation.Time.Values.Count == 0) { Log.Error("At least one precipitation value should be present"); Status = ActivityStatus.Failed; // Cancels the model run } if (Status == ActivityStatus.Failed) { return; } // Initialize the volume output feature coverage volume.Features.AddRange(basin.Catchments); volume.FeatureVariable.FixedSize = basin.Catchments.Count; volume.FeatureVariable.AddValues(basin.Catchments); } /// <summary> /// The actual calculation during the model runs /// </summary> protected override bool OnExecute() { // Obtain all times of the precipitation time series var times = precipitation.Time.GetValues(); // Loop all times foreach (var time in times) { // Obtain the precipitation value for the current time var timeSeriesValue = (double) precipitation[time]; // Loop all catchments and calculate (a dummy) volume value based on catchment area and precipitation value var volumeValues = basin.Catchments.Select(c => c.AreaSize * timeSeriesValue); // Add the calculated volume values to the output feature coverage volume[time] = volumeValues; } return true; } } }
The model class derives the ModelBase class in order to automatically implement some basic time dependent modeling logic.
Furthermore, the comments in the code should explain the different parts of the model implementation.
The model uses some basic data structures like data items, (feature) coverages and timeseries (functions). A description on the backgrounds and usage of these data structures is not part of this tutorial.
[TODO]
Add links to some wiki pages?Register the model in the application plugin class
Register the model in the application plugin by adding the following code to VolumeModelApplicationPlugin.cs:
using DeltaShell.Plugin.DemoApp.Models;
and
public override IEnumerable<ModelInfo> GetModelInfos() { yield return new ModelInfo { Name = "Volume Model", Category = "DemoApp models", CreateModel = o => new VolumeModel() }; }
Delta Shell should now be able to detect, create and run volume models.
Exercise results
First of all, obtain the following WaterML2 XML file: WaterML2_precipitation_data.XML
Also obtain and unzip the following shape files: Gemeenten.7z
Then run the application and start creating a new model item (right click on project | Add | New Model ...). Ensure the new model is visible in the model selection dialog like shown in the following image:
[TODO]
After selecting the volume model, a new model item should be added to the project with a structure like shown in the following image:
[TODO]
Run the model (right click on the volume model item | Run Model) and check the Messages window; some error messages should be present like shown in the following image:
[TODO]
In order to perform a successful model run, some precipitation and catchment input data needs to be imported.
First, start importing some WaterML2 data on the precipitation time series item (right click the precipitation item | Import...). A file selection dialog automatically pops up. Select the obtained WaterML2 XML file.
After finishing the import action, the precipitation item should contain data like shown in the following image:
[TODO]
Secondly, start importing a shape file on the basin item (right click the basin item | Import...). A GIS import wizard automatically pops up. Walk through the wizard like shown in the following images:
[TODO]
[TODO]
=> Catchments
=> File
=> Click "Add to import list"
=> Part of import list
[TODO]
=> GM_NAAM
[TODO]
[TODO]
After finishing the import action, the basin item should contain data like shown in the following image:
[TODO]
Run the model again and check the Messages window; no error messages should be present.
Open the volume output and ensure the model results are like shown in the following image:
[TODO]
In order to inspect time dependent (output) data, open the Time Navigator window and move the slider or click one of the auto play buttons.