0

I was hoping someone could help me with a loop function I'm working with. I have tried searching Google and Stack Overflow extensively but, as I don't know the exact search terminology, I fear I am missing some results. With that in mind, I apologise in advance if this question has already been asked but I hope that someone can point me in the right direction for a solution.

About My Data

I have downloaded 1000s of files from NASAs MODIS satellite. As my study area covers a large area, I've had to download the data over an area of 6 tiles. As a result, my list of downloaded files are actually 'grouped' together in bunches of 6 (despite appearing as just a list of files within explorer).

I have written some simple for loops within R to do some initial processing (in the example below this is to resample the tiles so that they can then be stitched together using raster::mosaic. This processing can happen on the files individually at this stage so I have found the below loop to be perfect for my needs:

resampled.raster<-list()

for (l in 1:24){
  cat(l,"\n")
  resampled.raster[[l]]<-resample(rst[[l]], s, method="ngb")
}

The Problem

The next stage of my processing requires me to mosaic the rasters in groups of 6, before moving onto the next batch of 6. For example, if I have a list of 24 files, the first 6 will need to be processed as a collection of files and then saved (in a list) before moving to the next 6. After 4 iterations of this (to make the 24 files total), I need the loop to stop.

The closest solution I have found from searching the internet is here but this isn't a solution to my problem.

Is this 'batch' processing possible with using a for loop within R? Many thanks in advance for your help.

Simon
  • 991
  • 8
  • 30
  • 1
    I've never used `raster` but to select a batch you can do something like: `step <- 6` with `for(i in seq(1,24,step ))` and then inside the loop `batchIndex <- i:(i+step)` – StatMan Jul 26 '16 at 14:26
  • you could make .txt file with a list of file names grouped by batches – Adam Jul 26 '16 at 14:43
  • Thank you both for the comments. RobertH's solution below is working and I have accepted this answer. – Simon Jul 26 '16 at 15:32

1 Answers1

0

First note that it is almost certainly wrong to use resample and mosaic. If these are tiles, you should be able to use merge to merge them.

There surely is something unique about the names of the files that need to be grouped? That is what you need to use.

Get a vector with filenames

# ff <- list.files()

Here a toy example

ff <- c('fileA1', 'fileA2', 'fileB1', 'fileB2') 

Get the unique groups

code <- gsub('file', '', ff)
code <- substr(code, 1, 1)
uc <- unique(code)

Loop over the groups

for (u in uc) {
    files <- ff[u == code]
    r <- lapply(files, raster)
    r$filename <- paste0("merged/", u, ".tif")
    m <- do.call(merge, r)
}
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Hi Robert, thank you very much for your assistance. This is a really elegant solution and it has worked perfectly. I'm still new to programming so a detailed answer like this has really helped me to understand what's happening. Thanks again for your time. – Simon Jul 26 '16 at 15:31
  • In case anyone in future will find this helpful, here is some extra information. As RobertH asks, there is something unique about the file names. Each file is named something like: MOD15A2.A2016169.h09v07.005.2016183085001 Using Robert's code detailed above I removed "MOD15A2." and proceeded to use the next 8 characters in the file name as the unique identifier (in this case it's A22016169) As my data span 6 tiles across the globe, there are six files each containing A2016169. These were the 6 files that were merged. – Simon Jul 26 '16 at 15:37