1

I have a series of raster stacks representing median and maximum composite images in a 35-year time series. Each stack has five bands: NDVI, NBR, brightness, greenness, wetness. I need to convert them from .grd to .tif on a band by band basis and label each band with the appropriate name and year (ex: Med_2001_NDVI).

In the past I've used this code to export stacks by band. It works well and assigns the correct band names, but can only process one raster at a time.

namesb<-c("NDVI_13med"," NBR_13med", "Bright_13med", "Green_13med", "Wet_13med")
for(i in seq_along(s13med)){writeRaster(s13med[[i]], file=namesb[i], format="GTiff")}

I would like to modify it to read every raster in the folder then writeRaster by layer with the correct names assigned. This is my preliminary code. I'm unsure how to name them appropriately.

a<-list.files(pattern="*.grd$")
b<-lapply(a, stack)
year<-substr(a, 1, 8)
bands<-c("NDVI","NBR","Brightness","Greenness", "Wetness")
for(i in seq_along(b)){writeRaster(b[[i]], file=(???), bylayer=T, format="GTiff")}

The final name for each band needs to be a combination of "year" and the appropriate band name from "bands". The name format doesn't need to exactly match my 2013 example. I just need to know the band name, the year, and whether it's a median or maximum composite image.

1 Answer 1

2

I think you can do that like this:

a <- list.files(pattern="\\.grd$")
b <- lapply(a, stack)
year <- substr(a, 1, 8)
bands <- c("NDVI","NBR","Brightness","Greenness", "Wetness")

fnames <- lapply(year, function(y) paste0(bands, "_", y, ".tif"))


for(i in seq_along(b)){
    writeRaster(b[[i]], filename=fnames[[i]], bylayer=TRUE)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.