Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The availability of Sentinel-2 satellite data has been a turning point for a widespread kind of users, which can now take advantage from a dataset sufficiently dense (revisiting time of 5 days) to perform time series analysis, and which is characterised by a decametric resolution that allows to discriminate many natural and agronomic targets.

Data access is free (upon registration to ESA data hub). Although many solutions are available to obtain images (a detailed list is available here), R users should download them manually (or using ESA API Hub) and then perform some preprocessing operations before being able to use them (e.g, converting SAFE format, clipping and reshaping images on their own area of interest, masking cloudy areas, computing spectral indices, etc.).

Some of these recurrent operations can now be easily performed using the R package sen2r. This post provides an overview of its main functionalities; I will deepen specific possibilities offered by the package during next weeks.

# What is sen2r for

The main functions offered by the package are here above listed:

• search the list of available SAFE archives over a user defined area of interest in a certain time window;
• download them, in the level 1C or 2A depending on user requirements1;
• if level 2A images are not available, download and automatically correct with Sen2Cor the corresponding 1C images;
• perform geometric transformation: merge adjacent tiles, clip (and eventually mask) on the area of interest, reproject and/or resize the output;
• apply a cloud mask, basing of the automatic classification map available in level 2A product;
• compute spectral indices;
• generate RGB images.

# Installation and documentation

The source code of the package is hosted on GitHub at ranghetti/sen2r repository (it will be published on CRAN in future) and documented here. The Installation page provides the instructions for installing the package (and its dependences) on several Linux distributions and over Windows. Support for MacOS was not yet provided, but Mac users can use it in a Docker image (see the page Run in a Docker container).

# Launching sen2r with the GUI

The most useful feature of sen2r, especially for users with limited R skills, is the possibility to set the parameters of the processing using a graphical user interface. To do it, simply launch the command

sen2r()

Using the GUI is quite self-explaining (some help popups are present in the interface – to see them, click on the icons “”). Here below some screenshots.

# Non-interactive execution

The function sen2r() can also be launched without opening the GUI, setting the parameters of the processing using the arguments of the function. To know how to set them, see the sen2r() documentation. See an example here above:

sen2r(
gui = FALSE,
timewindow = c(as.Date("2019-01-01"), Sys.Date()),
extent = "~/sen2rtest/my_extent.kml", extent_name = "Myextent",
list_prods = c("BOA"),
list_indices = c("NDVI", "MSAVI2"),
path_l1c = "~/SAFE", path_l2a = "~/SAFE", path_out = "~/sen2rtest/out1"
)

Moreover, it is possible to interactively set the parameters in a first time, and to non-interactively launch the processing later (or on a different machine). This can be useful i.e. in order to schedule a recurrent execution of a processing chain, so ti keep a local archive updated (I will show how to do it in a future post). To do it, just set the GUI and then click on “Save options as…”: this allows to export a JSON file containing the parameters (which can be eventually edited also by hand). Then, launch the function with

sen2r("~/sen2rtest/param.json")

using the path of the paramter file as argument.

If the user wants to use a saved JSON file changing only some parameters, he can also pass both the file path and some arguments:

sen2r(
"~/sen2rtest/param.json",
extent = "~/sen2rtest/another_extent.kml", extent_name = "Anothersite",
path_out = "~/sen2rtest/out2"
)

this will reproduce the same processing chain in a different area of interest.

sen2r() is not the only function provided by the package; other ones are available to perform intermediate steps or specific tasks. The reference page provides a list of them.

# Conclusions

The new package sen2r provides ready to use functions to facilitate the processing of Sentinel-2 images, especially for R users but also for people with limited R skills /thanks to the GUI). Further development is required to implement new features and to improve the stability; users are encouraged to report bugs and suggestions opening an issue on the GitHub page of the package.

## Credits

sen2r is developed by Luigi Ranghetti and Lorenzo Busetto (IREA-CNR), and it is released under the GNU GPL-3 license.

The functionalities to search and download SAFE tiles are based on the Python tool Sentinel-download by Olivier Hagolle, released under the GNU General Public License version 2 (GPL‑2).

Using sen2r for production (including scientific products) requires to cite it, using the following entry:

Ranghetti, L. and Busetto, L. (2019). sen2r: an R toolbox to find, download and preprocess Sentinel-2 data. R package version 1.0.0. DOI: 10.5281/zenodo.1240384. URL: http://sen2r.ranghetti.info.

@Manual{sen2r,
title  = {sen2r: an R toolbox to find, download and preprocess Sentinel-2 data},
author = {Luigi Ranghetti and Lorenzo Busetto},
year   = {2019},
note   = {R package version 1.0.0},
doi    = {10.5281/zenodo.1240384},
url    = {http://sen2r.ranghetti.info},
}

1. functions to search and download SAFE archives are based on a modified version of the Python script Sentinel-download by Olivier Hagolle (see Credits).