-
Notifications
You must be signed in to change notification settings - Fork 1
/
1a_getERA5.R
156 lines (127 loc) · 5.1 KB
/
1a_getERA5.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
# CDS dataset description
# https://cds.climate.copernicus.eu/cdsapp#!/dataset/sis-agrometeorological-indicators?tab=overview
# Has a number of dependencies, working on alternatives
# devtools::install_github("bluegreen-labs/ecmwfr")
# install.packages("ecmwfr")
library("ecmwfr")
# save key for CDS
# use the credentials I shared
wf_set_key(user = UID,
key = key,
service = "cds")
getERA5 <- function(i, qq, year, month, datadir){
q <- qq[i,]
format <- "zip"
ofile <- paste0(paste(q$variable, q$statistics, year, month, sep = "-"), ".",format)
if(!file.exists(file.path(datadir,ofile))){
ndays <- lubridate::days_in_month(as.Date(paste0(year, "-" ,month, "-01")))
ndays <- 1:ndays
ndays <- sapply(ndays, function(x) ifelse(length(x) == 1, sprintf("%02d", x), x))
ndays <- dput(as.character(ndays))
cat("Downloading", q[!is.na(q)], "for", year, month, "\n"); flush.console();
request <- list("dataset_short_name" = "sis-agrometeorological-indicators",
"variable" = q$variable,
"statistic" = q$statistics,
"year" = year,
"month" = month,
"day" = ndays,
"area" = "90/-180/-90/179.9", # download global #c(ymax,xmin,ymin,xmax)?
"time" = q$time,
"format" = format,
"target" = ofile)
request <- Filter(Negate(anyNA), request)
file <- wf_request(user = "76741", # user ID (for authentification)
request = request, # the request
transfer = TRUE, # download the file
path = datadir)
} else {
cat("Already exists", q[!is.na(q)], "for", year, month, "\n"); flush.console();
}
return(NULL)
}
########################################################################################################
# TODO: change data directory
# datadir <- "~/data/input/climate/era5/"
datadir <- "/cluster01/workspace/ONECGIAR/Data/climate/era5/sis-agromet/zip"
# datadir <- "~/data/input/climate/AgERA5/"
dir.create(datadir, FALSE, TRUE)
# combinations to download
qq <- data.frame(variable = c("precipitation_flux","solar_radiation_flux",rep("2m_temperature",3),
"10m_wind_speed", "2m_relative_humidity"),
statistics = c(NA, NA, "24_hour_maximum", "24_hour_mean", "24_hour_minimum",
"24_hour_mean", NA),
time = c(NA,NA,NA,NA,NA,NA, "12_00"))
# temporal range
years <- as.character(1979:2020)
months <- c(paste0("0", 1:9), 10:12)
# all download
for (i in 1:nrow(qq)){
for (year in years){
for (month in months){
tryCatch(getERA5(i, qq, year, month, datadir), error = function(e)NULL)
}
}
}
for (year in as.character(1999:2001)){
for (month in months){
getERA5(i=5, qq, year, month, datadir)
}
}
# unzip example
# unzip(file.path(datadir, ofile), exdir = datadir)
datadir <- "/cluster01/workspace/ONECGIAR/Data/climate/era5/sis-agromet/zip"
zz <- list.files(datadir, ".zip$", full.names = TRUE)
vars <- c("precipitation_flux","solar_radiation_flux", "2m_temperature","10m_wind_speed", "2m_relative_humidity")
extractNC <- function(var, zz, datadir, ncores = 10){
z <- grep(var, zz, value = TRUE)
fdir <- file.path(dirname(datadir), "nc", var)
dir.create(fdir, showWarnings = FALSE, recursive = TRUE)
parallel::mclapply(z, function(x){unzip(x, exdir = fdir)}, mc.cores = ncores, mc.preschedule = FALSE)
return(NULL)
}
for (var in vars){
extractNC(var, zz, datadir, ncores = 40)
}
# check download status
eraDownload <- function(i, qq, ff){
var <- paste0(qq$variable[i], "-", qq$statistics[i])
f <- grep(var, ff, val = T)
yr <- sapply(strsplit(f, "-"), "[[", 3)
return(list(var, table(yr)))
}
ff <- list.files(datadir, pattern=".zip$")
lapply(1:nrow(qq), eraDownload, qq, ff)
############################################################################################
# unsuccessful attempts
# IMP: install csdapi
# Linux: https://cds.climate.copernicus.eu/api-how-to#install-the-cds-api-key
# Windows: https://confluence.ecmwf.int/display/CKB/How+to+install+and+use+CDS+API+on+Windows
# install.packages("reticulate")
# library(reticulate)
#
# #install the python ECMWF API
# # py_install("cdsapi")
#
# #import python library cdsapi
# cdsapi <- import('cdsapi')
#
# c <- cdsapi$Client()
#
# dataset <- 'sis-agrometeorological-indicators'
# request <- "{
# 'format': 'zip',
# 'variable': '2m_temperature',
# 'statistics': '24_hour_maximum',
# 'year': '1979',
# 'month': '01',
# }"
#
# result <- c$retrieve(dataset, request, 'download.zip')
# Previous attempt to get ERA5 data from Google Earth Engine
# Requirement:
# use daily data as it is, convert hourly data to daily data
# export for each country to Google Cloud Storage (GCS)/ Google Drive
# copy from cloud to \\dapadfs.cgiarad.org\workspace_cluster_13\WFP_ClimateRiskPr (???)
# Earth Engine link to export ERA5 data
# https://code.earthengine.google.com/a48864fc4198e1e8a79a70750f0f1525
# add clean and load