Skip to content

Commit 553421f

Browse files
committed
Added makeGroup(), fixed CFVariable indexing and $summarise()`
1 parent 49a216c commit 553421f

5 files changed

Lines changed: 55 additions & 5 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export(groups)
3535
export(makeAxis)
3636
export(makeCharacterAxis)
3737
export(makeDiscreteAxis)
38+
export(makeGroup)
3839
export(makeLatitudeAxis)
3940
export(makeLongitudeAxis)
4041
export(makeTimeAxis)

R/CFVariable.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ CFVariable <- R6::R6Class("CFVariable",
710710
stop("The 'time' axis is too short to summarise on.", call. = FALSE) # nocov
711711
if (is.factor(f)) f <- list(f)
712712

713-
tm <- sum(private$YXZT() > 0L) # Test which oriented axes are present, T is the last one
713+
tm <- which(names(private$.axes) == tax$name)
714714

715715
# Private group for the result
716716
grp <- CFGroup$new("", NULL)
@@ -1205,6 +1205,8 @@ CFVariable <- R6::R6Class("CFVariable",
12051205
private$.id <- private$.NCobj$id
12061206
} else {
12071207
pack <- private$.NCobj$is_packed
1208+
ax_names <- names(private$.axes)
1209+
scalars <- rep(FALSE, length(ax_names))
12081210
}
12091211

12101212
# Packing attributes
@@ -1217,7 +1219,7 @@ CFVariable <- R6::R6Class("CFVariable",
12171219

12181220
# Set "coordinates" attribute for scalar axes and labels
12191221
if (any(scalars) || length(lbls))
1220-
self$set_attribute("coordinates", "NC_CHAR", paste(c(names(private$.axes[scalars]), lbls), collapse = " "))
1222+
self$set_attribute("coordinates", "NC_CHAR", paste(c(ax_names[scalars], lbls), collapse = " "))
12211223

12221224
private$write_data(dt, pack = pack, na.mode = 2)
12231225
private$.dirty <- FALSE
@@ -1439,6 +1441,8 @@ dimnames.CFVariable <- function(x) {
14391441
#' summer <- pr[, , 173:263]
14401442
#' str(summer)
14411443
"[.CFVariable" <- function(x, i, j, ..., drop = FALSE) {
1444+
caller_env <- parent.frame()
1445+
14421446
numaxes <- x$ndims
14431447
t <- vector("list", numaxes)
14441448
names(t) <- dimnames(x)[1:numaxes]
@@ -1473,7 +1477,7 @@ dimnames.CFVariable <- function(x) {
14731477
if (!is.null(tm)) t[[d]] <- tm
14741478
} else {
14751479
# Subset the axis
1476-
v <- eval(sc[[d]])
1480+
v <- eval(sc[[d]], envir = caller_env)
14771481
ex <- range(v)
14781482
start[d] <- ex[1L]
14791483
count[d] <- ex[2L] - ex[1L] + 1L

R/makeCFObjects.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,27 @@ create_ncdf <- function() {
1717
ds
1818
}
1919

20+
#' Create a new group
21+
#'
22+
#' This function creates a new group for CF objects. Groups are organized in an
23+
#' hierarchy, starting with a root node. A root node is specified by an empty
24+
#' string for a name and with argument `parent_group = NULL`.
25+
#' @param name A name for the group. This must be a valid name for CF objects.
26+
#' The default value is the empty string "".
27+
#' @param parent_group Optionally, a parent for the current group. This must be
28+
#' an instance of `CFGroup` itself.
29+
#' @return A new instance of `CFGroup`.
30+
#' @export
31+
#' @examples
32+
#' root <- makeGroup()
33+
#' sub_group <- makeGroup("sub", root)
34+
makeGroup <- function(name = "", parent_group = NULL) {
35+
CFGroup$new(name, parent_group)
36+
}
37+
2038
#' Create an axis
2139
#'
22-
#' With this method you can create an axis to use with new [CFVariable]
40+
#' With this function you can create an axis to use with new [CFVariable]
2341
#' instances. Depending on the `orientation` argument and the type of the
2442
#' `values` argument an instance of a class descending from [CFAxis] will be
2543
#' returned.

man/makeAxis.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/makeGroup.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)