Hi,
I am trying to create a cloud mask from the Sentinel-2 schene classification band (SCL). Particularly I want to flag pixels with a value of 3 (cloud shadow), 8 (clouds medium probability) and 9 (clouds high probability). I tried to adjust the example shown here.
Here’s the code (not strictly reproducible due to authentication, but if you have a connection you should be able to process it):
library(openeo)
collections = "SENTINEL2_L2A_SENTINELHUB"
bands = c("B02", "B03", "B04", "B08", "SCL")
period = c("2021-10-01", "2021-10-31")
ext = list(11.00204, 49.66901, 11.28197, 49.85029)
names(ext) = c("west", "south", "east", "north")
# change to login with your credentials
# con = openeo::connect("https://openeo.cloud")
# login(
# login_type = "oidc"
# , provider = "egi"
# , config = list(
# client_id = "<client_id>"
# , secret = "<secret>"
# )
# )
procs = processes()
cube = procs$load_collection(
id = collections
, spatial_extent = ext
, temporal_extent = period
, bands = bands
)
filterSCL = function(data, context) {
cl_shadow = procs$eq(data[5], y = 3)
cl_medium = procs$eq(data[5], y = 8)
cl_high = procs$eq(data[5], y = 9)
cl = procs$or(cl_medium, cl_high)
cl = procs$or(cl, cl$shadow)
return(cl)
}
mask = procs$reduce_dimension(
data = cube
, reducer = filterSCL
, dimension = "bands"
)
## apply mask
cube_masked = procs$mask(cube, mask)
## create process graph
graph = procs$save_result(
data = cube_masked
, format = "GTiff"
)
## create and start job
job = openeo::create_job(graph)
openeo::start_job(job)
id = as.character(job$id)
jobs = openeo::list_jobs()
dirout = path.expand("~/Downloads/openEO/mask")
if (!dir.exists(dirout)) dir.create(dirout)
openeo::download_results(
job = jobs[[id]]$id
, folder = dirout
)
## create stars object
fls = list.files(dirout, pattern = "^openEO_", full.names = TRUE)
stars = stars::read_stars(fls, along = "time")
stars = stats::setNames(stars, nm = "value")
Now my questions:
- Obiously “or” only compares two values, not many. Chaining several “or” statements is not very flexible (and not elegant). Is there a better way how to do this?
- Subsetting to the SCL band by name (as in the linked example) throws a warning and returns an “error” from the backend. Subsetting by index works. Any idea why?