R Under development (unstable) (2024-11-05 r87288 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ## IGNORE_RDIFF_BEGIN > library(stars) Loading required package: abind Loading required package: sf Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE > ## IGNORE_RDIFF_END > > if (suppressPackageStartupMessages(require(sp, quietly = TRUE)) && require(spacetime, quietly = TRUE)) { + data(air) # this loads several datasets in .GlobalEnv + dim(air) + d = st_dimensions(station = st_set_crs(st_as_sfc(stations), 4326), time = dates) + + blocks = st_make_grid(st_as_sfc("POLYGON ((5.871619 47.26986, 15.03811 47.26986, 15.03811 55.05653, 5.871619 55.05653, 5.871619 47.26986))", crs = 4326), + n = c(3,3)) + + print(aq <- st_as_stars(list(PM10 = air), dimensions = d)) + print(a <- aggregate(aq, blocks, mean, na.rm = TRUE)) + } stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. NA's PM10 0 9.921 14.792 17.69728 21.992 274.333 157659 dimension(s): from to offset delta refsys point station 1 70 NA NA WGS 84 TRUE time 1 4383 1998-01-01 1 days Date FALSE values station POINT (9.585911 53.67057),...,POINT (9.446661 49.24068) time NULL stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. NA's PM10 1 11 15.57425 18.1715 22.17197 183.271 8429 dimension(s): from to offset delta refsys point geometry 1 9 NA NA WGS 84 FALSE time 1 4383 1998-01-01 1 days Date FALSE values geometry POLYGON ((5.871619 47.269...,...,POLYGON ((11.98261 52.460... time NULL > > # adapted from ?read_stars: > m = array(1:720, dim = c(x = 10, y = 12, t = 6)) # named dim > st = st_as_stars(m) > attr(st, "dimensions")$y$delta = -1 > attr(st, "dimensions")$y$offset = 12 > tm = as.Date("2019-02-19") + 1:6 > st = st_set_crs(st_set_dimensions(st, 3, values = tm), 4326) > > tmp = tempfile(fileext = ".tif") > write_stars(st, tmp) > > (red <- setNames(read_stars(tmp, RasterIO = list(nXOff = 1, nYOff = 1, nXsize = 10, nYSize = 12, + nBufXSize = 2, nBufYSize = 2)), "foo")) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. foo 33 199.25 365.5 365.5 531.75 698 dimension(s): from to offset delta refsys point x/y x 1 2 0 5 WGS 84 FALSE [x] y 1 2 12 -6 WGS 84 FALSE [y] band 1 6 NA NA NA NA > > sfc = st_set_crs(st_as_sfc(red, as_points = FALSE), st_crs(st)) > (a = aggregate(st, st_sf(a = 1, geom = sfc), mean)) stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 28 194.25 360.5 360.5 526.75 693 dimension(s): from to offset delta refsys point geom 1 4 NA NA WGS 84 FALSE t 1 6 2019-02-20 1 days Date FALSE values geom POLYGON ((0 12, 5 12, 5 6...,...,POLYGON ((5 6, 10 6, 10 0... t NULL > (a = aggregate(st, sfc, mean)) stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 28 194.25 360.5 360.5 526.75 693 dimension(s): from to offset delta refsys point geometry 1 4 NA NA WGS 84 FALSE t 1 6 2019-02-20 1 days Date FALSE values geometry POLYGON ((0 12, 5 12, 5 6...,...,POLYGON ((5 6, 10 6, 10 0... t NULL > if (require(raster)) { + print(a <- aggregate(st, sfc, mean, exact = TRUE)) + print(a[[1]]) + print(sum(a[[1]])*30 == sum(1:720)) + } Loading required package: raster stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 28 194.25 360.5 360.5 526.75 693 dimension(s): from to offset delta refsys point geometry 1 4 NA NA WGS 84 FALSE t 1 6 2019-02-20 1 days Date FALSE values geometry POLYGON ((0 12, 5 12, 5 6...,...,POLYGON ((5 6, 10 6, 10 0... t NULL [,1] [,2] [,3] [,4] [,5] [,6] [1,] 28 148 268 388 508 628 [2,] 33 153 273 393 513 633 [3,] 88 208 328 448 568 688 [4,] 93 213 333 453 573 693 [1] TRUE > > tm0 = as.Date("2019-02-19") + -1:8 > (a = aggregate(st, tm0, mean, na.rm = TRUE)) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. NA's A1 1 180.75 360.5 360.5 540.25 720 480 dimension(s): from to offset delta refsys point x/y time 1 10 2019-02-18 1 days Date NA x 1 10 0 1 WGS 84 FALSE [x] y 1 12 12 -1 WGS 84 FALSE [y] > (a = aggregate(st, "days", mean, na.rm = TRUE)) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 1 180.75 360.5 360.5 540.25 720 dimension(s): from to offset delta refsys point x/y time 1 6 2019-02-20 1 days Date NA x 1 10 0 1 WGS 84 FALSE [x] y 1 12 12 -1 WGS 84 FALSE [y] > yd = function(x) as.POSIXlt(x)$yday > (a = aggregate(st, yd, mean, na.rm = TRUE)) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 1 180.75 360.5 360.5 540.25 720 dimension(s): from to offset delta refsys point values x/y geometry 1 6 NA NA NA NA 50,...,55 x 1 10 0 1 WGS 84 FALSE NULL [x] y 1 12 12 -1 WGS 84 FALSE NULL [y] > > # with "by" geometry not overlapping x > pt = st_point(c(-10,-10)) > (sfc = c(sfc, st_sfc(pt, crs = st_crs(sfc)))) Geometry set for 5 features Geometry type: GEOMETRY Dimension: XY Bounding box: xmin: -10 ymin: -10 xmax: 10 ymax: 12 Geodetic CRS: WGS 84 POLYGON ((0 12, 5 12, 5 6, 0 6, 0 12)) POLYGON ((5 12, 10 12, 10 6, 5 6, 5 12)) POLYGON ((0 6, 5 6, 5 0, 0 0, 0 6)) POLYGON ((5 6, 10 6, 10 0, 5 0, 5 6)) POINT (-10 -10) > (a = aggregate(st, sfc, mean)) stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. NA's A1 28 194.25 360.5 360.5 526.75 693 6 dimension(s): from to offset delta refsys point geometry 1 5 NA NA WGS 84 FALSE t 1 6 2019-02-20 1 days Date FALSE values geometry POLYGON ((0 12, 5 12, 5 6...,...,POINT (-10 -10) t NULL > (a = aggregate(st, st, mean)) stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. A1 1 180.75 360.5 360.5 540.25 720 dimension(s): from to offset delta refsys point geometry 1 120 NA NA WGS 84 FALSE t 1 6 2019-02-20 1 days Date FALSE values geometry POLYGON ((0 12, 1 12, 1 1...,...,POLYGON ((9 1, 10 1, 10 0... t NULL > > proc.time() user system elapsed 4.76 0.29 5.09