Repository navigation
Expand file tree
/
Copy pathkriging.R
More file actions
323 lines (306 loc) · 12.7 KB
/
Copy pathkriging.R
File metadata and controls
323 lines (306 loc) · 12.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
#!/usr/bin/env Rscript
rm(list = ls())
library("foreach",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-redhat-linux-gnu-library/3.3")
#library(foreach)
library("doParallel",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("gstat",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("sp",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("nlme",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
#library("RNetCDF",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("ncdf4",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("ncdf4.helpers",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("lattice",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
#library("ggplot2",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("raster",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
library("plyr",lib.loc="/home-nfs/ollie/cluettig/R/x86_64-pc-linux-gnu-library/3.3")
source("fitmodel.R")
######### INPUT ###################################################################################################
args <- commandArgs(TRUE)
fname <- args[1]
fname2 <- args[2]
fnewname <- args[3]
fnewname2 <- args[4]
fvarname <- args[5]
dir <- args[6]
#setwd("~/Documents/Daten/Interpolation/test4_Kombination")
#source("fitmodel.R")
#fname <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/surface_gaps_filled.nc'
#fname2 <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/kriging_radius_inside.nc'
#fnewname <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/kriging_values_inside.nc'
#fnewname2 <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/interim_inside.nc'
#fvarname <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/kriging_variance_inside.nc'
#dir <- '/Users/cluettig/Documents/Daten/Interpolation/test4_Kombination/test/'
######### READ ###################################################################################################
nc_data <- nc_open(fname) # input
#proj4string <- nc.get.proj4.string(nc_data,'z')
z_data <- ncvar_get(nc_data,varid = "z")
x_coor <- ncvar_get(nc_data,varid = "x")
y_coor <- ncvar_get(nc_data,varid = "y")
nc_close(nc_data)
nc_data <- nc_open(fname2) # contains polygon diameters
print(nc_data)
krig_data <- ncvar_get(nc_data,varid = "Band1")
nc_close(nc_data)
print("ok")
######### FORMAT DATA ############################################################################################
# raster to vectors:
z_a <- as.vector(z_data)
y_a <- rep(x_coor,times = length(y_coor))
x_a <- rep(y_coor,each = length(x_coor))
krig_a <- as.vector(krig_data)
print(length(z_a))
# Values in NA positions
z_a[z_a > 1e+30] <- NA
krig_nan <- krig_a[krig_a != 0 & is.na(z_a)]
x_nan <- x_a[krig_a != 0 & is.na(z_a)]
y_nan <- y_a[krig_a != 0 & is.na(z_a)]
z_nan <- z_a[krig_a != 0 & is.na(z_a)]
d_nan <- data.frame(x_nan,y_nan)
coordinates(d_nan) = ~x_nan+y_nan
# Values in given positions:
x_notnan <- x_a[!is.na(z_a)]
y_notnan <- y_a[!is.na(z_a)]
z_notnan <- z_a[!is.na(z_a)]
d_notnan <- data.frame(x_notnan,y_notnan,z_notnan)
# Possibility to take only a subsample for kriging:
sub1 = length(z_notnan)/1 # number of data points in subsample
if (length(z_notnan) > sub1)
{
s <- 1:length(z_notnan)
IX <- sample(s,sub1)
x <- x_notnan[IX]
y <- y_notnan[IX]
z <- z_notnan[IX]
} else
{
x <- x_notnan
y <- y_notnan
z <- z_notnan
}
d <- data.frame(x,y,z)
coordinates(d) = ~x+y
# Possibility to take only outer points for kriging:
outer_p <- 1
print("reaches outer")
if (outer_p == 1)
{
print("if 1")
z_outer <- z_data
nan_mat <- is.na(z_data)
for (i in 1:length(x_coor))
{
print("for 1")
for (j in 1:length(y_coor))
{
print("for 2")
if (nan_mat[i,j]==0)
{
print("if 2")
if (i-1 >= 1 && i+1 <= length(x_coor) && j-1 >= 1 && j+1 <= length(y_coor))
{
print("if 3")
n_nan_neighbours <- nan_mat[i-1,j] + nan_mat[i+1,j] + nan_mat[i,j-1] + nan_mat[i,j+1]
if (n_nan_neighbours == 0)
{
print("if 4")
z_outer[i,j] <- NA
print("do nan")
}
}
}
}
}
print("after fors")
z_o <- as.vector(z_outer)
y_o <- rep(x_coor,times = length(y_coor))
x_o <- rep(y_coor,each = length(x_coor))
x <- x_o[!is.na(z_o)]
y <- y_o[!is.na(z_o)]
z <- z_o[!is.na(z_o)]
print("outer points")
print(length(z))
print(length(z_notnan))
print(length(z_nan))
} else
{
x <- x_notnan
y <- y_notnan
z <- z_notnan
}
# Possibility to take only a subsample for variogram estimation:
sub2 = 1000 # number of data points in subsample
if (length(z) > sub2)
{
s <- 1:length(z)
IX <- sample(s,sub2)
x_v <- x[IX]
y_v <- y[IX]
z_v <- z[IX]
} else {
x_v <- x
y_v <- y
z_v <- z
}
################ TREND REMOVAL ###########################################################################################
print("rend removal")
model <- fitmodel(x_v,y_v,z_v, dir)
model$coefficients[is.na(model$coefficients)] <- 0
if (length(model$coefficients) == 16)
{
print("cubic trend model")
z_v <- z_v - (model$coefficients[1] + model$coefficients[2]*x_v + model$coefficients[3]*x_v^2
+ model$coefficients[4]*x_v^3 + model$coefficients[5]*y_v + model$coefficients[6]*y_v^2
+ model$coefficients[7]*y_v^3 + model$coefficients[8]*x_v*y_v + model$coefficients[9]*x_v^2*y_v
+ model$coefficients[10]*x_v^3*y_v + model$coefficients[11]*x_v*y_v^2 + model$coefficients[12]*x_v^2*y_v^2
+ model$coefficients[13]*x_v^3*y_v^2 + model$coefficients[14]*x_v*y_v^3 + model$coefficients[15]*x_v^2*y_v^3
+ model$coefficients[16]*x_v^3*y_v^3)
z <- z - (model$coefficients[1] + model$coefficients[2]*x + model$coefficients[3]*x^2
+ model$coefficients[4]*x^3 + model$coefficients[5]*y + model$coefficients[6]*y^2
+ model$coefficients[7]*y^3 + model$coefficients[8]*x*y + model$coefficients[9]*x^2*y
+ model$coefficients[10]*x^3*y + model$coefficients[11]*x*y^2 + model$coefficients[12]*x^2*y^2
+ model$coefficients[13]*x^3*y^2 + model$coefficients[14]*x*y^3 + model$coefficients[15]*x^2*y^3
+ model$coefficients[16]*x^3*y^3)
}else if (length(model$coefficients) == 9)
{
print("quadratic trend model")
z_v <- z_v - (model$coefficients[1] + model$coefficients[2]*x_v + model$coefficients[3]*x_v^2
+ model$coefficients[4]*y_v + model$coefficients[5]*y_v^2 + model$coefficients[6]*x_v*y_v
+ model$coefficients[7]*x_v^2*y_v + model$coefficients[8]*x_v*y_v^2
+ model$coefficients[9]*x_v^2*y_v^2)
z <- z - (model$coefficients[1] + model$coefficients[2]*x + model$coefficients[3]*x^2
+ model$coefficients[4]*y + model$coefficients[5]*y^2 + model$coefficients[6]*x*y
+ model$coefficients[7]*x^2*y + model$coefficients[8]*x*y^2 + model$coefficients[9]*x^2*y^2)
}else if (length(model$coefficients) == 4)
{
print("linear trend model")
z_v <- z_v - (model$coefficients[1] + model$coefficients[2]*x_v
+ model$coefficients[3]*y_v + model$coefficients[4]*x_v*y_v)
z <- z - (model$coefficients[1] + model$coefficients[2]*x
+ model$coefficients[3]*y + model$coefficients[4]*x*y)
}else{
print("Error while substracting the trend model")
}
d_v <- data.frame(x_v,y_v,z_v)
# Plot of variables after removing the trend:
png(paste(dir,"corr_model_substr.png"))
plot(d_v)
dev.off()
########## VARIOGRAM ESTIMATION ###################################################################################################
print("variogram")
coordinates(d_v) = ~x_v+y_v
d <- data.frame(x,y,z)
coordinates(d) = ~x+y
g <- gstat(id = "test", formula = z_v~1,data = d_v)
v <- variogram(g, alpha = c(0,45,90,135),width = 10,cutoff = 10000000)
v2 <- variogram(g, width = 100,cutoff = 10000000)
# Plot of the variogram:
png(paste(dir,"variogram.png"))
plot(v2)
dev.off()
vm <- vgm(psill = max(v2$gamma)/2,"Sph",range = max(v2$dist)/3)# ,anis = c(45,0.3))
fit <- fit.variogram(v,vm)
#Plot of the fitted variogram function:
png(paste(dir,"variogram_fit.png"))
plot(v, fit)
dev.off()
t3 <- Sys.time()
########## KRIGING #############################################################################################################
print("kriging")
x_notnan <- x_a[krig_a == 0 | (krig_a != 0 & !is.na(z_a))]
y_notnan <- y_a[krig_a == 0 | (krig_a != 0 & !is.na(z_a))]
z_notnan <- z_a[krig_a == 0 | (krig_a != 0 & !is.na(z_a))]
d_notnan <- data.frame(x_notnan,y_notnan,z_notnan)
d_notnan <- rename(d_notnan, c("x_notnan" = "x", "y_notnan" = "y","z_notnan" = "z"))
kriging <- function(i){
if (unique(krig_nan)[i] > 25000) {max_kriging_points = 3000}else{max_kriging_points = 500}
x_nan_i <- x_nan[krig_nan == unique(krig_nan)[i]]
y_nan_i <- y_nan[krig_nan == unique(krig_nan)[i]]
d_n <- data.frame(x_nan_i,y_nan_i)
coordinates(d_n) = ~x_nan_i+y_nan_i
cat(i, 'of', n_iterations, " at time", as.character(Sys.time()), "for",length(x_nan_i),"points and with maximal", max_kriging_points, "kriging points \n")
zk <- krige(formula = z~1, locations = d, newdata = d_n, model = fit, maxdist = unique(krig_nan)[i], nmax=300)
#maxdist = unique(krig_nan)[i]
zw <- zk$var1.pred
print(zw)
xw <- x_nan_i
yw <- y_nan_i
dw <- data.frame(xw,yw,zw)
dw <- rename(dw, c("xw"="x", "yw"="y", "zw"="z"))
}
n_cores <- detectCores()
print(n_cores)
cl <- makeCluster(n_cores)
registerDoParallel(cl)
max_length_gap=length(krig_nan/n_cores)
for (j in unique(krig_nan))
{
if (length(krig_nan[krig_nan==j])>max_length_gap)
{
part=max_length_gap/length(krig_nan[krig_nan==j])
for (k in 1:floor(part))
{
krig_nan[krig_nan==j][(k-1)*max_length_gap+1:k*max_length_gap]=krig_nan[(k-1)*max_length_gap+1:k*max_length_gap]+k*0.01
}
}
}
n_iterations <- length(unique(krig_nan))
kriging_results <- foreach(test_i = 1:n_iterations,
.combine = list,
.multicombine = TRUE,
.packages = c("gstat", "sp", "nlme", "ncdf4", "lattice", "plyr")) %dopar%
kriging(test_i)
stopImplicitCluster()
d_result <- kriging_results[[1]]
for (i in 2:length(kriging_results)) {
d_result <- rbind(d_result,kriging_results[[i]])
}
z_n <- d_result$z
x_n <- d_result$x
y_n <- d_result$y
t4 <- Sys.time()
cat("Kriging runtime:", t4 - t3)
# Addition of substracted trend model:
if (length(model$coefficients) == 16)
{
z_n <- z_n + (model$coefficients[1] + model$coefficients[2]*x_n + model$coefficients[3]*x_n^2
+ model$coefficients[4]*x_n^3 + model$coefficients[5]*y_n + model$coefficients[6]*y_n^2
+ model$coefficients[7]*y_n^3 + model$coefficients[8]*x_n*y_n + model$coefficients[9]*x_n^2*y_n
+ model$coefficients[10]*x_n^3*y_n + model$coefficients[11]*x_n*y_n^2 + model$coefficients[12]*x_n^2*y_n^2
+ model$coefficients[13]*x_n^3*y_n^2 + model$coefficients[14]*x_n*y_n^3 + model$coefficients[15]*x_n^2*y_n^3
+ model$coefficients[16]*x_n^3*y_n^3)
}else if (length(model$coefficients) == 9)
{
z_n <- z_n + (model$coefficients[1] + model$coefficients[2]*x_n + model$coefficients[3]*x_n^2
+ model$coefficients[4]*y_n + model$coefficients[5]*y_n^2 + model$coefficients[6]*x_n*y_n
+ model$coefficients[7]*x_n^2*y_n + model$coefficients[8]*x_n*y_n^2 + model$coefficients[9]*x_n^2*y_n^2)
}else if (length(model$coefficients) == 4)
{
z_n <- z_n + (model$coefficients[1] + model$coefficients[2]*x_n
+ model$coefficients[3]*y_n + model$coefficients[4]*x_n*y_n)
}else{
print("Error while addition of the model")
}
############# WRITE ##################################################################################################
print("writing")
# kriged values:
d_z_n <- data.frame(x_n,y_n,z_n)
d_z_n <- rename(d_z_n, c("x_n" = "x", "y_n" = "y", "z_n" = "z"))
total <- rbind(d_z_n,d_notnan)
total <- total[with(total, order(x, y)),]
z_new <- as.matrix(total[c("z")], nrow = length(x_coor), ncol = length(y_coor))
nc_data <- nc_open(fnewname, write = TRUE)
z_data <- ncvar_put(nc_data, varid = "z",vals = z_new)
nc_close(nc_data)
# kriging variance:
# z_notnan <- array(NA,c(length(z_notnan),1))
# d_notnan <- data.frame(x_notnan,y_notnan,z_notnan)
# d_notnan <- rename(d_notnan, c("x_notnan" = "x", "y_notnan" = "y", "z_notnan" = "z"))
# d_z_n <- data.frame(x_n,y_n,var_n)
# d_z_n <- rename(d_z_n, c("x_n" = "x", "y_n" = "y", "var_n" = "z"))
# d_z_n <- d_z_n[c("x", "y","z")]
# total <- rbind(d_z_n,d_notnan)
# total <- total[with(total, order(x, y)),]
# z_var <- as.matrix(total[c("z")], nrow = length(x_coor), ncol = length(y_coor))
# nc_data <- nc_open(fvarname, write = TRUE)
# z_data <- ncvar_put(nc_data, varid = "z", vals = z_var)
# nc_close(nc_data)