Skip to content

Commit 21b2fee

Browse files
committed
Add argument "parallel"
1 parent 133bc74 commit 21b2fee

8 files changed

+178
-103
lines changed

R/MODIStsp.R

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
#' MODIStsp_process will abort, Default: 20
111111
#' @param verbose `logical` If FALSE, suppress processing messages,
112112
#' Default: TRUE
113+
#' @param parallel `logical` If TRUE (default), the function is run using parallel
114+
#' processing, to speed-up the computation for large rasters (with a maximum
115+
#' of 8 cores).
116+
#' The number of cores is automatically determined; specifying it is also
117+
#' possible (e.g. `parallel = 4`). In this case, more than 8 cores can be
118+
#' specified. If FALSE (default), single core processing is used.
113119
#' @param ... not used for values, forces later arguments to bind by name
114120
#' @return NULL
115121
#'
@@ -126,15 +132,15 @@
126132
#' @importFrom utils unzip
127133
#' @examples
128134
#'
129-
#' \dontrun{
130135
#' #' # - Running the tool using the GUI
131-
#'
132136
#' # Running the tool without any option will start the GUI with the default or
133137
#' # last used settings, in interactive mode (i.e., with gui = TRUE).
134-
#'
135-
#' # MODIStsp()
138+
#' \donttest{
139+
#' if (interactive()) {
140+
#' MODIStsp()
136141
#' }
137-
#' \dontrun{
142+
#' }
143+
#'
138144
#'
139145
#' #' # - Running the tool specifying processing arguments in the call
140146
#'
@@ -143,25 +149,29 @@
143149
#' # Here we process layers __NDVI__ and __EVI__ and quality indicator __usefulness__
144150
#' # of product __M*D13Q1__, considering both Terra and Aqua platforms, for dates
145151
#' # comprised between 2020-06-01 and 2020-06-15 and saves output to R tempdir
146-
#' # --> See name and available layers for product M*D13Q1
152+
#' # --> See name and available layers for product M*D13Q1.
153+
#' # Note that this example (as well as the following ones) is run in single
154+
#' # core to follow CRAN policies, by setting parallel = FALSE.
155+
#' # Users can exploit multicore functionalities skipping to set this argument.
147156
#'
148157
#' MODIStsp_get_prodlayers("M*D13A2")
149-
#'
150-
#' # --> Launch the processing
151-
#' MODIStsp(gui = FALSE,
152-
#' out_folder = "$tempdir",
153-
#' selprod = "Vegetation_Indexes_16Days_1Km (M*D13A2)",
154-
#' bandsel = c("EVI", "NDVI"),
155-
#' quality_bandsel = "QA_usef",
156-
#' indexes_bandsel = "SR",
157-
#' user = "mstp_test" ,
158-
#' password = "MSTP_test_01",
159-
#' start_date = "2020.06.01",
160-
#' end_date = "2020.06.15",
161-
#' verbose = FALSE)
158+
#' \donttest{
159+
#' MODIStsp(
160+
#' gui = FALSE,
161+
#' out_folder = "$tempdir",
162+
#' selprod = "Vegetation_Indexes_16Days_1Km (M*D13A2)",
163+
#' bandsel = c("EVI", "NDVI"),
164+
#' quality_bandsel = "QA_usef",
165+
#' indexes_bandsel = "SR",
166+
#' user = "mstp_test" ,
167+
#' password = "MSTP_test_01",
168+
#' start_date = "2020.06.01",
169+
#' end_date = "2020.06.15",
170+
#' verbose = FALSE,
171+
#' parallel = FALSE
172+
#' )
162173
#' }
163174
#'
164-
#' \dontrun{
165175
#'
166176
#' #' # - Running the tool using the settings previously saved in a specific options file
167177
#'
@@ -173,62 +183,73 @@
173183
#' # and retrieves NDVI and EVI data, plus the Usefulness Index Quality Indicator.
174184
#'
175185
#' opts_file <- system.file("testdata/test_MOD13A2.json", package = "MODIStsp")
176-
#' MODIStsp(gui = FALSE, opts_file = opts_file, verbose = TRUE)
186+
#' \donttest{
187+
#' MODIStsp(gui = FALSE, opts_file = opts_file, verbose = TRUE, parallel = FALSE)
177188
#' }
178189
#'
179-
#' \dontrun{
180190
#'
181191
#' # Running the tool using the settings previously saved in a specific option file
182192
#' # and specifying the extent from a spatial file allows to re-use the same
183193
#' # processing settings to perform download and reprocessing on a different area
184194
#'
185195
#' opts_file <- system.file("testdata/test_MOD13A2.json", package = "MODIStsp")
186196
#' spatial_file <- system.file("testdata/lakeshapes/garda_lake.shp", package = "MODIStsp")
187-
#' MODIStsp(gui = FALSE, opts_file = opts_file,
197+
#' \donttest{
198+
#' MODIStsp(
199+
#' gui = FALSE,
200+
#' opts_file = opts_file,
188201
#' spatmeth = "file",
189-
#' spafile = spatial_file, verbose = TRUE)
202+
#' spafile = spatial_file,
203+
#' verbose = TRUE,
204+
#' parallel = FALSE
205+
#' )
190206
#' }
191207
#'
192-
#' \dontrun{
193208
#'
194209
#' # Running the tool using the settings previously saved in a
195210
#' # specific options file and specifying each time the extent from a different
196211
#' # spatial file (e.g., to perform the same processing on several extents)
197-
#'
198-
#' extent_list <- c(system.file("testdata/lakeshapes/garda_lake.shp",
199-
#' package = "MODIStsp"),
200-
#' system.file("testdata/lakeshapes/iseo_lake.shp",
201-
#' package = "MODIStsp"))
202-
#' extent_list
203-
#'
204212
#' # Note that you can also put all your extent files in a specific folder and
205213
#' # create the extent list using for example.
206-
#' # extent_list = list.files(system.file("testdata/lakeshapes/", package = "MODIStsp"),
207-
#' # full.names = TRUE, "\\.shp$")
208-
#'
214+
#'
215+
#' extent_list = list.files(
216+
#' system.file("testdata/lakeshapes/", package = "MODIStsp"),
217+
#' "\\.shp$",
218+
#' full.names = TRUE
219+
#' )
220+
#' extent_list
209221
#' opts_file <- system.file("testdata/test_MOD13A2.json", package = "MODIStsp")
222+
#'
223+
#' \donttest{
210224
#' for (single_shape in extent_list) {
211-
#' MODIStsp(gui = FALSE, opts_file = opts_file,
212-
#' spatmeth = "file",
213-
#' spafile = single_shape, verbose = TRUE)
225+
#' MODIStsp(
226+
#' gui = FALSE,
227+
#' opts_file = opts_file,
228+
#' spatmeth = "file",
229+
#' spafile = single_shape,
230+
#' verbose = TRUE,
231+
#' parallel = FALSE
232+
#' )
214233
#' }
215234
#'
216235
#' # output files are placed in separate folders:
217-
#' outfiles_garda <- list.files(file.path(tempdir(), "MODIStsp/garda_lake/VI_16Days_1Km_v6/NDVI"),
218-
#' full.names = TRUE)
236+
#' outfiles_garda <- list.files(
237+
#' file.path(tempdir(), "MODIStsp/garda_lake/VI_16Days_1Km_v6/NDVI"),
238+
#' full.names = TRUE
239+
#' )
219240
#' outfiles_garda
220-
#'
221241
#' library(raster)
222242
#' plot(raster(outfiles_garda[1] ))
223243
#'
224-
#' outfiles_iseo <- list.files(file.path(tempdir(), "MODIStsp/iseo_lake/VI_16Days_1Km_v6/NDVI"),
225-
#' full.names = TRUE)
244+
#' outfiles_iseo <- list.files(
245+
#' file.path(tempdir(), "MODIStsp/iseo_lake/VI_16Days_1Km_v6/NDVI"),
246+
#' full.names = TRUE
247+
#' )
226248
#' outfiles_iseo
227-
#'
228249
#' plot(raster(outfiles_iseo[1]))
250+
#' }
229251
#'
230252
#' # See also https://docs.ropensci.org/MODIStsp/articles/noninteractive_execution.html
231-
#' }
232253

