<commit_before>library(raster)

writeplot = function(file,gmBounds,date,tcb,tcg,tcw,tca,first=F,last=F){
  start=paste('var data = {"PlotID": 1,','"LatLon":[',gmBounds[1,2],',',gmBounds[1,1],'],','"bounds":[',gmBounds[2,2],',',gmBounds[3,2],',',gmBounds[3,1],',',gmBounds[2,1],'],','"Values": [')
  int=', '
  end=']}'
  imginfo = paste(
    paste('{"Year": ', date,',',sep=""),
    paste(' "TCB": ', tcb,',',sep=""),
    paste(' "TCG": ', tcg,',',sep=""),
    paste(' "TCW": ', tcw,',',sep=""),
    paste(' "TCA": ', tca,sep=""),
    '}',
    sep="")
  if(first == T){
    write(start, file, append=TRUE)
    write(paste(imginfo,int,sep=""), file, append=TRUE)
  }
  if(first != T & last != T){write(paste(imginfo,int,sep=""), file, append=TRUE)}
  if(last == T){
    write(imginfo, file, append=TRUE)
    write(end, file, append=TRUE)
  }
}

makeChipStrip = function(imgStack){ 
  singleShift = xres(imgStack)*255
  for(i in 1:nlayers(imgStack)){
    if(i == 1){
      fullSeries = subset(imgStack,i)
    } else{
      shifted = shift(subset(imgStack,i),x=0,y=singleShift*(i-1)*-1)
      fullSeries = merge(fullSeries,shifted)
    }
  }
  return(fullSeries)
}

stretchBGW = function(template, index){
  img = as.matrix(template)
  n_stdev = 2
  bmin = 3098-(1247*n_stdev)
  bmax = 3098+(1247*n_stdev)
  gmin = 1549-(799*n_stdev)
  gmax = 1549+(799*n_stdev)
  wmin = -701-(772*n_stdev)
  wmax = -701+(772*n_stdev)
  
  if(index == "tcb"){
    these = which(img < bmin)
    if(length(these != 0)){img[these] = bmin}
    these = which(img > bmax)
    if(length(these != 0)){img[these] = bmax}  
    img = ((img-bmin)/(bmax-bmin))*255
  }
  if(index == "tcg"){
    these = which(img < gmin)
    if(length(these != 0)){img[these] = gmin}
    these = which(img > gmax)
    if(length(these != 0)){img[these] = gmax}
    img = ((img-gmin)/(gmax-gmin))*255
    
  }
  if(index == "tcw"){
    these = which(img < wmin)
    if(length(these != 0)){img[these] = wmin}
    these = which(img > wmax)
    if(length(these != 0)){img[these] = wmax}
    img = ((img-wmin)/(wmax-wmin))*255
  }
  img = setValues(template, round(img))
  return(img)
}

