darts_preprocessing.legacy
¶
PLANET scene based preprocessing.
calculate_ndvi
¶
Calculate NDVI (Normalized Difference Vegetation Index) from spectral bands.
NDVI is a widely-used vegetation index that indicates photosynthetic activity and vegetation health. Values range from -1 to 1, with higher values indicating denser, healthier vegetation.
Parameters:
-
optical(xarray.Dataset) –Dataset containing spectral bands: - nir (float32): Near-infrared reflectance [0-1] - red (float32): Red reflectance [0-1]
Returns:
Note
Formula: NDVI = (NIR - Red) / (NIR + Red)
Input bands are clipped to [0, 1] before calculation to avoid numerical instabilities from negative reflectance values or sensor artifacts. The final result is also clipped to ensure values remain in the valid [-1, 1] range.
Example
Calculate NDVI from optical data:
Source code in darts-preprocessing/src/darts_preprocessing/engineering/indices.py
calculate_slope
¶
Calculate slope of the terrain surface from an ArcticDEM Dataset.
Slope represents the rate of change of elevation, indicating terrain steepness.
Parameters:
Returns:
-
xarray.Dataset–xr.Dataset: Input Dataset with new data variable added:
-
xarray.Dataset–-
slope (float32): Slope in degrees [0-90].
-
long_name: "Slope"
- units: "degrees"
- source: "ArcticDEM"
-
Note
Slope is calculated using finite difference methods on the DEM. Values approaching 90° indicate near-vertical terrain.
Example
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
calculate_topographic_position_index
¶
calculate_topographic_position_index(
arcticdem_ds: xarray.Dataset,
outer_radius: int,
inner_radius: int,
) -> xarray.Dataset
Calculate the Topographic Position Index (TPI) from an ArcticDEM Dataset.
TPI measures the relative topographic position of a point by comparing its elevation to the mean elevation of the surrounding neighborhood. Positive values indicate higher positions (ridges), negative values indicate lower positions (valleys).
Parameters:
-
arcticdem_ds(xarray.Dataset) –The ArcticDEM Dataset containing the 'dem' variable (float32).
-
outer_radius(int) –The outer radius of the neighborhood in meters. Can also be specified as string with units (e.g., "100m" or "10px").
-
inner_radius(int) –The inner radius of the annulus kernel in meters. If > 0, creates an annulus (ring) instead of a circle. Set to 0 for a circular kernel. Can also be specified as string with units (e.g., "50m" or "5px").
Returns:
-
xarray.Dataset–xr.Dataset: The input Dataset with a new data variable added:
-
xarray.Dataset–-
tpi (float32): Topographic Position Index values.
-
long_name: "Topographic Position Index (TPI)"
- description: Details about the kernel used
-
Note
Kernel shape combinations:
- inner_radius=0: Circular kernel comparing each cell to all neighbors within outer_radius
- inner_radius>0: Annulus kernel comparing each cell to neighbors in a ring between inner_radius and outer_radius. Useful for multi-scale terrain analysis.
The actual radii used are rounded to the nearest pixel based on the DEM resolution.
Example
Calculate TPI with circular and annulus kernels:
from darts_preprocessing import calculate_topographic_position_index
# Circular kernel (100m radius)
arcticdem_with_tpi = calculate_topographic_position_index(
arcticdem_ds=arcticdem,
outer_radius=100,
inner_radius=0
)
# Annulus kernel (50-100m ring)
arcticdem_multi_scale = calculate_topographic_position_index(
arcticdem_ds=arcticdem,
outer_radius=100,
inner_radius=50
)
Source code in darts-preprocessing/src/darts_preprocessing/engineering/arcticdem.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
preprocess_legacy_arcticdem_fast
¶
preprocess_legacy_arcticdem_fast(
ds_arcticdem: xarray.Dataset,
tpi_outer_radius: int,
tpi_inner_radius: int,
) -> xarray.Dataset
Preprocess the ArcticDEM data with legacy (DARTS v1) preprocessing steps.
Parameters:
-
ds_arcticdem(xarray.Dataset) –The ArcticDEM dataset.
-
tpi_outer_radius(int) –The outer radius of the annulus kernel for the tpi calculation in number of cells.
-
tpi_inner_radius(int) –The inner radius of the annulus kernel for the tpi calculation in number of cells.
Returns:
Source code in darts-preprocessing/src/darts_preprocessing/legacy.py
preprocess_legacy_fast
¶
preprocess_legacy_fast(
ds_optical: xarray.Dataset,
ds_arcticdem: xarray.Dataset,
ds_tcvis: xarray.Dataset,
tpi_outer_radius: int = 100,
tpi_inner_radius: int = 0,
device: typing.Literal["cuda", "cpu"]
| int = darts_utils.cuda.DEFAULT_DEVICE,
) -> xarray.Dataset
Preprocess optical data with legacy (DARTS v1) preprocessing steps, but with new data concepts.
The processing steps are: - Calculate NDVI - Calculate slope and relative elevation from ArcticDEM - Merge everything into a single ds.
The main difference to preprocess_legacy is the new data concept of the arcticdem. Instead of using already preprocessed arcticdem data which are loaded from a VRT, this step expects the raw arcticdem data and calculates slope and relative elevation on the fly.
Parameters:
-
ds_optical(xarray.Dataset) –The Planet scene optical data or Sentinel 2 scene optical dataset including data_masks.
-
ds_arcticdem(xarray.Dataset) –The ArcticDEM dataset.
-
ds_tcvis(xarray.Dataset) –The TCVIS dataset.
-
tpi_outer_radius(int, default:100) –The outer radius of the annulus kernel for the tpi calculation in m. Defaults to 100m.
-
tpi_inner_radius(int, default:0) –The inner radius of the annulus kernel for the tpi calculation in m. Defaults to 0.
-
device(typing.Literal['cuda', 'cpu'] | int, default:darts_utils.cuda.DEFAULT_DEVICE) –The device to run the tpi and slope calculations on. If "cuda" take the first device (0), if int take the specified device. Defaults to "cuda" if cuda is available, else "cpu".
Returns: