Commit ded39e3e authored by Dan Baston's avatar Dan Baston

Simplify offset and count calculation

parent 563d48bf
......@@ -322,6 +322,10 @@ check_var_list <- function(cdf, vars) {
#' @param dim_values a list whose keys represent dimension names and values
#' represent values along those dimensions
find_offset <- function(cdf, real_dims, dim_values) {
if (is.null(dim_values)) {
return(NA_integer_)
}
sapply(real_dims, function(dimname) {
if (dimname %in% names(dim_values)) {
i <- which(ncdf4::ncvar_get(cdf, dimname)==dim_values[[dimname]])
......@@ -339,5 +343,9 @@ find_offset <- function(cdf, real_dims, dim_values) {
#'
#' @inheritParams find_offset
find_count <- function(real_dims, dim_values) {
if (is.null(dim_values)) {
return(NA_integer_)
}
ifelse(real_dims %in% names(dim_values), 1, -1)
}
......@@ -210,21 +210,12 @@ write_vars_to_cdf <- function(vars,
}
verbose <- FALSE
if (is.null(write_slice)) {
start <- NA
count <- NA
} else {
start <- find_offset(ncout, dimnames, write_slice)
count <- find_count(dimnames, write_slice)
}
ncdf4::ncvar_put(nc=ncout,
varid=param,
vals=dat,
start=start,
count=count,
verbose=verbose)
ncdf4::ncvar_put(nc = ncout,
varid = param,
vals = dat,
start = find_offset(ncout, dimnames, write_slice),
count = find_count(dimnames, write_slice),
verbose = verbose)
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment