##### Written by Jeannine Cavender-Bares. 2/21/2019 ##### Calculate Euclidean spectral distances among pixels within plots (or subplots) ##### Distances can start with raw spectra or use vector normalized spectra setwd("") library(pls) library(picante) spec.plots <- read.csv("raw/all.plots.csv") splots <- data.frame(spec.plots) ############################################### #distance calculation for loop with raw spectra smat <- spec.plots #assign a data matrix dmat <- vector() #create empty vector p <- matrix() #create empty matrix r <- vector() for (i in 1:24) { #set for all Plots in matrix p <- subset(smat, Plot==i) # subset all rows (pixels) within the designated plot d <- p[, c(8:178)] # strip off the plot name and pixel number ds <- as.matrix(dist(d)) dsum <- (sum(ds) / ((nrow(ds) * ncol(ds)) - nrow(ds))) dmat <- rbind(dmat, dsum) dmat <- as.data.frame(dmat) rownames(dmat)[i] <- i r <- rbind(r,i) print(dmat[i,]) } dmat$Plot <- r #add the plot numbers back on #save raw spectral distances per plot #write.csv(dmat, file="processed/spec.dist.plots.csv") ################################################# #vector normalize the spectra in all of the plots #### don't rerun - takes a long time fmat <- spec.plots mat <- vector() for (i in 1:nrow(fmat)){ t <- fmat[i,c(8:178)] s <- sqrt(rowSums((t)^2)) m <- t/s mat <- rbind(mat,m) #print(i) } mat$Plot <- fmat$Plot #put the plot numbers back on mat$pixel <- fmat$pixel #put the pixel numbers back on # save the vn spectra write.csv(mat, file="processed/spec.vn.plots.csv") ########################################### # calculate the distances for the vn spectra #distance calculation for loop smat <- mat #assign data matrix vmat <- vector() #create empty vector p <- matrix() #create empty matrix r <- vector() for (i in 1:24) { #set for all Plots in matrix #i=1 p <- subset(smat, Plot==i) # subset all rows (pixels) within the designated plot d <- p[, c(1:171)] # strip off the plot name and pixel number ds <- as.matrix(dist(d)) dsum <- (sum(ds) / ((nrow(ds) * ncol(ds)) - nrow(ds))) vmat <- rbind(vmat, dsum) vmat <- as.data.frame(vmat) rownames(vmat)[i] <- i r <- rbind(r,i) print(vmat[i,]) } vmat$Plot <- r #add the plot numbers back on write.csv(vmat, file="processed/spec.vn.dist.plots.csv") ############################################### #SUBPLOT SCALE ############################################### # number subplots consecutively and calculate average pairwise distances using above loops sp.dat<- read.csv("raw/subplot.airborne.spn.csv") sp.dat$TransectT <- paste("T",sp.dat$Transect,sep="") sp.dat$Plot.t.sp <-paste(sp.dat$Plot, sp.dat$TransectT,sp.dat$subplot,sep=".") ################################################# #vector normalize the spectra in all of the plots #### don't rerun - takes a long time fmat <- sp.dat mat <- vector() for (i in 1:nrow(fmat)){ t <- fmat[i,c(8:178)] s <- sqrt(rowSums((t)^2)) m <- t/s mat <- rbind(mat,m) #print(i) } mat$Plot <- fmat$Plot #put the plot numbers back on mat$pixel <- fmat$pixel #put the pixel numbers back on mat$Plot.t.sp <- fmat$Plot.t.sp mat$subplot.no <- fmat$subplot.no # save the vn spectra write.csv(mat, file="processed/spec.vn.subplots.csv") ############################################### # Use VN spectra ############################################### #distance calculation for loop with VN spectra smat <- mat #assign a data matrix dmat <- vector() #create empty vector p <- matrix() #create empty matrix r <- vector() for (i in 1:192) { #set for all subplots in matrix p <- subset(smat, subplot.no==i) # subset all rows (pixels) within the designated plot d <- p[, c(1:171)] # strip off the plot name and pixel number ds <- as.matrix(dist(d)) dsum <- (sum(ds) / ((nrow(ds) * ncol(ds)) - nrow(ds))) dmat <- rbind(dmat, dsum) dmat <- as.data.frame(dmat) rownames(dmat)[i] <- i r <- rbind(r,i) print(dmat[i,]) } dmat$subplot <- r #add the plot numbers back on #use these as a look up table to put the subplot ID back on by hand #save raw spectral distances per subplot write.csv(dmat, file="processed/spec.vn.dist.subplots.csv") ############################################### # Raw spectral dist ############################################### #distance calculation for loop with raw spectra smat <- sp.dat #assign a data matrix dmat <- vector() #create empty vector p <- matrix() #create empty matrix r <- vector() for (i in 1:192) { #set for all subplots in matrix p <- subset(smat, subplot.no==i) # subset all rows (pixels) within the designated plot d <- p[, c(6:183)] # strip off the plot name and pixel number ds <- as.matrix(dist(d)) dsum <- (sum(ds) / ((nrow(ds) * ncol(ds)) - nrow(ds))) dmat <- rbind(dmat, dsum) dmat <- as.data.frame(dmat) rownames(dmat)[i] <- i r <- rbind(r,i) print(dmat[i,]) } dmat$subplot <- r #add the plot numbers back on #use these as a look up table to put the subplot ID back on by hand #save raw spectral distances per subplot write.csv(dmat, file="processed/spec.dist.subplots.csv")