Skip to content

Commit

Permalink
catch for TXY dim orders
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejohnson51 committed Jan 23, 2024
1 parent 09fd28b commit 3f495a0
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3f495a0

Please sign in to comment.