Using sen2r() from the command line

knitr::opts_chunk$set(echo = TRUE)

Although the easiest way to set-up and launch a processing chain is probably by setting parameters with the GUI and launching it right away, it is often very useful to be able to launch a processing from the command line without opening the GUI.

This allows using sen2r functionalities (provided both by the main function sen2r() or other package functions) as part of more complex scripts, or scheduling a processing so to automatically update a time series of S2 products.

Three main processing modes are available:

  1. specify all processing parameters in the call to sen2r();
  2. load processing parameters from a previously saved JSON file;
  3. load processing parameters from a previously saved JSON file, but change some of them in the call to sen2r().

Specify all processing parameters in the call to sen2r()

In this case, the user is expected to specify all arguments required for processing within the call to sen2r() (see sen2r() documentation for a detailed description of each option -- note that unspecified arguments will be set to default values, when possible).

For example, the following code chunk would perform the following operations:

# Set paths
out_dir_1  <- tempfile(pattern = "sen2r_out_1_") # output folder
safe_dir <- tempfile(pattern = "sen2r_safe_")  # folder to store downloaded SAFE

myextent_1 <- system.file("extdata/vector/barbellino.geojson", package = "sen2r") 

library(sen2r)
out_paths_1 <- sen2r(
  gui = FALSE,
  step_atmcorr = "l2a",
  extent = myextent_1,
  extent_name = "Barbellino",
  timewindow = c(as.Date("2020-11-13"), as.Date("2020-11-25")),
  list_prods = c("BOA","SCL"),
  list_indices = c("NDVI","MSAVI2"),
  list_rgb = c("RGB432B"),
  mask_type = "cloud_and_shadow",
  max_mask = 10, 
  path_l2a = safe_dir,
  path_out = out_dir_1
)
[2020-11-27 16:59:58] #### Starting sen2r execution. ####
[2020-11-27 16:59:59] Searching for available SAFE products on SciHub...
[2020-11-27 17:00:10] Computing output names...
Linking to GEOS 3.7.1, GDAL 2.2.3, PROJ 4.9.3
[2020-11-27 17:00:11] Processing group 1 of 5...
[2020-11-27 17:00:11] Starting to download the required level-2A SAFE products.
[2020-11-27 17:00:11] Check if products are available for download...
[2020-11-27 17:00:11] Downloading Sentinel-2 image 1 of 1
           (S2A_MSIL2A_20201114T101301_N0214_R022_T32TNS_20201114T120346.SAFE)...
[2020-11-27 17:00:59] Download of level-2A SAFE products terminated.

# (skipping most processing messages here)

After subsequent messages describing the processing operations which are being done, a report summarising the conducted processing is issued at the end:

╔══════════════════════════════════════════════════════════════════════════════
║ sen2r Processing Report
╟──────────────────────────────────────────────────────────────────────────────
║ Dates to be processed based on processing parameters: 5
║ Processing completed for: all expected dates.
╚══════════════════════════════════════════════════════════════════════════════
[2020-11-27 17:10:30] #### sen2r session terminated. ####
The processing chain can be re-launched with the command:
  sen2r("/home/lranghetti/.sen2r/proc_par/s2proc_20201127_165959.json")

In this case, it shows that all the 5 S2 images satisfying the spatial-temporal query were downloaded and properly processed.

S2 original SAFE images are stored in the folder specified by safe_dir, and are not deleted after processing (unless the user sets also the argument rm_safe to TRUE).

list.files(safe_dir)
[1] "S2A_MSIL2A_20201114T101301_N0214_R022_T32TNS_20201114T120346.SAFE"
[2] "S2A_MSIL2A_20201117T102321_N0214_R065_T32TNS_20201117T130408.SAFE"
[3] "S2A_MSIL2A_20201124T101341_N0214_R022_T32TNS_20201124T115339.SAFE"
[4] "S2B_MSIL2A_20201119T101319_N0214_R022_T32TNS_20201119T120649.SAFE"
[5] "S2B_MSIL2A_20201122T102339_N0214_R065_T32TNS_20201122T122607.SAFE"

