Skip to content

Commit

Permalink
#4 filter has now year and month as args
Browse files Browse the repository at this point in the history
  • Loading branch information
hurielreichel committed Feb 22, 2023
1 parent 8880354 commit 55df987
Show file tree
Hide file tree
Showing 30 changed files with 241 additions and 29 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidyopeneo
Type: Package
Title: tidyopeneo : Wrap dplyr functions for openEO datacubes (OpenEOWrap)
Title: tidyopeneo : Wrap dplyr functions for openEO datacubes (tidyopeneo)
Version: 0.1.0
Author: Huriel Reichel
Maintainer: The package maintainer <huriel.reichel@protonmail.com>
Expand All @@ -13,7 +13,7 @@ Encoding: UTF-8
LazyData: true
Depends: R (>= 3.3.3), dplyr, openeo, cli, rjson
Imports: sf, methods
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown,
Expand Down
2 changes: 1 addition & 1 deletion R/datacube.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' openeo::processes()$load_collection() (https://processes.openeo.org/#load_collection).
#' This is understood as the **starting point** when working with tidyopeneo. (optional)
#' @param data ProcessNode datacube from openeo (optional)
#' @param .con (optional) character link to openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) character link to openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import openeo
Expand Down
184 changes: 180 additions & 4 deletions R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#' never both.
#' For **filter_bbox**, the bounding box, which may include a vertical axis
#' (see `base` and `height`).
#' @param .year integer or list of integers stating the years you want to filter on the
#' datacube.
#' @param .month integer or list of integers referring to the months you want to filter
#' on the datacube.
#' @param .dimension (optional) For **filter_temporal** : The name of the temporal dimension
#' to filter on. If no specific dimension is specified or it is set to `null`,
#' the filter applies to all temporal dimensions. Fails with a `DimensionNotAvailable`
Expand All @@ -28,7 +32,7 @@
#' filtering, specified as GeoJSON.
#' @param .context (optional) : any Additional data to be passed to the condition.
#' Mandatory for filter_labels or array_filter processes.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand All @@ -39,10 +43,17 @@
#' library(sf)
#'
#' con = connect(host = "https://openeo.cloud")
#' dc = datacube(id = "SENTINEL_5P_L2")
#' dc = datacube(id = "SENTINEL1_GRD")
#'
#' # filter_temporal
#' dc_y = dc %>% filter(.year = c(2020, 2021, 2022))
#'
#' dc_m = dc %>% filter(.month = c(6,7,8))
#'
#' dc_ym = dc %>% filter(.year = c(2020, 2021, 2022), .month = c(6,7,8))
#'
#' # filter_temporal and filter_bbox
#' dc = dc %>%
#' dc2 = dc %>%
#' filter(.extent = c("2021-01-01", "2021-03-03")) %>%
#' filter(.extent = c(west = 6.09, east = 6.99, south = 46.15, north = 46.57))
#'
Expand All @@ -55,7 +66,7 @@
#' st_bbox() %>%
#' st_as_sfc()
#'
#' dc = dc %>% filter(.geometries = pol)
#' dc = dc2 %>% filter(.geometries = pol)
#'
#' # array_filter
#' # ToDO...
Expand All @@ -65,6 +76,7 @@
filter.datacube <- function(.data = NULL, ...,
.condition = NULL, .dimension = NULL, .context = NULL,
.extent = NULL, .geometries = NULL,
.year = NULL, .month = NULL,
.p = openeo::processes(.con), .con = NULL) {

#check dots ...
Expand All @@ -89,6 +101,170 @@ filter.datacube <- function(.data = NULL, ...,
dc = .p$filter_temporal(data = .data, extent = .extent, dimension = .dimension)
cli::cli_alert_success("filter_temporal applied")

## Working with years
} else if (!is.null(.year) & is.null(.month) & is.null(.geometries) & is.null(.condition)&
is.null(.context)){

### Extract starting year of collection
process_json = .p$save_result(data = .data, format = list_file_formats()$output$JSON) %>%
as("Process") %>% openeo::toJSON() %>% rjson::fromJSON() %>% suppressWarnings()
id = process_json$process_graph[[1]]$arguments$id

collections = list_collections() %>% suppressWarnings()
native_time_ext = c(collections[[id]]$extent$temporal[[1]][[1]] %>% as.Date() %>% format("%04Y-%m-%d"),
collections[[id]]$extent$temporal[[1]][[2]])
native_time_ext = ifelse(is.na(native_time_ext), Sys.Date() %>% format("%04Y-%m-%d"), native_time_ext)

### stop if year outside of range
if (min(.year) < native_time_ext[1] %>% substr(1,4) | max(.year) > native_time_ext[2] %>% substr(1,4)){
stop(
cli::cli_alert_danger("You're trying to filter outside of the range of the collection {native_time_ext}")
)
}

### Iterate through the called years
dcs = list()
for (i in 1:length(.year)){
dcs[[i]] <- .p$filter_temporal(
data = .data,
extent = c(paste(.year[i], "01-01", sep = "-"), paste(.year[i], "12-31", sep = "-")),
dimension = .dimension)
}

### merge generated data cubes
if (length(dcs) > 1){
for (i in 1:length(dcs)){
if (i == 1){i = i+1}
else if (i == 2){
dc = .p$merge_cubes(dcs[[i]], dcs[[i-1]])
} else {
dc = .p$merge_cubes(dc, dcs[[i]])
}
}
cli::cli_alert_success("merge_cubes applied")

### Do not merge --- simple filter for a single year
} else {
dc = .p$filter_temporal(
data = .data,
c(paste(.year, "01-01", sep = "-"), paste(.year, "12-31", sep = "-")),
dimension = .dimension)
cli::cli_alert_success("filter_temporal applied")
}

## Working with Months
} else if (is.null(.year) & !is.null(.month) & is.null(.geometries) & is.null(.condition)&
is.null(.context)){

### Extract time extent of collection
process_json = .p$save_result(data = .data, format = list_file_formats()$output$JSON) %>%
as("Process") %>% openeo::toJSON() %>% rjson::fromJSON() %>% suppressWarnings()
id = process_json$process_graph[[1]]$arguments$id

collections = list_collections() %>% suppressWarnings()
native_time_ext = c(collections[[id]]$extent$temporal[[1]][[1]] %>% as.Date() %>% format("%04Y-%m-%d"),
collections[[id]]$extent$temporal[[1]][[2]])
native_time_ext = ifelse(is.na(native_time_ext), Sys.Date() %>% format("%04Y-%m-%d"), native_time_ext)

### Iterate through the existing years and called months
years = seq(native_time_ext[1] %>% substr(1, 4), native_time_ext[2] %>% substr(1, 4), 1)
months = ifelse(.month < 10, paste0("0", .month), .month) %>% as.character()
next_months = ifelse((.month + 1) == 13, 1, .month)
next_months = ifelse(next_months < 10, paste0("0", next_months), next_months) %>% as.character()
dcs = list()
for (y in 1:length(years)){
for (m in 1:length(months)){
date1 = paste(years[y], months[m], "01", sep = "-")
date2 = paste(years[y], next_months[m], "01", sep = "-")
date2 = as.Date(date2) - 1
date2 = date2 %>% format("%04Y-%m-%d")

if (date1 < native_time_ext[1] | date2 > native_time_ext[2]){
next
}

dcs <- append(dcs, .p$filter_temporal(
data = .data, extent = c(date1, date2, dimension = .dimension)))
}
}
cli::cli_alert_success("filter_temporal applied")

### merge generated data cubes
for (i in 1:length(dcs)){
if (i == 1){i = i+1}
else if (i == 2){
dc = .p$merge_cubes(dcs[[i]], dcs[[i-1]])
} else {
dc = .p$merge_cubes(dc, dcs[[i]])
}
}
cli::cli_alert_success("merge_cubes applied")

## Working with Months and Years
} else if (!is.null(.year) & !is.null(.month) & is.null(.geometries) & is.null(.condition)&
is.null(.context)){

### Extract time extent of collection
process_json = .p$save_result(data = .data, format = list_file_formats()$output$JSON) %>%
as("Process") %>% openeo::toJSON() %>% rjson::fromJSON() %>% suppressWarnings()
id = process_json$process_graph[[1]]$arguments$id

collections = list_collections() %>% suppressWarnings()
native_time_ext = c(collections[[id]]$extent$temporal[[1]][[1]] %>% as.Date() %>% format("%04Y-%m-%d"),
collections[[id]]$extent$temporal[[1]][[2]])
native_time_ext = ifelse(is.na(native_time_ext), Sys.Date() %>% format("%04Y-%m-%d"), native_time_ext)

### stop if year outside of range
if (min(.year) < native_time_ext[1] %>% substr(1,4) | max(.year) > native_time_ext[2] %>% substr(1,4)){
stop(
cli::cli_alert_danger("You're trying to filter outside of the range of the collection {native_time_ext}")
)
}

### Iterate through the existing years and called months
years = .year
months = ifelse(.month < 10, paste0("0", .month), .month) %>% as.character()
next_months = ifelse((.month + 1) == 13, 1, .month)
next_months = ifelse(next_months < 10, paste0("0", next_months), next_months) %>% as.character()
dcs = list()
for (y in 1:length(years)){
for (m in 1:length(months)){
date1 = paste(years[y], months[m], "01", sep = "-")
date2 = paste(years[y], next_months[m], "01", sep = "-")
date2 = as.Date(date2) - 1
date2 = date2 %>% format(., "%04Y-%m-%d")

if (date1 < native_time_ext[1] | date2 > native_time_ext[2]){
next
}

dcs <- append(dcs, .p$filter_temporal(
data = .data, extent = c(date1, date2, dimension = .dimension)))
}
}
cli::cli_alert_success("filter_temporal applied")

### merge generated data cubes
if (length(dcs) > 1){
for (i in 1:length(dcs)){
if (i == 1){i = i+1}
else if (i == 2){
dc = .p$merge_cubes(dcs[[i]], dcs[[i-1]])
} else {
dc = .p$merge_cubes(dc, dcs[[i]])
}
}
cli::cli_alert_success("merge_cubes applied")

### Do not merge --- simple filter for a single year
} else {
dc = .p$filter_temporal(
data = .data,
c(paste(.year, "01-01", sep = "-"), paste(.year, "12-31", sep = "-")),
dimension = .dimension)
cli::cli_alert_success("filter_temporal applied")
}

#filter_bbox
} else if (length(.extent == 4) & is.null(.geometries) & is.null(.condition)&
is.null(.context)) {
Expand Down
2 changes: 1 addition & 1 deletion R/group_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
#' and/or times. Is only required to be specified if the values for the start of
#' the temporal intervals are not distinct and thus the default labels would not
#' be unique. The number of labels and the number of groups need to be equal.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli sf
Expand Down
2 changes: 1 addition & 1 deletion R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#' after, so an overlap of 8 pixels will add 8 pixels on both sides of the window,
#' so 16 in total.Be aware that large overlaps increase the need for computational
#' resources and modifying overlapping data in subsequent operations have no effect.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion R/rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' `DimensionNotAvailable` exception if the specified dimension does not exist.
#' @param .target A new Name for the dimension. Fails with a `DimensionExists`
#' exception if a dimension with the specified name exists.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion R/resample.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#' to no-data (`null`).
#' @param .target_process (optional) either "spatial" to apply **resample_cube_spatial** or
#' "temporal" to apply **resample_cube_temporal**.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @details if arg .target is not defined, resample_spatial is gonna be run.
Expand Down
2 changes: 1 addition & 1 deletion R/select.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' specified in micrometers. The order of the specified array defines the
#' order of the bands in the data cube. If multiple bands match the wavelengths,
#' all matched bands are included in the original order.
#' @param .con openeo connection. Default to "https://openeo.cloud"
#' @param .con openeo connection. Default to NULL
#' @param .p processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' If n is greater than the number of days available in the collection or datacube (or prop > 1), the result
#' will be truncated to the group size. If the proportion of a group size does not yield an integer number of
#'rows, the absolute value of prop * ndays(.data) is rounded down.
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion R/summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param .dimension The name of the dimension over which to reduce. Fails with
#' a `DimensionNotAvailable` exception if the specified dimension does not exist.
#' @param .context (optional) Additional data to be passed to the reducer (optional).
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion R/summarize.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param .dimension The name of the dimension over which to reduce. Fails with
#' a `DimensionNotAvailable` exception if the specified dimension does not exist.
#' @param .context (optional) Additional data to be passed to the reducer (optional).
#' @param .con (optional) openeo connection. Default to "https://openeo.cloud"
#' @param .con (optional) openeo connection. Default to NULL
#' @param .p (optional) processes available at .con
#' @return datacube
#' @import dplyr openeo cli
Expand Down
2 changes: 1 addition & 1 deletion man/datacube.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions man/filter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/group_by.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 55df987

Please sign in to comment.