233254
MODIStsp <- function(...,
234255
gui = TRUE,
@@ -268,7 +289,8 @@ MODIStsp <- function(...,
268289
compress = NULL,
269290
test = NULL,
270291
n_retries = 5,
271-
verbose = TRUE) {
292+
verbose = TRUE,
293+
parallel = TRUE) {
272294

273295
# Make so that "raster" functions does not automatically add extensions on
274296
# output files. This is automatically reset to TRUE at the end of the session
@@ -540,7 +562,8 @@ MODIStsp <- function(...,
540562

541563
MODIStsp_process(proc_opts,
542564
n_retries = n_retries,
543-
verbose = verbose)
565+
verbose = verbose,
566+
parallel = parallel)
544567
}
545568

546569
}

R/MODIStsp_process.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#' In case any download function fails more than `n_retries` times consecutively,
2424
#' MODIStsp_process will abort, Default: 20
2525
#' @param verbose `logical` If FALSE, suppress processing messages, Default: TRUE
26+
#' @param parallel `logical` If TRUE (default), the function is run using parallel
27+
#' processing, to speed-up the computation for large rasters (with a maximum
28+
#' of 8 cores).
29+
#' The number of cores is automatically determined; specifying it is also
30+
#' possible (e.g. `parallel = 4`). In this case, more than 8 cores can be
31+
#' specified. If FALSE (default), single core processing is used.
2632
#' @return The function is called for its side effects.
2733
#' @author Lorenzo Busetto, phD (2014-2017)
2834
#' @author Luigi Ranghetti, phD (2015) \email{luigi@@ranghetti.info}
@@ -36,7 +42,8 @@
3642

