---
title: "Using sen2r() from the command line"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using sen2r() from the command line}
  %\VignetteEngine{knitr::knitr}
  %\usepackage[UTF-8]{inputenc}
---

```{r setup, include=FALSE}
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 
<span style="color:#5793dd;vertical-align:top;font-size:90%;font-weight:normal;">sen</span><span style="color:#6a7077;vertical-align:baseline;font-size:115%;font-weight:bolder;">2</span><span style="color:#2f66d5;vertical-align:baseline;font-size:90%;font-weight:bold;">r</span>
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()`](#specify-all-processing-parameters-in-the-call-to-sen2r);
2. [load processing parameters from a previously saved JSON file](#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()`](#load-parameters-from-a-json-file-changing-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:

- download all S2 images acquired over the area specified in the 
    `barbellino.geojson` spatial file between 1 and 15 July 2019;
- process only dates for which the cloud mask derived from the SCL dataset
    (type `"cloud_and_shadow"`, in this case)
    shows that less than 10% of the area of interest was covered by clouds;
- create the following output products:
    1. BOA reflectances;
    2. Scene Classification Map (SCL);
    3. spectral indices NDVI and MSAVI2;
    4. true colour RGB obtained from BOA reflectances.

```{r eval=FALSE}
# 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. 
<!--However, outputs for two of those dates were not created because cloudiness %
over the spatial extent was above the specified threshold.-->

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`).

```{r eval=FALSE}
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`. 

```{r eval=FALSE}
list.files(out_dir_1)
```
```
[1] "BOA"     "MSAVI2"  "NDVI"    "RGB432B" "SCL"    
```
```{r eval=FALSE}
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](outstructure.html) 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):

```{r eval=FALSE}
# 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"
```
```{r eval=FALSE}
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 
(see https://scihub.copernicus.eu/userguide/LongTermArchive).

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.

```{r eval=FALSE}
# use the previously saved JSON path
json_path_2
```
```
[1] "/tmp/RtmpDLx7qh/file30ac6089ea3_sen2r_params.json"
```
```{r eval=FALSE}
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. 
