-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrscript_get_data.R
227 lines (209 loc) · 8.48 KB
/
rscript_get_data.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
library(dplyr)
library(rbeni)
library(tidyr)
library(purrr)
library(ncdf4)
library(lubridate)
library(ggplot2)
library(readr)
library(rsofun)
library(ingestr)
library(stringr)
source("R/get_data_mct_global.R")
source("R/extract_points_filelist.R")
source("R/convert_et.R")
source("R/align_events.R")
##------------------------------------------------------------------------
## Adjust site set by hand ("sj02" or "fluxnet")
##------------------------------------------------------------------------
siteset <- "sj02"
##------------------------------------------------------------------------
## get site meta info
##------------------------------------------------------------------------
if (siteset == "sj02"){
## Get meta info of sites (lon, lat)
siteinfo <- read_csv("~/data/rootingdepth/root_profiles_schenkjackson02/data/root_profiles_D50D95.csv") %>%
dplyr::filter(Wetland == "N" & Anthropogenic == "N" & Schenk_Jackson_2002 == "YES") %>%
dplyr::rename(sitename = ID, lat = Latitude, lon = Longitude) %>%
dplyr::mutate(elv = ifelse(elv==-999, NA, elv)) %>%
dplyr::filter(lon!=-999 & lat!=-999) %>%
dplyr::mutate(year_start = 1982, year_end = 2011) %>%
dplyr::select(sitename, lon, lat, elv, year_start, year_end)
} else {
siteinfo <- read_csv("~/data/FLUXNET-2015_Tier1/siteinfo_fluxnet2015_sofun+whc.csv") %>%
rename(sitename = mysitename) %>%
filter(!(classid %in% c("CRO", "WET"))) %>%
#filter(year_start<=2007) %>% # xxx USE ONLY FOR LANDEVAL xxx
mutate(date_start = lubridate::ymd(paste0(year_start, "-01-01"))) %>%
mutate(date_end = lubridate::ymd(paste0(year_end, "-12-31")))
}
df_grid <- siteinfo %>%
dplyr::select(sitename, lon, lat, elv) %>%
dplyr::rename(idx = sitename)
##------------------------------------------------------------------------
## Get data from global fields (WATCH-WFDEI and LandFlux)
##------------------------------------------------------------------------
## df_grid must contain columns lon, lat, elv, and idx
# ## PT-JPL
# filn <- paste0("data/df_pt_jpl_", siteset, ".Rdata")
# filn_csv <- str_replace(filn, "Rdata", "csv")
# if (!file.exists(filn)){
# if (!file.exists(filn_csv)){
# df_pt_jpl <- get_data_mct_global(
# df_grid,
# dir_et = "~/data/landflux/et_prod/", fil_et_pattern = "ET_PT-SRB-PU_daily_",
# dir_prec = "~/data/watch_wfdei/Rainf_daily/", fil_prec_pattern = "Rainf_daily_WFDEI_CRU",
# dir_snow = "~/data/watch_wfdei/Snowf_daily/", fil_snow_pattern = "Snowf_daily_WFDEI_CRU",
# dir_temp = "~/data/watch_wfdei/Tair_daily/", fil_temp_pattern = "Tair_daily_WFDEI",
# get_watch = TRUE, get_landeval = TRUE, get_alexi = FALSE,
# year_start_watch = 1984, year_end_watch = 2007
# )
# save(df_pt_jpl, file = filn)
# df_pt_jpl %>%
# tidyr::unnest(df) %>%
# write_csv(path = filn_csv)
# } else {
# df_pt_jpl <- read_csv(file = filn_csv) %>%
# group_by(idx, lon, lat) %>%
# tidyr::nest() %>%
# dplyr::mutate(data = purrr::map(data, ~as_tibble(.))) %>%
# dplyr::rename(df = data)
# }
# } else {
# load(filn)
# df_pt_jpl %>%
# tidyr::unnest(df) %>%
# write_csv(path = paste0("data/df_pt_jpl_", siteset, ".csv"))
# }
# ## PM
# filn <- paste0("data/df_pm_mod_", siteset, ".Rdata")
# filn_csv <- str_replace(filn, "Rdata", "csv")
# if (!file.exists(filn)){
# if (!file.exists(filn_csv)){
# df_pm_mod <- get_data_mct_global(
# df_grid,
# dir_et = "~/data/landflux/et_prod/", fil_et_pattern = "ET_PM-SRB-PU_daily_",
# dir_prec = "~/data/watch_wfdei/Rainf_daily/", fil_prec_pattern = "Rainf_daily_WFDEI_CRU",
# dir_snow = "~/data/watch_wfdei/Snowf_daily/", fil_snow_pattern = "Snowf_daily_WFDEI_CRU",
# dir_temp = "~/data/watch_wfdei/Tair_daily/", fil_temp_pattern = "Tair_daily_WFDEI",
# get_watch = TRUE, get_landeval = TRUE, get_alexi = FALSE,
# year_start_watch = 1984, year_end_watch = 2007
# )
# save(df_pm_mod, file = filn)
# df_pm_mod %>%
# tidyr::unnest(df) %>%
# write_csv(path = filn_csv)
# } else {
# df_pm_mod <- read_csv(file = filn_csv) %>%
# group_by(idx, lon, lat) %>%
# tidyr::nest() %>%
# dplyr::mutate(data = purrr::map(data, ~as_tibble(.))) %>%
# dplyr::rename(df = data)
# }
# } else {
# load(filn)
# df_pm_mod %>%
# tidyr::unnest(df) %>%
# write_csv(path = paste0("data/df_pm_mod_", siteset, ".csv"))
# }
# ## SEBS
# filn <- paste0("data/df_sebs_", siteset, ".Rdata")
# filn_csv <- str_replace(filn, "Rdata", "csv")
# if (!file.exists(filn)){
# if (!file.exists(filn_csv)){
# df_sebs <- get_data_mct_global(
# df_grid,
# dir_et = "~/data/landflux/et_prod/", fil_et_pattern = "ET_SEBS-SRB-PU_daily_",
# dir_prec = "~/data/watch_wfdei/Rainf_daily/", fil_prec_pattern = "Rainf_daily_WFDEI_CRU",
# dir_snow = "~/data/watch_wfdei/Snowf_daily/", fil_snow_pattern = "Snowf_daily_WFDEI_CRU",
# dir_temp = "~/data/watch_wfdei/Tair_daily/", fil_temp_pattern = "Tair_daily_WFDEI",
# get_watch = TRUE, get_landeval = TRUE, get_alexi = FALSE,
# year_start_watch = 1984, year_end_watch = 2007
# )
# save(df_sebs, file = filn)
# df_sebs %>%
# tidyr::unnest(df) %>%
# write_csv(path = filn_csv)
# } else {
# df_sebs <- read_csv(file = filn_csv) %>%
# group_by(idx, lon, lat) %>%
# tidyr::nest() %>%
# dplyr::mutate(data = purrr::map(data, ~as_tibble(.)))%>%
# dplyr::rename(df = data)
# }
# } else {
# load(filn)
# df_sebs %>%
# tidyr::unnest(df) %>%
# write_csv(path = paste0("data/df_sebs_", siteset, ".csv"))
# }
# ##------------------------------------------------------------------------
# ## WATCH-WFDEI and ALEXI
# ##------------------------------------------------------------------------
# filn <- paste0("data/df_alexi_", siteset, ".Rdata")
# filn_csv <- str_replace(filn, "Rdata", "csv")
# if (!file.exists(filn)){
# if (!file.exists(filn_csv)){
# df_alexi <- get_data_mct_global(
# df_grid,
# dir_et = "~/data/alexi_tir/netcdf/", fil_et_pattern = "EDAY_CERES_",
# dir_prec = "~/data/watch_wfdei/Rainf_daily/", fil_prec_pattern = "Rainf_daily_WFDEI_CRU",
# dir_snow = "~/data/watch_wfdei/Snowf_daily/", fil_snow_pattern = "Snowf_daily_WFDEI_CRU",
# dir_temp = "~/data/watch_wfdei/Tair_daily/", fil_temp_pattern = "Tair_daily_WFDEI",
# get_watch = TRUE, get_landeval = FALSE, get_alexi = TRUE,
# year_start_watch = 2003, year_end_watch = 2018
# )
# save(df_alexi, file = filn)
# df_alexi %>%
# tidyr::unnest(df) %>%
# write_csv(path = filn_csv)
# } else {
# df_alexi <- read_csv(file = filn_csv) %>%
# group_by(idx, lon, lat) %>%
# tidyr::nest() %>%
# dplyr::mutate(data = purrr::map(data, ~as_tibble(.))) %>%
# dplyr::rename(df = data)
# }
# } else {
# load(filn)
# df_alexi %>%
# tidyr::unnest(df) %>%
# write_csv(path = paste0("data/df_alexi_", siteset, ".csv"))
# }
##------------------------------------------------------------------------
## SiF-downscaled from Duveiller
##------------------------------------------------------------------------
filn <- paste0("data/df_sif_", siteset, ".Rdata")
filn_csv <- str_replace(filn, "Rdata", "csv")
if (!file.exists(filn)){
if (!file.exists(filn_csv)){
df_sif <- get_data_mct_global(
df_grid,
dir_et = "~/data/sif_tir/netcdf/", fil_et_pattern = "EDAY_CERES_",
dir_prec = "~/data/watch_wfdei/Rainf_daily/", fil_prec_pattern = "Rainf_daily_WFDEI_CRU",
dir_snow = "~/data/watch_wfdei/Snowf_daily/", fil_snow_pattern = "Snowf_daily_WFDEI_CRU",
dir_temp = "~/data/watch_wfdei/Tair_daily/", fil_temp_pattern = "Tair_daily_WFDEI",
dir_sif = "~/data/gome_2_sif_downscaled/data_orig/", fil_sif_pattern = "GOME_JJ_dcSIF_005deg_8day_",
get_watch = FALSE, get_landeval = FALSE, get_alexi = FALSE, get_sif = TRUE
)
save(df_sif, file = filn)
df_sif %>%
tidyr::unnest(df) %>%
write_csv(path = filn_csv)
} else {
df_sif <- read_csv(file = filn_csv) %>%
group_by(idx, lon, lat) %>%
tidyr::nest() %>%
dplyr::mutate(data = purrr::map(data, ~as_tibble(.))) %>%
dplyr::rename(df = data)
}
} else {
load(filn)
df_sif %>%
tidyr::unnest(df) %>%
write_csv(path = paste0("data/df_sif_", siteset, ".csv"))
}
# ## compare
# ggplot() +
# geom_line(data = df_pt_jpl$df[[29]], aes(x = date, y = et_mm)) +
# geom_line(data = df_alexi$df[[70]], aes(x = date, y = et_mm), col = 'red')