Read and chip a Sentinel-2 SAFE product#
Read a 128-by-128, ten-channel chip from a Sentinel-2 L2A SAFE product on a 10 m grid, ready for Clay preprocessing on the GPU.
Requirements#
Use an unpacked, single-granule, compact L2A product with processing baseline 04.00 or later. Keep its metadata and referenced JPEG 2000 files together. ZIP archives, remote SAFE URLs and multigranule mosaics are unsupported.
Reading pixels requires an NVIDIA GPU, CuPy and cugeo-raster built with the
nvjpeg2k feature. Follow the GPU installation recipe
for JPEG 2000 support. Opening the product catalog and inspecting metadata
does not require the GPU codec. The PyTorch handoff below also requires
CUDA-enabled PyTorch.
cugeo-raster also supports compact L1C products and standalone PSD15 L1B
granules. L1C supports the same resolution-based subdataset workflow. L1B
provides individual raw band readers through product.open_band() and has
no map CRS or affine transform; it does not expose these subdataset groups.
See the SAFE reader reference for those profiles.
Get an L2A product#
Use Copernicus Browser to download a complete Sentinel-2 L2A product. A free Copernicus Data Space account is required; follow the registration and download instructions.
For a specific example, select tile T18TWL on 18 September 2026 and find this product:
S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE
Download the complete product ZIP, including its XML metadata and referenced
image files. Save it as product.zip and extract it into a local data
directory:
unzip product.zip -d data
The extracted .SAFE directory contains MTD_MSIL2A.xml and GRANULE/.
Keep that directory structure intact. The code below uses this example’s
directory name; adjust product_path if you downloaded a different product
or extracted it elsewhere.
Discover the subdatasets#
A SAFE product contains XML metadata and several image files. Opening its
root directory returns a product catalog. The catalog has no read() method:
select a subdataset to obtain a raster reader.
Pass the extracted L2A directory, or its MTD_MSIL2A.xml file.
from pathlib import Path
import cugeo.raster as raster
product_path = Path(
"data/S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE"
)
with raster.open(product_path) as product:
subdatasets = product.subdatasets
for name in subdatasets:
print(name)
For this product, the output is as follows, with the absolute data-directory
path shown as /data:
SENTINEL2_L2A:/data/S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE/MTD_MSIL2A.xml:10m:EPSG_32618
SENTINEL2_L2A:/data/S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE/MTD_MSIL2A.xml:20m:EPSG_32618
SENTINEL2_L2A:/data/S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE/MTD_MSIL2A.xml:60m:EPSG_32618
SENTINEL2_L2A:/data/S2A_MSIL2A_20260918T155701_N0512_R054_T18TWL_20260918T235909.SAFE/MTD_MSIL2A.xml:TCI:EPSG_32618
Each name identifies a logical raster view over the product’s image files.
It contains the absolute product XML path, a resolution or TCI selector,
and an EPSG code identifying the source CRS.
The names can be opened independently after the catalog closes. Keep the product files in place: a subdataset name refers to those files rather than copying their pixels.
L2A products can expose these groups:
Selector |
Grid spacing |
Ordered bands |
|---|---|---|
|
10 m |
B04, B03, B02, B08 |
|
20 m |
B05, B06, B07, B8A, B11, B12, AOT, CLD, SCL, SNW, WVP |
|
60 m |
B01, B09, AOT, CLD, SCL, SNW, WVP |
|
10 m |
Red, green, blue from the supplied true-colour image |
AOT is aerosol optical thickness, WVP is water vapour, SCL is scene
classification, and CLD and SNW are cloud and snow probabilities. Numeric
groups are listed when the product references every required member; TCI
is listed when its true-colour image is referenced. Use the discovered list
to choose a group available in your product.
These groups describe supplied images on their stored grids. Selecting
20m does not resample the 10m group, and the EPSG suffix does not request
reprojection. The numeric groups also include auxiliary layers, so they
are not simply all spectral images found in a resolution folder.
Read an RGB chip#
Open the 10m group and read its first three bands, B04, B03 and B02,
in red, green and blue order:
import cupy as cp
from cugeo.raster.windows import Window
name_10m = next(name for name in subdatasets if ":10m:" in name)
window = Window(col_off=0, row_off=0, width=128, height=128)
with raster.open(name_10m) as src:
rgb = src.read(indexes=(1, 2, 3), window=window)
assert rgb.shape == (3, 128, 128)
assert rgb.dtype == cp.uint16
The CuPy array has CHW axes: bands, rows, columns. These are raw spectral
samples; no reflectance scaling or display stretch is applied. The TCI
subdataset instead provides the product’s supplied UInt8 true-colour image.
Read a ten-channel chip for Clay#
Clay’s Sentinel-2 L2A configuration uses this channel order on a 10 m grid:
B02, B03, B04, B05, B06, B07, B08, B8A, B11, B12
Four bands come from the 10m group and six from 20m. Read a 128-by-128
window at 10 m and a 64-by-64 window at 20 m to cover the same 1.28 km square.
Upsample the 20 m samples with nearest-neighbour on the GPU, then combine
the bands in Clay’s order. Auxiliary layers and TCI are excluded.
Nearest-neighbour matches
Clay’s reference chip extractor.
Indexes start at one within each group: the 10m group’s indexes
(3, 2, 1, 4) select B02, B03, B04 and B08. The 20m group’s first six
indexes select B05, B06, B07, B8A, B11 and B12. You can inspect
src.tags(index)["BANDNAME"] for names such as B2 and B8A.
import cupy as cp
from cugeo.raster.windows import Window
name_10m = next(name for name in subdatasets if ":10m:" in name)
name_20m = next(name for name in subdatasets if ":20m:" in name)
window_10m = Window(col_off=0, row_off=0, width=128, height=128)
window_20m = Window(col_off=0, row_off=0, width=64, height=64)
with raster.open(name_10m) as src10, raster.open(name_20m) as src20:
assert src10.crs == src20.crs
assert src20.transform == src10.transform * raster.Affine.scale(2)
bands_10m = src10.read(indexes=(3, 2, 1, 4), window=window_10m)
bands_20m = src20.read(indexes=(1, 2, 3, 4, 5, 6), window=window_20m)
bands_20m = bands_20m.repeat(2, axis=1).repeat(2, axis=2)
chip = cp.concatenate(
(bands_10m[:3], bands_20m[:3], bands_10m[3:4], bands_20m[3:]), axis=0
)
assert chip.shape == (10, 128, 128)
assert chip.dtype == cp.uint16
The grid checks ensure that the source CRS, origin and pixel spacing align.
Each 20 m sample becomes a 2-by-2 block on the 10 m grid; upsampling adds no
spatial detail. To move the chip, use even row and column offsets in
window_10m and halve those offsets in window_20m.
The chip has CHW axes: channels, rows, columns. It contains raw stored
samples, with no radiometric conversion, normalization or cloud masking.
SAFE subdataset reads do not accept out_shape; the explicit CuPy operations
perform the upsampling and channel assembly.
Pass the chip to PyTorch#
Convert the samples to Float32 on the GPU, add a batch axis and share that allocation with PyTorch through DLPack:
import torch
chip_float = chip.astype(cp.float32)
pixels = torch.from_dlpack(chip_float[None, ...])
assert pixels.shape == (1, 10, 128, 128)
assert pixels.dtype == torch.float32
assert pixels.is_cuda
The conversion allocates Float32 storage on the GPU. DLPack shares that storage with PyTorch without copying pixels through host memory; the cast does not scale or normalize their values.
Pass pixels to your Clay preprocessing workflow. Apply the radiometric
handling and per-channel normalization expected by your checkpoint, and
supply its wavelength and time/location inputs before inference. See
Clay’s usage guide
for model input metadata and normalization statistics.