#!/usr/bin/env Rscript # Reproduce PART 1 of Blöschl et al. (2020, Nature) analysis.R for ONE seed. # Usage: Rscript run_seed.R # Faithful to https://github.com/tuwhydro/500yrfloods analysis.R (loop seed 11:60) # Difference: prediction on the grid is done in chunks to avoid an integer # overflow in spam::nearest.dist (n1*n2 > .Machine$integer.max); results identical. args <- commandArgs(trailingOnly = TRUE) seed <- as.integer(args[1]) stopifnot(!is.na(seed)) suppressPackageStartupMessages({ library(sp); library(sf); library(raster); library(rgeos); library(fields) }) setwd(dirname(sub("--file=", "", grep("^--file=", commandArgs(FALSE), value = TRUE)))) sites <- read.csv('sites.csv') i_f <- read.csv('i_f.csv') bias <- read.csv('bias.csv') # Event intensities: only class 2 and 3 floods are used for interpolation i_f$numIndex <- c(NA, 2, 3, NA, NA, NA)[as.numeric(as.factor(i_f$Index))] # Annual intensities: euclidean norm aggregation of multiple events per year i_a <- matrix(NA, nrow = 517, ncol = 103) rootsumsquares <- function(x) { sqrt(sum(x^2)) } rownames(i_a) <- seq(1500, 2016) colnames(i_a) <- unique(i_f$Code) for (j in 1:ncol(i_a)) { series <- i_f[i_f$Code == colnames(i_a)[j], ] anni <- series$Year cehflood <- series$numIndex dummy <- tapply(cehflood, anni, rootsumsquares) i_a[rownames(i_a) %in% as.character(anni), j] <- dummy } # Project sites in km space (azimuthal equidistant, centred 51N 7E) coor <- array(cbind(sign((sites$lon4 == 'O') - 0.5) * (sites$lon1 + sites$lon2/60 + sites$lon3/3600), sites$lat1 + sites$lat2/60 + sites$lat3/3600), dim = c(length(sites$code), 2), dimnames = list(sites$code, c('lon', 'lat'))) coordinates(sites) <- coor proj4string(sites) <- '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' lonC = 7; latC = 51 sites_km103 <- spTransform(sites, CRS(paste('+proj=aeqd +lat_0=', latC, ' +lon_0=', lonC, ' +units=km', sep = ''))) sites_km <- sites_km103[sites_km103$Interp == 1, ] # Grid for the interpolation bbbox <- as(extent(as.vector(t(matrix(c(-1450, -1600, 1150, 1500), ncol = 2)))), 'SpatialPolygons') proj4string(bbbox) <- paste('+proj=aeqd +lat_0=', latC, ' +lon_0=', lonC, ' +units=km', sep = '') tr <- seq(1500, 2018, by = 4) kmperyear = 50 xr <- seq(bbox(bbbox)[1, 1], bbox(bbbox)[1, 2], length = length(tr)/2)/kmperyear yr <- seq(bbox(bbbox)[2, 1], bbox(bbbox)[2, 2], length = length(tr)/2)/kmperyear griglia <- expand.grid(x = xr, y = yr, t = tr) pixelsize <- diff(seq(bbox(bbbox)[1, 1], bbox(bbbox)[1, 2], length = length(tr)/2))[1] * diff(seq(bbox(bbbox)[2, 1], bbox(bbbox)[2, 2], length = length(tr)/2))[1] voxelsize <- pixelsize * diff(tr)[1] # Interpolation parameters (as in analysis.R) pow0 = 10; scaleU = 2; powU = 2; kappaW = c(0.2, 1, 1.5) tapering = 20; smoothing = 10 set.seed(seed) timewindow = 100 i_a_add0 <- i_a for (j in 1:ncol(i_a_add0)) { numIndex <- i_a_add0[, j] firstlastfloods <- c(1, length(numIndex)) isfloodgr2 <- numIndex; isfloodgr2[is.na(isfloodgr2)] <- 0 isfloodgr2[isfloodgr2 <= 1] <- 0; isfloodgr2[isfloodgr2 > 1] <- 1 floodfrequency <- filter(isfloodgr2, filter = rep(1, timewindow))/timewindow dummy <- range(which(!is.na(floodfrequency))) floodfrequency[1:dummy[1]] <- floodfrequency[dummy[1]] floodfrequency[dummy[2]:length(floodfrequency)] <- floodfrequency[dummy[2]] floodfrequency[floodfrequency < 1/timewindow] <- 1/timewindow dummyi <- rep(1, length(numIndex)); dummyi[bias[, j + 1] < 1.1] <- 0 probab0 <- floodfrequency * dummyi; probab0 <- 1 - (1 - probab0)^pow0 probab0[probab0 > 1] <- 1 add0 <- as.logical(rbinom(n = length(numIndex), size = 1, prob = probab0)) dummy <- numIndex[add0]; dummy[is.na(dummy)] <- 0 numIndex[add0] <- dummy if (firstlastfloods[1] > 1) numIndex[1:(firstlastfloods[1] - 1)] <- NA if (firstlastfloods[2] < length(numIndex)) numIndex[(firstlastfloods[2] + 1):length(numIndex)] <- NA i_a_add0[, j] <- numIndex } # prepare input for fastTps coor_km <- coordinates(sites_km) anni <- as.integer(rownames(i_a_add0)) tabellone_km_add0 <- NULL for (j in 1:length(sites_km)) { codice <- as.character(sites_km$code[j]) dummy <- data.frame(code = codice, x = coor_km[codice, 1], y = coor_km[codice, 2], yr = anni, flood = i_a_add0[, codice], u = sites_km$Repres[j]) tabellone_km_add0 <- rbind(tabellone_km_add0, dummy[!is.na(dummy$flood), ]) } coordinates(tabellone_km_add0) = ~x + y proj4string(tabellone_km_add0) <- paste('+proj=aeqd +lat_0=', latC, ' +lon_0=', lonC, ' +units=km', sep = '') weights00 <- (tabellone_km_add0$u/scaleU)^powU weights00 <- weights00 * as.numeric(as.character( cut(tabellone_km_add0$flood, c(0, 1.5, 2.5, 10), include.lowest = TRUE, labels = kappaW))) # Thin plate spline interpolation for the 3D space-time cube tps_intrp <- fastTps(x = cbind(coordinates(tabellone_km_add0)[, 1]/kmperyear, coordinates(tabellone_km_add0)[, 2]/kmperyear, tabellone_km_add0$yr), Y = tabellone_km_add0$flood, m = 2, theta = tapering, lambda = smoothing, weights = weights00) # chunked prediction (identical values, avoids spam integer overflow) chunksize <- 5000L ngrid <- nrow(griglia) out_intrp <- numeric(ngrid) i0 <- 1L while (i0 <= ngrid) { i1 <- min(i0 + chunksize - 1L, ngrid) out_intrp[i0:i1] <- predict(tps_intrp, griglia[i0:i1, , drop = FALSE]) i0 <- i1 + 1L } intrp_cube <- cbind(griglia, round(out_intrp, 4)) names(intrp_cube)[4] <- 'floodIntensity' dir.create('seeds', showWarnings = FALSE) save(intrp_cube, file = paste('seeds/intrp_cubeSEED', seed, '.RData', sep = ''), compress = 'xz') cat('seed', seed, 'done; grid points:', nrow(intrp_cube), '\n')