Spain.R 3.38 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 SPAIN
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/Spain/tree.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
8
9
10
11
12
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)

13
14
15
16
species.clean$Latin_name[species.clean$sp=='sp.243'] <- 'Quercus pubescens'
species.clean$Latin_name_syn[species.clean$sp=='sp.243'] <- 'Quercus pubescens'
species.clean$Latin_name[species.clean$sp=='sp.258'] <- 'Populus canadensis'
species.clean$Latin_name_syn[species.clean$sp=='sp.258'] <- 'Populus canadensis'
Georges Kunstler's avatar
Georges Kunstler committed
17
18
19
## select column to keep

## read in data
20
21
data.TRY.std <- readRDS("output/formatted/TRY/data.TRY.std.rds")
max.height <- read.csv(file="output/formatted/Spain/max.height.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
22
23
24
max.height$sp <- paste("sp",max.height$sp,sep=".")

## extract traits and height
25
26
27
28
29
data.traits <- fun.extract.format.sp.traits.TRY(sp=species.clean[["sp"]],
                                                sp.syno.table=species.clean,
                                                data=data.TRY.std)
data.traits <- merge(data.traits,subset(max.height,select=c("sp","Max.height.mean","Max.height.sd")),
                     by="sp",all.x=TRUE,all.y=FALSE)
30
data.traits$Max.height.genus <- FALSE
Georges Kunstler's avatar
Georges Kunstler committed
31
# genus mean for height
32
33
height.genus.DF <- do.call("rbind",lapply(data.traits$Latin_name,
                                          fun.compute.mean.genus,data.traits,"Max.height.mean"))
Georges Kunstler's avatar
Georges Kunstler committed
34
data.traits[is.na(data.traits[["Max.height.mean"]]),
35
36
            c("Max.height.mean","Max.height.sd","Max.height.genus")] <-
    height.genus.DF[is.na(data.traits[["Max.height.mean"]]),]
Georges Kunstler's avatar
Georges Kunstler committed
37

38
39
40
41
42
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

#### 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('Crataegus spp.','Crataegus laciniata',
                                                    'Pyrus spp.','Morus spp.','Salix spp.',
                                                    'Betula spp.','Salix elaegnos',
                                                    'Tilia spp.','Sorbus spp.',
                                                    'Prunus spp.','Larix spp.'),'Pheno.T'] <- 'D'

data.cat.extract[data.cat.extract$Latin_name %in% c('Juniperus turbinata',
                                                    'Otros pinos'),'Pheno.T']<-  'EV'



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

# write
68
write.csv(data.traits,file="output/formatted/Spain/traits.csv",row.names = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
69