3743
MODIStsp_process <- function(proc_opts,
3844
n_retries,
39-
verbose = TRUE) {
45+
verbose = TRUE,
46+
parallel = TRUE) {
4047

4148
# Based on sel_prod, retrieve needed variables from prod_opts file
4249

@@ -425,7 +432,7 @@ MODIStsp_process <- function(proc_opts,
425432
proc_opts$out_format, outrep_file, proc_opts$compress,
426433
proc_opts$out_res_sel, proc_opts$out_res, proc_opts$resampling,
427434
proc_opts$nodata_change,
428-
gui, verbose
435+
gui, verbose, parallel
429436
)
430437
}
431438
} # END Cycle on available MODIS Bands

R/MODIStsp_process_bands.R

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
#' @param outrep_file `character` Full path of the file where results of the
3030
#' processing are to be stored (created in `MODIStsp_process`)
3131
#' @param verbose `logical` If FALSE, suppress processing messages, Default: TRUE
32+
#' @param parallel `logical` If TRUE, the function is run using parallel
33+
#' processing, to speed-up the computation for large rasters (with a maximum
34+
#' of 8 cores).
35+
#' The number of cores is automatically determined; specifying it is also
36+
#' possible (e.g. `parallel = 4`). In this case, more than 8 cores can be
37+
#' specified. If FALSE (default), single core processing is used.
3238
#' @return The function is called for its side effects
3339
#' @rdname MODIStsp_process_bands
3440
#' @author Lorenzo Busetto, phD (2014-2017)
@@ -50,7 +56,7 @@ MODIStsp_process_bands <- function(out_folder_mod, modislist,
5056
out_format, outrep_file, compress,
5157
out_res_sel, out_res, resampling,
5258
nodata_change,
53-
gui, verbose) {
59+
gui, verbose, parallel) {
5460

5561
tmpdir <- file.path(tempdir(), "mstp_temp")
5662
dir.create(tmpdir, showWarnings = FALSE)
@@ -62,7 +68,13 @@ MODIStsp_process_bands <- function(out_folder_mod, modislist,
6268
# number of available cpus > 8 (this to avoid overloading servers with high
6369
# number of cpus)
6470

65-
ncores <- min(c(8, parallel::detectCores() - 2))
71+
ncores <- if (is.numeric(parallel)) {
72+
as.integer(parallel)
73+
} else if (parallel == FALSE) {
74+
1
75+
} else {
76+
min(c(8, parallel::detectCores() - 2))
77+
}
6678

6779
# check to see if the patch to correct wrong resolution/bbox in some HDF4 ----
6880
# original layers (e.g. albedo) is needed

R/split_nodata_values.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
#' Notice that the first NoData value is the only one which is considered if
4444
#' 'Change NoData values' was set to 'No'.
4545
#' @author Luigi Ranghetti, phD (2018) \email{luigi@@ranghetti.info}
46-
#' @examples
47-
#' MODIStsp:::split_nodata_values(c("255","250,254:255"))
48-
#' MODIStsp:::split_nodata_values(c("255","250,254:255"), take_all = FALSE)
4946

5047
split_nodata_values <- function(nodata_in, take_all = TRUE) {
5148

0 commit comments

Comments
 (0)