 Hello, in this video we are going to add a PCRuster dynamic model to the processing toolbox and we will use the interface of QGIS to configure the layers that are needed in the model and to configure the amount of time steps. We will use the demo data that can be downloaded from the PCRuster website, you find it in the software menu, download demo data and choose there the dataset for your operating system. Then go to QGIS and open a Python console and also add the editor. In the editor we are going to load the runoff.py dynamic model from PCRuster, which uses the PCRuster dynamic modeling framework. So let's have a closer look at this runoff model. It's a dynamic model in the PCRuster dynamic framework which initializes with the clone map. Then in the initial method you find there that the rain zones are defined and the infiltration capacity is linked to the soil map with a lookup table, that the LDD map is derived from the DEM and that there are some calculations done in the dynamic section to derive the runoff. Now if we run this model we will give an error, that's because this script is run from a working directory which is not the same as the directory where the input files are stored. So the error is that it cannot open the mask.map file because it can't find it. So we can give an instruction to go to another directory and therefore we need to import the OS package. Then with OS change there we can change to the directory, we need to define it as a string and this is also not working because the slashes are escape characters. So you either have to do them to the other side, forward slashes or double backslash and then there's the command os.get current working directory which gives where we are and there we see it uses the double slashes, double backslash to escape one of the backslashes and it prints now these dots for each time step which means the model has run and we can find the outputs in the working directory. So let's open a terminal by clicking right on the folder and choose open in terminal and then you can see that it produced these result rusters and then we can also visualize it using the Aguila tool which comes with PCRuster and because it's dynamic I can use time steps 1 to 28 and I want to see the log runoff and there we see it and we can animate it. Now it would be really nice if a user could run this from the QGIS interface and define all the inputs and outputs through a graphical user interface so that's what we are going to make now. Let's first delete the outputs. Now in this model all the inputs and outputs are hard coded with strings and we need to replace them with variables because these variables will be passed from the processing script. So it reads here ratestop.map and I can make this variable. It needs to have a global variable therefore I use self dot and let's call it rainstations and it needs to be added to the init arguments without self and then we make here a list of variables with self so we declare here self to the rainstations equals rainstations and we're going to do that for all the other variables and add them to the init method as arguments so we can pass that on from the QGIS script. We're not going to write the time series of the runoff at locations so I'm going to comment this out. Also comment outline 55 because we are not writing this data at the sample locations and I've also commented out some report statements and I removed the last lines because they are going to the PyQGIS script that we're going to make now. So I save this model and it's now ready to be imported in the PyQGIS processing script. So let's install the PCRasterTools plugin and it's also good to use an existing example which we can get from the resource sharing plugin. There are existing scripts that we can use and modify. Let's go to the settings of the QGIS resource sharing plugin and add PCRasterRepository. I'll also add it in the description of this video and don't forget to add .git. Here we see the collections being added and we're going to add now the userscript collection as examples. Then in the processing toolbox under scripts, PCRasterUserscripts, you can find these nine scripts. Let's edit the burned up script. To make it easier at this point I import all the PCRaster functions and for the framework we also need to import the PCRaster framework and we need to import our runoff model and we can import that because it's already open here in the console so it will be recognized. Now I need this line to import console because the model will print those or try to print those dots for each time step but that will cause problems and by inserting something later from the console package we can prevent that it hangs. We need to add a few more of these processing parameter functions here. Then we can change the class name to the name of the program. Let's call it runoffModel. Let's modify this so it shows all the inputs and outputs that we use. We're input to clone. That's the first input. Here at the instance we need to return the same main runoffModel in here and name to identify it. The runoffModel can only be lowercase and this is the name that the user will see and we add it to a new group which we call PCRasterDynamicModels and that group also needs an ID that goes PCRasterDynamic. Here's the help string, we'll change that later. And here we need to modify the user interface so what kind of widget and the text that the user sees we remove what we don't need. So the first one is to clone. We can do this in a similar way for the other raster layers. For the rainfall time series file we need another function here hit the parameter file and we copy and adjust it for other files that are in the input. Then the first and last time step are numbers. Let's remove now what we don't need. Before the output map stacks we'll use parameter file destination to store the puts on the user interface as raster layers strings integers in new variables. New variables store the paths that we will use. And then we can copy those lines that we saved for this moment to run the model. We're going to clone map to variable and we're going to add the other variables that have to be substituted in the runoff script to the runoff model. So we need to use here the console package because PCRaster is expecting to print the dots to the console but there is no console in QGIS. With this that is solved we can visualize the results with Aguila. We define a string with the command. We add placeholders for the variables. That will be first step, last step and the variables that we want to see so it's the precipitation and discharge. And here with dot format outside of the string we can then identify what goes into these placeholders. It's useful for the user to see the command so it can always be copied to terminal to run the same command to visualize the result. And then we use feedback pushing foam. Problem is that it will open a terminal window quite large on the screen and it's better to create no window and therefore use these few lines. And we can copy and modify those to create the 3D view. Because the results are not loaded in the QGIS map stack we keep the results empty so I'm going to move line 260. And then the script is ready to save and we save it in our profile scripts folder. So now we can run the model by clicking the play button and there we see the widgets but we also see that we still need to update the help string. So I quickly pasted some prepared text there and I save it and if I run it again I see the updated help string and it even includes URLs to the official documentation of PC Raster and Agila. Now let's try it for real. So we need some data for the model. Load DEM. You can see that it does not recognize any coordinate reference system but that's also because PC Raster doesn't deal with it so it doesn't matter for a model. And then I find the model in the processing toolbox and I set the inputs in the dialog. For the output map stacks I need to define the folder and the output file name but it's not a standard file name because these map stacks of PC Raster they have the file extension 001, 002 etc. for the amount of time steps and that will be automatically added. So I'm just creating here a dummy file name and I replace it with the first characters of the time series and I copy this also to get the runoff map stack so before the dot it will fill it up with 0s until 8 characters before the dot. We see that there's still an error so let's debug it further I can edit the script and then we see that the data provider has to be in capitals. Let's run it again. There's still one more error which is in fact an important error it cannot find the runoff model so let's go back and we did not use the runoff model class yet that we imported and that needs to be put there. So to create runoff.runoff model with all the arguments and there's the result when we run it it automatically opens the viewers we can animate the results we can click on a pixel when we click right on the legend we can see the time series and the windows are connected so we can see also here the precipitation time series and compare precipitation with the discharge we see that they are related and when we exit those windows we also see the 3D window and after closing the windows it shows you the commands that you can simply copy to a terminal to visualize the results in the same way so in this video you've learned how to create a processing tool for an existing PCRuster model but I invite you to share your model here in this resource sharing repository for PCRuster and you can share the script for QJS and the model you need to share them both and here is also uploaded the script that I've used so with the resource sharing plugin you will then get the runoff model script that you can use in the processing toolbox but runoff.py script that's what you need to open in the Python console of QJS before you can use the pyQJS script from the processing toolbox