-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclimate_api.R
More file actions
370 lines (315 loc) · 10.7 KB
/
Copy pathclimate_api.R
File metadata and controls
370 lines (315 loc) · 10.7 KB
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
# Function to download climate data from WB AWS S3-----
get_climate_data <- function(collection,
variable_code,
product = "climatology",
scenario = NULL,
aggregation = "annual",
time_period = NULL,
product_type = "climatology",
model = NULL,
percentile = NULL,
statistic = "mean") {
# Construct file path following CCKP structure
model_scenario <- paste0(model, "-", scenario)
if(collection == "era5-x0.25") {
collection_id <- paste0(collection, "-historical")
filename <- paste0(
paste(product_type, variable_code, aggregation, statistic, sep="-"), "_",
collection, "_",
collection_id, "_",
product_type, "_",
statistic, "_",
time_period, ".nc"
)
url <- paste0(
"https://wbg-cckp.s3.amazonaws.com/data/",
collection, "/",
variable_code, "/",
collection_id, "/",
filename
)
} else if (collection == "cmip6-x0.25") {
filename <- paste0(
paste(product, variable_code, aggregation, statistic, sep="-"),
"_", collection, "_",
paste0(model, "-", scenario), "_",
product_type, "_", percentile, "_",
time_period, ".nc"
)
url <- paste0(
"https://wbg-cckp.s3.amazonaws.com/data/",
collection, "/",
variable_code, "/",
paste0(model, "-", scenario), "/",
filename
)
} else if(collection == "pop-x0.25") {
collection_id <- paste0("gpw-v4-rev11-", scenario)
filename <- paste0(
paste(product_type, variable_code, aggregation, "mean", sep="-"),
"_", collection, "_",
collection_id, "_",
product_type, "_",
"mean", "_",
time_period, ".nc"
)
url <- paste0(
"https://wbg-cckp.s3.amazonaws.com/data/",
collection, "/",
variable_code, "/",
collection_id, "/",
filename
)
} else (message("ERROR: not a valid collection"))
print(url)
local_file <- file.path(here::here("Data", "raw_climate"), filename)
head_response <- httr::HEAD(url)
expected_size <- httr::headers(head_response)$`content-length`
response <- httr::GET(
url,
httr::write_disk(local_file, overwrite = TRUE),
httr::progress()
)
downloaded_size <- httr::headers(response)$`content-length`
actual_size <- file.size(local_file)
if(httr::status_code(response) != 200) {
stop(paste("Failed to download file:", httr::http_status(response)$message))
}
if (!is.null(expected_size) && !is.null(downloaded_size)) {
if (expected_size != downloaded_size) {
warning("Warning: Downloaded size differs from expected size")
} else {
message("File sizes match as expected")
}
}
gc()
return(local_file)
}
# Function to batch process climate data in parallel from AWS S3 -----
get_climate_data_batch_parallel <- function(collection,
variables,
scenarios = NULL,
product = "climatology",
aggregation = "annual",
time_period = NULL,
product_type = "climatology",
model = "ensemble-all",
percentile = "median",
statistic = "mean",
chunk_size = 3) {
if(collection == "era5-x0.25") {
scenarios <- "historical_era5"
time_period <- time_period %||% "1991-2020"
} else if (collection == "cmip6-x0.25") {
if (product == "climatology") {
scenarios <- "historical"
time_period <- time_period %||% "1995-2014"
} else {
scenarios <- scenarios %||% c("ssp245", "ssp585")
time_period <- time_period %||% "2040-2059"
}
} else {
stop("Unknown collection type: ", collection)
}
jobs <- expand.grid(variable = variables,
scenario = scenarios,
stringsAsFactors = FALSE
)
# Split variables into chunks
job_chunks <- split(jobs, ceiling(seq_along(1:nrow(jobs))/chunk_size))
# Set up parallel processing
future::plan(future::multisession
, workers = parallel::detectCores() - 2
)
on.exit(future::plan(future::sequential))
# Progress tracking
total_chunks <- length(job_chunks)
message("\nProcessing ", length(variables), " variables in "
, total_chunks, " chunks")
future_map(seq_len(nrow(jobs)),
function(i) {
tryCatch({
get_climate_data(
collection = collection,
variable_code = jobs$variable[i],
scenario = jobs$scenario[i],
product = product,
aggregation = aggregation,
time_period = time_period,
product_type = product_type,
model = model,
percentile = percentile,
statistic = statistic
)
}, error = function(e) {
message(sprintf("Failed: %s-%s: %s",
jobs$variable[i], jobs$scenario[i], e$message))
})
},
.options = furrr::furrr_options(seed = TRUE),
.progress = TRUE
)
gc()
# List downloaded files
pattern <- if(grepl("era5", collection)) {
sprintf("^.*era5-x0.25.*\\.nc$")
} else if(grepl("cmip6", collection)) {
sprintf("^.*cmip6-x0.25.*\\.nc$")
}
nc_files <- list.files(here("Data", "raw_climate"),
pattern = pattern,
full.names = TRUE)
# Create output file with same dimensions
outfile <- here::here("Output", paste("climate_data",
collection,
paste(scenarios, collapse="_"),
product_type,
time_period,
if(length(variables) == 1) variables[1],
"combined.tif",
sep="_"))
# Create a combined raster
# combined_nc <- c(rast(nc_files))
# nc_list <- lapply(nc_files, rast)
# combined_nc <- do.call(c, nc_list)
# Process each file to get variable names and create raster
rast_list <- lapply(nc_files, function(f) {
# Get variable name from metadata
meta <- nc_vars(f)
var_name <- meta$name[4]
# Create raster
r <- rast(f)
# Get times
time_vals <- terra::time(r)
# Name layers using variable and times
names(r) <- paste(var_name,
format(time_vals, "%Y"),
sep = "_")
return(r)
})
# Combine rasters
combined_nc <- do.call(c, rast_list)
# # Write single file
# terra::writeCDF(combined_nc
# , outfile
# , split = TRUE
# , overwrite = TRUE
# )
terra::writeRaster(combined_nc
, outfile
, overwrite = TRUE
)
# Cleanup
unlink(nc_files)
gc()
message("Created output file: ", outfile)
return(combined_nc)
}
# # Write to single NetCDF
# merged_nc <- terra::writeCDF(r, outfile)
#
# # Get back metadata from original
# nc <- nc_open(nc_files[1])
# r <- terra::rast(nc_files)
#
# # Read all files as SpatRaster, but looses some metadat attributes
# terra::writeCDF(r, outfile)
# nc_close(nc)
# # Open first file and get variable directly
# template_nc <- nc_open(nc_files[1])
# dims <- template_nc$dim
#
# # Define variables with dimensions and attributes
# var_defs <- list()
# for(f in nc_files) {
# src <- nc_open(f)
# var_name <- names(src$var)[1]
# var_defs[[var_name]] <- ncvar_def(var_name,
# src$var[[1]]$units,
# dims)
# nc_close(src)
# }
#
# nc_close(template_nc)
#
# # Add global attributes
# londim <- ncdim_def("lon", "degrees_east", lon)
# latdim <- ncdim_def("lat", "degrees_north", lat)
# timedim <- ncdim_def("time", tunits, time, unlim = TRUE)
#
# # Explicitly place unlimited dimension last
# var_defs <- lapply(nc_files, function(f) {
# src <- nc_open(f)
# var_name <- names(src$var)[1]
# var_def <- ncvar_def(var_name,
# src$var[[1]]$units,
# list(timedim,londim, latdim)) # Consistent order
# nc_close(src)
# return(var_def)
# })
#
# # Create file
# merged_nc <- nc_create(outfile, var_defs)
#
# # Copy attributes
# for(f in nc_files) {
# src <- nc_open(f)
#
# var_name <- names(src$var)[1]
# ncvar_put(merged_nc, var_name, ncvar_get(src))
# var_atts <- ncatt_get(src, var_name)
#
# for(att in names(var_atts)) {
# ncatt_put(merged_nc, var_name, att, var_atts[[att]])
# }
#
# nc_close(src)
# }
#
# ncatt_put(merged_nc, 0, "collection", collection)
# ncatt_put(merged_nc, 0, "source", if(collection == "era5-x0.25") "ERA5" else "CMIP6")
# ncatt_put(merged_nc, 0, "date_created", as.character(Sys.Date()))
# Test function with error handling
# test_parallel_api <- function(geocode = "FRA") {
# tryCatch({
# message("\nStarting parallel climate data retrieval for ", geocode)
#
# # All variables to process
# variables <- c("tas", "cdd65", "hdd65", "hd30", "hd35", "fd", "id", "r20mm")
#
# # Get data
# results <- get_climate_data_batch_parallel(
# geocode = geocode,
# variables = variables
# )
#
# if (!is.null(results)) {
# message("\nSuccessfully retrieved data:")
# message("Total rows: ", nrow(results))
#
# # Summary by variable
# summary <- results %>%
# group_by(variables) %>%
# summarise(
# n_observations = n(),
# .groups = 'drop'
# )
#
# message("\nSummary by variable chunk:")
# print(summary)
#
# return(results)
# } else {
# message("\nNo data retrieved")
# return(NULL)
# }
#
# }, error = function(e) {
# message("\nError in parallel processing: ", e$message)
# return(NULL)
# }, finally = {
# # Ensure we always clean up parallel processing
# future::plan(future::sequential)
# })
# }
# Blood, sweat, and tears went into this function.