Luquillo.R 4.34 KB
Newer Older
fhui28's avatar
fhui28 committed
1
2
#!/usr/bin/env Rscript

3
##### FORMAT TRAIT FOR Luquillo
fhui28's avatar
fhui28 committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
source("R/find.trait/trait.fun.R")

### read species names
data.tree <- read.csv("output/formatted/Luquillo/tree.csv", stringsAsFactors = FALSE)
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)



###################################### MASSAGE TRAIT DATA Use HEIGHT_AVG, LMALAM_AVD, SEED_DRY
data.trait <- read.csv("data/raw/Luquillo/luq_traits2georges.9sept2013.csv", as.is=T, header = T)

# species name
data.trait$sp <- paste("sp",data.trait$Code,sep=".")
data.trait[["Code"]] <- NULL
Georges Kunstler's avatar
Georges Kunstler committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

## ### read csv latin name adn add in sp.name 
fun.get.species <- function(x) paste(strsplit(x," ")[[1]][1:2],collapse=" ")
trim.trailing <- function(x) sub("\\s+$", "", x)

data.latin.name <- read.table("data/raw/Luquillo/species2.csv", header = TRUE,
    stringsAsFactors = FALSE, sep = ",",na.strings="")
data.latin.name$Present_Scientific_Name <- trim.trailing(data.latin.name$Present_Scientific_Name)
data.latin.name$Latin.name <- as.vector(sapply(data.latin.name$Present_Scientific_Name,fun.get.species))
data.latin.name <- data.latin.name[!duplicated(data.latin.name$LFDP_Species_Code),]
data.latin.name$LFDP_Species_Code <- paste("sp",data.latin.name$LFDP_Species_Code,sep=".")
## merge with data.trait 
data.trait <- merge(data.trait,subset(data.latin.name,select=c("LFDP_Species_Code","Latin.name")),
                  by.x="sp",by.y="LFDP_Species_Code",
                  all.x=TRUE,all.y=FALSE)

data.trait$Latin_name <- data.trait$Latin.name
fhui28's avatar
fhui28 committed
38
39

# leaf N
40
41
data.trait$Leaf.N.mean <- data.trait[["N"]]*10
data.trait$Leaf.N.sd <- NA ## NEED A UNIT CHECK % from 0 to 100  need to convert in mg/g GREATER THAN 1 ? 
fhui28's avatar
fhui28 committed
42
43
44
data.trait[["N"]] <- NULL

# SLA
45
46
data.trait$SLA.mean <- data.trait$sla/10
## NEED A UNIT CHECK cm2g-1 in Uriarte et al 2010 need to convert in mm2mg-1
fhui28's avatar
fhui28 committed
47
48
49
50
data.trait$SLA.sd <- NA
data.trait$sla <- NULL

# seed
51
data.trait$Seed.mass.mean <- data.trait[["seed"]]*1000 ## need to convert in mg OK ?
fhui28's avatar
fhui28 committed
52
53
54
55
data.trait$Seed.mass.sd <- NA
data.trait$seed <- NULL

# wd - 
56
data.trait$Wood.density.mean <- data.trait$wsg ##  need to convert to mg/mm3
Georges Kunstler's avatar
Georges Kunstler committed
57
data.trait$Wood.density.sd <- NA
fhui28's avatar
fhui28 committed
58
59

# max hght
60
data.trait$Max.height.mean <- data.trait[["h"]] ## m in Uriarte et al 2010
fhui28's avatar
fhui28 committed
61
62
63
64
data.trait$Max.height.sd <-  NA
data.trait$h <- NULL

data.TRAITS.std <- data.trait
Georges Kunstler's avatar
Georges Kunstler committed
65
write.csv(data.TRAITS.std,file="output/formatted/Luquillo/traits.std.csv",row.names = FALSE)
fhui28's avatar
fhui28 committed
66
67
68
69
70
71
72
73
rm(data.trait)

## extract
data.traits <- fun.extract.format.sp.traits.NOT.TRY(sp=species.clean$Latin_name, Latin_name=species.clean$Latin_name, data=data.TRAITS.std,name.match.traits="Latin_name")

## change sp
data.traits$sp <- species.clean$sp

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

#### 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('Prestoea acuminata',
                                                    'Matayba domingensis'),'Pheno.T'] <- 'D'

data.cat.extract[data.cat.extract$Latin_name %in% c('Cecropia schreberiana',
                                                    'Talipariti tiliaceum',
                                                    'Eugenia domingensis',
                                                    'Ficus crassinervia') ,'Pheno.T']<-  'EV'
data.cat.extract[data.cat.extract$Latin_name %in% c('Chione venosa') ,'Pheno.T']<-  'D_EV'


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

102
### 
fhui28's avatar
fhui28 committed
103
104
105
write.csv(data.traits,file="output/formatted/Luquillo/traits.csv",row.names = FALSE)