The GRSprocessor algorithm (Harmel et al., 2018) corrects high-spatial resolution multispectral images (Sentinel-2, Landsat 8/9) acquired over water for the atmosphere and for the sunlight reflected by the water surface (sunglint), which can reach the sensor of nadir or near-nadir viewing missions with SWIR bands. The processor corrects successively for:
- the gaseous absorption,
- the diffuse light of the sky and its reflection by the air-water interface,
- the sunglint, estimated in the SWIR bands,
to retrieve the remote-sensing reflectance
grs S2B_MSIL1C_20220731T103629_N0400_R008_T31TFJ_20220731T124834.SAFE \
--cams_file cams_forecast_2022-07.nc --resolution 20Installing on the CNES TREX cluster? See README_CNES.md for CNES-specific installation and run instructions.
GRSprocessor needs Python 3.11 or newer. It is a pure Python package: all its dependencies (including GRSdriver, which reads the L1C images) are installed by pip, and the GDAL Python bindings are not needed.
Create an environment, with conda or venv:
conda create -n grs python=3.12
conda activate grs
# or: python -m venv grs && source grs/bin/activateThe cloud masks use OmniCloudMask, which installs PyTorch. On a computer without a GPU, install the CPU build of PyTorch first, much smaller than the default (CUDA) one:
pip install torch --index-url https://download.pytorch.org/whl/cpuThen install GRSprocessor from PyPI (the package and the commands are grs):
pip install GRSprocessoror from the source code (e.g. for the development version):
git clone https://github.com/CNES/GRSprocessor.git
cd GRSprocessor
pip install . # or: pip install -e .[dev] to developCheck the installation:
grs -v
grs_configThe radiative transfer look-up tables (LUTs, about 1 GB of NetCDF files shared with hGRS) are not in the
package: they are downloaded from Zenodo (checksums verified)
the first time an image is processed, into the default LUT folder (~/.local/share/grs/lut on Linux).
To download them beforehand and check the configuration:
grs_config --download
grs_config- To keep the LUTs in another folder (e.g. a copy shared by several users), write it in your user
configuration file (
~/.config/grs/config.yml) withgrs_config --init /path/to/lut, or set theGRS_DATA_ROOTenvironment variable. - Without internet access (e.g. on the nodes of a cluster), download the LUTs beforehand and switch the
automatic download off with
GRS_NO_DOWNLOAD=1(orzenodo: auto_download: falsein the user configuration).
The configuration is read at run time: no need to reinstall the package after changing it. See the Configuration page of the documentation.
GRSprocessor uses the CAMS global atmospheric composition forecasts (aerosols, ozone, water vapour, pressure, wind) of the date of the image, from the Copernicus Atmosphere Data Store (ADS):
-
give a CAMS NetCDF file with
--cams_file; -
or let GRSprocessor find it in a CAMS archive: without
--cams_file, the forecasts are read from theYYYY/MM/DDtree ofpath: camsof the configuration,<path: cams>/YYYY/MM/DD/YYYY-MM-DD-cams-global-atmospheric-composition-forecasts.nc(global file of the day, e.g. updated every day byecmwf/download_cams_cds_day.py), or, if there is none, from the file of the tile in the same folder (..._<tile>.nc), downloaded from the ADS if needed. The download usescdsapi: create an ADS account, accept the licence of the dataset CAMS global atmospheric composition forecasts, and write your API key in~/.cdsapirc:url: https://ads.atmosphere.copernicus.eu/api key: <your-personal-access-token>
Process a Sentinel-2 L1C image (.SAFE folder) at 20 m:
grs S2B_MSIL1C_20220731T103629_N0400_R008_T31TFJ_20220731T124834.SAFE \
--cams_file cams_forecast_2022-07.nc --resolution 20 --odir ./L2AThe L2A product is a Zarr store (<odir>/<name>/<name>.zarr) with grs -h lists them all):
| option | |
|---|---|
--resolution res |
pixel size (m): 10, 20 or 60 for Sentinel-2 (default 60) |
--aot_resolution res |
pixel size (m) of the grid of the aerosol retrieval (default 240, aerosol section of the configuration) |
--dem_file file |
digital elevation model (GeoTIFF) of the tile, for the pressure correction |
--surfwater file |
external water mask (GeoTIFF) |
--max_cloud_cover cc |
skip the images whose L1C cloud cover is above cc (0-1) |
--opac_model name |
force the aerosol model (OPAC) |
--no_clobber |
skip the images already processed |
--snap_compliant |
one variable per band, for ESA SNAP |
Inputs and outputs can be on an object storage (S3 or S3-compatible) or a web server: the Level-1 product,
--cams_file, --dem_file, --surfwater and --odir accept URLs (s3://, https://...), and so does the
GRS_CONFIG configuration file (e.g. a raw file on a Git server). The credentials and the endpoint of the
bucket are those of the AWS environment (AWS_PROFILE, AWS_ENDPOINT_URL...):
export AWS_PROFILE=my-profile AWS_ENDPOINT_URL=https://s3.example.org
grs s3://bucket/l1c/S2B_MSIL1C_..._.SAFE --cams_file s3://bucket/cams/cams_forecast_2022-07.nc \
--odir s3://bucket/l2a --resolution 20Install the S3 / HTTPS drivers with pip install GRSprocessor[remote] (s3fs). The Level-1 bands are read in place
by GRSdriver (version with remote reading, GRSdriver.remote), the other
input files are copied once into a local cache (GRS_CACHE_DIR), the Zarr store is written directly to the
bucket, and the log files are written locally (--log_dir) then copied next to the product.
Exit status of grs (and of exe/launcher.py), for the processing chains: only 0 gives a product for
the next steps (e.g. Level-2B).
| status | |
|---|---|
| 0 | L2A product written (or, with --no_clobber, already there) |
| 1 | processing error (traceback in log_file.log and error.log) |
| 2 | invalid arguments, missing or unreadable input (Level-1, CAMS, SURFWATER, LUT) |
| 3 | image skipped on purpose, no product: cloud cover above --max_cloud_cover, or not enough water (marker file <name>_no_water.txt) |
From Python:
import grs
process = grs.Process()
process.execute('S2B_MSIL1C_...SAFE', cams_file='cams_forecast_2022-07.nc', resolution=20,
odir='S2B_MSIL2AGRS_....zarr')
process.write_output() # the processing is lazy: it is computed block by block while writingLandsat 8 and 9 Collection 2 L1 images (LC08_L1*, LC09_L1* folders) are processed in the same way.
See the tutorials for the processing, the reading of the L2A
products with xarray and their visualization with grstbx.
If you are on TREX CNES, see README_CNES.md for how to run GRSprocessor as a SLURM interactive job.
The image is processed lazily, block by block, by processor: ncpu threads (8 by default, user
configuration). For a Sentinel-2 tile at 20 m, expect about 10 GB of memory and a few minutes of computing
time; lower ncpu if memory is tight. The grs command limits the number of malloc arenas of glibc to 2
(MALLOC_ARENA_MAX=2, already set in the Docker images), which saves 2-3 GB: set this variable too when
running GRSprocessor from your own Python scripts.
Examples of output images before (1st column) and after (2nd column) sunglint correction:
pip install -e .[dev]
pytest grs/tests/unit/ # about 45 sgrs/tests/unit/: configuration and aerosol settings, Level-1 product, CAMS data and archive, smoothing filters, gaseous transmittance and correction (in memory and with dask), log files, command line helpers, masking, output product (Zarr and NetCDF), utilities, the retrievals of theKernel(grs/tests/unit/kernel/: LUT preparation, wind, aerosol, final processing), and an end-to-end processing of a small L1C subset (128 x 128 pixels at 60 m over the Camargue,grs/tests/data, made withgrs/tests/data/make_test_data.py) checking the L2A product. No Git LFS data is needed. The end-to-end tests are skipped when the look-up tables are not installed (grs_config --download).
See examples in exe.
First and foremost, you must have the corresponding version of GRSdriver at the same level as grs2. You should also make sure that the grsdata folder is full (it is a LTS). Eventually, you must get Dockerfile out of grs2 folder to have a structure as diplayed below.
head_folder
├grs2
├GRSdriver
└Dockerfile
Note that anything in this folder tree will be added to the Docker build context, so make it light. You might consider removing the notebooks and all useless files and directories from the grs2 & GRSdriver folders to make the resulting image as light as possible (.git, illustration, notebook...).
Once all those requirements are met, you can compile the Docker image using the following command:
docker build -t grs2:<version_tag> *path_to_head_folder* -f *path_to_Dockerfile*
When the compilation has ended, you can access the image with the command:
docker images
To run the Docker image in a container on a S2 raster you can use the run_docker.sh script as follow:
./run_docker.sh <image_ID> <S2_raster_path> <CMAS_data_path> <desired_name_for_output> <desired_path_for_output> <desired_resolution> <surfwater_tif_path>
Example:
./run_docker.sh grs2:V2_CNES \
/DATA/S2_raster/S2B_MSIL1C_20220228T102849_N0400_R108_T31TFJ_20220228T123819.SAFE \
/DATA/CAMS/2022-02-28-cams-global-atmospheric-composition-forecasts.nc \
S2B_L2Agrs_20220228T102849_N0400_R108_T31TFJ_20220228T123819 \
/DATA/grs_outputs \
60 \
/DATA/Surfwater/SURFWATER_OPTICAL-SINGLE_T31TFJ_20220228T103850_20220228T103850_1-0-4_06/SURFWATER_OPTICAL-SINGLE_T31TFJ_20220228T103850_20220228T103850_1-0-4_06.tif
The docker containers will be called grs2, which mean that you cannot currently launch multiple ones simultaneously. You can adapt the sh script to modify this behaviour.
GRSprocessor is developed on GitHub and mirrors part of its pipeline to an internal CNES GitLab instance for HPC-based validation. Two independent pipelines run on every push.
GitHub Actions (public, .github/workflows/)
main.yml — Pipeline CI (push to feature/*, release/*,
hotfix/*, bugfix/*, main, develop, tags; and pull requests to main/develop):
python-tests— installs the Python dependencies (Python 3.12, no GDAL), then runs the unit tests (grs/tests/unit/) with coverage, and writes a summary of the tests and of the coverage in the job page. On a tag, it also regenerates and commits the coverage badge onmain.podman-build(needspython-tests) — builds the Docker image (Dockerfile-github, Ubuntu 24.04, Python 3.12); on every push, it also publishes it to Docker Hub ascnes/grsprocessor:dev, except onmain(branch or tag) where it publishescnes/grsprocessor:<version>instead.lint(pull requests and tags, needspodman-build) — runsruffandmypyand uploads their reports as artifacts (non-blocking).
The package is published on PyPI by pypi.yml only (below).
gitlab-sync.yml — Sync GitLab CNES: mirrors the Gitflow
branches (feature/*, release/*, hotfix/*, bugfix/*) to the internal GitLab
(gitlab.cnes.fr/waterquality/grs2), opens a GitLab merge request for each GitHub pull request into
main/develop, and creates a GitLab release for each version tag. The Git LFS pointer files are
stripped on the way (mirror-to-gitlab action). This is
what triggers the GitLab pipeline described below.
pypi.yml — Publish on PyPI (tags v*, or by hand from the
Actions tab to TestPyPI or PyPI): runs the unit tests, then builds the sdist and the wheel of
GRSprocessor and publishes them with PyPI trusted publishing (see "Releasing on PyPI" in
CONTRIBUTING.md).
doc.yml — Pipeline documentation (branch pushes that modify
docs/, and tags): builds the Sphinx documentation (docs/, sphinx-book-theme, autosummary,
myst-nb notebooks, mermaid diagrams; also built on Read the Docs); on a tag it
publishes it to GitHub Pages.
GitLab CI (internal CNES, .gitlab-ci.yml)
Runs on the mirrored repository at CNES. See README_CNES.md for the detailed stage-by-stage breakdown.
In short: GitHub Actions runs the public-facing pipeline (tests, lint, PyPI, Docker Hub, docs), while the GitLab mirror runs CNES-internal validation (HPC functional tests against reference data, code quality/security gates) that cannot run outside the CNES network.
Contributions are welcome! Quick start:
git clone https://github.com/CNES/GRSprocessor.git
cd GRSprocessor
conda create -n grs_dev python=3.11
conda activate grs_dev
pip install -r requirements.txt
pip install -e .[dev]
pytest grs/tests/unit/- Create a branch off
developnamedfeature/<short-description>. - Make your changes, with tests where relevant, and make sure they pass locally.
- Open a pull request targeting
develop— thepython-testsjob runs automatically on the PR (see above). - A maintainer will review your PR.
See CONTRIBUTING.md for
the full guide: reporting issues, code style (black/isort, ruff/mypy in CI),
building the documentation, and the code of conduct.
- Tristan Harmel - Initial work - contact
See also the list of contributors who participated in this project.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details
- The Step forum and Marco Peters are acknowledged for their useful help to process Sentinel-2 data with the snappy API.
- The authors are very grateful to Olivier Hagolle for providing open source codes to perform gaseous absorption correction and massive Sentinel-2 data download.


