 Hello, this is Hans van der Kwas senior lecturer at IHG Delft Institute for Water Education. In a previous video I have demonstrated how to calculate the volume in a DEM that is below a certain elevation level. Now, this is also very useful to make the stage volume curve. And when you have a stage volume curve in practice, you can use it, for example, when you have the water level of a lake, and if you have the bathymetry, then you can determine the volume that corresponds with different measured water levels. In this video, I'm going to show a little PyQG script that I made to iterate over the tool that I used in the previous video, the raster surface volume tool. I'm going to explain how it works. So there are a few steps we need to do. I need to read the DEM. Then I need to determine the minimum and the maximum value of the DEM and calculate the range, the difference between the minimum and maximum. Then I want to have those points for the stage with 10% increments from the bottom of the pit. And then for those values, I want to know the volume. I also need to do some unit conversions because the tool reports the volumes in cubic meters and it's often more appropriate to do it in cubic kilometers. And also the volumes are reported in negative values, so we need to convert it to the absolute value. And then in the end, I want a table that I can visualize the results in the data plugly plugin. So here I define the path of the project where I have my DEM file and where I want my outputs to be stored. Here I give the DEM raster file, it's the only file here in this folder. And here I open the DEM, so input raster DEM, the name of the layer that will be visualized and I use GDAL because it's a GDAL format. Next I want to calculate the statistics and there is this function, band statistics and it only has one band, so it's number one. And I use this data provider under the DEM. Then I use this stats.minimum value to calculate the minimum value in the DEM and stats.maximum value for the maximum. I want to give the user some feedback, so I print those values to the screen. And here I calculate the range. I also print the range to the screen. I determine the 10% increment by dividing the range by 10 and I also print in the increment of the stage points to the screen. I make an empty list, I need that to store the tables for the different stage values and then later merge them into one table. So four level in the range minimum to maximum plus one because in Python the range does not include the maximum value. Here I wanted to include, so I add one meter to it and here I use the increment. Note that this is not range but F range, that's because in Python you cannot have floating point ranges. And therefore this is a function here where we have the F range with the starting value, the stop value and the step value that are also defined here as the minimum, the maximum plus one and the increment. Then I define the name of the out table and because the stage is in meters it has decimals but I don't want an extra dot in the file name and I want still to have a recognizable file name. So it's project path and then the file name is volume plus the level times 100, so basically in centimeters round it off and convert it to a string. And I also need it as a dbf and then this is basically the core of the function. You can get this from the history, so everything that you do in the processing toolbox is stored here in the history and if I want to do the surface, what's the surface volume tool and let's use the DEM and you can use here any level, that's not the point now and we use the count only below base level and the surface volume report, we keep that to temporary file and the surface volume table we say save to file, then when I run it gives this error like it did also previously but now in the processing toolbox if I go to the history I have this function. If you look at this closer then you see that it's a dictionary where the dialogue fields are here first and then this is the files and the parameters that you define and that means if we use this in a Python script that we can use those parameters as variables. So let's have a look here, it runs that function, input is the DEM layer that we defined up, we keep band number one, level is then the level from the loop for a level in the range, method one which is calculate the volume below the level, then the HTML file we don't need it so we keep it what it was and the table it needs a shape file although it will output a DBF therefore you get that error when you use it from the processing toolbox but here I define the shape file that we want to create so here I read the table that is the output of the processing tool and then this table only has one feature which has the volume of a certain stage so to get that I'm going to read the volume from the table and convert it to cubic kilometers so this is the field name and the value from that field name is divided by this amount to get it in cubic kilometers but since it's negative I also want the absolute value so I use the apps function then I'm going to load the DBF table again because I want to add two attributes I want to add the attribute of the level and of the volume in cubic kilometers to the table so here update fields starting the editing and then here for F in the table get features so for level fill in the value of the stage of the level and for the volume in cubic kilometers fill in the variable volume kilometers that we've calculated over there then update the feature and then commit the changes so basically we have this empty list and we use the append to add each time the file name of the DBF that comes out here to the list so in the next step outside of the loop in the end we can simply merge these vector layers so I used their this function merge vector layers and I created that processing tool so you can simply copy it like I did up before and you only need to define then the path and the output file name and here I load the output debase file and then I add it to the layers list and that's basically the result and then I'm going to use the data plot leap plug-in to create the graph so let's run it it's quite quick so this is the DM it's loaded and then we have here the table stage volume if I open the attribute table you can see the values by the way here it reported the minimum and the maximum value of the DM the elevation difference and increment value that's used for the stage for the different levels so here we see those levels with an increment and here we see the volume the absolute volume in cubic kilometer calculated it also gives us the layer names and paths you can still find those files in the folder that's it's added here you have it and then we are basically ready to create our data plotly graph it's a plotly panel we define for the X field the volume in cubic meters and for the Y field the stage the level and here we see the graph it's nicer to have it with a line connected so I use points and lines and let's give it a title stage volume the legend doesn't need a title and then we change the X label to volume in cubic kilometers and the level in meters and there's the final result so in this video you've seen how to create a stage volume curve using PyQGIS and processing tools that you could embed in the scripts if you like these videos please subscribe to my youtube channel and for more free GIS materials go to IHEDovsOpenCourseWare at GISOpenCourseWare.org