chronoInfo = function(dir,file,outpng){
  unlink(file)
  tcbstack = list.files(dir, "tcb_composite_stack.bsq", recursive = T, full.names = T)
  tcgstack = list.files(dir, "tcg_composite_stack.bsq", recursive = T, full.names = T)
  tcwstack = list.files(dir, "tcw_composite_stack.bsq", recursive = T, full.names = T)
  tcastack = list.files(dir, "tca_composite_stack.bsq", recursive = T, full.names = T)
  
  files = list.files(file.path(dir,"tcb"), "composite.bsq")
  years = sort(unique(substr(files,1,4)))
  
  tcbb = extend(brick(tcbstack),130)
  tcgb = extend(brick(tcgstack),130)
  tcwb = extend(brick(tcwstack),130)
  tcab = extend(brick(tcastack),130)
  
  coords = data.frame(x,y)
  reso = xres(tcbb)
  half = reso*127.5
  ul = matrix(c(x-half, y+half), nrow=1)
  lr = matrix(c(x+half, y-half), nrow=1)
  proj = projection(tcbb)
  spCoordProj = SpatialPoints(matrix(c(x, y), nrow=1),proj4string=CRS(proj))
  SpCoordLonLat = spTransform(spCoordProj, CRS("+init=epsg:4269"))
  SpCoordLonLat = coordinates(SpCoordLonLat)
  spUlProj = SpatialPoints(ul,proj4string=CRS(proj))
  SpUlLonLat = spTransform(spUlProj, CRS("+init=epsg:4269"))
  SpUlLonLat = coordinates(SpUlLonLat)
  spLrProj = SpatialPoints(lr,proj4string=CRS(proj))
  SpLrLonLat = spTransform(spLrProj, CRS("+init=epsg:4269"))
  SpLrLonLat = coordinates(SpLrLonLat)
  gmBounds = matrix(c(SpCoordLonLat,SpUlLonLat,SpLrLonLat),ncol = 2,byrow = T)
  
  
  
  tcbv = extract(tcbb, coords)
  tcgv = extract(tcgb, coords)
  tcwv = extract(tcwb, coords)
  tcav = extract(tcab, coords)
  
  #for making year composites
  len = length(years)
  for(i in 1:len){
    print(paste(i,"/",len,sep=""))
    thisyear=as.numeric(years[i])
    if(i == 1){writeplot(file,gmBounds,thisyear,tcbv[i],tcgv[i],tcwv[i],tcav[i], first=T, last=F)}
    if(i != 1 & i != len){writeplot(file,gmBounds,thisyear,tcbv[i],tcgv[i],tcwv[i],tcav[i], first=F, last=F)}
    if(i == len){writeplot(file,gmBounds,thisyear,tcbv[i],tcgv[i],tcwv[i],tcav[i], first=F, last=T)}
  }
  
  thisCell = cellFromXY(tcbb,coords)
  thisRowCol = rowColFromCell(tcbb,thisCell)
  e = extent(tcbb, thisRowCol[1,1]-127,thisRowCol[1,1]+127,thisRowCol[1,2]-127,thisRowCol[1,2]+127)
  
  tcbv = crop(tcbb, e)
  tcgv = crop(tcgb, e)
  tcwv = crop(tcwb, e)
  tcav = crop(tcab, e)
  
  tcbvs = stretchBGW(tcbv, "tcb")
  tcgvs = stretchBGW(tcgv, "tcg")
  tcwvs = stretchBGW(tcwv, "tcw")
  
  tcbseries = makeChipStrip(tcbvs)
  tcgseries = makeChipStrip(tcgvs)
  tcwseries = makeChipStrip(tcwvs)
  
  s = stack(tcbseries,tcgseries,tcwseries)
  
  png(outpng, width = 255, height=nrow(s))
  plotRGB(s,r=1, g=2, b=3, ext=NULL)
  dev.off()  
}



################################################################

#x=-1175387.0000
#y= 2532975.0000
#dir = "L:/composites/test10"
#file = "L:/composites/test10/test10.js"
#outpng = "L:/composites/test10/chipstrip.png"

#chronoInfo(dir,file,outpng)
<commit_msg>changes to process multiple plots<commit_after>

writeplot = function(file,plot,gmBounds,date,tcb,tcg,tcw,tca, first=F,last=F, end=F, finalPlot=F){
  chipstrip = paste('"imgs/plot_',plot,'_chipstrip.png,"',sep="")
  start=paste('{"PlotID": ',plot,',','"chipStrip":',chipstrip,'"LatLon":[',gmBounds[1,2],',',gmBounds[1,1],'],','"bounds":[',gmBounds[2,2],',',gmBounds[3,2],',',gmBounds[3,1],',',gmBounds[2,1],'],','"Values": [')
  int=', '
  if(end == T & finalPlot == F){end=']},'} else if(end == T & finalPlot == T){end=']}'}
  imginfo = paste(
    paste('{"Year": ', date,',',sep=""),
    paste(' "TCB": ', tcb,',',sep=""),
    paste(' "TCG": ', tcg,',',sep=""),
    paste(' "TCW": ', tcw,',',sep=""),
    paste(' "TCA": ', tca,sep=""),
    '}',
    sep="")
  if(first == T){
    write(start, file, append=TRUE)
    write(paste(imginfo,int,sep=""), file, append=TRUE)
  }
  if(first != T & last != T){write(paste(imginfo,int,sep=""), file, append=TRUE)}
  if(last == T){
    write(imginfo, file, append=TRUE)
    write(end, file, append=TRUE)
  }
}

