Skip to content

Commit

Permalink
Merge pull request #102 from njtierney/substitute-arg
Browse files Browse the repository at this point in the history
Simpler `tar_format()` with `targets` 1.8.0
  • Loading branch information
Aariq authored Oct 21, 2024
2 parents 44c0640 + dc57501 commit 7c0d174
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 276 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Language: en-GB
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
targets (>= 1.7.0),
targets (>= 1.8.0),
rlang (>= 1.1.3),
cli (>= 3.6.2)
Suggests:
Expand Down
99 changes: 38 additions & 61 deletions R/tar-stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,63 +204,50 @@ tar_stars_raw <- function(name,

driver <- rlang::arg_match0(driver, drv$name)

.read_stars <- eval(substitute(
function(path) {

## TODO: it appears envvar custom resources do not work in read function?
READ_FUN <- stars::read_stars
# mdim <- as.logical(Sys.getenv("GEOTARGETS_GDAL_RASTER_MDIM", unset = FALSE))
if (mdim) {
READ_FUN <- stars::read_mdim
}

# ncdf <- as.logical(Sys.getenv("GEOTARGETS_USE_NCMETA", unset = FALSE))
if (ncdf && requireNamespace("ncmeta")) {
READ_FUN <- stars::read_ncdf
}

# proxy <- as.logical(Sys.getenv("GEOTARGETS_PROXY", unset = FALSE))
READ_FUN(path, proxy = proxy)

}, list(ncdf = ncdf, mdim = mdim, proxy = proxy)))

# TODO: should multidimensional array use the same `options` as 2D?
.write_stars <- function(object, path) {

WRITE_FUN <- stars::write_stars

mdim <- as.logical(Sys.getenv("GEOTARGETS_GDAL_RASTER_MDIM",
unset = FALSE))
if (mdim) {
WRITE_FUN <- stars::write_mdim
}

dr <- Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER")

# stars requires character(0), not "", for no options set
co <- Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS")
co2 <- strsplit(co, ";")[[1]]

WRITE_FUN(
object,
path,
overwrite = TRUE,
driver = dr,
options = co
)
}

targets::tar_target_raw(
name = name,
command = command,
pattern = pattern,
packages = packages,
library = library,
format = targets::tar_format(
read = .read_stars,
write = .write_stars,
marshal = function(object) object, # Not currently used
unmarshal = function(object) object
read = function(path) {
if (ncdf) {
geotargets:::check_pkg_installed("ncmeta")
stars::read_ncdf(path, proxy = proxy)
} else if (isTRUE(mdim)) {
stars::read_mdim(path, proxy = proxy)
} else {
stars::read_stars(path, proxy = proxy)
}
},
write = function(object, path) {
if (mdim) {
stars::write_mdim(
object,
path,
overwrite = TRUE,
driver = driver,
options = options
)
} else {
stars::write_stars(
object,
path,
overwrite = TRUE,
driver = driver,
options = options
)
}

},
substitute = list(
ncdf = ncdf,
mdim = mdim,
proxy = proxy,
driver = driver,
options = options
)
),
repository = repository,
iteration = "list", #the only option that works
Expand All @@ -269,17 +256,7 @@ tar_stars_raw <- function(name,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = utils::modifyList(targets::tar_resources(
custom_format = targets::tar_resources_custom_format(
#these envvars are used in read and write functions of format
envvars = c("GEOTARGETS_GDAL_RASTER_DRIVER" = driver,
"GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" =
paste0(options, collapse = ";"),
"GEOTARGETS_GDAL_RASTER_MDIM" = mdim,
"GEOTARGETS_PROXY" = proxy,
"GEOTARGETS_USE_NCMETA" = ncdf)
)
), resources),
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue,
Expand Down
27 changes: 7 additions & 20 deletions R/tar-terra-rast.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ tar_terra_rast <- function(name,
retrieval = targets::tar_option_get("retrieval"),
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")) {
check_pkg_installed("terra")

filetype <- filetype %||% "GTiff"
gdal <- gdal %||% character(0)

#check that filetype option is available
drv <- get_gdal_available_driver_list("raster")
filetype <- rlang::arg_match0(filetype, drv$name)

check_pkg_installed("terra")

#ensure that user-passed `resources` doesn't include `custom_format`
if ("custom_format" %in% names(resources)) {
Expand Down Expand Up @@ -95,16 +95,14 @@ tar_terra_rast <- function(name,
terra::writeRaster(
object,
path,
filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"),
filetype = filetype,
overwrite = TRUE,
gdal = strsplit(
Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS",
unset = ";"),
";")[[1]]
gdal = gdal
)
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
unmarshal = function(object) terra::unwrap(object),
substitute = list(filetype = filetype, gdal = gdal)
),
repository = repository,
iteration = "list", #only "list" works right now
Expand All @@ -113,18 +111,7 @@ tar_terra_rast <- function(name,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = utils::modifyList(
targets::tar_resources(
custom_format = targets::tar_resources_custom_format(
#these envvars are used in write function of format
envvars = c(
"GEOTARGETS_GDAL_RASTER_DRIVER" = filetype,
"GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = (
paste0(gdal, collapse = ";")
)
)
)
), resources),
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue,
Expand Down
120 changes: 67 additions & 53 deletions R/tar-terra-sprc.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ tar_terra_sprc <- function(name,
name = name,
command = command,
pattern = pattern,
type = "sprc",
filetype = filetype,
gdal = gdal,
packages = packages,
library = library,
format = format_terra_collections(type = "sprc"),
repository = repository,
error = error,
memory = memory,
Expand Down Expand Up @@ -216,12 +216,12 @@ tar_terra_sds <- function(name,
tar_terra_collection_raw(
name = name,
command = command,
pattern = pattern,
type = "sds",
filetype = filetype,
pattern = pattern,
gdal = gdal,
packages = packages,
library = library,
format = format_terra_collections(type = "sds"),
repository = repository,
error = error,
memory = memory,
Expand All @@ -243,12 +243,12 @@ tar_terra_sds <- function(name,
tar_terra_collection_raw <- function(
name,
command,
type = type,
filetype = geotargets_option_get("gdal.raster.driver"),
gdal = geotargets_option_get("gdal.raster.creation.options"),
pattern = NULL,
packages = targets::tar_option_get("packages"),
library = targets::tar_option_get("library"),
format = format_terra_collections(),
repository = targets::tar_option_get("repository"),
error = targets::tar_option_get("error"),
memory = targets::tar_option_get("memory"),
Expand All @@ -261,32 +261,46 @@ tar_terra_collection_raw <- function(
cue = targets::tar_option_get("cue"),
description = targets::tar_option_get("description")
) {

targets::tar_target_raw(
name = name,
command = command,
pattern = pattern,
packages = packages,
library = library,
format = format,
format = targets::tar_format(
read = switch(type,
"sprc" = function(path) terra::sprc(path),
"sds" = function(path) terra::sds(path)
),
write = function(object, path) {
for (i in seq(object)) {
if (i > 1) {
opt <- "APPEND_SUBDATASET=YES"
} else {
opt <- ""
}
terra::writeRaster(
x = object[i],
filename = path,
filetype = filetype,
overwrite = (i == 1),
gdal = c(opt, gdal)
)
}
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object),
substitute = list(type = type, gdal = gdal, filetype = filetype)
),
repository = repository,
iteration = "list",
error = error,
memory = memory,
garbage_collection = garbage_collection,
deployment = deployment,
priority = priority,
resources = utils::modifyList(
targets::tar_resources(
custom_format = targets::tar_resources_custom_format(
#these envvars are used in write function of format
envvars = c(
"GEOTARGETS_GDAL_RASTER_DRIVER" = filetype,
"GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS" = (
paste0(gdal, collapse = ";")
)
)
)
), resources),
resources = resources,
storage = storage,
retrieval = retrieval,
cue = cue,
Expand All @@ -295,39 +309,39 @@ tar_terra_collection_raw <- function(
}




#' Format function for sprc and sds
#' @noRd
format_terra_collections <- function(type = c("sprc", "sds")) {
type <- match.arg(type)
targets::tar_format(
read = switch(type,
"sprc" = function(path) terra::sprc(path),
"sds" = function(path) terra::sds(path)
),
write = function(object, path) {
gdal <- strsplit(
Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS",
unset = ";"),
";")[[1]]

for (i in seq(object)) {
if (i > 1) {
opt <- "APPEND_SUBDATASET=YES"
} else {
opt <- ""
}
terra::writeRaster(
x = object[i],
filename = path,
filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"),
overwrite = (i == 1),
gdal = c(opt, gdal)
)
}
},
marshal = function(object) terra::wrap(object),
unmarshal = function(object) terra::unwrap(object)
)
}
#'
#'
#' #' Format function for sprc and sds
#' #' @noRd
#' format_terra_collections <- function(type = c("sprc", "sds")) {
#' type <- match.arg(type)
#' targets::tar_format(
#' read = switch(type,
#' "sprc" = function(path) terra::sprc(path),
#' "sds" = function(path) terra::sds(path)
#' ),
#' write = function(object, path) {
#' gdal <- strsplit(
#' Sys.getenv("GEOTARGETS_GDAL_RASTER_CREATION_OPTIONS",
#' unset = ";"),
#' ";")[[1]]
#'
#' for (i in seq(object)) {
#' if (i > 1) {
#' opt <- "APPEND_SUBDATASET=YES"
#' } else {
#' opt <- ""
#' }
#' terra::writeRaster(
#' x = object[i],
#' filename = path,
#' filetype = Sys.getenv("GEOTARGETS_GDAL_RASTER_DRIVER"),
#' overwrite = (i == 1),
#' gdal = c(opt, gdal)
#' )
#' }
#' },
#' marshal = function(object) terra::wrap(object),
#' unmarshal = function(object) terra::unwrap(object)
#' )
#' }
Loading

0 comments on commit 7c0d174

Please sign in to comment.