Skip to content

Commit 7cabb76

Browse files
committed
All fields in classes are private
1 parent ac981fe commit 7cabb76

7 files changed

Lines changed: 170 additions & 155 deletions

File tree

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Maintenance release
44

55
* Support for CF-1.13.
6+
* Fields in `CFTime` and `CFClimatology` classes have been made private, corresponding active fields added.
67
* `CFClimatology` instances can now be extended with the `+` operator.
78
* When extending a `CFTime` or `CFClimatology` instance with another such instance using the `+` operator, bounds are preserved if both instances have bounds set.
89
* Documentation updated.

R/CFClimatology.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ CFClimatology <- R6::R6Class("CFClimatology",
4444
self$set_bounds(bounds)
4545

4646
# Determine period and years from the bounds of the first offset
47-
time <- self$cal$offsets2time(bounds[1L:2L, 1L])
47+
time <- private$.cal$offsets2time(bounds[1L:2L, 1L])
4848
private$.years <- time$year
4949
if (all(time$day == 1L)) {
5050
if (all(time$month == 1L)) {
@@ -67,19 +67,19 @@ CFClimatology <- R6::R6Class("CFClimatology",
6767
#' @param ... Ignored.
6868
#' @return `self` invisibly.
6969
print = function(...) {
70-
noff <- length(self$offsets)
70+
noff <- length(private$.offsets)
7171
d <- self$range()
7272
el <- if (noff > 1L) {
7373
sprintf(" Elements: [%s .. %s] (average of %f %s between %d elements)\n",
74-
d[1L], d[2L], self$resolution, CFt$units$name[self$cal$unit], noff)
74+
d[1L], d[2L], private$.resolution, CFt$units$name[private$.cal$unit], noff)
7575
} else
7676
paste0(" Elements: ", d[1L], "\n")
7777

7878
p <- paste0(" Period : ", private$.period, "\n")
7979
if (private$.years[1L] == private$.years[2L]) y <- paste0(" Year : ", private$.years[1L], "\n")
8080
else y <- paste0(" Years : ", private$.years[1L], " - ", private$.years[2L], " (inclusive)\n")
8181

82-
cal <- capture.output(self$cal$print())
82+
cal <- capture.output(private$.cal$print())
8383
cat(paste(cal, collapse = "\n"), "\nClimatological time series:\n", el, p, y, sep = "")
8484
invisible(self)
8585
}

R/CFtime.R

Lines changed: 110 additions & 91 deletions
Large diffs are not rendered by default.

R/api.R

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ CFtime <- function(definition, calendar = "standard", offsets, from, to, by, len
7272
if (is.na(intv) || !length(intv_unit))
7373
stop("Argument `by` is an invalid time interval.", call. = FALSE)
7474

75-
doff <- intv * CFt$units$seconds[intv_unit] / CFt$units$seconds[t$cal$unit]
75+
doff <- intv * CFt$units$seconds[intv_unit] / CFt$units$seconds[t$calendar$unit]
7676

7777
# End point of the time series
7878
from_off <- t$offsets # There is only a single offset at this point
@@ -84,7 +84,7 @@ CFtime <- function(definition, calendar = "standard", offsets, from, to, by, len
8484
t + seq(from = from_off + doff, by = doff, length.out = length.out[1L] - 1L)
8585
} else {
8686
# Time series up to a finishing point
87-
to_off <- t$cal$parse(to[1L])$offset
87+
to_off <- t$calendar$parse(to[1L])$offset
8888
if (is.na(to_off))
8989
stop("Argument `to` must be a single character timestamp value.")
9090
t + seq(from = from_off + doff, to = to_off, by = doff)
@@ -124,23 +124,23 @@ CFtime <- function(definition, calendar = "standard", offsets, from, to, by, len
124124

125125
#' @describeIn properties The definition string of the `CFTime` instance.
126126
#' @export
127-
definition <- function(t) t$cal$definition
127+
definition <- function(t) t$calendar$definition
128128

129129
#' @describeIn properties The calendar of the `CFTime` instance.
130130
#' @export
131-
calendar <- function(t) t$cal$name
131+
calendar <- function(t) t$calendar$name
132132

133133
#' @describeIn properties The unit of the `CFTime` instance.
134134
#' @export
135135
unit <- function(t) t$unit
136136

137137
#' @describeIn properties The origin of the `CFTime` instance in timestamp elements.
138138
#' @export
139-
origin <- function(t) t$cal$origin
139+
origin <- function(t) t$calendar$origin
140140

141141
#' @describeIn properties The time zone of the calendar of the `CFTime` instance as a character string.
142142
#' @export
143-
timezone <- function(t) t$cal$timezone
143+
timezone <- function(t) t$calendar$timezone
144144

145145
#' @describeIn properties The offsets of the `CFTime` instance as a numeric vector.
146146
#' @export
@@ -472,7 +472,7 @@ slice <- function(x, extremes, rightmost.closed = FALSE) {
472472
#' e2 <- CFtime("days since 1850-01-01 00:00:00", "standard", 0:364)
473473
#' e1 == e2
474474
"==.CFTime" <- function(e1, e2)
475-
e1$cal$is_equivalent(e2$cal) &&
475+
e1$calendar$is_equivalent(e2$calendar) &&
476476
length(e1$offsets) == length(e2$offsets) &&
477477
all(e1$offsets == e2$offsets)
478478

@@ -527,19 +527,19 @@ slice <- function(x, extremes, rightmost.closed = FALSE) {
527527
"+.CFTime" <- function(e1, e2) {
528528
diff <- NULL
529529
new_time <- if (inherits(e2, "CFTime")) {
530-
if (!e1$cal$is_compatible(e2$cal)) stop("Calendars not compatible", call. = FALSE) # nocov
531-
if (all(e1$cal$origin[1:6] == e2$cal$origin[1:6]))
532-
CFTime$new(e1$cal$definition, e1$cal$name, c(e1$offsets, e2$offsets))
530+
if (!e1$calendar$is_compatible(e2$calendar)) stop("Calendars not compatible", call. = FALSE) # nocov
531+
if (all(e1$calendar$origin[1:6] == e2$calendar$origin[1:6]))
532+
CFTime$new(e1$calendar$definition, e1$calendar$name, c(e1$offsets, e2$offsets))
533533
else {
534-
diff <- e1$cal$parse(paste(e2$cal$origin_date, e2$cal$origin_time))$offset
535-
CFTime$new(e1$cal$definition, e1$cal$name, c(e1$offsets, e2$offsets + diff))
534+
diff <- e1$calendar$parse(paste(e2$calendar$origin_date, e2$calendar$origin_time))$offset
535+
CFTime$new(e1$calendar$definition, e1$calendar$name, c(e1$offsets, e2$offsets + diff))
536536
}
537537
} else if (is.numeric(e2) && .validOffsets(e2)) {
538-
CFTime$new(e1$cal$definition, e1$cal$name, c(e1$offsets, e2))
538+
CFTime$new(e1$calendar$definition, e1$calendar$name, c(e1$offsets, e2))
539539
} else {
540-
time <- e1$cal$parse(e2)
540+
time <- e1$calendar$parse(e2)
541541
if (anyNA(time$year)) stop("Argument `e2` contains invalid timestamps", call. = FALSE) # nocov
542-
CFTime$new(e1$cal$definition, e1$cal$name, c(e1$offsets, time$offset))
542+
CFTime$new(e1$calendar$definition, e1$calendar$name, c(e1$offsets, time$offset))
543543
}
544544

545545
if (!is.null(be1 <- e1$bounds) && !is.null(be2 <- e2$bounds)) {
@@ -561,13 +561,13 @@ slice <- function(x, extremes, rightmost.closed = FALSE) {
561561
"+.CFClimatology" <- function(c1, c2) {
562562
if (!inherits(c2, "CFClimatology"))
563563
stop("Can only merge a `CFClimatology` instance to another `CFClimatology`")
564-
if (!c1$cal$is_compatible(c2$cal)) stop("Calendars not compatible", call. = FALSE) # nocov
564+
if (!c1$calendar$is_compatible(c2$calendar)) stop("Calendars not compatible", call. = FALSE) # nocov
565565

566-
if (all(c1$cal$origin[1:6] == c2$cal$origin[1:6]))
567-
CFClimatology$new(c1$cal$definition, c1$cal$name, c(c1$offsets, c2$offsets), cbind(c1$bounds, c2$bounds))
566+
if (all(c1$calendar$origin[1:6] == c2$calendar$origin[1:6]))
567+
CFClimatology$new(c1$calendar$definition, c1$calendar$name, c(c1$offsets, c2$offsets), cbind(c1$bounds, c2$bounds))
568568
else {
569-
diff <- c1$cal$parse(paste(c2$cal$origin_date, c2$cal$origin_time))$offset
570-
CFClimatology$new(c1$cal$definition, c1$cal$name, c(c1$offsets, c2$offsets + diff), cbind(c1$bounds, c2$bounds + diff))
569+
diff <- c1$calendar$parse(paste(c2$calendar$origin_date, c2$calendar$origin_time))$offset
570+
CFClimatology$new(c1$calendar$definition, c1$calendar$name, c(c1$offsets, c2$offsets + diff), cbind(c1$bounds, c2$bounds + diff))
571571
}
572572
}
573573

@@ -840,13 +840,13 @@ month_days <- function(t, x = NULL) {
840840
stopifnot(inherits(t, "CFTime"))
841841

842842
if (is.null(x))
843-
return(t$cal$month_days())
843+
return(t$calendar$month_days())
844844
else {
845845
if (!(is.character(x))) stop("Argument `x` must be a character vector of dates in 'YYYY-MM-DD' format")
846846

847-
ymd <- t$cal$parse(x)
847+
ymd <- t$calendar$parse(x)
848848
if (anyNA(ymd$year)) warning("Some dates could not be parsed. Result contains `NA` values.", call. = FALSE)
849-
return(t$cal$month_days(ymd))
849+
return(t$calendar$month_days(ymd))
850850
}
851851
}
852852

@@ -908,14 +908,14 @@ month_days <- function(t, x = NULL) {
908908
#' parse_timestamps(t, timestamps)
909909
parse_timestamps <- function(t, x) {
910910
stopifnot(is.character(x), inherits(t, "CFTime"))
911-
if (t$cal$unit > 4) stop("Parsing of timestamps on a 'month' or 'year' time unit is not supported.", call. = FALSE)
911+
if (t$calendar$unit > 4) stop("Parsing of timestamps on a 'month' or 'year' time unit is not supported.", call. = FALSE)
912912

913-
out <- t$cal$parse(x)
913+
out <- t$calendar$parse(x)
914914
if (anyNA(out$year))
915915
warning("Some dates could not be parsed. Result contains `NA` values.") # nocov
916916
if (length(unique(na.omit(out$tz))) > 1)
917917
warning("Timestamps have multiple time zones. Some or all may be different from the calendar time zone.") # nocov
918-
else if (out$tz[1] != t$cal$timezone)
918+
else if (out$tz[1] != t$calendar$timezone)
919919
warning("Timestamps have time zone that is different from the calendar.") # nocov
920920
out
921921
}

man/CFtime.Rd

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

tests/testthat/test-CFtime.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,20 @@ test_that("test all variants of creating a CFtime object and useful functions",
134134

135135
test_that("Leap years on some calendars", {
136136
t <- CFTime$new("days since 2025-01-01", "360_day")
137-
expect_true(all(!t$cal$leap_year(c(2000:2025))))
137+
expect_true(all(!t$calendar$leap_year(c(2000:2025))))
138138
t <- CFTime$new("days since 2025-01-01", "366_day")
139-
expect_true(all(t$cal$leap_year(c(2000:2025))))
139+
expect_true(all(t$calendar$leap_year(c(2000:2025))))
140140
})
141141

142142
test_that("Calendar 'none'", {
143143
t <- CFTime$new("days since 2025-01-01", "none")
144-
expect_true(inherits(t$cal, "CFCalendarNone"))
144+
expect_true(inherits(t$calendar, "CFCalendarNone"))
145145
t <- t + 0:4
146146
ymd <- data.frame(year = c(rep(2025, 5), NA), month = c(rep(1, 5), NA), day = c(1, 1, 1, 2, 3, NA))
147-
expect_equal(t$cal$valid_days(ymd), c(T, T, T, F, F, NA))
148-
expect_equal(t$cal$month_days(), rep(NA, 12))
149-
expect_equal(t$cal$month_days(ymd), rep(NA, 6))
150-
expect_equal(t$cal$leap_year(ymd$year), rep(NA, 6))
151-
expect_equal(t$cal$date2offset(ymd), c(0, 0, 0, 0, 0, NA))
152-
expect_equal(t$cal$offset2date(1:5), data.frame(year = rep(2025, 5), month = rep(1, 5), day = rep(1, 5)))
147+
expect_equal(t$calendar$valid_days(ymd), c(T, T, T, F, F, NA))
148+
expect_equal(t$calendar$month_days(), rep(NA, 12))
149+
expect_equal(t$calendar$month_days(ymd), rep(NA, 6))
150+
expect_equal(t$calendar$leap_year(ymd$year), rep(NA, 6))
151+
expect_equal(t$calendar$date2offset(ymd), c(0, 0, 0, 0, 0, NA))
152+
expect_equal(t$calendar$offset2date(1:5), data.frame(year = rep(2025, 5), month = rep(1, 5), day = rep(1, 5)))
153153
})

tests/testthat/test-parse_deparse.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test_that("timestamp string parsing to offsets and deparsing of offsets to times
88
offsets <- -1000:1000
99
def <- paste(CFt$units$name[u], "since 1992-08-20")
1010
t <- CFtime(def, c, offsets)
11-
time <- t$cal$offsets2time(t$offsets)
11+
time <- t$calendar$offsets2time(t$offsets)
1212
ts <- as_timestamp(t, "timestamp")
1313
cf2 <- CFtime(def, c)
1414
tp <- parse_timestamps(cf2, ts)
@@ -18,10 +18,10 @@ test_that("timestamp string parsing to offsets and deparsing of offsets to times
1818

1919
# Add no offsets, test return value
2020
t <- CFTime$new("days since 1992-08-20", "standard")
21-
res <- t$cal$offsets2time()
21+
res <- t$calendar$offsets2time()
2222
expect_equal(nrow(res), 0L)
2323
t <- CFTime$new("days since 1992-08-20", "utc")
24-
res <- t$cal$offsets2time()
24+
res <- t$calendar$offsets2time()
2525
expect_equal(nrow(res), 0L)
2626
})
2727

@@ -33,7 +33,7 @@ test_that("testing calendars with leap years", {
3333
for (d in c("1996-01-15", "1996-02-29", "1996-04-01")) {
3434
def <- paste("days since", d)
3535
t <- CFtime(def, c, c(1:2500, 36501:39000))
36-
time <- t$cal$offsets2time(t$offsets)
36+
time <- t$calendar$offsets2time(t$offsets)
3737
ts <- as_timestamp(t, "timestamp")
3838
cf2 <- CFtime(def, c)
3939
tp <- parse_timestamps(cf2, ts)
@@ -52,7 +52,7 @@ test_that("testing calendars with leap years", {
5252
# offsets <- runif(10000, max = 10000)
5353
# def <- paste(CFt$units$name[u], "since 1978-08-20 07:34:12.2")
5454
# expect_warning(t <- CFtime(def, c, offsets)) # not ordered
55-
# time <- t$cal$offsets2time(t$offsets)
55+
# time <- t$calendar$offsets2time(t$offsets)
5656
# ts <- as_timestamp(t, "timestamp")
5757
# cf2 <- CFtime(def, c)
5858
# tp <- parse_timestamps(cf2, ts)
@@ -76,7 +76,7 @@ test_that("Gregorian/Julian calendar gap in the standard calendar", {
7676
t <- CFtime("days since 1582-10-01", "standard", 0:3)
7777
ts <- as_timestamp(t)
7878
expect_equal(ts[4], "1582-10-04")
79-
expect_false(t$cal$POSIX_compatible(0:3))
79+
expect_false(t$calendar$POSIX_compatible(0:3))
8080

8181
t <- t + 4:10
8282
ts <- as_timestamp(t)
@@ -90,9 +90,9 @@ test_that("Gregorian/Julian calendar gap in the standard calendar", {
9090
expect_equal(ts[6], "1582-10-15")
9191

9292
expect_false(t$POSIX_compatible()) # use t$offsets
93-
expect_true(t$cal$POSIX_compatible(0:100)) # use supplied offsets
94-
expect_true(t$cal$POSIX_compatible(-5:100))
95-
expect_false(t$cal$POSIX_compatible(-6:100))
93+
expect_true(t$calendar$POSIX_compatible(0:100)) # use supplied offsets
94+
expect_true(t$calendar$POSIX_compatible(-5:100))
95+
expect_false(t$calendar$POSIX_compatible(-6:100))
9696
})
9797

9898
test_that("Leap seconds in the utc calendar work fine", {
@@ -133,7 +133,7 @@ test_that("Fractional time parts and replace Z timezone with offset", {
133133
for (u in 1:4) {
134134
def <- paste(CFt$units$name[u], "since 1978-08-20")
135135
t <- CFtime(def, c)
136-
p <- t$cal$parse(offsets)
136+
p <- t$calendar$parse(offsets)
137137
expect_equal(round(p$offset %% 1, 4), res[,u])
138138
expect_equal(p$tz, rep("+0000", 4))
139139
}
@@ -147,7 +147,7 @@ test_that("Fractional time parts and replace Z timezone with offset", {
147147
for (u in 1:4) {
148148
def <- paste(CFt$units$name[u], "since 1978-08-20")
149149
t <- CFtime(def, "utc")
150-
p <- t$cal$parse(offsets)
150+
p <- t$calendar$parse(offsets)
151151
expect_equal(round(p$offset %% 1, 4), res[,u])
152152
}
153153

0 commit comments

Comments
 (0)