seems like a good direction, but this does give different results than the first approach.
Though I can't comment on which of the two is accurate...
calc1 <- function(x,y,PrognosticSubDomainFactor,DXK,DYK){
set.seed(42)
#CUTK is a nonegative matrix e.g. 282x239
CUTK <- array(runif(n=(y+2)*(x+2),min = 0,max=10), dim=c(y+2,x+2))
NII <-y
NJJ<-x
ADVDOM <- array(0, dim=c(y+2,x+2))
for(i in seq(1,NII + 2)){
for(j in seq(1,NJJ + 2)){
if(CUTK[i,j] > 0){
iplus <- trunc(PrognosticSubDomainFactor* CUTK[i,j] / DXK)
jplus <- trunc(PrognosticSubDomainFactor* CUTK[i,j] / DYK)
for(i1 in seq(i - iplus, i + iplus + 1)){
for(j1 in seq(j - jplus, j+ jplus)){
if(i1 <= NII + 1 && i1 > 1 && j1 <= NJJ + 1 && j1 > 1){
if( ADVDOM[i1,j1] ==0){
}
ADVDOM[i1,j1] <- 1
}
}
}
}
}
}
return(ADVDOM)
}
calc2 <- function(x,y,PrognosticSubDomainFactor,DXK,DYK){
set.seed(42)
#CUTK is a nonegative matrix e.g. 282x239
CUTK <- array(runif(n=(y+2)*(x+2),min = 0,max=10), dim=c(y+2,x+2))
NII <-y
NJJ<-x
ADVDOM <- array(0, dim=c(y+2,x+2))
non.zeros <- which(CUTK > 0, arr.ind = TRUE)
iplus <- trunc(PrognosticSubDomainFactor * CUTK[CUTK > 0] / DXK)
jplus <- trunc(PrognosticSubDomainFactor * CUTK[CUTK > 0] / DYK)
i1 <- (non.zeros[,"row"] - iplus)
i2 <- (non.zeros[,"row"] + iplus + 1)
j1 <- (non.zeros[,"col"] - jplus)
j2 <- (non.zeros[,"col"] + jplus)
for(z in seq_along(i1)){
il <- pmin(pmax(1,i1[z]:i2[z]), NII + 1)
jl <- pmin(pmax(1,j1[z]:j2[z]), NJJ + 1)
ADVDOM[il ,jl] <- 1
}
return(ADVDOM)
}
#dummy process
calc3<- function(x,y)
{
ADVDOM <- array(1, dim=c(y+2,x+2))
ADVDOM[,1] <- 0
ADVDOM[1,] <- 0
ADVDOM[,x+2] <- 0
ADVDOM[x+2,] <- 0
return(ADVDOM)
}
library(microbenchmark)
microbenchmark(
res_1 <- calc1(5,5,15,4,4),
res_2 <- calc2(5,5,15,4,4),
res_3 <- calc3(5,5)
, times = 1L)
identical(res_1,res_2)
identical(res_1,res_3)
microbenchmark(
res_1 <- calc1(300,300,15,4,4),
res_2 <- calc2(300,300,15,4,4),
res_3 <- calc3(300,300)
, times = 1L)
identical(res_1,res_2)
identical(res_1,res_3)
Open question for me if there are particular numbers of PrognosticSubDomainFactor , DXK, DYK, that would lead a difference from the uniform behaviour so far observed and so would differentiate res_1 from res_3