Skip to content

About

Atmospheric correction processor for high spatial resolution multispectral satellite images

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1,107 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GRSprocessor

Python 3.11 License GRSprocessor Coverage Docker Image Version PyPI

GRSprocessor (Glint Removal for Sentinel-2-like sensors)

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 $R_{rs}$ at the water surface.

grs S2B_MSIL1C_20220731T103629_N0400_R008_T31TFJ_20220731T124834.SAFE \
    --cams_file cams_forecast_2022-07.nc --resolution 20

Getting Started

Installing on the CNES TREX cluster? See README_CNES.md for CNES-specific installation and run instructions.

Installation

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/activate

The 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/cpu

Then install GRSprocessor from PyPI (the package and the commands are grs):

pip install GRSprocessor

or 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 develop

Check the installation:

grs -v
grs_config

Look-up tables

The 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) with grs_config --init /path/to/lut, or set the GRS_DATA_ROOT environment 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 (or zenodo: auto_download: false in 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.

CAMS atmospheric data

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 the YYYY/MM/DD tree of path: cams of 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 by ecmwf/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 uses cdsapi: 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>
    

Usage

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 ./L2A

The L2A product is a Zarr store (<odir>/<name>/<name>.zarr) with $R_{rs}$, the sunglint reflectance, the aerosol optical thickness, the flags and the mask of the valid water pixels, and its overviews (pyramid). Useful options (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 20

Install 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 writing

Landsat 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.

Memory and computing time

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.

Example

image_output

Examples of output images before (1st column) and after (2nd column) sunglint correction:

image_output

Running the tests

pip install -e .[dev]
pytest grs/tests/unit/                 # about 45 s
  • grs/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 the Kernel (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 with grs/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).

Deployment

See examples in exe.

Compile Docker image locally

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.

How the CI/CD Pipeline Works

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):

  1. 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 on main.
  2. podman-build (needs python-tests) — builds the Docker image (Dockerfile-github, Ubuntu 24.04, Python 3.12); on every push, it also publishes it to Docker Hub as cnes/grsprocessor:dev, except on main (branch or tag) where it publishes cnes/grsprocessor:<version> instead.
  3. lint (pull requests and tags, needs podman-build) — runs ruff and mypy and 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.

How to Contribute

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/
  1. Create a branch off develop named feature/<short-description>.
  2. Make your changes, with tests where relevant, and make sure they pass locally.
  3. Open a pull request targeting develop — the python-tests job runs automatically on the PR (see above).
  4. 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.

Authors

  • Tristan Harmel - Initial work - contact

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

Acknowledgments

  • 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.

About

Atmospheric correction processor for high spatial resolution multispectral satellite images

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages