Skip to content

Commit

Permalink
clean up c.stars() for time sequences
Browse files Browse the repository at this point in the history
closes #703
  • Loading branch information
edzer committed Jan 25, 2025
1 parent a628290 commit 30375da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# version 0.6-8

* `c.stars()` is more strict when combining time sequences; #703

* fix plotting when breaks contain duplicates; #728

* fix `st_as_stars.im()`; #727 and #648, thanks to Barry Rowlingson
Expand Down
8 changes: 5 additions & 3 deletions R/dimensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,11 @@ as.POSIXct.stars = function(x, ...) {
d = st_dimensions(x)
e = expand_dimensions(d)
for (i in seq_along(d)) {
p = try(as.POSIXct(e[[i]]), silent = TRUE)
if (!inherits(p, "try-error"))
d[[i]] = create_dimension(values = p)
if (inherits(e[[i]], c("Date", "POSIXt", "udunits", "PCICt"))) {
p = try(as.POSIXct(e[[i]]), silent = TRUE)
if (!inherits(p, "try-error"))
d[[i]] = create_dimension(values = p)
}
}
structure(x, dimensions = d)
}
Expand Down
21 changes: 14 additions & 7 deletions R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -904,13 +904,20 @@ merge.stars = function(x, y, ..., name = "attributes") {
}

sort_out_along = function(ret) {
d1 = st_dimensions(ret[[1]])
d2 = st_dimensions(ret[[2]])
if ("time" %in% names(d1) && (isTRUE(d1$time$offset != d2$time$offset) ||
!any(d1$time$values %in% d2$time$values)))
"time"
else
NA_integer_
# https://github.com/r-spatial/stars/issues/703 :
# 1. check that time is a dimension name in all objects
l = lapply(ret, st_dimensions)
if (!all(sapply(l, function(x) "time" %in% names(x))))
return(NA_integer_)
# 2. check that time values do not overlap
lv = lapply(l, st_get_dimension_values, "time")
for (i in seq_along(lv)) {
if (!inherits(lv[[i]], c("POSIXt", "Date", "PCICt")))
return(NA_integer_)
if (i < length(lv) && max(lv[[i]]) >= min(lv[[i+1]])) # no sequence
return(NA_integer_)
}
return("time")
}


Expand Down

0 comments on commit 30375da

Please sign in to comment.