Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting negative and low reflectance NDVI values to 0 #23

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 deletions examples/single_image.ipynb

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion openet/sims/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ def aggregate_image(agg_start_date, agg_end_date, date_format):
ndvi_img = (
daily_coll.filterDate(agg_start_date, agg_end_date)
.select(['ndvi']).mean().float()

)
image_list.append(ndvi_img)
if ('scene_count' in variables) or ('count' in variables):
Expand Down
25 changes: 24 additions & 1 deletion openet/sims/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,27 @@ def _ndvi(landsat_image):
ee.Image

"""
return landsat_image.normalizedDifference(['nir', 'red']).rename(['ndvi'])

# Force the input values to be at greater than or equal to zero
# since C02 surface reflectance values can be negative
# but the normalizedDifference function will return nodata
ndvi = (
landsat_image.select(['nir', 'red'])
.max(0)
.normalizedDifference(['nir', 'red'])
.rename(['ndvi'])
)

# Assume that low reflectance values are unreliable for computing NDVI
# If both reflectance values are below the minimum, set the output to 0
# If either of the reflectance values was negative, set the output to 0
# The 0.01 threshold was chosen arbitrarily and may need to be adjusted
nir = landsat_image.select(['nir'])
red = landsat_image.select(['red'])
ndvi = ndvi.where(nir.lt(0.01).And(red.lt(0.01)), 0)
#ndvi = ndvi.where(nir.lt(0).Or(red.lt(0)), 0)
#ndvi = ndvi.where(nir.lte(0).And(red.lte(0.01)), 0)
#ndvi = ndvi.where(nir.lte(0.01).And(red.lte(0)), 0)
return ndvi

# return landsat_image.normalizedDifference(['nir', 'red']).rename(['ndvi'])
11 changes: 11 additions & 0 deletions openet/sims/tests/test_c_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def test_Image_init_date_properties():
[0.2, 0.8, 0.6],
[0.1, 17.0 / 30, 0.7],
[0.1, 0.9, 0.8],
# First check that negative values are not masked
[-0.01, 0.1, 1.0],
[0.1, -0.01, -1.0],
# Check that low reflectance values are set to 0
[-0.1, -0.1, 0.0],
[0.0, 0.0, 0.0],
[0.009, 0.009, 0.0],
[0.009, -0.01, 0.0],
[-0.01, 0.009, 0.0],
# Don't adjust NDVI if only one reflectance value is low
[0.005, 0.1, 0.9047619104385376],
]
)
def test_Image_static_ndvi_calculation(red, nir, expected, tol=0.000001):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openet-sims"
version = "0.2.5"
version = "0.2.6"
authors = [
{ name = "Alberto Guzman", email = "[email protected]" },
]
Expand Down
Loading