Outputs are automatically subsetted and masked over the study area, and stored in appropriate subfolders of out_dir.

list.files(out_dir_1)
[1] "BOA"     "MSAVI2"  "NDVI"    "RGB432B" "SCL"    
list.files(file.path(out_dir_1, "NDVI"))
[1] "S2A2A_20201114_022_Barbellino_NDVI_10.tif"
[2] "S2A2A_20201117_065_Barbellino_NDVI_10.tif"
[3] "S2A2A_20201124_022_Barbellino_NDVI_10.tif"
[4] "S2B2A_20201119_022_Barbellino_NDVI_10.tif"
[5] "S2B2A_20201122_065_Barbellino_NDVI_10.tif"
[6] "thumbnails"                               

See this vignette for more info about folder structure and naming conventions of sen2r() outputs.

Load processing parameters from a previously saved JSON file

Users can set the desired parameters with the GUI, export them to a JSON file and run the command sen2r() specifying the JSON path in the argument param_list to specify processing options. For example, the command would launch sen2r() using settings specified in file "myparams.json" (or "file30ac6089ea3_sen2r_params.json" in the reproducible case):

# set the path to an existing JSON file 
# (commented here, and substituted with an instruction that creates 
# a test JSON file)
# json_path <- "/path/to/myparams.json"
json_path_2 <- build_example_param_file()
json_path_2
[1] "/tmp/RtmpHUtF1k/file9a625065871c_sen2r_params.json"
out_paths_2 <- sen2r(param_list = json_path_2)
[2020-11-27 17:25:23] #### Starting sen2r execution. ####
[2020-11-27 17:25:23] Searching for available SAFE products on SciHub...
[2020-11-27 17:25:26] Computing output names...
[2020-11-27 17:25:28] Starting to download the required level-2A SAFE products.

# (skipping most processing messages here)

╔══════════════════════════════════════════════════════════════════════════════
║ sen2r Processing Report
╟──────────────────────────────────────────────────────────────────────────────
║ Dates to be processed based on processing parameters: 1
║ Processing completed for: all expected dates.
╚══════════════════════════════════════════════════════════════════════════════
[2020-11-27 17:26:43] #### sen2r session terminated. ####
The processing chain can be re-launched with the command:
  sen2r("/home/lranghetti/.sen2r/proc_par/s2proc_20201127_172523.json")

This is for example particularly useful if a sen2r() processing requires ordering images from the LTA archive.

The user can in fact, in that case:

  1. set the processing parameters in the GUI and save them to JSON;
  2. launch the processing a first time as shown above: sen2r() will process all already online dates, and automatically order the missing ones;
  3. wait some time for the ordered images to be put back on line;
  4. launch the processing again to complete the processing.

See https://luigi.ranghetti.info/post/order-s2-lta/ for a more detailed discussion about how LTA orders are dealt with in sen2r().

Load parameters from a JSON file changing some of them in the call to sen2r()

This allows users to use a previously saved JSON file as a "template" for a processing, but changing "manually" any desired parameter.

For example, the following instructions would execute the same processing as in the previous example, but changing both the extent and time window of the analysis.

# use the previously saved JSON path
json_path_2
[1] "/tmp/RtmpDLx7qh/file30ac6089ea3_sen2r_params.json"
out_dir_3 <- tempfile(pattern = "sen2r_out_3_")  # new output folder

myextent_3 <- system.file("extdata/vector/scalve.kml", package = "sen2r")

out_paths_3 <- sen2r(
  param_list = json_path_2, 
  extent = myextent_3, 
  extent_name = "newxtent",
  timewindow = c(as.Date("2020-10-01"), as.Date("2020-10-30")),
  path_out = out_dir_3
)

This allows for example to easily run the same processing over different spatial/temporal extents.



Try the sen2r package in your browser

Any scripts or data that you put into this service are public.

sen2r documentation built on Nov. 10, 2023, 9:08 a.m.