Commit 45480830 authored by fhui28's avatar fhui28
Browse files

merge data script for FUSHAN

No related merge requests found
Showing with 97 additions and 0 deletions
+97 -0
### MERGE Fushan DATA
### Edited by FH
rm(list = ls()); source("./R/format.function.R"); library(reshape)
#########################
## READ DATA
####################
### read individuals tree data
data.fushan <- load("./data/raw/DataFushan/fushan.RData"); data.fushan <- data.frame(fushan); rm(fushan)
data.trait <- read.table("./data/raw/DataFushan/fs_trait_Kunstler.txt",header=T, sep = "\t")
colnames(data.trait) <- c("sp","sla","wd","seedmassmg","meanN","maxheightm")
### read species names
species.clean <- read.csv("./data/raw/DataFushan/Splist_Fushan_En.csv",stringsAsFactors=FALSE)
######################################
## MASSAGE TRAIT DATA
############################
## Obtain maximum height per species from data.trait; no sd available as we have only one observation for species
data.max.height <- data.frame(sp = data.trait$sp, Max.height = log10(data.trait$maxheightm))
data.fushan <- merge(data.fushan, data.max.height, by = "sp")
##########################################
## FORMAT INDIVIDUAL TREE DATA
#############
## change unit and names of variables to be the same in all data for the tree - DON'T HAVE A YEAR MEASUREMENT
data.fushan$G <- 10*(data.fushan$dbh2 -data.fushan$dbh1)/data.fushan$Interval ## diameter growth in mm per year
data.fushan$year <- data.fushan$Interval ## number of year between measurement - MISSING
data.fushan$D <- data.fushan[["dbh1"]]; ## diameter in cm
data.fushan$dead <- as.numeric(data.fushan$status2 == "alive") ## dummy variable for dead tree 0 alive 1 dead
data.fushan$plot <- (data.fushan[["PLOT_ID"]]) ## plot code - MISSING
data.fushan$htot <- rep(NA,length(data.fushan[["dbh1"]]))## height of tree in m - MISSING
data.fushan$tree.id <- data.fushan$tag ## tree unique id - MISSING
data.fushan$sp.name <- NA; v <- species.clean$SP
for(i in 1:length(unique(data.fushan$sp))) {
sel.spp <- which(data.fushan$sp == unique(data.fushan$sp)[i])
data.fushan$sp.name[sel.spp] <- paste(species.clean$family[sel.spp],species.clean$genus[sel.spp], species.clean$epithet[sel.spp], sep = ".") }
##########################################
## CHANGE COORDINATE SYSTEM
#############
## DON'T KNOW!
######################
## ECOREGION
###################
## DON'T SEE DATA FOR THIS AT THE MOMENT, ALTHOUGH IT'S PROBABLY ALL IN ONE ECOREGION ANYWAY?
######################
## PERCENT DEAD
###################
## CANNOT DO THIS WITHOUT A PLOTID
## compute numer of dead per plot to remove plot with disturbance
perc.dead <- tapply(data.fushan[["dead"]],INDEX=data.fushan[["plot"]],FUN=function.perc.dead)
# ## VARIABLE TO SELECT PLOT WITH NOT BIG DISTURBANCE KEEP OFTHER VARIABLES IF AVAILABLE (disturbance record)
data.fushan <- merge(data.fushan,data.frame(plot=names(perc.dead),perc.dead=perc.dead), by = "plot", sort=FALSE)
###########################################################
### PLOT SELECTION FOR THE ANALYSIS
###################
## Remove data with dead == 1
table(data.fushan$dead)
data.fushan <- data.fushan[data.fushan$dead == 1,]
colnames(data.fushan)[c(3,1,11,13)] <- c("sp","plot","w")
vec.abio.var.names <- c("MAT","MAP")
vec.basic.var <- c("tree.id","sp","sp.name","plot","D","G","dead","year","htot","gx","gy","perc.dead")
data.tree <- subset(data.fushan,select=c(vec.basic.var,vec.abio.var.names))
##############################################
## COMPUTE MATRIX OF COMPETITION INDEX WITH SUM OF BA PER SPECIES IN EACH PLOT in m^2/ha without the target species
###########################
## CANNOT DO WITHOUT A PLOT ID
data.BA.SP <- BA.SP.FUN(id.tree=as.vector(data.fushan[["tree.id"]]), diam=as.vector(data.fushan[["D"]]),
sp=as.vector(data.fushan[["sp"]]), id.plot=as.vector(data.fushan[["plot"]]),
weights=1/(pi*(0.5*data.fushan[["dbh1"]])^2), weight.full.plot=NA)
## change NA and <0 data for 0
data.BA.SP[is.na(data.BA.SP)] <- 0; data.BA.SP[,-1][data.BA.SP[,-1]<0] <- 0
### CHECK IF sp and sp name for column are the same
if(sum(!(names(data.BA.SP)[-1] %in% unique(data.fushan[["sp"]]))) >0) stop("competition index sp name not the same as in data.tree")
#### compute BA tot for all competitors
BATOT.COMPET <- apply(data.BA.SP[,-1],1,sum,na.rm=TRUE)
data.BA.SP$BATOT.COMPET <- BATOT.COMPET; rm(BATOT.COMPET)
### create data frame
names(data.BA.SP) <- c("tree.id",names(data.BA.SP)[-1])
data.BA.sp <- merge(data.frame(tree.id=data.fushan[["tree.id"]],ecocode=data.fushan[["ecocode"]]),data.BA.SP,by="tree.id",sort=FALSE)
## test
if(sum(!data.BA.sp[["tree.id"]] == data.tree[["tree.id"]]) >0) stop("competition index not in the same order than data.tree")
## save everything as a list
list.canada <- list(data.tree=data.tree,data.BA.SP=data.BA.sp,data.traits=data.traits)
save(list.spain,file="./data/process/list.canada.Rdata")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment