{ "cells": [ { "cell_type": "markdown", "id": "79589e26", "metadata": {}, "source": [ "\n", "# Post-processing with NetCDF\n", "\n", "During a hydraulic study, we want to visualize simulations results directly over the study area. This tutorial presents a method for visualizing Watlab simulation results using a **NetCDF** file (`.nc` file). This format allows temporal animation and efficient spatial data handling. NetCDF files can be visualized in **QGIS**.\n", "\n", "To generate a `.nc` file, Watlab provides a function: `watlab.utils.generate_netcdf_file_from_picture`.\n", "\n", "## Function generating the NetCDF file\n", "\n", "- `node_file` → path to the `\"node.txt\"` file located in the model’s `Input` folder \n", "- `cells_file` → path to the `\"cells.txt\"` file located in the model’s `Input` folder \n", "- `friction_values` → path to the `\"friction_values.txt\"` file located in the model’s `Input` folder \n", "- `pic_path_template` → template path to the result files located in the model’s `Output` folder \n", "- `output_file` → path and name of the NetCDF file to be created \n", "- `max_time` → total simulation duration (in seconds) \n", "- `time_step` → interval between time steps (in seconds) \n", "- `EPSG_CODE` → coordinate reference system (Lambert 72 = 31370) \n", "- `initial_time` → simulation start time (usually 0)\n" ] }, { "cell_type": "markdown", "id": "d75662c7", "metadata": {}, "source": [ "```python\n", "from watlab.utils import generate_netcdf_file_from_picture\n", "\n", "generate_netcdf_file_from_picture(\n", " nodes_file = \"Input/nodes.txt\",\n", " cells_file = \"Input/cells.txt\",\n", " manning_values = \"Input/friction_values.txt\",\n", " pic_path_template = \"Output/pic_{:d}_00.txt\",\n", " output_file = \"Andenne_100_50_70Q.nc\",\n", " max_time = 86400,\n", " time_step = 360,\n", " EPSG_CODE = \"31370\",\n", " initial_time = 0\n", ")\n", "```" ] }, { "cell_type": "markdown", "id": "75e27798", "metadata": {}, "source": [ "## Visualizing the NetCDF File in QGIS\n", "\n", "1. **Add the NetCDF file as a mesh layer**\n", " \n", " In QGIS, go to: \n", " `Layer` → `Add Layer` → `Add Mesh Layer...` \n", " Select the `.nc` file generated earlier.\n", "\n", "
\n", "\"Add\n", "
\n", "\n", "2. **Configure the layer properties** \n", " Open the properties of the newly added layer.\n", "\n", " - **Source**:\n", " - Make sure the CRS is correctly set to Lambert 72 (`EPSG:31370`).\n", " - Choose the variable you want to visualize from the available dataset.\n", " - If you check “Treat as static dataset”, you can select a specific time step to display.\n", "\n", " - **Symbology**:\n", " - Adjust the rendering to suit your needs (e.g. color ramp).\n", " - To display the mesh structure, enable “Native mesh rendering”.\n", "\n", " - **Temporal**:\n", " - Activate the Temporal Controller Panel to animate the results.\n", " - You can adjust the time units and time range according to your simulation settings in the layer’s temporal properties.\n", "\n", "
\n", "\"Properties\n", "
\n", "\n", "💡 *To activate Temporal Controller Panel, right-click anywhere in the toolbar area at the top of the QGIS window. A list of available panels will appear.*\n", "\n", "
\n", "\"Temporal\n", "
\n", "\n", "### Example of NedCDF files vizualization in QGIS\n", "\n", "The image below shows a typical result obtained by loading a NetCDF file into QGIS. Using the Temporal Controller Panel, you can navigate through different time steps of the simulation and observe how hydraulic variables evolve over time.\n", "\n", "
\n", "\"Exemple\n", "
\n", "\n" ] }, { "cell_type": "markdown", "id": "05fe6480", "metadata": {}, "source": [ "## Elevation profile\n", "\n", "After navigating through the time using the NetCDF file, you can extract the water surface elevation and compare it to your DEM at a specific time step. To represent water surface elevation relative to terrain, you need to use the `pic_XX_XX.txt` file generated by Watlab in the folder `Output`.\n", "\n", "### 1. Import the result file into QGIS\n", "Go to : `Layer` → `Add Layer` → `Add Delimited Text Layer...`\n", "\n", "
\n", "\"Add\n", "
\n", "\n", "**Parameters:**\n", "- **File name**: Click the three dots to browse to your `Output` folder and select the `pic_XX_XX.txt` file corresponding to the desired time step.\n", "- **File format**: Choose “Custom delimiter” and select **Tab**.\n", "- **Sample Data**: Make sure the columns are correctly detected (e.g., `x`, `y`, `zb`, `h`, etc.).\n", "- **Geometry definition**: \n", " - Type: `Point` \n", " - X field: `x` \n", " - Y field: `y` \n", " - Coordinate reference system: `Lambert 72` (EPSG:31370)\n", "\n", "Once the layer is added, you should see a cloud of points representing the model mesh, one point per cell.\n", "\n", "### 2. Add the Water surface elevation to the attribute table\n", "\n", "To represent water surface elevation relative to terrain, you need to combine the water depth `h` with the terrain elevation `zb` from the model. You have to add a new column `h_zb` to the attribute table of your result file.\n", "\n", "1. Open the **attribute table** of the `pic_XX_XX.txt` layer (right-click on the layer → Open Attribute Table).\n", "2. Open the **Field Calculator**:\n", " - Create a new field\n", " - Name: `h_zb`\n", " - Output field type: `Decimal (double)`\n", " - Expression:\n", " ```\n", " \"h\" + \"zb\"\n", " ```\n", "3. Apply the calculation.\n", "\n", "
\n", "\"Attribute\n", "
\n", "\n", "### 3. Interpolate the water surface elevation\n", "\n", "To visualize the results as a continuous surface, interpolate the point data into a raster.\n", "\n", "Open the **Processing Toolbox**: \n", "`Processing Toolbox` → `TIN Interpolation`\n", "\n", "**Parameters:**\n", "- **Vector layer**: Select the `pic` layer imported earlier.\n", "- **Interpolation attribute**: Choose `h_zb`. Click the `+` to add it.\n", "- **Interpolation method**: `Linear`\n", "- **Extent**: Use the extent of the `pic` layer\n", "- **Pixel size**: Set according to the desired resolution\n", "\n", "Give the TIF file a clear name to identify the time step and variable. In the layer properties, adjust the symbology for optimal readability. \n", "\n", "💡 *To produce high-quality visual outputs, QGIS also allows you to design map layouts with legends, titles, and scale bars using `Project` → `New Print Layout`. This is ideal for exporting clean figures for reports or presentations.*\n", "\n", "### 4. Terrain profile\n", "\n", "To generate a terrain profile, you can use the `Profile Tool` plugin. This allows you to draw a temporary line directly on the map or use a predefined shapefile if you want to consistently apply the same cross-section across different datasets.\n", "\n", "Once the raster is generated, you can compare the interpolated water level with your DEM to produce elevation profiles at a specific time step.\n", "\n", "
\n", "\"Terrain\n", "
\n", "\n", "The picture below illustrates a typical elevation profile:\n", "\n", "- **Red curve**: Digital Elevation Model (DEM), representing the terrain.\n", "- **Blue curve**: Water surface elevation (`h + zb`), showing the simulated water level.\n", "\n", "💡 *You can export the data (right-click → Export) and create beautiful plots using Python libraries such as Matplotlib or Seaborn.*\n", "\n", "
\n", "\"Elevation\n", "
\n" ] } ], "metadata": { "kernelspec": { "display_name": "watlabrelease", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 5 }