S2 subset band selection and memory needs

  1. I’m trying to subset with snappy, I cannot find how to select bands the subsetOp
    parameters- sourceBands
    in code:
    op = SubsetOp()
    there is no setSourceBands method and just giving the op.sourceBands value did not work,
    I tried with band names as tuple and list but neither worked. How does this work?

  2. I’m having memory problems and cannot write the subset. This is pretty weird, since the subset is 1700x1200 pix. Without the band selection, there are 45 bands (I managed to output one tif) so I’m assuming the masks are written as bands and that’s why the need to subset bands. This subsetting works just fine from snap gui, no memory errors at all. But the question is does snappy write large temporary files in the current folder (from which the code is run) or something?

And I have set the java memory options that have been discussed in this forum.


Hi, my recent reply also applies to your first question. Please have look.

Regarding you second question. There is no data written to the current directory. But when you read Sentinel-2 products the data is extracted from the Jpeg2000 files and written to the temporary folder for faster access.


Just to let anyone struggling with the same issue know, the parameter of type sourceBands>string,string,string,… was accepted like this: bands = “B2,B3,B4,B5,B6,B7,B8” . I would think this is string and “B2”,“B3”… is string,string,…

But the band selection did not accept the masks with their names as in masking example “opaque_clouds” or “scl_cloud_shadow” are not valid: Operator ‘SubsetOp’: Value for ‘Source Bands’ is invalid: ‘opaque_clouds’ . How can I select the masks?


Probably you need to append ‘_10m’, ‘_20m’ or ‘_60m’ to the mask names. But a ‘scl_cloud_shadow’ I haven’t seen.

Also before subsetting the data you need to resample it to a common resolution. To do so you can use the ‘Resample’ operator.

Nope, it doesn’t work with “opaque_clouds_10m” the masks are shown in snap gui in folders, do I need to get in the folder and from there the mask? I tried to give opaque_clouds/opaque_clouds_10m but it is not correct. scl/scl_cloud_shadow is also of interest.

Well, it seems that it is not possible to subset the masks in GUI either, so maybe I’ll just subset the bands.


What happens if you execute the following code:

from snappy import ProductIO
product = ProductIO.readProduct("Path to xml file")
maskGroup = product.getMaskGroup()
names = maskGroup.getNodeNames()
for name in names:

I get the result in the attached file mask-list.txt (2.6 KB)

The groups you see in SNAP Desktop are only visually, no real groups.

Maybe one of the S2-guys (@jmalik, @Nicolas, @obarrilero ) can also have a look at this.

I got the same result. But how to select/subset the masks?


OK, now I got it.
The problem is that the subset operator does not allow to specify masks. Normally this is also not necessary.
Unfortunately the masks of the S2 products are a bit special and get lost when doing a subset. This is indeed a known issue (SIITBX-183).
So how can you preserve the masks?

You can turn them into real bands by using the BandMaths operator. Simply use the mask names as expression. Afterwards you can use the Merge operator to merge the result of the Subset and the BandMaths together.

I have to admit that this is pretty inconvenient. We should give this issue a higher priority.