Search code examples
rimage-processingrastermasksentinel2

How to use s2_mask() function in R to mask clouds in Sentinel 2 image?


I want to use the toolbox sen2r to process Sentinel 2 L2A data in R. I have already manually downloaded the images in .SAFE format.

I have used the s2_translate() to convert .SAFE format to geotif:

in_dir <- "D:/data/s2"
out_dir <-"D:/s2_geotifs"

## translate .safe to geotif

s2_example <- file.path(
  in_dir,
  "S2B_MSIL2A_20200525T104619_N0214_R051_T31UFT_20200525T133932.SAFE")

s2_raster_dir <- s2_translate(s2_example, 
                              format="GTiff",
                              outdir = out_dir)

This results is a raster brick with 11 layers, all corresponding to the optical bands of sentinel 2 as far as i can see.

Now i want to apply the s2_mask function (specifically to band 4 and 8 because i want to make NDVI) but the documentation for the code says you need the SCL product as input. The SCL product are bands with the classified cloud pixels used for masking. If i load the .SAFE image into SNAP e.g. i can see the SCL products. However i cannot find the SCL in my s2_translate() output, or in the original .SAFE for that matter.

According to the documentation the input should be as follows:

sen2r.ranghetti.info/reference/s2_mask.html

So the issue is that i cannot find the SCL product anywhere. I have applied s2_translate as required.


Solution

  • By default, s2_translate only generates BOA output. I think you need to explicitly generate also the SCL file from the SAFE using again s2_translate, using something on the lines:

    s2_translate(s2_example,
                 prod_type = "SCL", 
                 format="GTiff",
                 outdir = out_dir)
    

    see documentation here:

    http://sen2r.ranghetti.info/reference/s2_translate.html http://sen2r.ranghetti.info/reference/safe_shortname.html