The function safe_getMetadata() scans a Sentinel2 product (main path, granule path, main / granule xml file or GDAL object) to retrieve information about the product.

The accessory function rm_invalid_safe() remove a SAFE archive in the case it is not recognised by safe_getMetadata().

The accessory function safe_isvalid() scan the SAFE name to understand if it is a valid SAFE.

safe_getMetadata(s2, info = "all", abort = TRUE)


safe_isvalid(s2, info = "fileinfo")



A Sentinel-2 product, being both a character (path of an existing product, or simply product name) or python object of class osgeo.gdal.Dataset. This input parameter can be the main path of a S2 file, the path of the xml with metadata, the path of a single granule, the xml path of a single granule, or a 'osgeo.gdal.Dataset' object (obtained reading the product with python). If the product does not exist locally, the function can run only with option info = "nameinfo" (see below).


(optional) A character vector with the list of the metadata which should be provided. Accepted values are:

  • "all" (default): all the retrievable metadata are provided;

  • "fileinfo": only the metadata obtained by scanning the file name and product structure (without opening it with GDAL) are provided.

  • "nameinfo": only the metadata obtained by scanning the file name are provided (it is faster and there is no need to have downloaded yet the file).

  • a vector of single specific information (one or more from the followings):

    • "prod_type" ('singlegranule' or 'product');

    • "version" ('old' or 'compact');

    • "tiles" (vector with the tiles ID available in the product);

    • "utm" (vector with the UTM zones used in the product);

    • "xml_main" (name of the main XML file with metadata);

    • "xml_granules" (names of the XML with granule metadata);

    • "level" ('1C' or '2A');

    • "creation_datetime", "id_tile", "mission", "centre", "file_class", "id_orbit", "orbit_number", "sensing_datetime", "id_baseline": metadata specific of the product type and version (they are returned only if obtainable for the specified input);

    • "clouds", "direction", "orbit_n", "preview_url", "proc_baseline", "level", "sensing_datetime", "nodata_value", "saturated_value": information retrieved from the metadata stored in the XML file.

    In this version, querying for specific elements requires the product to be present in the filesystem; in future this will be changed (see the second example for a workaround to scan for specific elements without needing the file to have been downloaded).


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


safe_getMetadata() returns a list of the output metadata;

rm_invalid_safe() returns TRUE if the s2 product was removed, FALSE elsewhere.

safe_isvalid() returns TRUE if the product is a valid SAFE, FALSE if not.


License: GPL 3.0


# Define product name s2_examplename <- "S2A_MSIL1C_20170703T101021_N0205_R022_T32TNS_20170703T101041.SAFE" # Return only the information retrievable from the file names (files are not scanned) safe_getMetadata(s2_examplename, info="nameinfo")
#> $prod_type #> [1] "product" #> #> $version #> [1] "compact" #> #> $mission #> [1] "2A" #> #> $level #> [1] "1C" #> #> $sensing_datetime #> [1] "2017-07-03 10:10:21 UTC" #> #> $id_baseline #> [1] "0205" #> #> $id_orbit #> [1] "022" #> #> $id_tile #> [1] "32TNS" #> #> $creation_datetime #> [1] "2017-07-03 10:10:41 UTC" #>
# Return some specific information without scanning files safe_getMetadata(s2_examplename, info="nameinfo")[c("level", "id_tile")]
#> $level #> [1] "1C" #> #> $id_tile #> [1] "32TNS" #>
# Return a single information without scanning files # (in this case, the output is a vector instead than a list) safe_getMetadata(s2_examplename, info="nameinfo")[["level"]]
#> [1] "1C"
if (FALSE) { # Download a sample SAFE archive (this can take a while) s2_exampleurl <- paste0("", "Products(\'5f590bcb-ee55-4a20-8e75-bde99f5b93d4\')/$value") names(s2_exampleurl) <- "S2A_MSIL1C_20170703T101021_N0205_R022_T32TNS_20170703T101041.SAFE" s2_download(s2_exampleurl, outdir=tempdir()) s2_examplepath <- file.path(tempdir(), names(s2_exampleurl)) # Return all the available information safe_getMetadata(s2_examplepath) # Return some specific information safe_getMetadata(s2_examplepath, info=c("tiles", "level", "id_tile")) # Return a single information safe_getMetadata(s2_examplepath, info="orbit_n") # Delete it if it is not recognised rm_invalid_safe(s2_examplepath) }