# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages
# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats
# For example, here's several helpful packages to load in 

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

system("ls ../input")

# Any results you write to the current directory are saved as output.

## Plot composite 16 channel tiles
# @JeffH

tokeep <- list.files(path="../input/sixteen_band/", pattern="_P.tif", full.names=T, recursive=FALSE)

table(substr(tokeep, 12+2+10, 15+2+10))

for(j in unique(substr(tokeep, 12+2+10, 15+2+10))) {
  library(raster)
  png(paste0(j, '_P.png'))
  tokeep0 = grep(j, tokeep)
  par(mfrow = c(5,5))
  par(mar = rep(0,4), oma = rep(0,4)) # c(0.1,0.1,0.1,0.1)
  for(i in tokeep0){
    thisfile <- tokeep[i]
    print(thisfile)
    img <- stack(thisfile)
    plotRGB(img, stretch = "hist") # plot(img, axes = FALSE, legend = FALSE)
  }
  dev.off()
}