makeChipStrip = function(imgStack){ 
  singleShift = xres(imgStack)*255
  for(i in 1:nlayers(imgStack)){
    if(i == 1){
      fullSeries = subset(imgStack,i)
    } else{
      shifted = shift(subset(imgStack,i),x=0,y=singleShift*(i-1)*-1)
      fullSeries = merge(fullSeries,shifted)
    }
  }
  return(fullSeries)
}

stretchBGW = function(template, index){
  img = as.matrix(template)
  n_stdev = 2
  bmin = 3098-(1247*n_stdev)
  bmax = 3098+(1247*n_stdev)
  gmin = 1549-(799*n_stdev)
  gmax = 1549+(799*n_stdev)
  wmin = -701-(772*n_stdev)
  wmax = -701+(772*n_stdev)
  
  if(index == "tcb"){
    these = which(img < bmin)
    if(length(these != 0)){img[these] = bmin}
    these = which(img > bmax)
    if(length(these != 0)){img[these] = bmax}  
    img = ((img-bmin)/(bmax-bmin))*255
  }
  if(index == "tcg"){
    these = which(img < gmin)
    if(length(these != 0)){img[these] = gmin}
    these = which(img > gmax)
    if(length(these != 0)){img[these] = gmax}
    img = ((img-gmin)/(gmax-gmin))*255
    
  }
  if(index == "tcw"){
    these = which(img < wmin)
    if(length(these != 0)){img[these] = wmin}
    these = which(img > wmax)
    if(length(these != 0)){img[these] = wmax}
    img = ((img-wmin)/(wmax-wmin))*255
  }
  img = setValues(template, round(img))
  return(img)
}


chronoInfo = function(imgdir, outdir, plot, x, y, tcbb, tcgb, tcwb, tcab, years, jsfile, finalPlot=F){
  coords = data.frame(x,y)
  reso = xres(tcbb)
  half = reso*127.5
  ul = matrix(c(x-half, y+half), nrow=1)
  lr = matrix(c(x+half, y-half), nrow=1)
  proj = projection(tcbb)
  spCoordProj = SpatialPoints(matrix(c(x, y), nrow=1),proj4string=CRS(proj))
  SpCoordLonLat = spTransform(spCoordProj, CRS("+init=epsg:4269"))
  SpCoordLonLat = coordinates(SpCoordLonLat)
  spUlProj = SpatialPoints(ul,proj4string=CRS(proj))
  SpUlLonLat = spTransform(spUlProj, CRS("+init=epsg:4269"))
  SpUlLonLat = coordinates(SpUlLonLat)
  spLrProj = SpatialPoints(lr,proj4string=CRS(proj))
  SpLrLonLat = spTransform(spLrProj, CRS("+init=epsg:4269"))
  SpLrLonLat = coordinates(SpLrLonLat)
  gmBounds = matrix(c(SpCoordLonLat,SpUlLonLat,SpLrLonLat),ncol = 2,byrow = T)
  
  tcbv = extract(tcbb, coords)
  tcgv = extract(tcgb, coords)
  tcwv = extract(tcwb, coords)
  tcav = extract(tcab, coords)
  
  #for making year composites
  len = length(years)
  for(y in 1:len){
    thisyear=as.numeric(years[y])
    if(y == 1){writeplot(jsfile,plot,gmBounds,thisyear,tcbv[y],tcgv[y],tcwv[y],tcav[y], first=T, last=F, end=F)}
    if(y != 1 & y != len){writeplot(jsfile,plot,gmBounds,thisyear,tcbv[y],tcgv[y],tcwv[y],tcav[y], first=F, last=F, end=F)}
    if(y == len){writeplot(jsfile,plot,gmBounds,thisyear,tcbv[y],tcgv[y],tcwv[y],tcav[y], first=F, last=T, end=T, finalPlot=finalPlot)}
  }
  
  thisCell = cellFromXY(tcbb,coords)
  thisRowCol = rowColFromCell(tcbb,thisCell)
  e = extent(tcbb, thisRowCol[1,1]-127,thisRowCol[1,1]+127,thisRowCol[1,2]-127,thisRowCol[1,2]+127)
  
  tcbv = crop(tcbb, e)
  tcgv = crop(tcgb, e)
  tcwv = crop(tcwb, e)
  tcav = crop(tcab, e)
  
  tcbvs = stretchBGW(tcbv, "tcb")
  tcgvs = stretchBGW(tcgv, "tcg")
  tcwvs = stretchBGW(tcwv, "tcw")
  
  tcbseries = makeChipStrip(tcbvs)
  tcgseries = makeChipStrip(tcgvs)
  tcwseries = makeChipStrip(tcwvs)
  
  s = stack(tcbseries,tcgseries,tcwseries)
  
  outpng = file.path(outdir,"imgs",paste("plot_",plot,"_chipstrip.png",sep=""))
  png(outpng, width = 255, height=nrow(s))
  plotRGB(s,r=1, g=2, b=3, ext=NULL)
  dev.off()  
}

