Skip to content

Commit 966e463

Browse files
committed
Add docstrings and logfiles
1 parent aa58edf commit 966e463

9 files changed

Lines changed: 61 additions & 6 deletions

File tree

workflow/rules/automatic.smk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ rule download_cutout_slope:
1010
vector="resources/user/shapes/{shape}.parquet",
1111
output:
1212
path="resources/automatic/cutout/{shape}/slope.tif",
13+
log:
14+
"logs/{shape}/download_cutout_slope.log",
1315
wrapper:
1416
"v7.2.0/geo/rasterio/clip-geotiff"
1517

@@ -23,6 +25,8 @@ rule download_cutout_bathymetry:
2325
vector="resources/user/shapes/{shape}.parquet",
2426
output:
2527
path="resources/automatic/cutout/{shape}/bathymetry.tif",
28+
log:
29+
"logs/{shape}/download_cutout_bathymetry.log",
2630
wrapper:
2731
"v7.2.0/geo/rasterio/clip-geotiff"
2832

@@ -34,6 +38,8 @@ rule download_globcover:
3438
url=internal["resources"]["automatic"]["globcover"],
3539
output:
3640
"resources/automatic/global/globcover.zip",
41+
log:
42+
"logs/download_globcover.log",
3743
conda:
3844
"../envs/shell.yaml"
3945
shell:
@@ -70,6 +76,8 @@ rule download_ghsl:
7076
url=internal["resources"]["automatic"]["ghsl"],
7177
output:
7278
"resources/automatic/global/ghsl_built_s.zip",
79+
log:
80+
"logs/download_ghsl.log",
7381
conda:
7482
"../envs/shell.yaml"
7583
shell:
@@ -86,6 +94,8 @@ rule unzip_ghsl:
8694
zipfile=rules.download_ghsl.output,
8795
output:
8896
"resources/automatic/global/ghsl_built_s.tif",
97+
log:
98+
"logs/unzip_ghsl.log",
8999
conda:
90100
"../envs/shell.yaml"
91101
shell:

workflow/rules/prepare.smk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ rule cutout_landcover:
99
landcover=rules.unzip_globcover.output,
1010
output:
1111
"resources/automatic/cutout/{shape}/landcover.tif",
12+
log:
13+
"logs/{shape}/cutout_landcover.log",
1214
conda:
1315
"../envs/default.yaml"
1416
shell:
@@ -25,6 +27,8 @@ rule cutout_settlement:
2527
settlement=rules.unzip_ghsl.output,
2628
output:
2729
"resources/automatic/cutout/{shape}/settlement.tif",
30+
log:
31+
"logs/{shape}/cutout_settlement.log",
2832
conda:
2933
"../envs/default.yaml"
3034
shell:

workflow/rules/process.smk

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ rule prepare_resampled_inputs:
1515
"resources/automatic/{shape}.resampled_inputs.png",
1616
category="resampled_input",
1717
),
18+
log:
19+
"logs/{shape}/prepare_resampled_inputs.log",
1820
conda:
1921
"../envs/default.yaml"
2022
shell:
2123
"""
24+
set -x
2225
python "{input.script}" \
2326
"{input.shapes}" "{input.land_cover_path}" "{input.slope_path}" "{input.settlement_path}" "{input.bathymetry_path}" "{input.protected_area_path}" \
24-
"{output.resampled_input}" "{output.plot}"
27+
"{output.resampled_input}" "{output.plot}" 2> "{log}"
2528
"""
2629

2730

@@ -41,12 +44,14 @@ rule area_potential:
4144
"results/{shape}/area_potential_{tech}.png",
4245
category="area_potential",
4346
),
47+
log:
48+
"logs/{shape}/area_potential_{tech}.log",
4449
conda:
4550
"../envs/default.yaml"
4651
shell:
4752
"""
4853
set -x
49-
python "{input.script}" "{input.shapes}" "{input.resampled_path}" "{params.config}" "{params.buffer_crs}" "{output.area_potential}" "{output.plot}"
54+
python "{input.script}" "{input.shapes}" "{input.resampled_path}" "{params.config}" "{params.buffer_crs}" "{output.area_potential}" "{output.plot}" 2> "{log}"
5055
"""
5156

5257

@@ -66,6 +71,8 @@ rule area_potential_report:
6671
"results/{shape}/area_potential_report.html",
6772
category="area_potential_report",
6873
),
74+
log:
75+
"logs/{shape}/area_potential_report.log",
6976
conda:
7077
"../envs/default.yaml"
7178
script:

workflow/scripts/area_potential.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
"""This script calculates the area potential based on the provided configuration."""
2+
13
import click
24
import geo
35
import geopandas as gpd
4-
import matplotlib.pyplot as plt
56
import xarray as xr
67
import yaml
78

@@ -16,6 +17,20 @@
1617
def get_area_potential(
1718
shapes_path, resampled_path, config, buffer_crs, output_path, plot_path
1819
):
20+
"""Calculate the area potential based on the provided configuration.
21+
22+
Args:
23+
shapes_path (str): Path to the input shapes in the parquet format.
24+
resampled_path (str): Path to the resampled input data in the NetCDF format.
25+
config (str): Configuration YAML string.
26+
buffer_crs (str): Coordinate Reference System for buffering shapes.
27+
output_path (str): Path to save the resulting area potential raster.
28+
plot_path (str): Path to save the plot of the area potential.
29+
30+
Returns:
31+
None
32+
33+
"""
1934
shapes = gpd.read_parquet(shapes_path)
2035
ds = xr.open_dataset(resampled_path, decode_coords="all")
2136
# FIXME: this is a workaround for the CRS not being set correctly; not sure why
@@ -82,7 +97,7 @@ def get_area_potential(
8297
potential_da.rio.to_raster(output_path, driver="GTiff", compress="LZW")
8398

8499
plot = potential_da.plot()
85-
plt.savefig(plot_path, bbox_inches="tight")
100+
plot.savefig(plot_path, bbox_inches="tight")
86101

87102

88103
if __name__ == "__main__":

workflow/scripts/geo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""This module provides functions to buffer geometries using UTM projections."""
2+
13
import warnings
24

35
import geopandas as gpd

workflow/scripts/report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
"""This script generates a report summarizing area potentials for different technologies."""
2+
13
import geopandas as gpd
24
import pandas as pd
35
import rioxarray as rxr
46
import xarray as xr
57

68

79
def report(shapes, resampled_path, area_potentials, csv_path, html_path):
10+
"""Generate a report summarizing area potentials for different technologies."""
811
shapes = gpd.read_parquet(shapes)
912
ds_inputs = xr.open_dataset(resampled_path, decode_coords="all")
1013
# FIXME: this is a workaround for the CRS not being set correctly; not sure why

workflow/scripts/resample.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""This script resamples various geospatial datasets to a common shape and resolution."""
2+
13
import math
24

35
import click
@@ -68,6 +70,7 @@
6870

6971

7072
def get_suitable_land_cover_type(ds_land_cover, suitable_land_cover_types):
73+
"""Convert raw GlobCover data to a dataset with suitable land cover types."""
7174
suitable_land_cover = xr.Dataset(coords=ds_land_cover.coords)
7275

7376
# convert the input value to land cover type of interest
@@ -158,7 +161,7 @@ def _rasterize_regions(shapes, reference_raster):
158161
@click.argument("protected_area_path", type=str)
159162
@click.argument("output_path", type=str)
160163
@click.argument("plot_path", type=str)
161-
def get_same_shape_and_resolution(
164+
def resample_inputs(
162165
shapes_path,
163166
land_cover_path,
164167
slope_path,
@@ -168,6 +171,12 @@ def get_same_shape_and_resolution(
168171
output_path,
169172
plot_path,
170173
):
174+
"""Resample various geospatial datasets to a common shape and resolution.
175+
176+
Results are saved to the specified output path in NetCDF format,
177+
and a plot of the resampled data is saved to the specified plot path.
178+
179+
"""
171180
shapes = gpd.read_parquet(shapes_path)
172181

173182
##
@@ -291,4 +300,4 @@ def get_same_shape_and_resolution(
291300

292301

293302
if __name__ == "__main__":
294-
get_same_shape_and_resolution()
303+
resample_inputs()

workflow/scripts/script_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
"""Utility functions."""
2+
13
import math
24

35
import matplotlib.pyplot as plt
46

57

68
def plot_all_dataset_variables(ds, ncols=2, savefig=None):
9+
"""Plot all variables in an xarray dataset on a grid of plots."""
710
# Drop dimensionless variables
811
ds = ds.drop_vars(lambda x: [v for v, da in x.variables.items() if not da.ndim])
912

workflow/scripts/unzip_like.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Emulates the `unzip` command across platforms."""
2+
13
import os
24
import zipfile
35

0 commit comments

Comments
 (0)