Skip to content

Instantly share code, notes, and snippets.

@EmileSonneveld
Last active April 27, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmileSonneveld/e22d7b6e010098165704c2815cdc7c3f to your computer and use it in GitHub Desktop.
Save EmileSonneveld/e22d7b6e010098165704c2815cdc7c3f to your computer and use it in GitHub Desktop.
import json
import os
import datetime
import openeo
connection = openeo.connect("openeo.cloud").authenticate_oidc()
now = datetime.datetime.now()
print(connection.root_url + " time: " + str(now))
precipitation_dc = connection.load_collection(
"AGERA5",
temporal_extent=["2010-01-01", "2023-04-30"], # Small sample date
bands=["precipitation-flux"],
)
precipitation_dc = (precipitation_dc * 0.01)
precipitation_dc = precipitation_dc.filter_spatial(
geometries="https://raw.githubusercontent.com/georgique/world-geojson/develop/countries/south_africa.json")
# Formula: SPI = (current - median ) / SD
current_band = precipitation_dc.band("precipitation-flux")
mean_band = precipitation_dc.reduce_temporal("mean").band("precipitation-flux")
difference_band = current_band.merge_cubes(mean_band, overlap_resolver="subtract")
sd_band = precipitation_dc.reduce_temporal("sd").band("precipitation-flux")
spi_band = difference_band.merge_cubes(sd_band, overlap_resolver="divide")
# Test in between values:
# precipitation_dc.reduce_temporal("mean").download("precipitation-flux_mean.nc")
# precipitation_dc.reduce_temporal("min").download("precipitation-flux_min.nc")
# precipitation_dc.reduce_temporal("max").download("precipitation-flux_max.nc")
# precipitation_dc.reduce_temporal("sd").download("precipitation-flux_sd.nc")
job = spi_band.execute_batch(
title="ANIN SPI",
description="Standardized precipitation index for South Africa",
filename_prefix="SPI",
format="netCDF",
)
job.get_results().download_files("ANIN_SPI") # Can also download result later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment