This is an advanced tutorial. This tutorial requires experience with SOBEK, a working hydraulic model and (basic) experience with Python. The following beginner and intermediate tutorials are recommended prior to trying this tutorial:

If you are new to Python in general, consider following online courses to get acquainted with the basics, e.g.:

 

 

In this tutorial we will use DeltaShell's build-in Python scripting module to access results from a SOBEK (Flow1D) model. We will plot results using DeltaShell's graphing functionality and write results to csv file for easy access with your favourite text or spreadsheet editor.

We will use Python scripts from the OpenEarth repository, which make use of the standard library delivered with any DeltaShell distribution. If you are not familiar how to access the OpenEarth scripts, please see this tutorial first. 

 

Step-by-step guide

  1. Check out the 'ModellerFunctions' package from the Open Earth repository (Direct link)
  2. Open your project in DeltaShell. Make sure that the model: 
    1. has at least 1 observation point
    2. has 'Water level (op)' enabled as output
    3. has successfully completed a simulation run. 
  3. In the toolbox window, add a new script. 
  4. First we will import the necessary modules from the ModellerFunctions packages:

    from ModellerFunctions import dsget  # This module contains convenience functions for quick access
    from ModellerFunctions import dsplot as dpl # This module provides a more 'matplotlib' like plotting interface
  5. With the packages imported, we need to retrieve the flow model.

    # Retrieve the flow model
    flow = dsget.GetFlow1DModel()
     
    # Check if you have the correct model:
    print 'Retrieving data from model: %s' % flow.Name
  6. With the flow model available, let's retrieve the data for the observation point. 

    # If you renamed your observation point, change the script accordingly
    observation_point_name = 'ObservationPoint1'
     
    # Get data from observation point
    data = dsget.GetOutputForObservationPoint(flow, observation_point_name)
    
    
    # This will return a python dictionary. We need the following data:
    time = data['Water level (op)']['time']
    waterlevel = data['Water level (op)']['value']
  7. To plot this data in DeltaShell, we will use the 'dsplot' module

    # Create a line element
    line = dpl.drawline(time, waterlevel)
     
    # Give the line a name for in the legend
    line.Title = observation_point_name
     
    # Create a chart object
    chart = dpl.plot([line], title = observation_point_name)
    
    # Change the label of the y-axis
    chart.LeftAxis.Title = 'Water level [m]'
    
    # Plot to screen
    dpl.OpenView(chart)
  8. After calling 'OpenView', a new chart tab will open. The output should look like this. Use the chart buttons in the ribbon and properties window to change the appearance and save the figure to file. 
  9. Next, we want to output the data of this graph to a *.csv file in the same directory as the *.dsproj project file. For this, we will use 'dsget' to retrieve the location of the current project, and use regular Python code to save to a csv file

    # Name and extension of the output file
    output_file_name = 'data.csv'
    
    # We want the file to be saved next to the *.dsproj file
    path = os.path.join(dsget.GetProjectPath(), output_file_name)
    
    # Save to file
    with open(path, 'w') as f:
        for i in range(len(time)):
            f.write('%s, %f \n' % (time[i], waterlevel[i]))     

That's it! Here's the entire code:

 

import os
from ModellerFunctions import dsget  # This module contains convenience functions for quick access
from ModellerFunctions import dsplot as dpl # This module provides a more 'matplotlib' like plotting interface

# Retrieve the flow model
flow = dsget.GetFlow1DModel()

# Check if you have the correct model:
print 'Retrieving data from model: %s' % flow.Name
    
# If you renamed your observation point, change the script accordingly
observation_point_name = 'ObservationPoint1'

# Get data from observation point
data = dsget.GetOutputForObservationPoint(flow, observation_point_name)

# This will return a python dictionary. We need the following data:
time = data['Water level (op)']['time']
waterlevel = data['Water level (op)']['value']

# Create a line element
line = dpl.drawline(time, waterlevel)

# Give the line a name for in the legend
line.Title = observation_point_name

# Create a chart object
chart = dpl.plot([line], title = observation_point_name)

# Change the label of the y-axis
chart.LeftAxis.Title = 'Water level [m]'

# Plot to screen
dpl.OpenView(chart)

# Name and extension of the output file
output_file_name = 'data.csv'

# We want the file to be saved next to the *.dsproj file
path = os.path.join(dsget.GetProjectPath(), output_file_name)

# Save to file
with open(path, 'w') as f:
    for i in range(len(time)):
        f.write('%s, %f \n' % (time[i], h[i]))