NVS.R 4.32 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 NVS
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/NVS/tree.csv", stringsAsFactors = FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
8
9
10
11
12
13
14
15
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))

16
## read in local trait data
Georges Kunstler's avatar
Georges Kunstler committed
17

18
data.trait <- read.csv("data/raw/NVS/nz_traits_130801.csv", , header = TRUE, stringsAsFactors = FALSE)
19
20
21
data.trait <- data.trait[, -1]
colnames(data.trait)[1] <- "sp"
data.trait$Latin_name <- data.trait$sp
22
data.trait$sp <- paste("sp",data.trait$sp,sep=".")
23
24
25
26
27
28
29
30
data.trait$Leaf.N.mean <- data.trait$leafn*10; data.trait$leafn <- NULL ## conversion from % to mg/g
data.trait$Leaf.N.sd <- NA ## conversion from % to mg/g
data.trait$Seed.mass.mean <- data.trait$seed.mg; data.trait$seed.mg <- NULL
data.trait$Seed.mass.sd <- NA
data.trait$SLA.mean <- 1/data.trait$lma.gm2; data.trait$SLA.mean <- data.trait$SLA.mean*1000;data.trait$lma.gm2 <- NULL ## conversion of g/m2 to mm2/g1
data.trait$SLA.sd <- NA
data.trait$Wood.density.mean <- data.trait$wood; data.trait$wood <- NULL
data.trait$Wood.density.sd <- NA
31
data.trait$Max.height.mean <- data.trait$height.m; data.trait$height.m <- NULL
32
33
data.trait$Max.height.sd <- NA

34
## read traits from data
35
data.traits <- fun.extract.format.sp.traits.NOT.TRY(sp=species.clean$sp, Latin_name=species.clean$Latin_name, data=data.trait,name.match.traits="Latin_name")
Georges Kunstler's avatar
Georges Kunstler committed
36

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
68
69
70
71
72
73
74
75
76
## TODO GET ANGIO CONIF DECIDUOUS EVERGREEN BUT NEED SPECIES LATIN NAME
NVS.SPECIES.name <- read.csv("data/raw/NVS/CurrentNVSNames.csv",sep=';', stringsAsFactors = FALSE)
species.clean$Latin_name <- NVS.SPECIES.name$Species.Name[match(species.clean$Latin_name_syn,
                                                                NVS.SPECIES.name$NVS.Code)]
data.traits$Latin_name <- species.clean$Latin_name

#### GET THE ANGIO/CONIF AND EVERGREEN/DECIDUOUS

## FOR NEW ZEALADN BETTER TO FOLLOW  McGlone et al. 2004. Winter leaf loss in the New Zealand woody flora. New Zealand Journal of Botany, 2004, Vol. 42: 1-19. which report 11 species asdeciduous adn 11 species as semi-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)

## FOR NEW ZEALAND BETTER TO FOLLOW  McGlone et al. 2004. Winter leaf loss in the New Zealand woody flora. New Zealand Journal of Botany, 2004, Vol. 42: 1-19. which report 11 species asdeciduous adn 11 species as semi-deciduous

## according to McGlone et al. 2004. Winter leaf loss in the New Zealand woody flora. New Zealand Journal of Botany, 2004, Vol. 42: 1-19. all other species are evergreen
data.cat.extract[,'Pheno.T'] <- 'EV'

data.cat.extract[data.cat.extract$Latin_name %in% c('Discaria toumatou',
'Fuchsia excorticata','Hoheria glabrata','Hoheria lyallii',
'Muehlenbeckia astonii','Olearia fragantissima','Olearia gardneri','Olearia hectori',
'Olearia odorata','Plagianthus divaricatus','Plagianthus regius'),'Pheno.T'] <- 'D'

data.cat.extract[data.cat.extract$Latin_name %in% c('Aristotelia serrata',
'Carmichaelia odorata','Carmichaelia arborea',
'Coprosma virescens','Coriaria arborea var. arborea','Fuchsia perscandens',
'Fuchsia procumbens','Muehlenbeckia australis','Muehlenbeckia complexa',
'Olearia fimbriata','Urtica ferox'),'Pheno.T'] <- 'D_EV'

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

Georges Kunstler's avatar
Georges Kunstler committed
77
### TODO ADD GENUS MEAN FOR HEIGHT IF SPECIES IS MISSING
78
write.csv(data.traits,file="output/formatted/NVS/traits.csv",row.names = FALSE)