Commit 8db8e7df authored by Dan Baston's avatar Dan Baston

Pull extra dim definition out of write_vars_to_cdf

parent 5f10823c
make_netcdf_dims <- function(extent, ids, dims=NULL) {
if (is.null(ids)) {
lats <- lat_seq(extent, dims)
lons <- lon_seq(extent, dims)
#' Create netCDF dimensions
#'
#' @inheritParams write_vars_to_cdf
#' @return list of \code{ncdim4} objects
#'
make_netcdf_dims <- function(vars, extent, ids, extra_dims) {
c(make_netcdf_base_dims(vars, extent, ids),
make_netcdf_extra_dims(extra_dims))
}
make_netcdf_base_dims <- function(vars, extent, ids) {
is_spatial <- is.null(ids)
if (is_spatial) {
lats <- lat_seq(extent, dim(vars[[1]]))
lons <- lon_seq(extent, dim(vars[[1]]))
return(list(
ncdf4::ncdim_def("lon", units="degrees_east", vals=lons, longname="Longitude", create_dimvar=TRUE),
ncdf4::ncdim_def("lat", units="degrees_north", vals=lats, longname="Latitude", create_dimvar=TRUE)
lon = ncdf4::ncdim_def("lon", units="degrees_east", vals=lons, longname="Longitude", create_dimvar=TRUE),
lat = ncdf4::ncdim_def("lat", units="degrees_north", vals=lats, longname="Latitude", create_dimvar=TRUE)
))
} else {
if (mode(ids) == 'character') {
......@@ -13,16 +25,29 @@ make_netcdf_dims <- function(extent, ids, dims=NULL) {
# old-school way, with fixed-length character arrays. Data written in this
# way seems to be interpreted correctly by software such as QGIS.
return(list(
ncdf4::ncdim_def("id", units="", vals=1:length(ids), create_dimvar=FALSE)
id = ncdf4::ncdim_def("id", units="", vals=1:length(ids), create_dimvar=FALSE)
))
} else {
# integer ids
return(list(
ncdf4::ncdim_def("id", units="", vals=ids, create_dimvar=TRUE)
id = ncdf4::ncdim_def("id", units="", vals=ids, create_dimvar=TRUE)
))
}
}
}
make_netcdf_extra_dims <- function(extra_dims) {
extra_ncdf_dims <- list()
for (dimname in names(extra_dims)) {
vals <- extra_dims[[dimname]]
if (mode(vals) == 'character') {
new_dim <- ncdf4::ncdim_def(dimname, units='', vals=1:length(vals), create_dimvar=FALSE)
} else {
new_dim <- ncdf4::ncdim_def(dimname, units='', vals=vals, create_dimvar=TRUE)
}
extra_ncdf_dims[[dimname]] <- new_dim
}
return(extra_ncdf_dims)
}
......@@ -115,7 +115,7 @@ write_vars_to_cdf <- function(vars,
}
if (!append || !quick_append) {
dims <- make_netcdf_dims(extent, ids, dim(vars[[1]]))
dims <- make_netcdf_dims(vars, extent, ids, extra_dims)
if (!is_spatial) {
if (is.null(extra_dims) || !is.null(write_slice)) {
......@@ -124,19 +124,6 @@ write_vars_to_cdf <- function(vars,
verify_var_size(vars, length(ids)*prod(sapply(extra_dims, length)))
}
}
extra_ncdf_dims <- list()
for (dimname in names(extra_dims)) {
vals <- extra_dims[[dimname]]
if (mode(vals) == 'character') {
new_dim <- ncdf4::ncdim_def(dimname, units='', vals=1:length(vals), create_dimvar=FALSE)
} else {
new_dim <- ncdf4::ncdim_def(dimname, units='', vals=vals, create_dimvar=TRUE)
}
extra_ncdf_dims[[dimname]] <- new_dim
}
dims <- c(dims, extra_ncdf_dims)
}
# Create all variables, putting in blank strings for the units. We will
......@@ -182,7 +169,7 @@ write_vars_to_cdf <- function(vars,
for (dimname in names(extra_dims)) {
vals <- extra_dims[[dimname]]
if (mode(vals) == 'character') {
ncvars[[dimname]] <- create_char_dimension_variable(extra_ncdf_dims[[dimname]], dimname, vals)
ncvars[[dimname]] <- create_char_dimension_variable(dims[[dimname]], dimname, vals)
}
}
}
......@@ -216,7 +203,7 @@ write_vars_to_cdf <- function(vars,
}
for (dimname in names(extra_dims)) {
ncdf4::ncvar_put(ncout, extra_ncdf_dims[[dimname]], extra_dims[[dimname]])
ncdf4::ncvar_put(ncout, dims[[dimname]], extra_dims[[dimname]])
}
}
......
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