How can i get a single image from a Collection

Hi, I’m new at OpenEO and I was trying to find a way to replicate the ImageCollection.mean() from Google Earth Engine.
This method creates a single image from a collection that calculating the mean of all values at each pixel across the stack of all matching bands.

Basically I want to obtain a mean image from a OpenEO’s collection without using GEE.

Hi @s.deluca38 and welcome!

The key process which you would need to use for this scenario is called reduce_dimension. Are you a Python or R user? I’m not sure you need to compute the average over the bands or over time?

Here is a Python code snippet for computing the temporal average:

import openeo

# Connect to the back-end
connection = openeo.connect("https://openeo.cloud").authenticate_oidc()


loadcollection1 = connection.load_collection(
    collection_id = "SENTINEL2_L2A",
    bands = ["B04", "B03", "B02"],
    spatial_extent = {"east": 11.362296269398062,
                       "north": 46.50385449499987,
                       "south": 46.48422542997616,
                       "west": 11.32881897740506},
    temporal_extent = ["2022-06-01T00:00:00Z",
                       "2022-06-30T00:00:00Z"]
)

reduce1 = loadcollection1.reduce_dimension(reducer = "mean", dimension = "t")
save2 = reduce1.save_result(format = "netCDF")

save2.download("save2.nc")

import xarray as xr
data = xr.open_dataset("save2.nc",decode_coords="all")
print(data)

>> <xarray.Dataset>
Dimensions:  (x: 265, y: 227)
Coordinates:
  * x        (x) float64 6.787e+05 6.787e+05 6.787e+05 ... 6.813e+05 6.813e+05
  * y        (y) float64 5.153e+06 5.153e+06 5.153e+06 ... 5.15e+06 5.15e+06
    crs      |S1 ...
Data variables:
    B04      (y, x) float32 ...
    B03      (y, x) float32 ...
    B02      (y, x) float32 ...
Attributes:
    Conventions:  CF-1.9
    institution:  openEO platform

If you want to compute the average of the bands instead, you can just replace the dimension parameter in reduce_dimension to bands:

reduce1 = loadcollection1.reduce_dimension(reducer = "mean", dimension = "bands")

which gives you this result:

<xarray.Dataset>
Dimensions:  (t: 11, x: 265, y: 227)
Coordinates:
  * t        (t) datetime64[ns] 2022-06-02 2022-06-05 ... 2022-06-25 2022-06-27
  * x        (x) float64 6.787e+05 6.787e+05 6.787e+05 ... 6.813e+05 6.813e+05
  * y        (y) float64 5.153e+06 5.153e+06 5.153e+06 ... 5.15e+06 5.15e+06
    crs      |S1 ...
Data variables:
    var      (t, y, x) float32 ...
Attributes:
    Conventions:  CF-1.9
    institution:  openEO platform
1 Like

Thanks for the reply.
I think the temporal average is the closest to what I needed.

Remember that you can also use different reducers depending on what you want to achieve: min, max, median, variance, sd (standard deviation), count.