Landsat 8 Bulk Preprocessing

Imagine you have 50 Landsat 8 records. Of course, it would be very tedious to modify and start the script 50 times. That’s why there is now an automated solution for any number of data products!

Again, the prerequisite is that you have the L8 Level-2 datasets downloaded to a folder (“/media/sf_exchange/landsatdata” in our example), in which only all of your Landsat 8 scenes are stored – no other files! Of course you should replace the file path according to your storage location and create a character variable:

pathToFiles <- "/media/sf_exchange/landsatdata"

## [1] "LC081920232017083001T1-SC20180613163601.tar.gz"
## [2] "LC081930232017060201T1-SC20180613160412.tar.gz"

You can check the files inside pathToFiles with the dir() function. It should list all your Landsat 8 files. If that is not the case, make sure you did not mess up the file path name.
We can write all the products that exist in the folder in a vector products using the list.files() function:

products <- list.files(pathToFiles, full.names = TRUE)

## [1] "/media/sf_exchange/landsatdata/LC081920232017083001T1-SC20180613163601.tar.gz"
## [2] "/media/sf_exchange/landsatdata/LC081930232017060201T1-SC20180613160412.tar.gz"

To go through all the steps we saw in the previous section above for each of the scenes in products, we use a for-loop. Conceptually, the for-loop does the following:

for (i in products) {
  print("do all the preprocessing stuff")
## [1] "/media/sf_exchange/landsatdata/LC081920232017083001T1-SC20180613163601.tar.gz"
## [1] "do all the preprocessing stuff"
## [1] "/media/sf_exchange/landsatdata/LC081930232017060201T1-SC20180613160412.tar.gz"
## [1] "do all the preprocessing stuff"

For all landsat scenes i in products, it will do all the preprocessing stuff. The variable, or iterator, i is just a placeholder, which is sequentially occupied with the file names of the Landsat products.
Thus, you can just imagine any filename (as a string) every time you see the iterator i.
The remaining steps are then identical to those described above.

Here is the complete code. Just adjust your pathToFiles and run it in R-Studio to preprocess all of your Landsat 8 Level-2 files!


pathToFiles <- "/media/sf_exchange/landsatdata"

products <- list.files(pathToFiles, full.names = TRUE)

for (i in products) {
  print( paste0("processing: ", i) )
  productname <- substr(i, 1, nchar(i) - 7) 

  untar(i, exdir = productname)
  productfiles <- list.files(productname, full.names = TRUE)
  bands <- c(grep('band1', productfiles, value=TRUE),
             grep('band2', productfiles, value=TRUE),
             grep('band3', productfiles, value=TRUE),
             grep('band4', productfiles, value=TRUE),
             grep('band5', productfiles, value=TRUE),
             grep('band6', productfiles, value=TRUE),
             grep('band7', productfiles, value=TRUE)
              paste0(productname, ".tif"), 
              format = "GTiff"
  unlink(productname, recursive=TRUE)

  makeOVR <- paste0("gdaladdo -ro ", productname, ".tif 2 4 8 16")
  system( makeOVR )

What does the script for Level-1 data look like? Get an answer and check your knowledge during the following tasks!