-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_gcm_bc_by_chunks.R
88 lines (72 loc) · 2.78 KB
/
_gcm_bc_by_chunks.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
# -------------------------------------------------- #
# Climate Risk Profiles -- CMIP6 data: GCM bias-correction performed by Quantile-mapping by chunks
# H. Achicanoy, A. Ghosh & C. Navarro
# Alliance Bioversity-CIAT, 2021
# -------------------------------------------------- #
options(warn = -1, scipen = 999)
suppressMessages(library(pacman))
suppressMessages(pacman::p_load(tidyverse,tidyft))
source('https://raw.githubusercontent.com/CIAT-DAPA/WFP-profiles/main/_main_functions.R') # Main functions
source('https://raw.githubusercontent.com/CIAT-DAPA/WFP-profiles/main/_gcm_bc.R') # GCMs bias-correction
OSys <<- Sys.info()[1]
root <<- switch(OSys,
'Linux' = '/CATALOGUE/Workspace14/WFP_ClimateRiskPr',
'Windows' = '//CATALOGUE/Workspace14/WFP_ClimateRiskPr')
iso <- 'TZA'
model <- 'INM-CM5-0'
period <- '2041-2060'
his_obs <- paste0(root,"/1.Data/observed_data/",iso,"/",iso,".fst")
his_gcm <- paste0(root,"/1.Data/future_data/",model,"/",iso,"/downscale/1995-2014/",iso,".fst")
fut_gcm <- paste0(root,"/1.Data/future_data/",model,"/",iso,"/downscale/",period,"/",iso,".fst")
# Historical climate data (observed)
ho <- his_obs %>%
tidyft::parse_fst() %>%
tidyft::select_fst(id) %>%
tidyft::distinct() %>%
base::as.data.frame()
ho <- ho$id
# Historical GCM climate data (modeled)
hg <- his_gcm %>%
tidyft::parse_fst() %>%
tidyft::select_fst(id) %>%
tidyft::distinct() %>%
base::as.data.frame()
hg <- hg$id
# Furure GCM climate data (modeled)
fg <- fut_gcm %>%
tidyft::parse_fst() %>%
tidyft::select_fst(id) %>%
tidyft::distinct() %>%
base::as.data.frame()
fg <- fg$id
# Indentify pixels intersection
px <- base::intersect(ho, hg)
px <- base::intersect(px, fg)
px <- sort(px); rm(ho, hg, fg)
chnks <- chunk(px, 4000)
1:length(chnks) %>%
purrr::map(.f = function(j){
pft <<- chnks[[j]]
his_obs <- his_obs %>%
tidyft::parse_fst(path = .) %>%
tidyft::filter_fst(id %in% pft) %>%
base::as.data.frame()
his_gcm <- his_gcm %>%
tidyft::parse_fst(path = .) %>%
tidyft::filter_fst(id %in% pft) %>%
base::as.data.frame()
fut_gcm <- fut_gcm %>%
tidyft::parse_fst(path = .) %>%
tidyft::filter_fst(id %in% pft) %>%
base::as.data.frame()
his_bc <- paste0(root,"/1.Data/future_data/",model,"/",iso,"/bias_corrected/1995-2014/",iso,"_chunk_",j,".fst")
fut_bc <- paste0(root,"/1.Data/future_data/",model,"/",iso,"/bias_corrected/",period,"/",iso,"_chunk_",j,".fst")
BC_Qmap_lnx(his_obs = his_obs,
his_gcm = his_gcm,
fut_gcm = fut_gcm,
his_bc = his_bc,
fut_bc = fut_bc,
period = period,
ncores = 1)
return(cat(paste0('Chunk ',j,' finished\n')))
})