diff --git a/R/utils.R b/R/utils.R index 5eda58d..333f450 100644 --- a/R/utils.R +++ b/R/utils.R @@ -492,51 +492,30 @@ var_to_terra <- function(var, dap) { xmax <- dap$Xn + 0.5 * resx ymin <- dap$Y1 - 0.5 * resy ymax <- dap$Yn + 0.5 * resy - # - # dim(var) = c() - # + + if (length(dim(var)) == 2) { dim(var) <- c(dim(var), 1) } - # - # if (dim(var)[1] != dap$nrows) { - # var <- aperm(var, c(2, 1, 3)) - # } - # - # - # var = array(as.vector(var), c(dap$nrows, dap$ncols, dap$Tdim) ) - # - # - # dim(as.vector(var)) = c(dap$nrows, dap$ncols, dap$Tdim) - + r = rast(nrows = dap$nrows, ncols = dap$ncols, - crs = dap$crs, - nlyrs = dap$Tdim, - extent = c( - xmin = min(xmin, xmax), - xmax = max(xmax, xmax), - ymin = min(ymin, ymax), - ymax = max(ymin, ymax) - )) - - r[] = var - -# r = rast( - # var, - # crs = dap$crs, - # extent = c( - # xmin = min(xmin, xmax), - # xmax = max(xmax, xmax), - # ymin = min(ymin, ymax), - # ymax = max(ymin, ymax) - # ) - # ) - - - if (dap$toptobottom) { - r <- flip(r) + crs = dap$crs, + nlyrs = dap$Tdim, + extent = c( + xmin = min(xmin, xmax), + xmax = max(xmax, xmax), + ymin = min(ymin, ymax), + ymax = max(ymin, ymax) + )) + + if(grepl("XY", dap$dim_order)){ + terra::values(r) = var + } else { + r[] = var } + if (dap$toptobottom) { r <- flip(r) } + units(r) <- dap$units time(r) <- dates names(r) <- name