##12/7/2018: script to combine data from batch 2 of the bean experiment with CE3 and SAM 100 (save in RDATA file)

library(plyr)
##Import data:
## plant info:
plantinfo <- read.csv("plantCodes_beans2017.csv",stringsAsFactors = F)
# water use:
b2wat <- read.csv("batch2waterUse_beans2017.csv",stringsAsFactors = F)
# growth stage:
b2dev <- read.csv("batch2development_beans2017.csv",stringsAsFactors = F)
# chlorophyll:
chl <- read.csv("batch2Chlorophyll_beans2017.csv",stringsAsFactors = F)
# seed yield
b2yield <- read.table("batch2podMass_beans2017.txt",stringsAsFactors = F,header=T,sep=",")
# efficiency assays
b2h2 <- read.csv("H2assayOutput_batch2/batch2H2output.csv",stringsAsFactors = F)

##edit each one by one:
b2info <- plantinfo[plantinfo$batch==2,c("plantID","germinationDate","strain")]
b2info$germinationDate <- as.Date(b2info$germinationDate, format=c("%m/%d/%Y"))

##water use:
starttime <- paste(b2wat$date,b2wat$waterStartTime,sep=" ")
endtime <-  paste(b2wat$date, b2wat$waterEndTime,sep=" ")
starttime <- as.POSIXct(starttime, format = "%m/%d/%Y %I:%M%p")
endtime <- as.POSIXct(endtime, format = "%m/%d/%Y %I:%M%p")
b2wat$hoursElapsed <- as.numeric(endtime-starttime)
b2wat$hourlyWaterUse_mL <- b2wat$mLwaterUsed/b2wat$hoursElapsed
b2wat$date <- as.Date(b2wat$date,format=c("%m/%d/%Y"))
b2wat <- merge(b2wat, b2info, by="plantID")

##chlorophyll:
ch1 <- chl[,1:4]
ch2 <- chl[,c(1:3,5)]
ch3 <- chl[,c(1:3,6)]
names(ch1)[4] <- "atLeafUnits"
names(ch2)[4] <- "atLeafUnits"
names(ch3)[4] <- "atLeafUnits"
chlong <- rbind(ch1,ch2,ch3)
chlong <- chlong[!is.na(chlong$atLeafUnits),]
b2chl <- ddply(chlong, .(plantID, date), atLeafUnits.mean = mean(atLeafUnits,na.rm=T),leavesMeasured = length(unique(leaf)),n.measurements = length(atLeafUnits),summarize)
b2chl$date <- as.Date(b2chl$date, format=c("%m/%d/%Y"))
b2chl <- merge(b2chl, b2info, by="plantID")

##growth stage
b2dev$date <- as.Date(b2dev$date, format=c("%m/%d/%Y"))
b2dev <- merge(b2dev, b2info, by="plantID")

## seed yield
names(b2yield)[1] <- "plantID"
b2yield$plantID <- gsub(" ","",b2yield$plantID)
b2pod <- b2yield[grep("pod",b2yield$sample),]
b2seed <- b2yield[grep("seed",b2yield$sample),]
names(b2seed)[3] <- "seedDWg"
names(b2pod)[3] <- "totalPodDWg"
zz <- strsplit(b2seed$sample,"[.]")
b2seed$seedCount <- as.numeric(sapply(zz,function(x)x[2]))
b2yield <- merge(b2seed[,c(1,3,4)],b2pod[,c(1,3)],by="plantID")
b2yield <- merge(b2yield, b2info, by="plantID")

## h2 assays
##use the CO2drift intercept as the overall root respiration for 'specific' N2ase activity 
# the argon switch (CO2at0ppmH2) happened after the O2 steps, so respiration is a little depressed at this point. 
b2h2$specificN2ase_CO2 <- b2h2$H2ppm_Ar/b2h2$CO2drift_intercept
b2h2$plantID <- substr(b2h2$file,10,11)
adate <- substr(b2h2$file,1,8)
b2h2$assayDate <- as.Date(adate, format=c("%Y%m%d"))
b2h2 <- merge(b2h2, b2info, by="plantID")

#save(b2h2,b2yield,b2chl,b2wat,b2dev, file="batch2CombinedData_Edited.RData")


