-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a function that calculates NDVI for a given scene. It's better than having to remember the equations all the time. The function can also take care of adding some metadata and copying the metadata from the original scene. The function allows customizing the names of the red and NIR bands.
- Loading branch information
Showing
6 changed files
with
124 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright (c) 2022 The xlandsat developers. | ||
# Distributed under the terms of the MIT License. | ||
# SPDX-License-Identifier: MIT | ||
""" | ||
Calculate indices based on data in xarray.Datasets | ||
""" | ||
|
||
|
||
def ndvi(scene, red_band="red", nir_band="nir"): | ||
r""" | ||
Normalized Difference Vegetation Index | ||
Calculate the NDVI for the given scene, defined as: | ||
.. math:: | ||
NDVI = \dfrac{NIR - Red}{NIR + Red} | ||
Parameters | ||
---------- | ||
scene : :class:`xarray.Dataset` | ||
A Landsat scene, as read with :func:`xlandsat.load_scene`. | ||
red_band : str | ||
The name of the variable in ``scene`` that corresponds to the red band. | ||
nir_band : str | ||
The name of the variable in ``scene`` that corresponds to the NIR band. | ||
Returns | ||
------- | ||
ndvi : :class:`xarray.DataArray` | ||
The calculated NDVI, with the metadata attributes from the original | ||
scene. | ||
""" | ||
red = scene[red_band] | ||
nir = scene[nir_band] | ||
result = (nir - red) / (nir + red) | ||
result.name = "ndvi" | ||
attrs = {"long_name": "normalized difference vegetation index"} | ||
attrs.update(scene.attrs) | ||
result = result.assign_attrs(attrs) | ||
return result |