Sentinel-2 Level 1C to Surface Reflectance¶
A Sentinel-2 Level 1C scene is reflectance at the top of the atmosphere. zeit.level2 takes a product, a .SAFE directory or just its name, to surface reflectance: the water vapour and the gases, the aerosol estimated from the scene's dark targets, the adjacency effect, the topographic and BRDF corrections, and a cloud, shadow, snow and water mask from Zeit's own Fmask. It is what FORCE's force-l2ps does for one image, in the Zeit core.
.SAFE directory or the name of a product, and a DEM in metres (any grid and CRS) or dem="glo30"Dataset on the scene's 10 m grid: boa, qai, dst, vzn, hot, aod, wvpWhat is, and what is not, equal to FORCE
With FORCE's cloud flags, sun, geography and BRDF factor as inputs, every product of level2 equals that of force-l2ps in 100 % of the pixels. The public function does not take those from FORCE: it computes the cloud mask with Zeit's Fmask, the sun with Zeit's own implementation and the BRDF factor with nbar's c-factor. With them it differs a little from FORCE, as the validation shows. It is for Sentinel-2 only and keeps the products on the scene's 10 m grid. The merge of the 20 m bands (merge=) and the coregistration to a reference (reference=) are optional, off by default, and done on the TOA reflectance before the correction, as FORCE does; the merge is compared with FORCE, the coregistration is not (it is AROSICS', FORCE's is another method).
What you need¶
- The
.SAFEdirectory of a Level 1C product:MTD_MSIL1C.xmland oneGRANULE/<name>/with itsIMG_DATA. The 13 band files are read by GDAL (the product's JPEG 2000 files, or GeoTIFFs of the same names). - A DEM in metres, on any grid and CRS: Zeit puts it on the scene's 10 m grid (bilinear by default,
dem_resampling).topography=True, the default, needs it, and without it the call is aValueErrorthat says to pass the DEM or usetopography=False. The atmosphere uses the elevation and the Fmask uses the DEM when it is given. - Optionally, a Global Surface Water Occurrence raster (percent) to help the Fmask find water (
gswo).
Find and download a scene¶
The scene and the DEM can come from the network, with no account or credential:
scenes = zeit.find_l1c(tile="23LKC", start="2023-07-01", end="2023-07-31", max_cloud=30)
safe = zeit.download_l1c(scenes.iloc[0]) # the path of the .SAFE; a DataFrame downloads them all
dem = zeit.download_dem(safe) # one GeoTIFF of Copernicus GLO-30 over the scene
l2 = zeit.level2(safe, dem=dem)
Or in one call, with the name of the product and dem="glo30":
find_l1c searches the sentinel-2-l1c collection of Earth Search. download_l1c takes the files from Google Cloud's public bucket gcp-public-data-sentinel-2, because that one keeps the original layout of the product: only what level2 reads (the two XML files and the 13 band files, 672 MB in the 2023 scene of the validation), each checked by size and MD5, and an interrupted download resumes. They go to the cache (ZEIT_CACHE_DIR/l1c) unless dest= says otherwise. A product that Earth Search lists and the bucket does not have (some reprocessed ones) is a ValueError that names it.
download_dem mosaics the GLO-30 tiles over the scene in their own geometry. Where there is no tile (the sea) the value is the no-data of level2, an area with no tile at all is a ValueError that suggests topography=False, and a scene across the antimeridian is not handled.
Run it¶
import zeit
l2 = zeit.level2(
"S2B_MSIL1C_20230728T132239_N0509_R038_T23LKC_20230728T150149.SAFE",
dem="cop_glo30_23LKC.tif",
)
print(l2)
The result is an xarray.Dataset on the grid of the scene (10 m, the CRS and transform of the bands). attrs tell how the correction went: datetime (UTC), sensor, baseline, aod_green (the optical depth of the scene in B03), aod_map (False when the fallback spectrum was used), water_vapor_cm, n_targets (the valid water, shadow and vegetation dark targets), the summary of the Fmask as fmask_* keys, core_seconds and provenance.
Without a DEM, switch the topographic correction off:
The products¶
| Variable | Dimensions | What it is |
|---|---|---|
boa |
(band, y, x), int16 |
Surface reflectance times 10000, _FillValue -9999. band is BLUE, GREEN, RED, REDEDGE1, REDEDGE2, REDEDGE3, BROADNIR, NIR, SWIR1, SWIR2 (BROADNIR is B08, NIR is B8A) |
qai |
(y, x), int16 |
FORCE's quality bits: no data, cloud, shadow, snow, water, and what the correction flagged (below) |
dst |
(y, x), int16 |
Distance to the nearest cloud, shadow or snow, in metres on the 10 m grid (0 on the targets). 32767 in the whole image when the scene has no cloud target |
vzn |
(y, x), int16 |
View zenith angle, in degrees times 100 |
hot |
(y, x), int16 |
Haze optimized transformation, in FORCE's units |
aod |
(y, x), int16 |
Aerosol optical depth of the scene's green band, times 1000 |
wvp |
(y, x), int16 |
Water vapour, in cm times 1000 |
Reflectance in 0-1 comes from boa, with the fill value left out:
Reading the QAI flags¶
qai packs several flags in each 16-bit value. The ones to know, by bit position (bit 0 is the least significant):
| Bit(s) | Flag | Values |
|---|---|---|
| 0 | No data (outside the scene) | 1 = off |
| 1-2 | Cloud state | 0 none, 1 less confident (the buffer), 2 confident and opaque, 3 cirrus |
| 3 | Cloud shadow | 1 = shadow |
| 4 | Snow | 1 = snow |
| 5 | Water | 1 = water |
| 6-7 | Aerosol | 0 good, 1 interpolated, 2 high AOD, 3 fill value of the scene |
| 8 | Sub-zero reflectance | the surface reflectance was below 0 in a band |
| 9 | Saturation | the surface reflectance was above 1 in a band |
| 10 | Low sun | the sun's zenith is above 75 degrees |
| 11-12 | Illumination | 0 good, 1 moderate, 2 poor, 3 deep shadow |
| 13 | Slope | the slope is above 2 degrees |
| 14 | Water vapour fill | the water vapour of the pixel is the scene's average |
The cloud buffer of the QAI is 300 m, as in FORCE, and Zeit's Fmask never marks cirrus. A clear-sky mask, then, is a few bit operations:
q = l2.qai
off = (q & 1) != 0
cloud = ((q >> 1) & 3) != 0
shadow = ((q >> 3) & 1) != 0
snow = ((q >> 4) & 1) != 0
clear = ~(off | cloud | shadow | snow)
red = l2.boa.sel(band="RED").where(clear) / 10000
nir = l2.boa.sel(band="BROADNIR").where(clear) / 10000
ndvi = (nir - red) / (nir + red)
The same decoding is built in, for the qai of Zeit's level 2 and for the QAI of FORCE's own ARD:
usable = zeit.qa_mask(l2.qai, "force") # True where FORCE would keep the pixel
fmask = zeit.qa_to_fmask(l2.qai, "force") # 0 clear, 1 water, 2 shadow, 3 snow, 4 cloud, 255 no data
qa_mask follows FORCE's default screening of the higher-level modules (SCREEN_QAI = NODATA CLOUD_OPAQUE CLOUD_BUFFER CLOUD_CIRRUS CLOUD_SHADOW SNOW SUBZERO SATURATION): it removes no data, every cloud state (buffer, opaque, cirrus), shadow, snow, sub-zero and saturated pixels, and keeps water, aerosol, low sun, illumination, slope and water vapour flags. qa_to_fmask has no code for sub-zero and saturation, so a 0 or 1 there is not always usable in qa_mask.
Options¶
| Option | Default | What it does |
|---|---|---|
brdf |
True |
Normalises the BRDF to nadir view and the sun at the zenith of the scene (FORCE's DO_BRDF). The factor is nbar's c-factor (Roy et al. 2016, 2017), not FORCE's brdf_factor; it enters the AOD retrieval even with brdf=False |
topography |
True |
The topographic correction of the illumination (DO_TOPO). Needs dem |
aod |
True |
Estimates the aerosol optical depth from the scene's dark targets; False uses the fallback spectrum |
adjacency |
True |
The environmental weighting of the adjacency effect |
multiple_scattering |
True |
False is single scattering |
erase_cloud |
False |
Sets the cloud pixels to no data |
flags |
None |
The cloud, shadow, snow and water bits of the QAI per pixel, int16 (nrows, ncols) on the 10 m grid (only bits 1 to 5 are read). With them the Fmask does not run |
fmask |
True |
Runs Zeit's Fmask when flags is None. False runs no cloud detection: there is no cloud flag, and dst is 32767 in the whole image |
gswo |
None |
Global Surface Water Occurrence (percent) for the Fmask, put on the 20 m grid by nearest neighbour |
fmask_compat |
False |
True reproduces three oddities of the original Fmask 4.7 code; the default corrects them |
dem_resampling |
"bilinear" |
The resampling of the DEM onto the grid of the scene (FORCE's default) |
n_jobs |
8 |
Threads of the correction, the Fmask and the JPEG 2000 decoder |
merge |
None |
"regression" or "improphe": FORCE's resolution merge (RES_MERGE), the red edge 1-3, B8A, SWIR1 and SWIR2 predicted at 10 m from green, red and the broad NIR, on the TOA reflectance before the correction (see below) |
reference |
None |
A raster path or DataArray to coregister the scene to (AROSICS' global shift, coregister), on the TOA before the merge and the correction |
coreg_band |
"BROADNIR" |
The band of the scene that is matched to the reference |
coreg_ref_band |
None |
The band of reference to match (the first by default) |
Merge the 20 m bands and coregister¶
FORCE does both before the atmospheric correction, on the TOA reflectance, and so does level2 (the coregistration first): the aerosol, the vapour and the correction then see the merged and shifted bands, and every product comes out aligned.
l2 = zeit.level2(safe, dem="dem.tif", merge="improphe") # 10 m red edge, B8A, SWIR
l2 = zeit.level2(safe, dem="dem.tif", reference="base_nir.tif", coreg_band="BROADNIR", coreg_ref_band=1)
print(l2.attrs["coreg_x_shift_px"], l2.attrs["coreg_y_shift_px"], l2.attrs["coreg_success"])
The merge predicts a pixel only if it is not off and has no cloud and no shadow in the QAI (snow and water are predicted), as FORCE does, and uses the arithmetic of FORCE (force=True of sentinel2_resmerge). The coregistration finds one shift on coreg_band (a 10 m band is best) and moves the 13 bands (cubic) and the QAI (nearest neighbour) by it; the reference may be on another grid, it is put on the scene's. If no shift is found the scene is processed as it is and attrs say it (coreg_success, coreg_reason). The pixels the shift uncovers are off. The shift is AROSICS', not FORCE's: FORCE's coregister is another method, so it is not compared with FORCE.
On tile 23LKC (2023), with FORCE's cloud flags, sun, geography and BRDF factor, the merge gives the same qai, dst, hot, aod, vzn and wvp as force-l2ps with RES_MERGE in every pixel of the 120,560,400, and boa differs in 9 to 70 pixels per band with ImproPhe (by 1 to 3) and in 196 to 271 pixels of each of the six merged bands with the regression (by 2 to 12; compared with a FORCE run on one thread: FORCE's regression is not deterministic with several). The merge alone takes 11.4 s with ImproPhe and 182 s with the regression on this scene (8 threads; the regression of FORCE's arithmetic runs on one). A reference made from the scene's own BROADNIR moved by +2.5 pixels in x and -1.5 in y was found as +2.47 and -1.47 (reliability 95.6).
The merge costs memory: the whole call peaked at 21.6 GiB of RAM without it and at 28.7 GiB with either method (the full 10980 x 10980 tile, 8 threads), and took 146 s without it, 158 s with ImproPhe and 332 s with the regression.
Your own cloud mask¶
Zeit's Fmask runs at 20 m. If you have a mask you trust more, give it as flags: the Fmask does not run, and the QAI carries your bits.
import numpy as np
flags = np.zeros((10980, 10980), dtype=np.int16) # the scene's 10 m grid: (rows, columns)
flags[my_cloud] |= 2 << 1 # cloud state 2: confident and opaque
flags[my_shadow] |= 1 << 3 # shadow
l2 = zeit.level2(safe, dem="dem.tif", flags=flags)
Water and the Fmask¶
l2 = zeit.level2(safe, dem="dem.tif", gswo="gswo_23LKC.tif")
fmask_summary = {k: v for k, v in l2.attrs.items() if k.startswith("fmask_")}
The fmask_* keys of attrs are the Fmask's summary, as plain numbers, booleans and strings, so the Dataset can be written to NetCDF.
Save and look¶
l2.zeit.save("l2_20230728") # a folder: boa.tif, qai.tif, dst.tif, ...
l2.zeit.save("l2_20230728.tif") # or one multi-band file
l2.zeit.plot() # a selector of the variables
See .zeit.save and .zeit.plot for the options.
A whole series¶
A time series needs every scene of a tile over some dates, not one. zeit.level2_series does the whole chain, one scene at a time, and returns the series as one lazy cube:
cube = zeit.level2_series("23LKC", "2023-01-01", "2023-12-31", "l2_23LKC", max_cloud=60)
print(cube.dims, cube.dtype) # ('time', 'band', 'y', 'x') float32
print(cube.attrs["failed"]) # [] when every scene worked
It searches with find_l1c (or takes the products= you give: names, local .SAFE folders or a table with a product column), downloads each product, downloads the DEM once for the tile (dem="glo30"; a DEM path, or dem=None with level2_kw={"topography": False}, also work), runs level2 and saves the scene. The options of level2 go in level2_kw, for example level2_kw={"merge": "improphe", "brdf": False}; safe, dem and n_jobs are set by the series itself (n_jobs=8 is the threads of each scene).
What it writes in out_dir:
l2_23LKC/
S2B_MSIL1C_20230115T131239_N0509_R138_T23LKC_20230115T145717/ one folder per scene
boa.tif qai.tif dst.tif vzn.tif hot.tif aod.tif wvp.tif what .zeit.save writes
.zeit-level2.json the marker: the scene is done
...
dem/ the GLO-30 DEM of the tile, downloaded once
level2_series.json the journal: what was done and failed, with the errors
Resume, failures and duplicates. The call can be interrupted and repeated: a scene with its marker is skipped, one that was interrupted is redone from scratch and one that failed is tried again. A scene that fails (a download error, a product that the bucket lacks) is recorded in the journal and the others go on; cube.attrs["failed"] and a warning name it, and the call raises a RuntimeError only if no scene succeeded. Writing into a folder that has scenes made with other options (level2_kw or DEM) is an error: choose another out_dir. Two processings of the same acquisition (different baseline or processing time) are duplicates: the newest is kept and the older one is not processed. cube.attrs["journal"] is the path of the journal, with the status of every product (done, skipped, failed or duplicate) and the errors.
RAM, time and disk. A scene is processed at a time: on tile 23LKC (10980 x 10980 pixels, 8 threads) one took about 146 s and peaked at 21.6 GiB of RAM, and 28.7 GiB with level2_kw={"merge": ...}. Plan the RAM for that, not for the series. The downloaded .SAFE of a scene (about 670 MB) is deleted once the scene is done, unless keep_safe=True (safe_dir says where they go, out_dir/.safe by default). The disk is the boa.tif and qai.tif of every scene (and the other products); a warning says when the free space looks short after the first scene.
The cube. load_level2_series(out_dir) reopens the scenes later, without computing a pixel, and level2_series returns the same cube for the scenes of the run. It is float32, reflectance times 10000, (time, band, y, x) with the bands of boa (BLUE ... SWIR2), and it is NaN where the scene has no data and where FORCE's default screening of the QAI removes the observation (mask="force": no data, cloud states 1 to 3, shadow, snow, sub-zero and saturation; water is kept). mask=None leaves only the fill value as NaN. All the scenes must share one grid, or like= puts them on one.
cube = zeit.load_level2_series("l2_23LKC") # later, in another session
nir = zeit.temporal_metrics(cube, ["p10", "median", "p90", "count"], band="NIR") # (metric, y, x)
by_year = zeit.temporal_metrics(cube, ["mean"], band="NIR", by="year")
segments = zeit.ccdc(cube.sel(band=["GREEN", "RED", "NIR", "SWIR1", "SWIR2"]))
The same in N calls¶
level2_series only calls public functions. If you want to swap a step (another source for the scenes, your own DEM, your own cloud mask), this is the loop it runs:
from pathlib import Path
out = Path("l2_23LKC")
scenes = zeit.find_l1c(tile="23LKC", start="2023-01-01", end="2023-12-31", max_cloud=60)
dem = None
for product in scenes["product"]:
safe = zeit.download_l1c(product)
if dem is None:
dem = zeit.download_dem(safe, dest=out / "dem") # once for the tile
zeit.level2(safe, dem=dem).zeit.save(out / product.removesuffix(".SAFE"))
for folder in sorted(p for p in out.iterdir() if (p / "boa.tif").exists()):
boa = zeit.load_raster(folder / "boa.tif", masked="auto")
qai = zeit.load_raster(folder / "qai.tif", masked="auto").squeeze(drop=True)
usable = zeit.qa_mask(qai, "force")
clear = boa.where(usable) # NaN where FORCE would screen it out
What the series adds to this loop is the resume (the marker and the journal), the duplicates, the failures that do not stop the run, the deletion of the .SAFE, one grid check, and the cube with its dates. zeit.load_level2_series builds the cube from the folders that have a marker, so a loop of your own has to stack the scenes itself.
The same from the command line¶
It runs the series and prints how many scenes are done, skipped, failed and duplicates, the names of the failed ones and the journal; the exit code is 1 if any failed. See zeit level2.
Validation¶
level2 was compared with force-l2ps of FORCE 3.11.00 (8 threads) on tile 23LKC (Brazil, UTM 23S, 10980 x 10980 pixels at 10 m). The scenes and the FORCE outputs are not in the repository. The tables are in Against the Original Implementations.
With FORCE's inputs. Given FORCE's cloud flags, sun, geography and BRDF factor, boa (the 10 bands), qai, aod, wvp, hot, dst and vzn are equal to FORCE's in 100 % of the pixels in three runs: 2023-07-28 without BRDF, 2023-07-28 with BRDF (baseline 05.09) and 2021-07-28 without BRDF (baseline 03.01). In 2023 that is 120,560,400 pixels per band.
With Zeit's own sun. The sun of level2 is a clean-room implementation, up to 0.0105 degrees from FORCE's. With it and FORCE's cloud flags (2023, without BRDF), the pixels equal to FORCE's go from 75.04 % (blue) to 92.45 % (SWIR2), and the mean difference from 0.56 to 0.08 in reflectance times 10000.
With Zeit's Fmask. Its cloud-or-shadow mask marks 19.93 % of the pixels against 19.70 % in FORCE's, agreeing in 98.12 % of them (IoU 0.910). Only the shadow diverges, by construction: FORCE marks shadow also inside the confident cloud and uses another version of the Fmask. Against the Sen2Cor scene classification of ESA (2023), the Fmask of Zeit and FORCE's QAI agree with it in 91.28 % of the pixels (IoU 0.58 in cloud or shadow), and mark about 20 % against its 13 %, with the 300 m ring included.
Against ESA's L2A. The L2A of the same tile and date is another chain, so no equality is expected, and FORCE against the L2A is the control. On the pixels valid in the three products, clear in both masks and classified by the SCL as vegetation or bare soil (23,405,935 pixels in 2023), the bias in reflectance times 10000 is:
| Band (2023-07-28, baseline 05.09) | Zeit minus L2A | FORCE minus L2A |
|---|---|---|
| BLUE | -24.58 | -25.13 |
| RED | 6.16 | 6.05 |
| NIR | 34.94 | 34.92 |
| SWIR1 | -174.71 | -174.70 |
| SWIR2 | -242.22 | -242.22 |
Zeit and FORCE are at nearly the same distance from the L2A in every band. The L2A is 165 to 242 above both in the two SWIR bands; that bias was not checked against the literature, so it is not known which chain is closer to the truth.
Speed. The whole chain with Zeit's Fmask took 141 to 147 s (113 to 119 s in the core) against 197 s for force-l2ps, both with 8 threads, on a machine that was not dedicated: these times are not a clean benchmark.
Not compared. A scene without any cloud target gives dst 32767 in every pixel; that case was not compared with FORCE.
What it does not do¶
- It is for Sentinel-2 only.
- It keeps the products on the 10 m grid of the scene. FORCE's reprojection to its datacube is not done.
- It merges the 20 m bands and coregisters only when asked (
merge=,reference=), and only the global shift ofcoregister; the local warp ofcoregister_local, the merge of a time series and the other uses ofsentinel2_resmergeare steps of their own: Preparing images.
Credits¶
level2 is the atmospheric correction of FORCE 3.11.00 (David Frantz, GPL-3.0-or-later; Frantz 2019, Remote Sensing 11:1124) in the Zeit core. The cloud, shadow, snow and water mask is Zeit's port of Fmask 4.7 (Qiu, Zhu and Woodcock 2019, Remote Sensing of Environment 231:111205). The distance to cloud (dst) is rewritten in a clean room from FORCE's public documentation and the outputs of its DST product, and the sun is Zeit's own clean-room implementation. The full signature and every parameter are in the API reference.