Event detection and classification (rain versus irrigation)#
License: CC-BY-4.0
Github: BenjMy/centum
Subject: Tutorial
Authors:
Benjamin Mary
Email: benjamin.mary@ica.csic.es
ORCID: 0000-0001-7199-2885
Affiliation: ICA-CSIC
Date: 2025/01/10
import numpy as np
import pooch
import xarray as xr
import centum
import matplotlib.pyplot as plt
import july
from july.utils import date_range
import pandas as pd
from centum import plotting as pltC
from centum.delineation import ETAnalysis
Open datasets#
pooch_Majadas = pooch.create(
path=pooch.os_cache("AQUACROP_project"),
base_url="https://github.com/BenjMy/test_AQUACROP_centum_dataset/raw/refs/heads/main/AQUACROP/",
registry={
"EO_scenario_AquaCrop_sc7_weather_reference.netcdf": None,
"baseline_scenario_AquaCrop_sc7_weather_reference.netcdf": None,
"grid_xr_EO_AquaCrop_sc7_weather_reference.netcdf": None,
},
)
filename = pooch_Majadas.fetch('EO_scenario_AquaCrop_sc7_weather_reference.netcdf')
EO_scenario_AquaCrop_sc7_weather_reference_EO_ds = xr.load_dataset(filename)
EO_scenario_AquaCrop_sc7_weather_reference_EO_ds = EO_scenario_AquaCrop_sc7_weather_reference_EO_ds.isel(time=slice(1,100))
filename = pooch_Majadas.fetch('baseline_scenario_AquaCrop_sc7_weather_reference.netcdf')
baseline_scenario_AquaCrop_sc7_weather_reference_ds = xr.load_dataset(filename)
filename = pooch_Majadas.fetch('grid_xr_EO_AquaCrop_sc7_weather_reference.netcdf')
irrigation_districts_grid = xr.load_dataset(filename)
irrigation_districts_grid
Downloading file 'EO_scenario_AquaCrop_sc7_weather_reference.netcdf' from 'https://github.com/BenjMy/test_AQUACROP_centum_dataset/raw/refs/heads/main/AQUACROP/EO_scenario_AquaCrop_sc7_weather_reference.netcdf' to '/home/runner/.cache/AQUACROP_project'.
/tmp/ipykernel_2091/979136979.py:12: FutureWarning: In a future version of xarray decode_timedelta will default to False rather than None. To silence this warning, set decode_timedelta to True, False, or a 'CFTimedeltaCoder' instance.
EO_scenario_AquaCrop_sc7_weather_reference_EO_ds = xr.load_dataset(filename)
Downloading file 'baseline_scenario_AquaCrop_sc7_weather_reference.netcdf' from 'https://github.com/BenjMy/test_AQUACROP_centum_dataset/raw/refs/heads/main/AQUACROP/baseline_scenario_AquaCrop_sc7_weather_reference.netcdf' to '/home/runner/.cache/AQUACROP_project'.
/tmp/ipykernel_2091/979136979.py:16: FutureWarning: In a future version of xarray decode_timedelta will default to False rather than None. To silence this warning, set decode_timedelta to True, False, or a 'CFTimedeltaCoder' instance.
baseline_scenario_AquaCrop_sc7_weather_reference_ds = xr.load_dataset(filename)
Downloading file 'grid_xr_EO_AquaCrop_sc7_weather_reference.netcdf' from 'https://github.com/BenjMy/test_AQUACROP_centum_dataset/raw/refs/heads/main/AQUACROP/grid_xr_EO_AquaCrop_sc7_weather_reference.netcdf' to '/home/runner/.cache/AQUACROP_project'.
/tmp/ipykernel_2091/979136979.py:19: FutureWarning: In a future version of xarray decode_timedelta will default to False rather than None. To silence this warning, set decode_timedelta to True, False, or a 'CFTimedeltaCoder' instance.
irrigation_districts_grid = xr.load_dataset(filename)
<xarray.Dataset> Size: 1MB Dimensions: (x: 10, y: 10, time: 366, time_hours: 8784) Coordinates: * x (x) int64 80B 0 100 200 300 400 500 600 700 800 900 * y (y) int64 80B 0 100 200 300 400 500 600 700 800 900 * time (time) timedelta64[ns] 3kB 0 days 1 days ... 365 days * time_hours (time_hours) int64 70kB 0 1 2 3 4 ... 8780 8781 8782 8783 Data variables: DEM (x, y) float64 800B 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 0.999 irrigation_map (x, y) float64 800B 1.0 1.0 1.0 1.0 1.0 ... 1.0 1.0 1.0 1.0 ETp_daily (time, x, y) float64 293kB 1.794e-08 1.794e-08 ... 0.0 0.0 irr_daily (time, x, y) float64 293kB 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 rain_daily (time, x, y) float64 293kB 0.0 0.0 ... 8.183e-08 8.183e-08 net_atmbc (time, x, y) float64 293kB -1.794e-08 ... 8.183e-08
Show Earth Observation time serie to analyse#
Irrigation delineation#
scenario_analysis_usingET = ETAnalysis()
decision_ds, event_type = scenario_analysis_usingET.irrigation_delineation(EO_scenario_AquaCrop_sc7_weather_reference_EO_ds)
dates_dt = [pd.to_datetime(date).to_pydatetime() for date in decision_ds.datetime.values]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, name)
1512 variable = self._variables[name]
1513 except KeyError:
-> 1514 _, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
1515
KeyError: 'ETa'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, key)
1622 if isinstance(key, tuple):
1623 message += f"\nHint: use a list to select multiple variables, for example `ds[{list(key)}]`"
-> 1624 raise KeyError(message) from e
1625
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, name)
1512 variable = self._variables[name]
1513 except KeyError:
-> 1514 _, name, variable = _get_virtual_variable(self._variables, name, self.sizes)
1515
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/xarray/core/dataset.py in ?(variables, key, dim_sizes)
219 split_key = key.split(".", 1)
220 if len(split_key) != 2:
--> 221 raise KeyError(key)
222
KeyError: 'ETa'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
/tmp/ipykernel_2091/2509827761.py in ?()
1 scenario_analysis_usingET = ETAnalysis()
----> 2 decision_ds, event_type = scenario_analysis_usingET.irrigation_delineation(EO_scenario_AquaCrop_sc7_weather_reference_EO_ds)
3 dates_dt = [pd.to_datetime(date).to_pydatetime() for date in decision_ds.datetime.values]
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/centum-0.1.5-py3.10.egg/centum/delineation.py in ?(self, decision_ds, threshold_local, threshold_regional, time_window)
379 time_window=10,
380 ):
381
382 # Compute local and regional ETa/ETp ratios
--> 383 decision_ds = self.compute_ratio_ETap_local(decision_ds,time_window=time_window)
384 decision_ds = self.compute_ratio_ETap_regional(decision_ds,time_window=time_window)
385
386 # Apply local and regional threshold decision rules
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/centum-0.1.5-py3.10.egg/centum/delineation.py in ?(self, ds_analysis, ETa_name, ETp_name, time_window)
54 xr.Dataset
55 The dataset with added local ETa/ETp ratio and temporal differences.
56 """
57 # Compute the local ratio of ETa/ETp
---> 58 ds_analysis["ratio_ETap_local"] = ds_analysis[ETa_name] / ds_analysis[ETp_name]
59
60 # Compute the absolute temporal difference of the local ratio
61 ds_analysis["ratio_ETap_local_diff"] = abs(
/opt/hostedtoolcache/Python/3.10.16/x64/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, key)
1620
1621 # If someone attempts `ds['foo' , 'bar']` instead of `ds[['foo', 'bar']]`
1622 if isinstance(key, tuple):
1623 message += f"\nHint: use a list to select multiple variables, for example `ds[{list(key)}]`"
-> 1624 raise KeyError(message) from e
1625
1626 if utils.iterable_of_hashable(key):
1627 return self._copy_listed(key)
KeyError: "No variable named 'ETa'. Did you mean one of ('ETp',)?"
Show 2d result#
event_type_node_IN = event_type.mean(['x','y'])
reference_date = pd.Timestamp("2025-01-01")
datetime = pd.to_datetime(reference_date + event_type_node_IN.time)
fig, axs = plt.subplots(2,1,sharex=True,figsize=(7,4))
july.heatmap(datetime,
irrigation_districts_grid['irr_daily'].sum(['x','y']),
title='Irrigation (mm/h)',
cmap="github",
colorbar=True,
ax=axs[0]
)
july.heatmap(datetime,
irrigation_districts_grid['rain_daily'].sum(['x','y']),
title='Rain (mm/h)',
cmap="golden",
colorbar=True,
ax=axs[1]
)
<Axes: title={'center': 'Rain (mm/h)'}>

fig, ax = plt.subplots(figsize=(7,4))
im = july.heatmap(dates_dt,
event_type_node_IN.values,
title='Irrigation detected',
# cmap=white_cmap,
ax=ax,
linewidth=1,
value_label=True,
)
