Skip to content

Commit

Permalink
fixes #1658
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 22, 2024
1 parent 09a0d0f commit 2f0c0d9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions R/ncdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
nl <- nlyr(x)
ncvars <- list()
cal <- NA

for (i in 1:n) {
if ((nl[i] > 1) || (x[i]@pntr$hasTime)) {
y <- x[i]
Expand Down Expand Up @@ -172,19 +173,31 @@
ncdf4::ncatt_put(ncobj, ncvars[[n+1]], "geotransform", gt, prec="text")

opt <- spatOptions()

bsteps <- blocks(rast(x[[1]], nlyr=sum(nlyr(x))), 4)
if (bsteps$n > opt$progress) {
progress <- TRUE
pb <- txtProgressBar(0, bsteps$n)
pcnt <- 0
} else {
progress <- FALSE
}

for (i in 1:n) {
y = x[i]
readStart(y)
y <- x[i]
b <- blocks(y, 4)
readStart(y)
if (length(ncvars[[1]]$dim) == 3) {
for (j in 1:b$n) {
if (progress) { setTxtProgressBar(pb, pcnt); pcnt <- pcnt + 1 }
d <- readValues(y, b$row[j], b$nrows[j], 1, nc, FALSE, FALSE)
d[is.nan(d)] <- NA
d <- array(d, c(nc, b$nrows[j], nl[i]))
ncdf4::ncvar_put(ncobj, ncvars[[i]], d, start=c(1, b$row[j], 1), count=c(nc, b$nrows[j], nl[i]))
}
} else {
for (j in 1:b$n) {
if (progress) { setTxtProgressBar(pb, pcnt); pcnt <- pcnt + 1 }
d <- readValues(y, b$row[j], b$nrows[j], 1, nc, FALSE, FALSE)
d[is.nan(d)] <- NA
d <- matrix(d, ncol=b$nrows[j])
Expand All @@ -196,7 +209,8 @@
ncdf4::ncatt_put(ncobj, ncvars[[i]], "grid_mapping", "crs", prec="text")
}
}

if (progress) close(pb)

ncdf4::ncatt_put(ncobj, 0, "Conventions", "CF-1.4", prec="text")
pkgversion <- drop(read.dcf(file=system.file("DESCRIPTION", package="terra"), fields=c("Version")))
ncdf4::ncatt_put(ncobj, 0, "created_by", paste("R packages ncdf4 and terra (version ", pkgversion, ")", sep=""), prec="text")
Expand Down

0 comments on commit 2f0c0d9

Please sign in to comment.