llr_time_machine = function(imgdir,outdir,coordfile){
  
  #read the csv plot file
  plots = read.csv(coordfile) #plot#,x,y
  
  #create the javascript file path
  jsfile = file.path(outdir,"LLRtimeMachine.js")
  
  #make sure output dirs exist/created
  dir.create(outdir, recursive=T, showWarnings = F)
  dir.create(file.path(outdir,"imgs"), recursive=T, showWarnings = F)
  
  
  tcbstack = list.files(imgdir, "tcb_composite_stack.bsq$", recursive = T, full.names = T)
  tcgstack = list.files(imgdir, "tcg_composite_stack.bsq$", recursive = T, full.names = T)
  tcwstack = list.files(imgdir, "tcw_composite_stack.bsq$", recursive = T, full.names = T)
  tcastack = list.files(imgdir, "tca_composite_stack.bsq$", recursive = T, full.names = T)
  
  n_tcbstack = length(tcbstack)
  n_tcgstack = length(tcgstack)
  n_tcwstack = length(tcwstack)
  n_tcastack = length(tcastack)
  if(sum(n_tcbstack,n_tcgstack,n_tcwstack,n_tcastack) != 4){
    print("Error! You are missing some required files, or the provided directory is wrong.")
    if(n_tcbstack == 0){print(paste("There was no *tcb_composite_stack.bsq file found at this directory:",imgdir,"(recursive search)"))}
    if(n_tcgstack == 0){print(paste("There was no *tcg_composite_stack.bsq file found at this directory:",imgdir,"(recursive search)"))}
    if(n_tcwstack == 0){print(paste("There was no *tcw_composite_stack.bsq file found at this directory:",imgdir,"(recursive search)"))}
    if(n_tcastack == 0){print(paste("There was no *tca_composite_stack.bsq file found at this directory:",imgdir,"(recursive search)"))}
    return
    
  }
  
  
  files = list.files(file.path(imgdir,"tcb"), "composite.bsq")
  years = sort(unique(substr(files,1,4)))
  
  tcbb = extend(brick(tcbstack),130)
  tcgb = extend(brick(tcgstack),130)
  tcwb = extend(brick(tcwstack),130)
  tcab = extend(brick(tcastack),130)
  
  
  #for each plot create the data
  nplots = nrow(plots)
  for(i in 1:nplots){
    print(paste(i,"/",nplots,sep=""))
    if(i == 1){
      unlink(jsfile)
      write('var data = [', jsfile, append=TRUE)
    }
    
    if(i == nplots){
      chronoInfo(imgdir, outdir, plots$plot[i], plots$x[i], plots$y[i], tcbb, tcgb, tcwb, tcab, years, jsfile, finalPlot=T)
      write(']', jsfile, append=TRUE)
    } else{
      chronoInfo(imgdir, outdir, plots$plot[i], plots$x[i], plots$y[i], tcbb, tcgb, tcwb, tcab, years, jsfile)
    }
  }
}



{'fraction_non_alphanumeric': 0.19777554775045994, 'fraction_numerical': 0.027262083960528517, 'mean_word_length': 4.6704599336178285, 'pattern_counts': {'":': 19, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3478487', 'n_tokens_mistral': 5393, 'n_tokens_neox': 5059, 'n_words': 796}