Mbaiki.R 7.83 KB
Newer Older
fhui28's avatar
fhui28 committed
1
2
3
4
#!/usr/bin/env Rscript

### MERGE Mbaiki DATA

Georges Kunstler's avatar
Georges Kunstler committed
5
6
data.mbaiki$D <- data.mbaiki[["dbh1"]];
data.mbaiki$D[data.mbaiki$D == 0] <- NA;
Georges Kunstler's avatar
Georges Kunstler committed
7

fhui28's avatar
fhui28 committed
8
rm(list = ls())
9
10
# setwd("/home/ghislain/Documents/Ghislain-CIRAD/Traits_Competition_Georges/trait.competition.workshop")

Georges Kunstler's avatar
Georges Kunstler committed
11
source("R/format.data/format-fun.R")
fhui28's avatar
fhui28 committed
12
13
14
15
16
17
18
19
20
21
22
23
24
dir.create("output/formatted/Mbaiki", recursive=TRUE,showWarnings=FALSE)
library(reshape,quietly=TRUE)

############################ read individuals tree data
data.mbaiki <- read.table("data/raw/Mbaiki/Mbaiki_1995_2000_2005.csv",
                           header=TRUE,stringsAsFactors=FALSE,sep = ",")

colnames(data.mbaiki) <- c("forest","cluster","plot","tree","sp.name","x","y",
                            "circum1995","code1995","circum2000","code2000","circum2005",
                            "code2005","yeardied","typedeath")

data.mbaiki$tree.id <- apply(data.mbaiki[,c("cluster","plot","tree")],1,paste,collapse="_");

25
26
27
28
29
# Ghislain's comment:
# -------------------
# At Mbaiki, a cluster is made of four plots of 100m x 100m arrange as 200m x 200m forest clusters
# The coordinates of the trees are given for each cluster (origin at the bottom of the left side).

Georges Kunstler's avatar
Georges Kunstler committed
30

31
## Ghislain's comment: OK, tree location in clusters looks good
Georges Kunstler's avatar
Georges Kunstler committed
32

Georges Kunstler's avatar
Georges Kunstler committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## create small plot
square.s.t <- 20
make.quad <- do.call("rbind",lapply(unique(data.mbaiki$cluster),
                           FUN=fun.quadrat.cluster,
                           cluster=data.mbaiki$cluster,
                           tree.id=data.mbaiki$tree.id,
                           x=data.mbaiki$x,
                           y=data.mbaiki$y,
                           square.s=square.s.t))
data.mbaiki <- merge(data.mbaiki, make.quad,by='tree.id')

## Levels.cluster <- levels(as.factor(data.mbaiki$cluster))
## n.cluster <- length(Levels.cluster) # 10 clusters => 10 "plots"

## for (i in Levels.cluster){
##     dat <- subset(data.mbaiki, subset=data.mbaiki[["cluster"]]==i)
##     x11()
##     symbols(dat$x,dat$y,circles=dat$circum2000,inches=0.2,main=i,
##             fg=unclass(factor(dat$make.quad)))
## }   


Georges Kunstler's avatar
Georges Kunstler committed
55
### read species names
Georges Kunstler's avatar
Georges Kunstler committed
56
57
species.clean <- read.csv("data/raw/Mbaiki/Mbaiki.traits.csv",
                          stringsAsFactors=FALSE, header = T, sep = ",")
fhui28's avatar
fhui28 committed
58
59
60
61
species.clean$sp.name <- species.clean$Species
#data.mbaiki$spname %in% species.clean$sp.name
## All species in data.mbaiki are in species.clean. Good to go!

Georges Kunstler's avatar
Georges Kunstler committed
62
63
### Based on the familly provided by the tnrs web site from teh species list
### seems to be no tree fern or palm
64
65
66
### see files "data/raw/Mbaiki/tnrs_results_Mbaiki.txt" NEED to ckeck with ghislain


fhui28's avatar
fhui28 committed
67
68
69
70
71
72
73
############################################ FORMAT INDIVIDUAL TREE DATA
data.mbaiki2 <- data.mbaiki[rep(1:nrow(data.mbaiki),each=2),]
rownames(data.mbaiki2) <- 1:nrow(data.mbaiki2); 
data.mbaiki2 <- as.data.frame(data.mbaiki2)
data.mbaiki2$yr1 <- rep(c(1995,1995+5),nrow(data.mbaiki)); 
data.mbaiki2$yr2 <- rep(c(2000,2000+5),nrow(data.mbaiki))
data.mbaiki2$year <- rep(c(5,5),nrow(data.mbaiki))
Georges Kunstler's avatar
Georges Kunstler committed
74
75
76
77
78
79
80
81
data.mbaiki2$dbh1 <- c(rbind(data.mbaiki$circum1995/pi,
                             data.mbaiki$circum2000/pi))
