###########################
# cleaning the cloud points
###########################

#loading files (low quality cloud points)
v12 <- read.table("cloudlow.txt")[,1:6]
colnames(v12) <- c("lat", "lon", "alt", "R", "G", "B")
head(v12)
dim(v12)

# defining the limits of the experiment
limits <- c(-48.0190414181,-48.0180407045,-22.8492173427,-22.8470995535)


#first sweep - clean by the limits of the field
clean.xy <- function(cloud, x.max, x.min, y.max, y.min){
  
  cloud2 <- cloud[cloud[,1] > x.min & cloud[,1] < x.max & cloud[,2] < y.max & cloud[,2] > y.min,]

  tiff(paste(as.character(bquote(cloud)),"_xyclean.png", sep = ""), width = 6000, height = 2000, res = 300)
  par(mfrow = c(1,3))
  plot(cloud2[,1], cloud2[,2], xlab = "lat", ylab = "long")
  plot(cloud2[,2], cloud2[,3], xlab = "long", ylab = "alt")
  plot(cloud2[,1], cloud2[,3], xlab = "lat", ylab = "alt")
  dev.off()
  
  return(cloud2)
  
}

v12.1 <- clean.xy(cloud = v12, x.max = limits[2], x.min = limits[1], y.max = limits[4],  y.min = limits[3])
dim(v12.1)


# second sweep - clean by the bare soil and plant height z outliers
clean.z <- function(cloud, z.min, z.max){
  
  cloud2 <- cloud[cloud[,3] > z.min & cloud[,3] < z.max,]
  
  tiff(paste(as.character(bquote(cloud)), "_zclean.png", sep = ""), width = 6000, height = 2000, res = 300)
    par(mfrow = c(1,3))
    plot(cloud2[,1], cloud2[,2], xlab = "lat", ylab = "long")
    plot(cloud2[,2], cloud2[,3], xlab = "long", ylab = "alt")
    plot(cloud2[,1], cloud2[,3], xlab = "lat", ylab = "alt")
    dev.off()
    
    return(cloud2)
    
    }

v12.2 <- clean.z(cloud = v12.1, z.min = 394, z.max = 410)
dim(v12.2)


# third sweep - clean by sample
clean.s <- function(cloud, keep = 10){
  
  ss <- seq(from = 1, to = nrow(cloud), by = 100/keep)
  cloud2 <- cloud[ss,]
  
  tiff(paste(as.character(bquote(cloud)), "_sclean.png", sep = ""), width = 6000, height = 2000, res = 300)
  par(mfrow = c(1,3))
  plot(cloud2[,1], cloud2[,2], xlab = "lat", ylab = "long")
  plot(cloud2[,2], cloud2[,3], xlab = "long", ylab = "alt")
  plot(cloud2[,1], cloud2[,3], xlab = "lat", ylab = "alt")
  dev.off()
  
  return(cloud2)
  
}

v12.3 <- clean.s(cloud = v12.2, keep = 20)
dim(v12.3)

nrow(v12.3)/nrow(v12)*100

# saving the newest cloud points
write.table(v12.3, "cloudclean.txt", row.names = F, col.names = F)