aazuspan/wxee

Is it possible to do space aggregation (instead than time)?

guidocioni opened this issue · 1 comments

I would like to produce a time series of global daily temperature based on ERA5 data.
In order to do that I'd have to apply a Reducer.mean() operation but on space instead than time.

The docs only explain how to do this operation to average over time. Is there any way to compute an average over the x, y dimensions and then download the result?

Obviously I would like to avoid having to download all data and compute the average locally.

Hi @guidocioni, wxee doesn't have any tools to help with what you're looking to do, but it's definitely doable in Earth Engine without it.

I adapted the code below from an answer on Stackoverflow, since I think their solution won't work with the amount of data you're talking about. I haven't tested this, but it should hopefully get you 90% of the way there, at least.

import ee

ee.Initialize()


# Approximately a global extent
earth = ee.Geometry.BBox(-179.9, -89.9, 179.9, 89.9)

def get_global_temp(img: ee.Image) -> ee.Feature:
  """Convert an Image into a Feature that stores a date and a mean global temp."""

  tmp = img.reduceRegion(
    reducer=ee.Reducer.mean(), 
    geometry=earth, 
    maxPixels=1e13,
  ).getNumber('mean_2m_air_temperature')
  
  date = img.date()

  return ee.Feature(None, {"tmp": tmp, "date": date})

# Load a collection of daily ERA5 temperature
col = ee.ImageCollection("ECMWF/ERA5/DAILY").select("mean_2m_air_temperature")

# Calculate global mean temp for each image in the collection
tmp_values = col.map(get_global_temp)

# Export the CSV to Google Drive
task = ee.batch.Export.table.toDrive(
  collection=tmp_values,
  description="era5_global_temperature",
  fileFormat="CSV",
).start()

The basic approach is to map over every image in the collection, using reduceRegion to calculate the global mean temp on that day. Each value is stored along with its date in a FeatureCollection, which can be downloaded as a CSV to Google Drive.

Hope that helps!