Hint

You can run this notebook in a live session with Binder.

Post-process

While eodag ‘s main goal is to offer an easy way to search and download EO products, it doesn’t want to be a dead-end to the users who would like to post-process the products they have downloaded.

Products file path

The download methods all return the local path of each product downloaded. Theses paths can be fed into another program for further processing. See for instance the tutorial that makes use of SNAP to detect ships on Sentinel 1 images.

Note

The file paths returned (strings) are in the file URI format (e.g. file:///tmp on Linux). You may need in some occasions to remove the file:// prefix.

Sentinel: SAFE Format

Sentinel products are generally distributed in the SAFE format. AWS-based providers however offers Sentinel products in a “scattered” way, each file being able to be downloaded individually. eodag downloads these multiple files and automatically builds a SAFE product out of them. Image processing softwares such as SNAP work well with products in the SAFE format.

Data access with eodag-cube

At some point eodag had some capabilities to directly post-process a product, i.e. to access its data. These capabilities, which relied on rasterio, have been ported to the Python package eodag-cube to avoid the heavy dependencies associated with rasterio and GDAL in particular. Installing this package is enough to benefit from its capabilities.

eodag-cube is going to extend the EOProduct class with a get_data() method which returns a product’s image band as a xarray.DataArray. If the product has not yet been downloaded, get_data() will take care of downloading it first and will then return the required band data.

Setup

A search is made to get Sentinel 2 Level-1C products from PEPS.

Warning

eodag-cube needs to be installed to run correcly the following code.

[1]:
# Set the credentials with environment variables
import os
os.environ["EODAG__PEPS__AUTH__CREDENTIALS__USERNAME"] = "PLEASE_CHANGE_ME"
os.environ["EODAG__PEPS__AUTH__CREDENTIALS__PASSWORD"] = "PLEASE_CHANGE_ME"

# Create an output directory
workspace = 'eodag_workspace_post_process'
if not os.path.isdir(workspace):
    os.mkdir(workspace)

# Create an output directory
os.environ["EODAG__PEPS__DOWNLOAD__OUTPUTS_PREFIX"] = os.path.abspath(workspace)

# Set PEPS as the provider used for searching for products
from eodag import EODataAccessGateway
dag = EODataAccessGateway()
dag.set_preferred_provider("peps")

# Define the search criteria and search for products
search_criteria = {
    "productType": "S2_MSI_L1C",
    "start": "2021-03-01",
    "end": "2021-03-31",
    "geom": {"lonmin": 1.306, "latmin": 43.527, "lonmax": 1.551, "latmax": 43.662},
    "cloudCover": 1  # Cloud cover < 1
}

products, estimated_total_number = dag.search(**search_criteria)
[2]:
# Retrieve a single product
one_product = products[0]

A single product is downloaded.

[3]:
dag.download(one_product)


[3]:
'file:///home/maxime/TRAVAIL/06_EODAG/01_eodag/eodag/docs/notebooks/api_user_guide/eodag_workspace_post_process/S2B_MSIL1C_20210301T104859_N0209_R051_T31TCJ_20210301T115849/S2B_MSIL1C_20210301T104859_N0209_R051_T31TCJ_20210301T115849.SAFE'

get_data() requires the following parameters:

  • band: dataset band to retrieve (e.g. “B01”)

  • extent: output extent (minx, miny, maxx, maxy) whose coordinates are in the crs coordinate reference system

  • resolution: output resolution (given in the unit of the crs, e.g. 0.0001)

  • crs: output coordinate reference system

The example below shows how to calculate the NDVI from the downloaded product over Toulouse.

[4]:
common_params = dict(
    crs="epsg:4326",                       # the downloaded images are in 4326, don't reproject them
    resolution=0.0001,                     # but lower their resolution
    extent=(1.435, 43.586, 1.458, 43.603)  # and zoom over/crop the area of interest
)
[5]:
VIR = one_product.get_data(band="B04", **common_params)
NIR = one_product.get_data(band="B08", **common_params)
NDVI = (NIR - VIR * 1.) / (NIR + VIR)
[6]:
NDVI
[6]:
<xarray.DataArray (dim_0: 229, dim_1: 170)>
array([[-0.00838169,  0.00969697,  0.02765209, ...,  0.08899382,
         0.08953168,  0.15292459],
       [-0.00458415,  0.01956135,  0.02684236, ...,  0.09963695,
         0.09245055,  0.14425428],
       [ 0.00064725,  0.0250139 ,  0.02480916, ...,  0.12380952,
         0.10745698,  0.14026793],
       ...,
       [ 0.10257797,  0.12961097,  0.16120461, ...,  0.24607679,
         0.27561718,  0.27751196],
       [ 0.06745214,  0.08427058,  0.10302594, ...,  0.21860465,
         0.24454148,  0.24917188],
       [ 0.05295161,  0.06307043,  0.07108024, ...,  0.1889263 ,
         0.20932009,  0.21919386]])
Dimensions without coordinates: dim_0, dim_1

The plot methods of xarray.DataArray can be used to quickly plot the NDVI.

[7]:
NDVI.plot(cmap="RdYlGn", center=False)
[7]:
<matplotlib.collections.QuadMesh at 0x7fc38bbbcc10>
../../_images/notebooks_api_user_guide_8_post_process_21_1.png