{ "cells": [ { "cell_type": "markdown", "id": "4043e4a1", "metadata": {}, "source": [ "# Calculate spatial flux\n", "\n", "This example shows how to calculate a spatial greenhouse gas (GHG) flux based on modelled input from for instance [SOMERS](https://www.nobveenweiden.nl/wp-content/uploads/2024/11/rapportage-SOMERS-2.0-technische-beschrijving.pdf). Lusos calculates a spatial flux by taking a weighed average of the median fluxes of polygons that fall within each raster cell of a 2D grid. Following the previously explained [concept](./concept.ipynb), lusos computes the area of each polygon within a raster cell. The computed areas are used as the weights to assign the computed flux to each cell. This example shows the simple steps how to calculate to spatial flux. We will use the same example area from the [coverage](./coverage.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "ad7ffb14", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "from shapely import geometry as gmt\n", "\n", "import lusos\n", "\n", "# Bounding box for the example area\n", "xmin, ymin, xmax, ymax = 111_000, 455_000, 116_000, 460_000\n", "study_area = gpd.GeoDataFrame(geometry=[gmt.box(xmin, ymin, xmax, ymax)], crs=28992)\n", "\n", "emissions = lusos.data.sample_emissions()" ] }, { "cell_type": "markdown", "id": "a17940ca", "metadata": {}, "source": [ "Let's first checkout the emissions data:" ] }, { "cell_type": "code", "execution_count": null, "id": "d1ea5805", "metadata": {}, "outputs": [], "source": [ "print(emissions.head())\n", "m = emissions.explore() # Display the emissions data on a map\n", "study_area.explore(m=m, color=\"black\", fill=False)" ] }, { "cell_type": "markdown", "id": "bc242f49", "metadata": {}, "source": [ "The data contains emission values for polygons in `ton/ha`. Lusos expects this unit for the input data and translates this into a flux per m2. Using a different therefore means that the output will be in the wrong unit as well and must be translated to ton/m2.\n", "\n", "Lusos needs two columns to be present in the input data: \"parcel_id\" and \"median\". This is because lusos bases the calculation on the median emission and we need an id for each polygon to be able to identify it and correctly assign fluxes to cells. Without the presence of these columns, an error will be thrown. We can rename the column \"emission_t\" because this will be used as the median and make the other columns lowercase strings because \"PARCEL_ID\" is present in the data. " ] }, { "cell_type": "code", "execution_count": null, "id": "84f1a336", "metadata": {}, "outputs": [], "source": [ "emissions.columns = emissions.columns.str.lower()\n", "emissions.rename(columns={\"emission_t\": \"median\"}, inplace=True)\n", "emissions.head()" ] }, { "cell_type": "markdown", "id": "87a85194", "metadata": {}, "source": [ "Now we can define our grid again for the calculation and calculate the flux." ] }, { "cell_type": "code", "execution_count": null, "id": "11d06451", "metadata": {}, "outputs": [], "source": [ "grid = lusos.LassoGrid(xmin, ymin, xmax, ymax, 25, 25)\n", "flux_per_m2 = lusos.calculate_somers_emissions(emissions, grid)\n", "flux_per_m2" ] } ], "metadata": { "kernelspec": { "display_name": "default", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }