-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgather_isimip_data.Rmd
371 lines (269 loc) · 9.85 KB
/
gather_isimip_data.Rmd
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
---
title: "ISIMIP2a"
author: "Johannes Piipponen"
date: "9 12 2021"
output: html_document
---
# Introduction
In this file we gather ISIMIP2a modelled NPP data. See Appendix S2.10
Ref:
Reyer, C., Asrar, G., Betts, R., Chang, J., Chen, M., Ciais, P., Dury, M., François, L., Henrot, A.-J., Hickler, T., Ito, A., Jacquemin, I., Nishina, K., Mishurov, M., Morfopoulos, C., Munhoven, G., Ostberg, S., Pan, S., Rafique, R., … Büchner, M. (2019). ISIMIP2a Simulation Data from Biomes Sector (V. 1.1). https://doi.org/10.5880/PIK.2019.005
```{r include = FALSE}
library(tidyverse); library(raster); library(gdalUtils); library(scico);
library(sf); library(terra); library(data.table); library(here); library(tictoc)
# Create valid PROJ4 string for EPSG 4326
epsg4326 <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
# extents for templaterasters
e <- extent(-180,180,-90,90)
# timestep isimi
timestep_isimip <- 2000:2010
# options, mainly for terra package
terraOptions(tempdir= here("Temp_R"))
terraOptions()
rasterOptions(tmpdir= here("Temp_R"))
rasterOptions()
wopt_options <- list(gdal = c("BIGTIFF=YES",
"PREDICTOR = 2",
"NUM_THREADS = ALL_CPUS"))
data_for_simulations <- here("Data", "Output", "data_for_simulations.tif") %>%
rast()
data_for_simulations <- data_for_simulations$npp2015
months <- rep(seq(0,10), each = 12)
```
# get and modify data 1 (caraib)
Note that watch_nobc scenario has only years 1971-2001 so we won't use that
princeton_nobc in for 1971-2012 but we use it only until 2010
"Unit originally C m-2 s-1, and it indicates the average npp in kg per m2 per second of that month. So if you want to have annual total npp in g C m-2, you will need to average(12 monthly npp values) * 1000 g per kg * 86400 second per day * 365 days = g C m-2, where “average(12 monthly npp values) “ is the annual mean npp in kg C m-2 s-1. "
! We want the unit g C m2 yr !
```{r}
rastlist_isimip_caraib <-
list.files(path = here("Data", "Supplementary", "ISIMIP"),
pattern= paste0("caraib"),
full.names = TRUE)
rastlist_isimip_caraib
#conver to raster
caraib1 <- rastlist_isimip_caraib[1] %>% rast() # gswp3
caraib2 <- rastlist_isimip_caraib[2] %>% rast() # princeton
caraib3 <- rastlist_isimip_caraib[3] %>% rast() # watch-wfdei
# select only months that represent years 2000-2010
caraib1 <- subset(caraib1, c(349:480)) # only years 2000-2010
caraib2 <- subset(caraib2, c(349:480))
caraib3 <- subset(caraib3, c(349:480))
# 1
# yearly averages (multiply with seconds and days)
r <- stack(caraib1 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
caraib1_yr <- rast(all_years)
names(caraib1_yr) <- paste0("caraib1_", timestep_isimip)
# 2
r <- stack(caraib2 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
caraib2_yr <- rast(all_years)
names(caraib2_yr) <- paste0("caraib2_", timestep_isimip)
# 3
r <- stack(caraib3 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
caraib3_yr <- rast(all_years)
names(caraib3_yr) <- paste0("caraib3_", timestep_isimip)
# combine rasters
caraib_all <- c(caraib1_yr, caraib2_yr, caraib3_yr)
names(caraib_all)
plot(caraib_all)
# disaggregate and mask when all ISIMIP rasters combined
```
# get and modify data 2 (lpjml)
```{r}
rastlist_isimip_lpjml <-
list.files(path = here("Data", "Supplementary", "ISIMIP"),
pattern= paste0("lpjml"),
full.names = TRUE)
rastlist_isimip_lpjml # can use only 1-3
#conver to raster
lpjml1 <- rastlist_isimip_lpjml[1] %>% rast() # gswp3
lpjml2 <- rastlist_isimip_lpjml[2] %>% rast() # princeton
lpjml3 <- rastlist_isimip_lpjml[3] %>% rast() # watch-wfdei
# select only months that represent years 2000-2010
lpjml1 <- subset(lpjml1, c(349:480)) # only years 2000-2010
lpjml2 <- subset(lpjml2, c(349:480))
lpjml3 <- subset(lpjml3, c(349:480))
# same things than above
# 1
r <- stack(lpjml1 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
lpjml1_yr <- rast(all_years)
names(lpjml1_yr) <- paste0("lpjml1_", timestep_isimip)
# 2
r <- stack(lpjml2 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
lpjml2_yr <- rast(all_years)
names(lpjml2_yr) <- paste0("lpjml2_", timestep_isimip)
# 3
r <- stack(lpjml3 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
lpjml3_yr <- rast(all_years)
names(lpjml3_yr) <- paste0("lpjml3_", timestep_isimip)
# combine rasters
lpjml_all <- c(lpjml1_yr, lpjml2_yr, lpjml3_yr)
names(lpjml_all)
plot(lpjml_all)
```
# get and modify data 3 (dlem)
```{r}
rastlist_isimip_dlem <-
list.files(path = here("Data", "Supplementary", "ISIMIP"),
pattern= paste0("dlem"),
full.names = TRUE)
rastlist_isimip_dlem
dlem1 <- rastlist_isimip_dlem[1] %>% rast()
dlem2 <- rastlist_isimip_dlem[2] %>% rast()
dlem3 <- rastlist_isimip_dlem[3] %>% rast()
# select only months that represent years 2000-2010
dlem1 <- subset(dlem1, c(349:480)) # only years 2000-2010
dlem2 <- subset(dlem2, c(349:480))
dlem3 <- subset(dlem3, c(349:480))
# same things than above
# 1
r <- stack(dlem1 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
dlem1_yr <- rast(all_years)
names(dlem1_yr) <- paste0("dlem1_", timestep_isimip)
# dlem1_dis_cropmask <- disaggregate(dlem1_yr, fact = 6) %>%
# crop(., ab_5y_med ) %>%
# mask(., ab_5y_med )
# 2
r <- stack(dlem2 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
dlem2_yr <- rast(all_years)
names(dlem2_yr) <- paste0("dlem2_", timestep_isimip)
# dlem2_dis_cropmask <- disaggregate(dlem2_yr, fact = 6) %>%
# crop(., ab_5y_med ) %>%
# mask(., ab_5y_med )
# 3
r <- stack(dlem3 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
dlem3_yr <- rast(all_years)
names(dlem3_yr) <- paste0("dlem3_", timestep_isimip)
# dlem3_dis_cropmask <- disaggregate(dlem3_yr, fact = 6) %>%
# crop(., ab_5y_med ) %>%
# mask(., ab_5y_med )
# combine rasters
dlem_all <- c(dlem1_yr, dlem2_yr, dlem3_yr)
names(dlem_all)
plot(dlem_all)
```
# get and modify data 4 (orchidee)
```{r}
rastlist_isimip_orchidee <-
list.files(path = here("Data", "Supplementary", "ISIMIP"),
pattern= paste0("orchidee"),
full.names = TRUE)
rastlist_isimip_orchidee
orchidee1 <- rastlist_isimip_orchidee[1] %>% rast()
orchidee2 <- rastlist_isimip_orchidee[2] %>% rast()
orchidee3 <- rastlist_isimip_orchidee[3] %>% rast()
# select only months that represent years 2000-2010
orchidee1 <- subset(orchidee1, c(349:480))
orchidee2 <- subset(orchidee2, c(349:480))
orchidee3 <- subset(orchidee3, c(349:480))
# same things than above
# 1
r <- stack(orchidee1 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
orchidee1_yr <- rast(all_years)
names(orchidee1_yr) <- paste0("orchidee1_", timestep_isimip)
# 2
r <- stack(orchidee2 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
orchidee2_yr <- rast(all_years)
names(orchidee2_yr) <- paste0("orchidee2_", timestep_isimip)
# 3
r <- stack(orchidee3 * 365 * 86400)
all_years <- stack()
for (n in 0:10) {
index <- which(months == n)
annual_mean <- mean(r[[index]], na.rm =T)
all_years <- stack(all_years, annual_mean)}
orchidee3_yr <- rast(all_years)
names(orchidee3_yr) <- paste0("orchidee3_", timestep_isimip)
# combine rasters
orchidee_all <- c(orchidee1_yr, orchidee2_yr, orchidee3_yr)
names(orchidee_all)
plot(orchidee_all)
```
# Combine all rasters, disaggregate and mask
132 layers = 11 years * 3 climate forcing * 4 models
```{r}
isimip_all_kg_C_m2 <- c(caraib_all, lpjml_all, dlem_all, orchidee_all)
names(isimip_all)
# convert to kg C km2 and remove values smaller than 1
isimip_all_kg_C_km2 <- isimip_all_kg_C_m2 * 1e6
isimip_all_kg_C_km2[isimip_all_kg_C_km2 < 1] <- NA
summary(isimip_all_kg_C_km2)
isimip_all_disag_cropmask <- disaggregate(isimip_all_kg_C_km2, fact = 6) %>%
crop(., data_for_simulations) %>%
mask(., data_for_simulations)
# save as csv
isimip_all_disag_cropmask_df <-
as.data.frame(isimip_all_disag_cropmask, xy = T) %>%
as.data.table()
fwrite(isimip_all_disag_cropmask_df,
here("Data", "Supplementary", "isimip_npp_kgC_km2_yr.csv"))
# select only year 2000
isimip_2000 <- terra::subset(isimip_all_disag_cropmask,
grep('_2000', names(isimip_all),
value = T))
isimip_2000 %>% names()
isimip_2000 # here we find extreme range for uncertainty assessment
isimip_2000 %>% plot()
# find extremes for each cell- Probably best to convert to df
isimip_2000_df <- as.data.frame(isimip_2000, xy = TRUE)
isimip_2000_vars <- isimip_2000_df %>%
dplyr::select(contains("2000"))
isimip_range_2000 <- isimip_2000_vars %>%
mutate(min2000 =rowMins(as.matrix(isimip_2000_vars), value = TRUE),
max2000 = rowMaxs(as.matrix(isimip_2000_vars), value = TRUE))
```