test.tree.CWM.R 3.67 KB
Newer Older
Georges Kunstler's avatar
Georges Kunstler committed
1
2
###############################
### SCRIPT TO TEST structure of CWM data output of pocessed
Georges Kunstler's avatar
Georges Kunstler committed
3
rm(list = ls())
Georges Kunstler's avatar
Georges Kunstler committed
4

Georges Kunstler's avatar
Georges Kunstler committed
5
6
source("R/process.data/process-fun.R")
source("R/process.data/test.tree.CWM-fun.R")
Georges Kunstler's avatar
Georges Kunstler committed
7
8


9
10
#### RUN
filedir <- "output/processed"
Georges Kunstler's avatar
Georges Kunstler committed
11
12
sets.I <- c("Sweden","NVS","US","Canada","NSW","France","Swiss", "Spain")
sets.B <- c("BCI","Paracou","Japan","Fushan","Paracou","Luquillo","Mbaiki")
13
14
dir.create("figs/test.processed", recursive=TRUE,showWarnings=FALSE)

Georges Kunstler's avatar
Georges Kunstler committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28

## load all data in one big file
list.all.I <- lapply(sets.I,fun.load.set.in.big.file,filedir,type='I')
data.all.I <- do.call("rbind",list.all.I)
list.all.B <-lapply(sets.B,fun.load.set.in.big.file,filedir,type='B')
data.all.B <- do.call("rbind",list.all.B)
data.all.B <-data.all.B[,names(data.all.I)]
data.all <- rbind(data.all.I,data.all.B)

write.csv(data.all,file=file.path(filedir, "data.all.csv"), row.names=FALSE)
rm(list.all.I,list.all.B,data.all.I,data.all.B)
rm(data.all)

## test all set
29
30
library(doParallel)
registerDoParallel(cores=5)
31

32
lmat.perc.I <- mclapply(sets.I, fun.test.set.tree.CWM.I,filedir=filedir,mc.cores= getOption("mc.cores", 5))
Georges Kunstler's avatar
Georges Kunstler committed
33
mat.perc.I <- do.call("rbind", lmat.perc.I)
34

35
lmat.perc.B <-mclapply(sets.B,fun.test.set.tree.CWM.B,filedir=filedir,mc.cores= getOption("mc.cores", 5))
36
mat.perc.B <- do.call("rbind",lmat.perc.B)
37

38
39
mat.perc <- data.frame(rbind(mat.perc.I,mat.perc.B),stringsAsFactors=FALSE)
mat.perc <- data.frame(lapply(mat.perc, function(x) (unlist(x))))
40
41
write.csv(mat.perc,file=file.path(filedir, "all.sites.perc.traits.csv"),
          row.names=FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
42

Georges Kunstler's avatar
Georges Kunstler committed
43
44
## format table for report
## genus perc
45
46
mat.num.g <- mat.perc[,c(1:3,9:13)]
mat.num.g[,4:8] <- mat.perc[,9:13]/mat.perc[,3]
47

48
49
names(mat.num.g) <- c('set','ecoregion','P obs total','P Leaf N',
                      'P Seed mass','P SLA','P Wood density','P Max height')
50
51

library('pander')
52
53
54
pandoc.table(mat.num.g,
             caption="Number of tree radial growth observation per data sets and ecoregion.",
             split.tables='Inf')
55

Georges Kunstler's avatar
Georges Kunstler committed
56
## species perc
57
58
mat.num.sp <- mat.perc[,c(1:8)]
mat.num.sp[,4:8] <- mat.perc[,4:8]/mat.perc[,3]
59
60
61
pandoc.table(mat.num.sp,
             caption="Number of tree radial growth observation per data sets and ecoregion.",
             split.tables='Inf')
Georges Kunstler's avatar
Georges Kunstler committed
62
63


Georges Kunstler's avatar
Georges Kunstler committed
64
65
66
### read all data
data.all <- read.csv(file=file.path(filedir, "data.all.csv"))
 if(dim(data.all)[1] != sum(mat.perc[['num.obs']])) stop('error not same dimension per ecoregion and total')
Georges Kunstler's avatar
Georges Kunstler committed
67
68


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
data.all[['G']][!(trim.positive.growth(data.all[['G']]) &
trim.negative.growth(dbh1=data.all[['D']]*10,
                     dbh2=data.all[['D']]*10 +data.all[['year']]*data.all[['G']]))] <- NA

data.all[['BA.G']][!(trim.positive.growth(data.all[['G']]) &
trim.negative.growth(data.all[['D']]*10,dbh2=data.all[['D']]*10 +data.all[['year']]*data.all[['G']]))] <- NA


## plot

fun.plot.hist.trait.per.set(data.all)

to.pdf(fun.hist.var.set(data.all,var='BATOT',cex=0.6),
       filename="figs/test.processed/fig.BATOT.set.pdf") 
to.pdf(fun.hist.var.set(data.all,var='G',cex=0.6),
       filename="figs/test.processed/fig.G.set.pdf") 
to.pdf(fun.hist.var.set(data.all,var='BA.G',cex=0.6),
       filename="figs/test.processed/fig.BA.G.set.pdf") 
to.pdf(fun.hist.var.set(data.all,var='D',cex=0.6),
       filename="figs/test.processed/fig.D.set.pdf") 
to.dev(fun.plot.xy.set(data.all,var.x='BATOT',var.y='BA.G',cex=0.6),dev=png,
       filename="figs/test.processed/fig.xy.BATOT.BA.G.set.png") 
to.dev(fun.plot.xy.set(data.all,var.x='D',var.y='BA.G',cex=0.6),dev=png,
       filename="figs/test.processed/fig.xy.D.BA.G.set.png") 
to.dev(fun.plot.xy.set(data.all,var.x='D',var.y='G',cex=0.6),dev=png,
       filename="figs/test.processed/fig.xy.D.G.set.png") 





fun.plot.xy.trait.per.set(data.all,var.x='BATOT',var.y='BA.G')

Georges Kunstler's avatar
Georges Kunstler committed
102
103
104