I would like to extract the date (YYDDD format) from LANDSAT8 folder names and use that in vegetation indexes calculated for multiple images (52).
Here is what I have so far:
setwd('E:/Landsat8')
folders <- list.dirs(full.names=FALSE, recursive = FALSE)
for(scene in folders){
NIR <- raster(paste(scene,'/',scene,'_B5.TIF', sep=''))
SWIR1 <- raster(paste(scene,'/',scene,'_B6.TIF', sep=''))
LSWI <- overlay(x=NIR, y=SWIR1, fun=calcIndex) #calculates LSWI
writeRaster(LSWI, filename=paste(scene,'/',scene,'_LSWI.TIF', sep=''), format='GTiff', datatype='FLT4S', overwrite=TRUE)
}
The format of the filenames from LANDSAT8 imagery looks like: LC81730382014069LGN00
I would like to safe the file as 2014_069_LSWI.TIF. At the moment it is saving it as LC81730382014069LGN00_LSWI.TIF I can't find the solution to only extract the date from the middle of the name string. Hopefully somebody has an idea!
If am not mistaken, year and day of year substrings in Landsat filenames are always at the same position. Therefore this should do it:
#Sample image name
nm <- "LC81730382014069LGN00"
#Extract year
yr <- substr(nm, start=10, stop=13)
#Extract day of year
dy <- substr(nm, start=14, stop=16)
#Make name
nm2 <- paste(yr, dy, "LSWI.TIF", sep="_")
You can then pass nm2 as the filename during writeRaster
like as shown below. Note usage of paste0
;-)
writeRaster(LSWI, filename=paste0(scene,'/', nm2), format='GTiff', datatype='FLT4S', overwrite=T)