Input and Output Scripts
Input and Output Scripts
Input and Output Scripts
This paper contains useful information to code a custom hydraulic rule in the MATLAB HEC-RAS
interface.
An example which employs a lot of concepts presented in this paper is shown in the video tutorial 1 –
Simple automation example.
Table of contents
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 1
Common features between scripts
You can obtain detailed documentation about every variable presented in the following paragraph,
by writing in the MATLAB command window, after execution of a version of the interface, help
<variable name>, and then by clicking on hyperlink, like in the screenshot below :
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 2
Tableau 2 – useful subvariables
To call a new variable in a input or output script, you have to change two code lines : in the call of the
input or output function in the master function, and in the input or output function code file itself.
Script before adding files variable Script after adding files variable
Note : the sett.XSlist variable of the master script change name and is called XS in the input_init
function. Because there is no need to have the same name for local variable and main variable. Cf.
any programming language course or MATLAB internal documentation.
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 3
Input scripts
- Input_before_init, which is a function called only before the first step (the simulation step
during which a restart file is created for the next step, but which is not based on a restart file
because it is the first)
- Input_before_step which is a function called before every step (except the first one)
Code structure
Let variableX a variable that can be any variable from the master script, X an integer.
1. Get or instantiate variable1 with a useful value (e.g. water stage measured during previous
step.
2. Logical rule, which change the value of variable2, which will be written in HEC-RAS files
depending on the value of variable1.
3. Save into input_list MATLAB cell array, with parameters you want. To make MATLAB able to
understand where in the Ras files, it must save variable2.
If you encounter any error, these errors can happen in a low level function, i.e. which are called by a
lot of other functions. In this case, first check arguments given to input_list, before trying to edit a
low level function. Keep in mind that they have been tested and they worked on other Ras projects.
If, despite that, you think the function must be edited, please refer to Dev Doc paper.
Input_list object
This object is one of the main interfaces between your code and the MATLAB HEC-RAS interface
code. It is very important to use parameters presented below, to avoid any issue.
Data is a variable and supports different types of values depending on every other parameters.
Additional information is an unused field for the time being. But it should be used if the MATLAB
HEC-RAS interface supports editing of new types of parameters.
For the <empty> values, you can reduce the cell array size of input_list if there is a column or a row
full of <empty>. You can too write what you want (ASCII) in this cell, its content will not be
considered.
In this array, parameters ref_param and xs are not listed, because they have to much different values
possible.
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 4
The possible values of ref_param are available further in this paper, in the table 5.
It is not useful to give a list of possible values of xs, because cross section name possibilities are quite
endless. To know how to instantiate a XS object, please refer to the integrated doc of this class (in
the MATLAB code, or by writing help XS in the command window after an execution of launcher and
any version).
Table 3 – possible values of main parameters
Types description
Param = a standalone param, this string must appear only one time in the target Ras file.
If you want to edit a subordinate param (called ref_param, or if it is a two times subordinate param,
called sub_param). Please refer to Dev Doc, dependencies between parameters paragraph.
In this table, list of combinations tries to be exhaustive, but it is possible that some combinations not
tested work. If you find some, you can contact us to share your discovery.
1
Type is a MATLAB HEC-RAS interface param, defined to help the input_writing function recognize data types
2
Date strings must be in a format immediately recognized by the datetime function, like the date syntax used
in settings.ini
3
This string will be written as is in Ras files, MATLAB does not check anything about this string. Do not include a
newline character in this string, a RASnewline character will be automatically added.
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 5
Table 5 – Possible combinations of parameters
Cross
Type (for Name of the section
Full Ras file Additional
name
the reference where this
extension information
interface) parameter parameter
applies
Name in
script
Type Fileext Ref_param XS other_info
XS class object,
hydrograph .u Flow hydrograph not on a
structure <empty>
XS class object,
hydrograph .u Stage hydrograph not on a
structure <empty>
XS class object,
hydrograph .u Stage and Flow not on a
Hydrograph structure <empty>
XS class object,
rating curve .u only on a
Rating Curve boundary <empty>
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 6
Output scripts
Code structure
Here is an example of simple code structure, but you do not have to follow this pattern in every case.
There is a lot of computations, that you can perform after a simulation time step.
results object
results is a cell array of Results_ objects. So, results{1} give you access to a Results_ object, the
number 1 is related to the XS number chosen in settings.ini. There, results{1} contains results at
every simulation step of the XS saved in the XS1 variable of the settings.ini file.
But during call to GetValue method of the settings object, you have to give the xs{1} argument,
because the Results_ object is not aware of his cell number (there is potential improvement there).
xs object
xs is a cell array structured like results, xs contains XS objects given in settings.ini. Which is cross
section location information.
To get the xs number 1, you just have to get the cell number 1 in the xs array, like this : xs{1}.
rp object
Warning : Ras must run in background when MATLAB writes in Ras files.
Else, Results_ methods will be unable to ask information to Ras. That is why rp is required.
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 7
How to use the output_after_all script – concrete example
This example is based on an idea of Dr. Duviella, Professor at IMT Lille Douai
In the case of a real system automation, if you compute simulations for 24 hours, with a simulation
time step of 10 minutes, it will produce a big amount of data. It can be handy, to restart a new global
simulation, e.g. a master restart, based on the results you obtain during the last master execution.
Because instead of keeping in RAM, data of previous days, they will be saved on hard drive.
You can, for instance, call an auto-launch script (not included in the interface code), which will save
results in a spreadsheet and read thanks to input_before_init the content of this spreadsheet to
obtains good initials conditions.
Input and output scripts – Ronan DESHAYS for IMTLD, july 2020 – page 8