library(s2dv)

data <- array(rnorm(1:60000),
               dim = c(dat = 1, time = 120, member = 6, lon = 20, lat = 50))
time1 <- Sys.time()
res <- InsertDim(data, 2, 10, 'new')
time2 <- Sys.time()
time.taken <- time2 - time1
print(time.taken)
time1 <- Sys.time()
res <- InsertDim(data, 2, 10, 'new', ncores = 4)
time2 <- Sys.time()
time.taken <- time2 - time1
print(time.taken)
library(multiApply)
.InsertDim <- function(x, val) {
  x <- val
  return(x)
}
posdim = 2; lendim = 10; name = 'new';
if (posdim == 1) { # first dim
    outdim <- c(lendim, dim(data))
  } else {
    if (posdim > length(dim(data))) { # last dim
      outdim <- c(dim(data), lendim)
    } else { # middle dim
    outdim <- c(dim(data)[1:(posdim - 1)], lendim, dim(data)[posdim:length(dim(data))])
    }
  }
 outvar <- array(dim = c(outdim))
  ## give temporary names for Apply(). The name will be replaced by data in the end
  names(dim(outvar)) <- paste0('D', 1:length(outdim))
  names(dim(outvar))[posdim] <- name

time1 <- Sys.time()
res <- Apply(list(outvar), 
               margins = name, 
               fun = .InsertDim, 
               val = data,
               ncores = 1)$output1
time2 <- Sys.time()
time.taken <- time2 - time1
print(time.taken)

time1 <- Sys.time()
res <- Apply(list(outvar),
               margins = name,
               fun = .InsertDim,
               val = data,
               ncores = 4)$output1
time2 <- Sys.time()
time.taken <- time2 - time1
print(time.taken)



library(rbenchmark)

# Mean along 1 dim
benchmark("s2dv, 1 core" = {
            data <- array(rnorm(1:60000),
                          dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
            res <- s2dv::InsertDim(data, 2, 10, 'new', ncores = 1)
          },

          "s2dv, 2 cores" = {
            data <- array(rnorm(1:60000),
                          dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
             res_Mean1Dim <- s2dv::InsertDim(data, 2, 10, 'new', ncores = 2)
          },

          "s2dverification" = {
             data <- array(rnorm(1:60000),
                           dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
             res_MeanListDim <- s2dverification::InsertDim(data, 2, 10)
          },
          replications = 10,
          columns = c("test", "replications", "elapsed",
                      "relative", "user.self", "sys.self"))

# Mean along 1 dim
benchmark("s2dv, 1 core" = {
            data <- array(rnorm(1:60000),
                          dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
            res <- s2dv::InsertDim(data, 2, 10, 'new', ncores = 1)
          },

          "s2dv, 4 cores" = {
            data <- array(rnorm(1:60000),
                          dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
             res_Mean1Dim <- s2dv::InsertDim(data, 2, 10, 'new', ncores = 4)
          },

          "s2dverification" = {
             data <- array(rnorm(1:60000),
                           dim = c(dat = 1, time = 12, member = 60, lon = 20, lat = 50))
             res_MeanListDim <- s2dverification::InsertDim(data, 2, 10)
          },
          replications = 10,
          columns = c("test", "replications", "elapsed",
                      "relative", "user.self", "sys.self"))

# member = 5, lendim = 2
             test replications elapsed relative user.self sys.self
1    s2dv, 1 core           10   0.213    8.192     0.200    0.012
2   s2dv, 2 cores           10   0.283   10.885     0.172    0.060
3 s2dverification           10   0.026    1.000     0.024    0.000

# member = 50, lendim = 10
             test replications elapsed relative user.self sys.self
1    s2dv, 1 core           10   8.184    6.243     7.708    0.436
2   s2dv, 2 cores           10   7.937    6.054     4.752    0.800
3 s2dverification           10   1.311    1.000     1.237    0.064

             test replications elapsed relative user.self sys.self
1    s2dv, 1 core           10   7.793    5.886     7.320    0.452
2   s2dv, 2 cores           10   8.002    6.044     4.740    0.872
3 s2dverification           10   1.324    1.000     1.232    0.080

# member = 60, lendim = 10
             test replications elapsed relative user.self sys.self
1    s2dv, 1 core           10   9.370    5.827     8.828    0.508
2   s2dv, 2 cores           10   9.443    5.873     5.733    0.844
3 s2dverification           10   1.608    1.000     1.492    0.112

