-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.R
165 lines (121 loc) · 7.85 KB
/
draft.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
get_latest_url<-function()return("https://inventaire-forestier.ign.fr/dataifn/data/export_dataifn_2005_2020.zip")
get_latest_url()
download_NFI<-function(dir=getwd(),nfi_url=get_latest_url()){
dir.create(file.path(dir,"NFI_data"),showWarnings = F)
dir.create(file.path(dir,"NFI_data","Raw_data"),showWarnings = F)
path_zip<-file.path(dir,"NFI_data","Raw_data","data_NFI.zip")
download.file(nfi_url,path_zip)
return(path_zip)
}
download_NFI()
library(data.table)
library(stringr)
get_NFI<-function(dir=getwd(),nfi_url=nfi_url=get_latest_url(),write_csv=F,export_to_env=T,visit=1){
## download
path_data<-file.path(dir,"NFI_data")
path_raw_data<-file.path(dir,"NFI_data","Raw_data")
path_zip<-file.path(dir,"NFI_data","Raw_data","data_NFI.zip")
already_NFI<-!length(list.files(path_raw_data))==0
if(!already_NFI) download_NFI(dir,nfi_url)
if(already_NFI) {
user_input<- trimws(as.character(readline(prompt="raw NFI data already here, type 1 to use them or 2 to download them again : ")))
if(user_input=="2") download_NFI(dir,nfi_url)
if(!user_input%in%c("1","2")) user_input<- readline(prompt="raw NFI data already here, type 1 to use them or 2 to download them again : ")
}
## reading them
NFI_ecologie<-data.table(read.table(unz(path_zip,"ECOLOGIE.csv"),sep=";",header=T,stringsAsFactors =F))
NFI_plot_info<-data.table(read.table(unz(path_zip,"PLACETTE.csv"),sep=";",header=T,stringsAsFactors =F))
NFI_arbres<-data.table(read.table(unz(path_zip,"ARBRE.csv"),sep=";",header=T,stringsAsFactors =F))
NFI_cover<-data.table(read.table(unz(path_zip,"COUVERT.csv"),sep=";",header=T,stringsAsFactors =F))
NFI_flora<-data.table(read.table(unz(path_zip,"FLORE.csv"),sep=";",header=T,stringsAsFactors =F))
if( !any(c(visit==1 , visit==2 , visit==c(1,2)) ))stop("Need a valid visit code")
colnames(NFI_ecologie)<-tolower(colnames(NFI_ecologie));colnames(NFI_ecologie)[1]<-"campagne";NFI_ecologie[,x:=NULL]
colnames(NFI_plot_info)<-tolower(colnames(NFI_plot_info));colnames(NFI_plot_info)[1]<-"campagne";NFI_plot_info[,x:=NULL]
colnames(NFI_arbres)<-tolower(colnames(NFI_arbres));colnames(NFI_arbres)[1]<-"campagne";NFI_arbres[,x:=NULL]
colnames(NFI_cover)<-tolower(colnames(NFI_cover));colnames(NFI_cover)[1]<-"campagne";NFI_cover[,x:=NULL]
colnames(NFI_flora)<-tolower(colnames(NFI_flora));colnames(NFI_flora)[1]<-"campagne";NFI_flora[,x:=NULL]
NFI_plot_info<-NFI_plot_info[visite%in%visit,]
canopy_cover<-NFI_cover[strate=="R",.(couverttot=sum(tcl),couverttot_abs=sum(tca)),by=idp]
#
# NFI_plot_info[,gest_struct:=sfo]
# NFI_plot_info[,tmp:=switchv(as.character(sver),"0",`NA`="0",`0`="0",`X`="0",`2`="1",`3`="4",`4`="2",`5`="3",`6`="1")]
# NFI_plot_info$gest_struct[NFI_plot_info$campagne>2013]<-NFI_plot_info$tmp[NFI_plot_info$campagne>2013]
# NFI_plot_info[,gest_struct:=switchv(as.character(gest_struct),`NA`=NA,`0`="debois",`1`="FR",`2`="FIR",`3`="TSF",`4`="T")]
# NFI_plot_info[,gest_struct:=as.character(gest_struct)]
# NFI_plot_info[,tmp:=NULL]
NFI_ecologie[,campagne:=NULL]
# Imputation aux arbres simplifi?s de la valeur moyenne des mesures des arbres de la m?me placette, essences, et classe de taille
NFI_arbres[, dimess := cut(c13, breaks = c(0, 70.5, 117.5, 164.5, 1000), labels = c("PB", "BM", "GB", "TGB"), right = FALSE)]
NFI_arbres[, htot := ifelse(is.na(htot), mean(htot, na.rm = TRUE), htot), by = c("idp", "espar", "dimess")]
NFI_arbres[, ir5 := ifelse(is.na(ir5), mean(ir5, na.rm = TRUE), ir5), by = c("idp", "espar", "dimess")]
NFI_arbres<-NFI_arbres[veget=="0",]
NFI_arbres[,campagne_2 := floor(idp/100000)+2005]
# Definition d'une classe de sous-?tage
NFI_arbres[,strate:=ifelse(htot>0.5*max(htot, na.rm = T), "etagePrincipal", "sousEtage"),keyby=idp]
### Calcul des surfaces terri?res
NFI_arbres[,gFinal:=c13*c13*w/(4*pi)]
NFI_arbres[,gInitial:=pi/10000*(c13/(2*pi)-ir5/10)^2*w]
### Valeurs dendro ?chelle placette
NFI_dendro <- NFI_arbres[,.(basal_area=sum(gFinal),
Ntot=sum(w)), by = idp]
NFI_dendro[, DgTotFinal:=sqrt(4*basal_area/(pi*Ntot))*100]
## getting the metada for taxonomic purposes
meta_data<-data.table(read.table(unz(path_zip,"metadonnees.csv"),sep=";",skip=17,fill=T,quote="",row.names = NULL,encoding = "UTF-8",stringsAsFactors =F))
colnames(meta_data)<-c("code","value","description","advanced_description")
flora_taxo<-meta_data[code=="CDREF13",]
colnames(flora_taxo)[3]<-c("species_name")
flora_taxo[,French_vernacular_name:=stringr::str_detect(species_name," \\(.*")]
flora_taxo[,French_vernacular_name:=ifelse(French_vernacular_name,stringr::str_extract_all(species_name," \\(.*",simplify = T),NA)]
flora_taxo[,French_vernacular_name:=stringr::str_remove_all(French_vernacular_name," \\(")]
flora_taxo[,French_vernacular_name:=stringr::str_remove_all(French_vernacular_name,"\\)")]
flora_taxo[,species_name:=stringr::str_remove_all(species_name," \\(.*")]
flora_taxo[,value:=as.numeric(value)]
NFI_flora<-merge(NFI_flora,flora_taxo[,c("value","species_name")],by.x="cd_ref",by.y="value")
tree_NFI_codetaxo<-data.table(read.table(unz(path_zip,"espar-cdref13.csv"),sep=";",quote="",skip=1,row.names = NULL,encoding = "UTF-8",stringsAsFactors =F))
colnames(tree_NFI_codetaxo)<-c("espar","french_name","cd_ref","species_name")
tree_NFI_codetaxo[,espar:=ifelse(nchar(espar)==1,paste0("0",espar),espar)]
NFI_arbres<-merge(NFI_arbres,tree_NFI_codetaxo[,c("espar","species_name","french_name")],by="espar",all.x = T)
NFI_plot_info<<-NFI_plot_info[order(idp),]
NFI_ecologie<<-NFI_ecologie[order(idp),]
NFI_arbres<<-NFI_arbres[order(idp),]
NFI_dendro<<-NFI_dendro[order(idp),]
NFI_cover<<-NFI_cover[order(idp),]
NFI_flora<<-NFI_flora[order(idp),]
if(write_csv){
write.table(NFI_plot_info,file=file.path(path_data,"plot_info.csv"),sep=";",row.names = F)
write.table(NFI_ecologie,file=file.path(path_data,"ecology.csv"),sep=";",row.names = F)
write.table(NFI_arbres,file=file.path(path_data,"tree.csv"),sep=";",row.names = F)
write.table(NFI_dendro,file=file.path(path_data,"dendro.csv"),sep=";",row.names = F)
write.table(NFI_cover,file=file.path(path_data,"canopy_cover.csv"),sep=";",row.names = F)
write.table(NFI_flora,file=file.path(path_data,"flora.csv"),sep=";",row.names = F)
}
if(export_to_env){
NFI_plot_info<<-NFI_plot_info[order(idp),]
NFI_ecologie<<-NFI_ecologie[order(idp),]
NFI_arbres<<-NFI_arbres[order(idp),]
NFI_dendro<<-NFI_dendro[order(idp),]
NFI_cover<<-NFI_cover[order(idp),]
NFI_flora<<-NFI_flora[order(idp),]
}
return(NULL)
}
get_NFI(visit = 1)
library(FrenchNFIfindeR)
meta_data<-data.table(read.table(unz(file.path(getwd(),"NFI_data","Raw_data","data_NFI.zip"),"metadonnees.csv"),sep=";",skip=17,fill=T,quote="",row.names = NULL,encoding = "UTF-8",stringsAsFactors =F))
dim(meta_data)
colnames(meta_data)<-c("code","value","description","advanced_description")
flora_taxo<-meta_data[code=="CDREF13",]
flora_taxo[,French_vernacular_name:=stringr::str_detect(description," \\(.*")]
flora_taxo[,French_vernacular_name:=ifelse(French_vernacular_name,stringr::str_extract_all(description," \\(.*",simplify = T),NA)]
flora_taxo[,French_vernacular_name:=stringr::str_remove_all(French_vernacular_name," \\(")]
flora_taxo[,French_vernacular_name:=stringr::str_remove_all(French_vernacular_name,"\\)")]
flora_taxo[,description:=stringr::str_remove_all(description," \\(.*")]
flora_taxo[,value=as.numeric(value)]
tree_NFI_codetaxo<-data.table(read.table(unz(file.path(getwd(),"NFI_data","Raw_data","data_NFI.zip"),"espar-cdref13.csv"),sep=";",quote="",skip=1,row.names = NULL,encoding = "UTF-8",stringsAsFactors =F))
colnames(tree_NFI_codetaxo)<-c("espar","french_name","cd_ref","species_name")
tree_NFI_codetaxo[,espar:=ifelse(nchar(espar)==1,paste0("0",espar),espar)]
tree_code
NFI_arbres[,table(veget5)]
usethis::use_package("stringr")
nrow(NFI_arbres)
sum(NFI_arbres$espar%in%meta_data[code=="ESPAR1",]$value)