- I have downloaded MODIS NDVI Aqua 16 days composite for the year 2015 and converted to tiff. There are 23 files.
- I want to calculate the average of each raster and plot a graph where I have dates on x and NDVI values on the y axis.
- I stacked them together and removed some part of the file names to make it short ( only for visual easyness) and stored in rasterNames
- Next, I ran a loop to run through all the files, calculated mean and rescaled by 0.0001 to get the value in valid range (0-1)
Now, I got stock with what I have to do next to plot the graph where I have a vector avg_ndvi
with values and rasterNames
with the labels.
Is there any other smarter way to achieve the same? Because finally I am interested to add terra product in the same line and get 8 days composite but may show the labels only is some intervals , may be every month, or every 16 days.
#------------ Load the library--------
library(raster)
library(rgdal)
library(rasterVis)
setwd("C:/Working_Folder/R")
aqua_ndvi_path <- "aqua_ndvi_2015"
avg_ndvi<-vector()
date_ndvi <- vector()
#now load the Tiffs:
all_ndvi <- list.files(aqua_ndvi_path,
full.names = TRUE,
pattern = ".tif$")
ndvi_stack <- stack(all_ndvi)
# ndvi_stack_brick <- brick(ndvi_stack)
rasterNames <- gsub("MYD13Q1_2015.","", names(ndvi_stack)) # remove some portion of file name
rasterNames <- gsub(".250m_16_days_NDVI","",rasterNames) # remove some portion of the file name
rasterNames # This will give me short names so that i can use it as a label in graph/plot later
## --------- scale ndvi value by 10000 and calculate ndvi average of each raster and store it in the vector---------
for (i in all_ndvi) {
j=cellStats(brick(i),mean)
j=j*0.0001
avg_ndvi<- c(avg_ndvi,j)
}
print(avg_ndvi)