digitalearthafrica/deafrica-sandbox-notebooks

Cast Sentinel-2 Uint16 datatype before NDVI calculation (Beginners guide #5)

whisperingpixel opened this issue · 0 comments

The Beginners Guide Notebook 5 calculates NDVI using the Sentinel-2 Uint16 data type that does not support negative values: https://github.com/digitalearthafrica/deafrica-sandbox-notebooks/blob/main/Beginners_guide/05_Basic_analysis.ipynb

Therefore, we don't obtain the negative NDVI output values; instead, they become positive and have very high NDVI values, e.g., the water areas:

Picture1

Screenshot 2021-11-28 at 13 26 49

If we cast the values to float (before the subtraction and division), we obtain the negative values in the NDVI array.

Just as an example:

# Calculate the components that make up the NDVI calculation
nir = dataset.nir.astype(float)
red = dataset.red.astype(float)

band_diff = nir - red
band_sum = nir + red

# Calculate NDVI and store it as a measurement in the original dataset
ndvi = band_diff / band_sum

Screenshot 2021-11-28 at 13 28 00

Screenshot 2021-11-28 at 13 28 40