Skip to content

Commit

Permalink
add NDVI formula
Browse files Browse the repository at this point in the history
  • Loading branch information
bhass-neon committed Feb 15, 2024
1 parent af7ae0a commit 975546a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
## ----load-libraries----------------------------------------------------------------------------------------------------------------------------------------
## ----load-libraries, results="hide"-----------------------------------------------------------------------------------------------------------------------------
library(terra)
library(rhdf5)
library(neonUtilities)


## ----set-wd------------------------------------------------------------------------------------------------------------------------------------------------
## ----set-wd-----------------------------------------------------------------------------------------------------------------------------------------------------
# set working directory (this will depend on your local environment)
wd <- "~/data/"
setwd(wd)


## ----download-refl, eval=FALSE-----------------------------------------------------------------------------------------------------------------------------
## ----download-refl, eval=FALSE----------------------------------------------------------------------------------------------------------------------------------
## byTileAOP(dpID = 'DP3.30006.001',
## site = 'SJER',
## year = '2021',
Expand All @@ -19,16 +19,16 @@ setwd(wd)
## savepath = wd)


## ----define-h5, results="hide"-----------------------------------------------------------------------------------------------------------------------------
## ----define-h5, results="hide"----------------------------------------------------------------------------------------------------------------------------------
# Define the h5 file name to be opened
h5_file <- paste0(wd,"DP3.30006.001/neon-aop-products/2021/FullSite/D17/2021_SJER_5/L3/Spectrometer/Reflectance/NEON_D17_SJER_DP3_257000_4112000_reflectance.h5")


## ----view-file-structure, eval=FALSE, comment=NA-----------------------------------------------------------------------------------------------------------
## ----view-file-structure, eval=FALSE, comment=NA----------------------------------------------------------------------------------------------------------------
View(h5ls(h5_file,all=T))


## ----get-spatial-attributes--------------------------------------------------------------------------------------------------------------------------------
## ----get-spatial-attributes-------------------------------------------------------------------------------------------------------------------------------------

# define coordinate reference system from the EPSG code provided in the HDF5 file
h5EPSG <- h5read(h5_file,"/SJER/Reflectance/Metadata/Coordinate_System/EPSG Code" )
Expand All @@ -54,7 +54,7 @@ h5NoDataValue <- as.integer(reflInfo$Data_Ignore_Value)
cat('No Data Value:',h5NoDataValue)


## ----function-read-refl-data-------------------------------------------------------------------------------------------------------------------------------
## ----function-read-refl-data------------------------------------------------------------------------------------------------------------------------------------