data.mbaiki2$dbh2 <- c(rbind(data.mbaiki$circum2000/pi,
                             data.mbaiki$circum2005/pi))
data.mbaiki2$code1 <- c(as.numeric(rbind(data.mbaiki$code1995,
                                         data.mbaiki$code2000)))
data.mbaiki2$code2 <- c(as.numeric(rbind(data.mbaiki$code2000,
                                         data.mbaiki$code2005)))
fhui28's avatar
fhui28 committed
82
83

data.mbaiki2$dead <- rep(0,nrow(data.mbaiki)*2)
Georges Kunstler's avatar
Georges Kunstler committed
84
85
86
87
data.mbaiki2$dead[c(as.numeric(data.mbaiki[["yeardied"]]) %in% 1995:2000 &
                    (!is.na(data.mbaiki[["yeardied"]])),
                     as.numeric(data.mbaiki[["yeardied"]]) %in% 2000:2005 &
                    (!is.na(data.mbaiki[["yeardied"]])))] <- 1
fhui28's avatar
fhui28 committed
88
## remove tree dead at first census for both date - as in dead before 1995?
Georges Kunstler's avatar
Georges Kunstler committed
89
90
data.mbaiki <- data.mbaiki2[data.mbaiki2$yeardied > 1995 |
                            is.na(data.mbaiki2$yeardied),]
fhui28's avatar
fhui28 committed
91
92

## change unit and names of variables to be the same in all data for the tree 
Georges Kunstler's avatar
Georges Kunstler committed
93
94
data.mbaiki$G <- 10*(data.mbaiki$dbh2-data.mbaiki$dbh1)/data.mbaiki$year
## diameter growth in mm per year
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

## Ghislain's comment
## ------------------
## These are the code for MBaiki:
## Is_alive;code;description
## For dead trees
## ---------------
## FALSE;0;"mort naturelle"
## FALSE;1;"mort brûlé"
## FALSE;2;"arbre abattu par villageois"
## FALSE;3;"chablis primaire (dégât expl.)"
## FALSE;4;"Arbre exploité"
## FALSE;5;"Arbre empoisonné pour éclaircie"
## FALSE;6;"annélation par villageois"
## FALSE;7;"chablis secondaire"
## FALSE;9;"Mort naturelle arbre avec ruban (Code abandonné en 2003)"
## For leaving trees
## -----------------
## TRUE;0;"mesure normale"
## TRUE;1;"changement de hauteur"
## TRUE;2;"arbre très penché"
## TRUE;3;"arbre écorcé"
## TRUE;4;"contreforts/troncs irréguliers"
## TRUE;5;"arbre ressuscité (type rejet)"
## TRUE;6;"arbre mourant"
## TRUE;7;"arbre porteur d'1 Ficus"
## TRUE;8;"volis (cîme cassée)"
## TRUE;9;"mesure au relascope"
##
## So (1) for the competition (BA computation, all trees can be kept).
Georges Kunstler's avatar
Georges Kunstler committed
125
126
127
128
129
130
131
132
133
134
135
136
137
## (2) for a good measure of growth: remove trees with codes 1 and 9
## but keep the others.

data.mbaiki$G[data.mbaiki$code1 %in% c(1,9)] <- NA
## indivs with code indicating problem in dbh measurment at dbh1
data.mbaiki$G[data.mbaiki$code2 %in% c(1,9)] <- NA
## indivs with code indicating problem in dbh measurment at dbh2
data.mbaiki$BA.G <- (pi*(data.mbaiki$dbh2/2)^2-pi*(data.mbaiki$dbh1/2)^2)/
    data.mbaiki$year ## BA growth in cm2 pBer year
