-
Notifications
You must be signed in to change notification settings - Fork 1
/
1b_1_getCMIP6.R
106 lines (85 loc) · 3.43 KB
/
1b_1_getCMIP6.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
# -------------------------------------------------- #
# Climate Risk Profiles -- CMIP6 data search
# A. Ghosh
# -------------------------------------------------- #
source("1b_0_search_CMIP6_functions.R")
vars <- c("pr","tas","tasmax","tasmin")
# models <- c("BCC-CSM2-MR","CESM2","INM-CM5-0","MPI-ESM1-2-HR","MRI-ESM2-0")
models <- c("ACCESS-ESM1-5","EC-Earth3-Veg","INM-CM5-0","MPI-ESM1-2-HR","MRI-ESM2-0")
varmod <- expand.grid(vars, models)
names(varmod) <- c("vars", "models")
############################################################################################
# historical
dh <- list()
for (i in 1:nrow(varmod)){
var <- varmod$vars[i]
model <- varmod$models[i]
dh[[i]] <- try(getMetaCMIP6(offset = 0,
limit = 10000,
activity_id="CMIP",
experiment_id = "historical",
frequency = "day",
member_id = "r1i1p1f1",
variable_id = var,
source_id = model,
mip_era = "CMIP6"))
}
# remove any unsuccessful attempts
dhc <- lapply(dh, function(x){if(inherits(x, "data.table")){return(x)}else{NULL}})
dhist <- data.table::rbindlist(dhc, fill = TRUE)
####################################################################################
# future
df <- list()
for (i in 1:nrow(varmod)){
var <- varmod$vars[i]
model <- varmod$models[i]
df[[i]] <- try(getMetaCMIP6(offset = 0,
limit = 10000,
activity_id="ScenarioMIP",
experiment_id = "ssp585",
member_id = "r1i1p1f1",
frequency = "day",
variable_id = var,
source_id = model,
mip_era = "CMIP6"))
}
# remove any unsuccessful attempts
dfc <- lapply(df, function(x){if(inherits(x, "data.table")){return(x)}else{NULL}})
dfut <- data.table::rbindlist(dfc, fill = TRUE)
# combine both results
dd <- rbind(dhist, dfut)
data.table::fwrite(dd, paste0("data/cmip6_filter_index_", Sys.Date(), ".csv"), row.names = FALSE)
############################################################################################
# now download
options(timeout=3600)
# downloader function
getDataCMIP6 <- function(i, idx, downdir, silent=FALSE){
d <- idx[i,]
# print something
if (silent) print(d$file_url); flush.console()
# specify where to save
# fstr <- strsplit(d$file_url, "/CMIP6/|/cmip6/")[[1]][2]
flocal <- file.path(downdir, basename(d$file_url))
d$localfile <- flocal
# where to save
dir.create(dirname(flocal), FALSE, TRUE)
# should we download?
if (!file.exists(flocal)){
# try downloading
try(download.file(d$file_url, flocal, mode = "wb", quiet=silent))
}
return(NULL)
}
##########################################################################################
# change the data directory as needed
downdir <- "~/data/input/climate/CMIP6/daily"
idx <- read.csv("data/cmip6_filter_index_2021-03-24.csv", stringsAsFactors = FALSE)
downloadParallel <- FALSE
if (downloadParallel){
library(future.apply)
plan(multiprocess, workers = 12)
future_lapply(1:nrow(idx), getDataCMIP6, idx, downdir, silent=FALSE, future.seed = TRUE)
} else {
# download files
lapply(1:nrow(idx), getDataCMIP6, idx, downdir, silent=FALSE)
}