# file: the hdf5 file
# band: the band you want to process
Expand Down Expand Up @@ -85,7 +85,7 @@ band2Raster <- function(file, band, noDataValue, extent, CRS){



## ----create-raster-list------------------------------------------------------------------------------------------------------------------------------------
## ----create-raster-list-----------------------------------------------------------------------------------------------------------------------------------------

# create a list of the bands (R,G,B) we want to include in our stack
rgb <- list(58,34,19)
Expand All @@ -97,11 +97,11 @@ rgb_rast <- lapply(rgb,FUN=band2Raster, file = h5_file,
CRS=h5CRS)


## ----rgb-rast-properties-----------------------------------------------------------------------------------------------------------------------------------
## ----rgb-rast-properties----------------------------------------------------------------------------------------------------------------------------------------
rgb_rast


## ----raster-stack------------------------------------------------------------------------------------------------------------------------------------------
## ----raster-stack-----------------------------------------------------------------------------------------------------------------------------------------------
rgbStack <- rast(rgb_rast)


Expand All @@ -117,7 +117,7 @@ names(rgbStack) <- bandNames
rgbStack


## ----scale-plot-refl---------------------------------------------------------------------------------------------------------------------------------------
## ----scale-plot-refl--------------------------------------------------------------------------------------------------------------------------------------------
# scale the data as specified in the reflInfo$Scale Factor
rgbStack <- rgbStack/as.integer(reflInfo$Scale_Factor)

Expand Down Expand Up @@ -147,7 +147,7 @@ plotRGB(rgbStack,
stretch = "lin")


## ----save-raster-geotiff, eval=FALSE, comment=NA-----------------------------------------------------------------------------------------------------------
## ----save-raster-geotiff, eval=FALSE, comment=NA----------------------------------------------------------------------------------------------------------------
# Write out final raster
# Note: if you set overwrite to TRUE, then you will overwrite (and lose) any older version of the tif file!
writeRaster(rgbStack, file=paste0(wd,"NEON_hyperspectral_tutorial_example_RGB_image.tif"), overwrite=TRUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ These steps included loading required packages, downloading the data (optionally

First, let's load the required R packages, `terra` and `rhdf5`.

```{r load-libraries}
```{r load-libraries, results="hide"}
library(terra)
library(rhdf5)
library(neonUtilities)
Expand Down Expand Up @@ -342,7 +342,13 @@ In this last part, we will calculate some vegetation indices using raster math i

### About NDVI

NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum. Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data!
NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum.

$$
NDVI = \frac{NIR-RED}{NIR+RED}
$$

Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data.

```{r create-NDVI, fig.cap=c("Raster plot of a portion of the SJER field site showing calculated NDVI values. The x-axis and y-axis values represent the extent, which range from 257500 to 258000 meters easting, and 4112500 to 4113000 meters northing, respectively. Plot legend goes from -1 to 1.","Raster plot of a portion of the SJER field site showing calculated NDVI values with predefined breaks at 0, 0.25, 0.5, 05, and 1. The x-axis and y-axis values represent the extent, which range from 257500 to 258000 meters easting, and 4112500 to 4113000 meters northing, respectively. Plot legend goes from 0 to 1.") }
Expand Down Expand Up @@ -392,7 +398,7 @@ Try the following on your own:
1. Calculate the Normalized Difference Nitrogen Index (NDNI) using the following equation:

$$
\frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
NDNI = \frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
$$
2. Calculate the Enhanced Vegetation Index (EVI). Hint: Look up the formula, and apply the appropriate NEON bands. Hint: You can look at satellite datasets, such as <a href="https://www.usgs.gov/landsat-missions/landsat-enhanced-vegetation-index" target="_blank">USGS Landsat EVI.</a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,11 @@ <h3 id="challenge-other-band-combinations">Challenge: Other band combinations</h
<h2 id="raster-math-creating-ndvi-and-other-vegetation-indices-in-r">Raster Math - Creating NDVI and other Vegetation Indices in R</h2>
<p>In this last part, we will calculate some vegetation indices using raster math in R! We will start by creating NDVI or Normalized Difference Vegetation Index.</p>
<h3 id="about-ndvi">About NDVI</h3>
<p>NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum. Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data!</p>
<p>NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum.</p>
<p>$$
NDVI = \frac{NIR-RED}{NIR+RED}
$$</p>
<p>Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data.</p>
<pre><code># Calculate NDVI

# select bands to use in calculation (red, NIR)
Expand Down Expand Up @@ -493,7 +497,7 @@ <h3 id="challenge-work-with-indices">Challenge: Work with Indices</h3>
<li>Calculate the Normalized Difference Nitrogen Index (NDNI) using the following equation:</li>
</ol>
<p>$$
\frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
NDNI = \frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
$$
2. Calculate the Enhanced Vegetation Index (EVI). Hint: Look up the formula, and apply the appropriate NEON bands. Hint: You can look at satellite datasets, such as <a href="https://www.usgs.gov/landsat-missions/landsat-enhanced-vegetation-index" target="_blank">USGS Landsat EVI.</a></p>
<ol start="3">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,13 @@ In this last part, we will calculate some vegetation indices using raster math i

### About NDVI

NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum. Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data!
NDVI is a ratio between the near infrared (NIR) portion of the electromagnetic spectrum and the red portion of the spectrum.

$$
NDVI = \frac{NIR-RED}{NIR+RED}
$$

Please keep in mind that there are different ways to aggregate bands when using hyperspectral data. This example is using individual bands to perform the NDVI calculation. Using individual bands is not necessarily the best way to calculate NDVI from hyperspectral data.


# Calculate NDVI
Expand Down Expand Up @@ -517,7 +523,7 @@ Try the following on your own:
1. Calculate the Normalized Difference Nitrogen Index (NDNI) using the following equation:

$$
\frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
NDNI = \frac{log(\frac{1}{p_{1510}}) - log(\frac{1}{p_{1680}})}{log(\frac{1}{p_{1510}}) + log(\frac{1}{p_{1680}})}
$$
2. Calculate the Enhanced Vegetation Index (EVI). Hint: Look up the formula, and apply the appropriate NEON bands. Hint: You can look at satellite datasets, such as <a href="https://www.usgs.gov/landsat-missions/landsat-enhanced-vegetation-index" target="_blank">USGS Landsat EVI.</a>

Expand Down

0 comments on commit 975546a

Please sign in to comment.