Commit ac6f1385 authored by Dan Baston's avatar Dan Baston

Disable compression when appending to netCDF 3 files

parent fff7ba77
Pipeline #75971859 passed with stages
in 18 minutes and 9 seconds
......@@ -130,13 +130,15 @@ write_vars_to_cdf <- function(vars,
if (append && file.exists(filename)) {
ncout <- ncdf4::nc_open(filename, write=TRUE)
unlimited_dims <- Filter(function(dimname) ncout$dim[[dimname]]$unlim,
names(ncout$dim))
# Verify that our dimensions match up before writing
if (!quick_append) {
use_compression <- (ncout$format != 'NC_FORMAT_CLASSIC')
unlimited_dims <- Filter(function(dimname) ncout$dim[[dimname]]$unlim,
names(ncout$dim))
dims <- make_netcdf_dims(vars, extent, ids, extra_dims, unlimited_dims)
ncvars <- create_vars(vars, dims, ids, prec, extra_dims)
ncvars <- create_vars(vars, dims, ids, prec, extra_dims, use_compression)
# Check that the dimensions of the data to write match up
# with the data already in the file.
......@@ -165,7 +167,7 @@ write_vars_to_cdf <- function(vars,
} else {
# Creating a new file
dims <- make_netcdf_dims(vars, extent, ids, extra_dims)
ncvars <- create_vars(vars, dims, ids, prec, extra_dims)
ncvars <- create_vars(vars, dims, ids, prec, extra_dims, compress=TRUE)
ncout <- ncdf4::nc_create(filename, ncvars)
......@@ -464,7 +466,9 @@ standardize_ids <- function(ids) {
#' @param varname name of the variable
#' @param vals values for the variable
#' @param prec argument passed to write_vars_to_cdf
create_var <- function(dims, varname, vals, prec) {
#' @param compress should compression be enabled for the new variable?
#' @return a \code{ncvar4} object for the variable
create_var <- function(dims, varname, vals, prec, compress) {
is_spatial <- is.null(dims$id)
if (mode(vals) == "character") {
......@@ -489,15 +493,15 @@ create_var <- function(dims, varname, vals, prec) {
dim=vardims,
missval=var_fill(varname, vals, prec),
prec=var_prec(varname, vals, prec),
compression=1)
compression=ifelse(compress, 1, NA))
}
create_vars <- function(vars, dims, ids, prec, extra_dims) {
create_vars <- function(vars, dims, ids, prec, extra_dims, compress) {
regular_var_names <- names(vars)[!(names(vars) %in% names(dims))]
ncvars <- sapply(regular_var_names, function(varname) {
create_var(dims, varname, vars[[varname]], prec)
create_var(dims, varname, vars[[varname]], prec, compress)
}, simplify=FALSE)
# Add a CRS var
......
......@@ -446,6 +446,7 @@ test_that('we can write data where different variables have different numbers of
append=TRUE)
file.remove(fname)
succeed()
})
test_that('we can write multidimensional id-based data', {
......@@ -536,4 +537,25 @@ test_that('we can append to a file created with an unlimited id dim', {
write_vars_to_cdf(list(runoff=runoff), fname, ids=ids, append=TRUE)
file.remove(fname)
succeed()
})
test_that('we can append to a netcdf version 3 file', {
fname <- tempfile(fileext='.nc')
ids <- 3:17
precip <- runif(length(ids))*10
runoff <- precip*runif(length(ids))
id_dim <- ncdf4::ncdim_def('id', units='', vals=ids)
precip_var <- ncdf4::ncvar_def('precip', units='mm', dim=id_dim)
cdf <- ncdf4::nc_create(fname, precip_var)
ncdf4::ncvar_put(cdf, precip_var, precip)
ncdf4::nc_close(cdf)
write_vars_to_cdf(list(runoff=runoff), fname, ids=ids, append=TRUE)
file.remove(fname)
succeed()
})
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