Skip to content

Commit

Permalink
Merge pull request #23 from Open-ET/water-ndvi-nodata
Browse files Browse the repository at this point in the history
Setting negative and low reflectance NDVI values to 0
  • Loading branch information
cgmorton authored Nov 16, 2024
2 parents 94cecba + 21868f5 commit 4631d59
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 38 deletions.
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

0 comments on commit 4631d59

Please sign in to comment.