data.mbaiki$BA.G[data.mbaiki$code1 %in% c(1,9)] <- NA
## indivs with code indicating problem in dbh measurment at dbh1
data.mbaiki$BA.G[data.mbaiki$code2 %in% c(1,9)] <- NA
B## indivs with code indicating problem in dbh measurment at dbh2
fhui28's avatar
fhui28 committed
138
data.mbaiki$census <- data.mbaiki$yr1
Georges Kunstler's avatar
Georges Kunstler committed
139
140
data.mbaiki$D <- data.mbaiki[["dbh1"]];
data.mbaiki$D[data.mbaiki$D == 0] <- NA ;## diameter in cm
fhui28's avatar
fhui28 committed
141
142
143
data.mbaiki$cluster <- paste("p",data.mbaiki$cluster,sep=".")
data.mbaiki$htot <- rep(NA,length(data.mbaiki[["G"]])) ## height of tree in m
data.mbaiki$obs.id <- 1:nrow(data.mbaiki)
Georges Kunstler's avatar
Georges Kunstler committed
144
data.mbaiki$plot <-  data.mbaiki$make.quad
fhui28's avatar
fhui28 committed
145

146
### delete recruit in 2000 or 2005 for first census
fhui28's avatar
fhui28 committed
147
data.mbaiki <- subset(data.mbaiki,subset=!is.na(data.mbaiki$D))
148
149
## minimum diameter=10cm, thus delete all tree with a dbh <10cm
data.mbaiki <-  subset(data.mbaiki,subset= data.mbaiki[["D"]]>=10)
fhui28's avatar
fhui28 committed
150
151
152
153
154
155
156
157
158

data.mbaiki$circum1995 <- NULL
data.mbaiki$circum2000 <- NULL
data.mbaiki$circum2005 <- NULL
data.mbaiki$code1995 <- NULL
data.mbaiki$code2000 <- NULL
data.mbaiki$code2005 <- NULL
data.mbaiki$sp <- data.mbaiki$sp.name

Georges Kunstler's avatar
Georges Kunstler committed
159
160
161
162
163
164
165
166
167
168
169
170
171
####################
## add lat long
data.mbaiki[["Lon"]] <- 17.87
data.mbaiki[["Lat"]] <- 3.85 
data.mbaiki[["ecocode"]] <- "tropical"

#### get wc climate 
source("R/utils/climate.R")
clim <- GetClimate(data.mbaiki$Lat,data.mbaiki$Lon)
data.mbaiki$MAT <- clim$MAT
data.mbaiki$MAP <- clim$MAP

###################### PLOT SELECTION FOR THE ANALYSIS - NEEDS REDOING
Georges Kunstler's avatar
Georges Kunstler committed
172
173
vec.basic.var <- c("obs.id","tree.id", "sp", "sp.name", "cluster","plot",
                   "ecocode", "D", "G","BA.G","year", "dead",
Georges Kunstler's avatar
Georges Kunstler committed
174
    'Lon','Lat',"x", "y", "census",'MAT','MAP')
fhui28's avatar
fhui28 committed
175
176
177
178
179
data.tree <- subset(data.mbaiki, select = c(vec.basic.var))

## select tree above 10 cm and last census only
data.tree <- subset(data.tree,subset=data.tree$D>10 & !is.na(data.tree$D))
## data.tree <- subset(data.tree,subset=data.tree$census==2005)
180
data.tree <- subset(data.tree,subset=!is.na(data.tree$x) & !is.na(data.tree$y))
Georges Kunstler's avatar
Georges Kunstler committed
181
182
## convert var factor in character or numeric
data.tree <- fun.convert.type.B(data.tree)
fhui28's avatar
fhui28 committed
183

Georges Kunstler's avatar
Georges Kunstler committed
184
write.csv(data.tree,file="output/formatted/Mbaiki/tree.csv",row.names = FALSE)
fhui28's avatar
fhui28 committed
185
186
187

### write data plot with variables only at the plot level.

Georges Kunstler's avatar
Georges Kunstler committed
188
vec.basic.var.p <- c("plot", "cluster", "Lon","Lat","ecocode",'MAT','MAP')
Georges Kunstler's avatar
Georges Kunstler committed
189
190
data.plot <- subset(data.tree, subset=!duplicated(data.tree$cluster),
                    select = c(vec.basic.var.p))
Georges Kunstler's avatar
Georges Kunstler committed
191
write.csv(data.plot,file="output/formatted/Mbaiki/plot.csv",row.names = FALSE)
fhui28's avatar
fhui28 committed
192

Georges Kunstler's avatar
Georges Kunstler committed
193