This function renames a Sentinel-2 product in order to obtain shorten names. See the details for the structure of the adopted schema (named "sen2r naming convention"). The function applies only to product names (not to single granule names), since it is thought to be applied to entire products.

safe_shortname(prod_name, prod_type = NULL, ext = NULL, res = "10m",
  tiles = NA, force_tiles = FALSE, = TRUE, set.seed = NA,
  multiple_names = FALSE, abort = FALSE)



Input Sentinel-2 product name: it can be both a complete path or a simple file name. If the file exists, the product is scanned to retrieve the tiles list and UTM zones; otherwise the basename is simply translated in he short naming convention.


(optional) Output product (default: TOA for L1C, BOA for L2A); see the details for the list of accepted products.


(optional) Extension of the output filename (default: none).


(optional) Spatial resolution (one between '10m', '20m' or '60m'); default is '10m'. Notice that, choosing '10m' or '20m', bands with lower resolution will be rescaled to res. Band 08 is used with res = '10m', band 08A with res = '20m' and res = '60m'.


(optional) Character vector with the desired output tile IDs (if specified IDs are not present in the input SAFE product, they are not produced). Default (NA) is to consider all the found tiles.


(optional) Logical: if FALSE (default), only already existing tiles (specified using the argument tiles) are used; if TRUE, all specified tiles are considered. It takes effect only if tiles is not NA.

Logical value: if TRUE (default), all the input path is maintained (if existing); if FALSE, only basename is returned.


(internal parameter) Logical value: if TRUE, the generation of random tile elements is univocal (this is useful not to assign different names to the same element); if FALSE, the generation is completely random (this is useful not to assign the same name to products with the same names (e.g. product with old name downloaded in different moments which contains different subsets of tiles). Default value is TRUE if the file exists locally, FALSE if not.


Logical: if TRUE, multiple names are returned in case the SAFE product contains more than one tile; if FALSE (default), a random tile element is used.


Logical parameter: if TRUE, the function aborts in case prod_type is not recognised; if FALSE (default), a warning is shown.


Output product name


ESA Sentinel-2 naming convention is particularly long-winded; moreover, the convention changed after December 6th 2016, causing products to follow two different schemes.

The convention here adopted, named "sen2r naming convention", follows this schema:



  • S2mll (length: 5) shows the mission ID (S2A or S2B) and the product level (1C or 2A);

  • yyyymmdd (length: 8) is the sensing date (e.g. 20170603 for 2017-06-03) ; the hour is skipped, since a single sensor can not pass two times in a day on the same tile);

  • rrr (length: 3) is the relative orbit number (e.g. 022);

  • ttttt (length: 5) is the tile number (e.g. 32TQQ);

  • ppp (length: 3) is the output product, being one of these: for level 1C:

    • TOA: 13-bands Top-Of-Atmosphere Reflectance; for level 2A:

    • BOA: 13-bands Bottom-Of-Atmosphere Reflectance;

    • TCI: True Colour Image (3-band RGB 8-bit image);

    • AOT: Aerosol Optical Thickness;

    • WVP: Water Vapour;

    • SCL: Scene Classification Map;

    • CLD: Quality Indicators for cloud probabilities;

    • SNW: Quality Indicators for snow probabilities;

    • VIS: TODO Visibility (used for AOT);

  • rr (length: 2) is the original minimum spatial resolution in metres (10, 20 or 60);

  • fff (length: variable, generally 3) is the file extension.


License: GPL 3.0


# Define product names s2_compactname_example <- file.path( "/non/existing/path", "S2A_MSIL1C_20170603T101031_N0205_R022_T32TQQ_20170603T101026.SAFE") s2_oldname_example <- "S2A_OPER_PRD_MSIL1C_PDMC_20160809T051732_R022_V20150704T101337_20150704T101337.SAFE" s2_existing_example <- "/replace/with/the/main/path/of/existing/product/with/oldname/convention" # With compact names, it works also without scanning the file safe_shortname(s2_compactname_example, ext="tif")
#> [1] "/non/existing/path/S2A1C_20170603_022_32TQQ_TOA_10.tif"
# With old names, without scanning the file the id_tile is not retrieved, # so the tile filed is replaced with a random sequence unlist(lapply(rep(s2_oldname_example,5), safe_shortname,
#> [1] "S2A1C_20150704_022_XO076_TOA_10" "S2A1C_20150704_022_BW426_TOA_10" #> [3] "S2A1C_20150704_022_KE468_TOA_10" "S2A1C_20150704_022_LB069_TOA_10" #> [5] "S2A1C_20150704_022_DP673_TOA_10"
if (FALSE) { # If the file exists locally, the tile is retrieved from the content # (if unique; if not, a random sequence is equally used, but it is # always the same for the same product) safe_shortname(s2_existing_example, }