Sweden.R 6.08 KB
Newer Older
Daniel Falster's avatar
Daniel Falster committed
1
2
#!/usr/bin/env Rscript

Georges Kunstler's avatar
Georges Kunstler committed
3
##### FORMAT TRAIT FOR Sweden
4
source("R/find.trait/trait.fun.R")
Georges Kunstler's avatar
Georges Kunstler committed
5
6

### read species names
7
data.tree <- read.csv("output/formatted/Sweden/tree.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
8
9
10
11
12
13
14
15
16
17
species.clean <- data.frame(sp=data.tree[!duplicated(data.tree[["sp"]]),"sp"],
                             Latin_name=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             Latin_name_syn=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             stringsAsFactors =FALSE)
## delete the sp code with no species
species.clean <- subset(species.clean,subset=!is.na(species.clean$Latin_name))

## select column to keep

## read in data
18
data.TRY.std <- readRDS("output/formatted/TRY/data.TRY.std.rds")
Georges Kunstler's avatar
Georges Kunstler committed
19
## read France max height
20
max.height <- read.csv(file="output/formatted/France/max.height.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
21
## load latin name France
22
data.tree <- read.csv("output/formatted/France/tree.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
23
24
25
26
27
species.clean2 <- data.frame(sp=data.tree[!duplicated(data.tree[["sp"]]),"sp"],
                             Latin_name=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             Latin_name_syn=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             stringsAsFactors =FALSE)
## delete the sp code with no species
28
#species.clean2 <- subset(species.clean2,subset=!is.na(species.clean2$Latin_name)) ## add latin name
29
max.height <- merge(subset(max.height,select=c("sp","Max.height.mean","Max.height.sd","Max.height.nobs")),species.clean2,by="sp") 
Georges Kunstler's avatar
Georges Kunstler committed
30
## extract traits and height
31
32
data.traits <- fun.extract.format.sp.traits.TRY(sp=species.clean[["sp"]],sp.syno.table=species.clean,
                                                data=data.TRY.std)
33
#data.traits <- merge(data.traits,subset(max.height,select=c("Latin_name","Max.height.mean","Max.height.sd")),by="Latin_name",all.x=TRUE,all.y=FALSE)
34
35
data.traits <- merge(data.traits,subset(max.height,select=c("Latin_name","Max.height.mean","Max.height.sd")),
                     by="Latin_name",all.x=TRUE,all.y=FALSE)
36
data.traits$Max.height.genus <- FALSE
Georges Kunstler's avatar
Georges Kunstler committed
37
# genus mean for height
Georges Kunstler's avatar
Georges Kunstler committed
38
height.genus.DF <- do.call("rbind",lapply(data.traits$Latin_name,fun.compute.mean.genus,max.height,
39
                                          "Max.height.mean"))
Georges Kunstler's avatar
Georges Kunstler committed
40
41
42
data.traits[is.na(data.traits[["Max.height.mean"]]),
            c("Max.height.mean","Max.height.sd","Max.height.genus")] <- height.genus.DF[is.na(data.traits[["Max.height.mean"]]),]

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#### Assume sp.30 Betula spp is mainly sp.31 Betula pendula and sp.32 Betula pubsecens and take the mean of that
data.traits$Leaf.N.mean[data.traits$sp=="sp.30"] <- mean(data.traits$Leaf.N.mean[data.traits$sp %in%
                            c("sp.31","sp.32")])
data.traits$Seed.mass.mean[data.traits$sp=="sp.30"] <- mean(data.traits$Seed.mass.mean[data.traits$sp %in%
                               c("sp.31","sp.32")])
data.traits$SLA.mean[data.traits$sp=="sp.30"] <- mean(data.traits$SLA.mean[data.traits$sp %in%
                         c("sp.31","sp.32")])
data.traits$Wood.density.mean[data.traits$sp=="sp.30"] <- mean(data.traits$Wood.density.mean[data.traits$sp
                                  %in% c("sp.31","sp.32")])
data.traits$Max.height.mean[data.traits$sp=="sp.30"] <- mean(data.traits$Max.height.mean[data.traits$sp
                                %in% c("sp.31","sp.32")])

data.traits$Leaf.N.genus[data.traits$sp=="sp.30"] <- FALSE
data.traits$Seed.mass.genus[data.traits$sp=="sp.30"] <- FALSE
data.traits$SLA.genus[data.traits$sp=="sp.30"] <- FALSE
data.traits$Wood.density.genus[data.traits$sp=="sp.30"] <- FALSE
data.traits$Max.height.genus[data.traits$sp=="sp.30"] <- FALSE

## do the same for quercus robur and quercus petraea

data.quercus <-(data.frame(sp=c("sp.500","sp.501"),
                           Latin_name=c("Quercus robur","Quercus petraea"),
                           Latin_name_syn=c("Quercus robur","Quercus petraea")))
data.quercus <- fun.extract.format.sp.traits.TRY(sp=data.quercus[["sp"]],
                                                 sp.syno.table=data.quercus,
                                                 data=data.TRY.std)

data.traits$Leaf.N.mean[data.traits$sp=="sp.51"] <- mean(data.quercus$Leaf.N.mean)
data.traits$Seed.mass.mean[data.traits$sp=="sp.51"] <- mean(data.quercus$Seed.mass.mean)
data.traits$SLA.mean[data.traits$sp=="sp.51"] <- mean(data.quercus$SLA.mean)
data.traits$Wood.density.mean[data.traits$sp=="sp.51"] <- mean(data.quercus$Wood.density.mean)
Georges Kunstler's avatar
Georges Kunstler committed
74
data.traits$Max.height.mean[data.traits$sp=="sp.51"] <- mean(max.height$Max.height.mean[max.height$Latin_name
75
76
77
78
79
80
81
82
                                %in% c("Quercus robur","Quercus petraea")])

data.traits$Leaf.N.genus[data.traits$sp=="sp.51"] <- FALSE
data.traits$Seed.mass.genus[data.traits$sp=="sp.51"] <- FALSE
data.traits$SLA.genus[data.traits$sp=="sp.51"] <- FALSE
data.traits$Wood.density.genus[data.traits$sp=="sp.51"] <- FALSE
data.traits$Max.height.genus[data.traits$sp=="sp.51"] <- FALSE

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#### GET THE ANGIO/CONIF AND EVERGREEN/DECIDUOUS
# read try categrocial data
try.cat <- read.csv("data/raw/TRY/TRY_Categorical_Traits_Lookup_Table_2012_03_17_TestRelease.csv",
         stringsAsFactors=FALSE,na.strings = "")
Pheno.Zanne <- read.csv("data/raw/ZanneNature/GlobalLeafPhenologyDatabase.csv",
         stringsAsFactors=FALSE)
# extract
data.cat.extract <- do.call("rbind",lapply(data.traits$sp ,fun.get.cat.var.from.try,
                                           data.traits,try.cat,Pheno.Zanne))
# change category
data.cat.extract <- fun.change.factor.pheno.try(data.cat.extract)
data.cat.extract <- fun.change.factor.angio.try(data.cat.extract)
data.cat.extract <- fun.fill.pheno.try.with.zanne(data.cat.extract)

## fix pheno for species with issue
data.cat.extract[data.cat.extract$Latin_name %in% c('Betula ssp','Quercus robur_petraea',
    'Ulmus ssp'),'Pheno.T'] <- 'D'

data.cat.extract[data.cat.extract$Latin_name %in% c('Abies ssp',
       'Picea ssp other than P.abies') ,'Pheno.T']<-  'EV'


data.traits <- merge(data.traits,data.cat.extract[,c("sp","Phylo.group","Pheno.T")],by="sp")

### writing
108
write.csv(data.traits,file="output/formatted/Sweden/traits.csv",row.names = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
109

110
111