Analysis¶
Climate statistics¶
Available as climate_toolkit.analyze_climate_statistics.
climate_toolkit.climate_statistics.statistics.analyze_climate_statistics ¶
analyze_climate_statistics(
location_coord,
start_year,
end_year,
source,
fixed_season=None,
model=None,
scenario=None,
extra_months=6,
precip_source=None,
temp_source=None,
crop_name=None,
calendar_source=None,
calendar_system="rf",
spei_scale_months=None,
spei_fit="ub-pwm",
spei_ref_start=None,
spei_ref_end=None,
spi_scale_months=None,
spi_fit="ub-pwm",
spi_ref_start=None,
spi_ref_end=None,
custom_station_file=None,
custom_station_variables=None,
custom_station_name=None,
custom_temp_unit="c",
custom_precip_unit="mm",
livestock_type=DEFAULT_LIVESTOCK_TYPE,
livestock_climate_profile=DEFAULT_LIVESTOCK_CLIMATE_PROFILE,
livestock_elevation_override_m=None,
include_period_raw_summary=True,
include_season_raw_summary=True,
include_season_overall_statistics=True,
include_ltm_season_summary=True,
ee_project_id=None,
workers=1,
verbose=False,
)
Run the full seasonal climate-statistics pipeline for one location.
Single entrypoint that fetches daily climate data for a point, derives reference evapotranspiration (ET0, Hargreaves) and a daily water balance, detects growing seasons (automatically from rainfall, or from a fixed/crop-calendar window), and reduces everything to per-season, per-period, and long-term-mean (LTM) statistics, optionally adding monthly SPEI/SPI drought indices.
Pipeline steps
1. Validate source/period/calendar options; resolve the
livestock THI profile and human-heat (humidex) source bundle.
2. Fetch all daily climate variables for start_year-01-01
through end_year-12-31 plus a tail (up to
extra_months in auto mode, or a full extra year for
year-crossing fixed seasons) so late/cross-year seasons are
captured.
3. Add ET0 (Hargreaves) and daily water balance columns.
4. Detect seasons: automatic onset/cessation detection from
rainfall, or fixed windows from fixed_season /
crop_name + calendar_source (GGCMI crop-calendar
preset). If auto detection is unreliable and a calendar
preset was requested, the preset is applied as a fallback.
5. Compute per-season statistics (rainfall, temperature, dry
spells, water balance, livestock THI, humidex, ET0
sub-seasons), period-wide summaries, LTM season summaries,
and optional SPEI/SPI monthly series.
Progress and warnings are printed to stdout as the pipeline runs.
Recoverable input/validation problems are returned as
{"error": "..."} rather than raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location_coord
|
tuple of float
|
|
required |
start_year
|
int
|
First calendar year of the analysis period (inclusive). |
required |
end_year
|
int
|
Last calendar year of the analysis period (inclusive). A
baseline of at least 20 years is recommended for LTM output; a
shorter span triggers |
required |
source
|
str
|
Daily climate data source. Common choices:
Earth Engine-backed sources ( |
required |
fixed_season
|
str
|
Fixed season window(s) as |
None
|
model
|
str
|
Climate model name for |
None
|
scenario
|
str
|
Scenario for |
None
|
extra_months
|
int
|
In automatic season-detection mode, how many months of tail
data past |
6
|
precip_source
|
str
|
Precipitation source for paired mode (e.g.
|
None
|
temp_source
|
str
|
Temperature source for paired mode (e.g. |
None
|
crop_name
|
str
|
Crop name used to look up a crop-calendar season preset (e.g.
|
None
|
calendar_source
|
str
|
Crop-calendar dataset used with |
None
|
calendar_system
|
str
|
GGCMI calendar system: |
'rf'
|
spei_scale_months
|
int
|
Accumulation scale in months for monthly SPEI (e.g. 3 for SPEI-3). Default None (SPEI is not computed). |
None
|
spei_fit
|
str
|
SPEI distribution-fitting method. |
'ub-pwm'
|
spei_ref_start
|
str
|
Reference (calibration) period bounds for SPEI fitting, e.g.
|
None
|
spei_ref_end
|
str
|
Reference (calibration) period bounds for SPEI fitting, e.g.
|
None
|
spi_scale_months
|
int
|
Accumulation scale in months for monthly SPI. Default None (SPI is not computed). |
None
|
spi_fit
|
str
|
SPI fitting method: |
'ub-pwm'
|
spi_ref_start
|
str
|
Reference period bounds for SPI fitting. Default None. |
None
|
spi_ref_end
|
str
|
Reference period bounds for SPI fitting. Default None. |
None
|
custom_station_file
|
str
|
Path to a station CSV/JSON whose observations override the gridded values on matching dates (gridded data fills gaps). Default None. |
None
|
custom_station_variables
|
list of str
|
Variables to take from the station file, e.g.
|
None
|
custom_station_name
|
str
|
Label for the custom station in logs/metadata. Default None. |
None
|
custom_temp_unit
|
str
|
Temperature unit in the station file: |
'c'
|
custom_precip_unit
|
str
|
Precipitation unit in the station file: |
'mm'
|
livestock_type
|
str
|
Livestock profile for temperature-humidity-index (THI) heat
stress statistics. One of |
'cattle_dairy'
|
livestock_climate_profile
|
str
|
THI threshold climate adjustment: |
'auto'
|
livestock_elevation_override_m
|
float
|
Site elevation in metres used when inferring the livestock climate profile; overrides automatic elevation lookup. Default None. |
None
|
include_period_raw_summary
|
bool
|
Include the period-wide raw climate summary
( |
True
|
include_season_raw_summary
|
bool
|
Include a raw climate summary inside each season entry. |
True
|
include_season_overall_statistics
|
bool
|
Include full overall statistics inside each season entry. |
True
|
include_ltm_season_summary
|
bool
|
Include the long-term-mean summary across years per season
window ( |
True
|
ee_project_id
|
str
|
Google Cloud project ID for Earth Engine, overriding the
|
None
|
workers
|
int
|
Number of parallel workers for data fetching. |
1
|
verbose
|
bool
|
Print extra diagnostic detail during fetching and season detection. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
On validation or fetch failure,
|
Examples:
Seasonal climatology for maize in Nairobi, Kenya from AgERA5
(requires earthengine authenticate and GCP_PROJECT_ID;
use source='nasa_power' for a credential-free run):
>>> import climate_toolkit as ct
>>> result = ct.analyze_climate_statistics(
... location_coord=(-1.286, 36.817),
... start_year=2000,
... end_year=2020,
... source="agera_5",
... crop_name="maize",
... calendar_source="ggcmi",
... spei_scale_months=3,
... )
>>> sorted(result["ltm_season_summary"].keys())
['mode', 'windows']
>>> result["season_statistics"][0]["year"]
2000
Hazards¶
Available as climate_toolkit.evaluate_hazards.
climate_toolkit.calculate_hazards.hazards.calculate_hazards ¶
calculate_hazards(
crop_name,
location_coord,
date_from,
date_to,
season_start=None,
season_end=None,
fixed_season=None,
source="auto",
precip_source=None,
temp_source=None,
custom_thresholds=None,
gap_days=30,
min_season_days=30,
soilcp=DEFAULT_SOILCP,
soilsat=DEFAULT_SOILSAT,
spinup_days=DEFAULT_SPINUP_DAYS,
water_balance_window=FULL_WINDOW_WATER_BALANCE,
calendar_source=None,
calendar_system="rf",
livestock_type=DEFAULT_LIVESTOCK_TYPE,
livestock_climate_profile=DEFAULT_LIVESTOCK_CLIMATE_PROFILE,
livestock_elevation_override_m=None,
ee_project_id=None,
workers=1,
)
Assess growing-season climate hazards for a crop at a point location.
Fetches daily climate data (precipitation, temperature, humidity) for one or more growing seasons, runs a daily soil water-balance model, and scores each season against crop-specific hazard thresholds. Hazards assessed:
- Seasonal precipitation total versus crop rainfall requirements
- Mean season temperature versus crop temperature tolerance
- Extreme heat: NTx35 / NTx40 (days with Tmax above 35 / 40 deg C)
- Drought: NDD (dry days, < 1 mm), dry-spell statistics (>= 7 consecutive dry days), NDWS (water-stress days from the soil water-balance model), and WRSI (water requirement satisfaction index)
- Waterlogging: NDWL0 (days with soil water above field capacity)
- Livestock heat stress: THI (temperature-humidity index) for the chosen livestock type and climate profile
- Human heat stress: Humidex screening (mean/max, high and extreme days)
The growing-season window is resolved in one of three ways:
- Explicit window -- pass
season_startandseason_endtogether. - Fixed calendar window(s) -- pass
fixed_season(e.g.'03-01:06-30'), applied to every year betweendate_fromanddate_to. - Auto-detection (default) -- rainfall-based onset/cessation detection
per year; if detection is unreliable and
calendar_source='ggcmi'was given, falls back to the GGCMI crop-calendar preset window.
This function is exported as climate_toolkit.evaluate_hazards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crop_name
|
str
|
Crop to assess (case-insensitive). Crops with built-in hazard
thresholds: 'beans', 'maize', 'millet', 'groundnuts', 'sorghum',
'cassava', 'rice'. Other crops require |
required |
location_coord
|
tuple of float
|
|
required |
date_from
|
str
|
Analysis window as ISO dates |
required |
date_to
|
str
|
Analysis window as ISO dates |
required |
season_start
|
str
|
Explicit season window (ISO dates |
None
|
season_end
|
str
|
Explicit season window (ISO dates |
None
|
fixed_season
|
str
|
Fixed calendar window(s) as |
None
|
source
|
str
|
Climate dataset: 'auto', 'agera_5', 'era_5', 'chirps_v2+chirts'
(alias 'chirps+chirts'), 'nasa_power', or 'paired'. |
'auto'
|
precip_source
|
str
|
Required together when |
None
|
temp_source
|
str
|
Required together when |
None
|
custom_thresholds
|
dict
|
Per-metric threshold overrides merged over the built-in crop and
hazard-index thresholds. Shape |
None
|
gap_days
|
int
|
Intended dry-gap (days) that ends an auto-detected season. Currently accepted for CLI parity but not forwarded to the season detector, which uses its own default. |
30
|
min_season_days
|
int
|
Intended minimum auto-detected season length (days). Same caveat as
|
30
|
soilcp
|
float
|
Plant-available soil water capacity (mm) between wilting point and
field capacity, used by the water-balance model (NDWS/NDWL0/WRSI).
When both |
100.0
|
soilsat
|
float
|
Additional soil water storage (mm) between field capacity and
saturation; governs waterlogging (NDWL0). See |
100.0
|
spinup_days
|
int
|
Days of climate data fetched before season onset so the soil water balance starts from a realistic moisture state. |
60
|
water_balance_window
|
(full_window, crop_active)
|
How NDWS/NDWL0 are counted within fixed-season runs: 'full_window' uses the whole fixed window; 'crop_active' restricts counting to detected ETO sub-season(s) inside the window when available. |
'full_window'
|
calendar_source
|
ggcmi
|
Crop-calendar preset source used as a fallback when auto season detection is unreliable. Default None (no fallback). |
'ggcmi'
|
calendar_system
|
(rf, ir, both)
|
GGCMI crop-calendar system when |
'rf'
|
livestock_type
|
str
|
Livestock THI profile, e.g. 'cattle_dairy', 'cattle_general',
'cattle_beef', 'goats', 'sheep'. See
|
'cattle_dairy'
|
livestock_climate_profile
|
(auto, temperate, tropical)
|
THI climate context. 'auto' selects from latitude plus highland elevation when available. |
'auto'
|
livestock_elevation_override_m
|
float
|
Site elevation (metres above sea level) used by the 'auto' THI climate-context selection instead of an automatic elevation lookup. Default None. |
None
|
ee_project_id
|
str
|
Google Cloud project ID for Earth Engine-backed climate sources.
Overrides the |
None
|
workers
|
int
|
Bounded worker count for chunked historical Earth Engine fetches. |
1
|
Returns:
| Type | Description |
|---|---|
dict
|
On failure: When exactly one season is resolved, a flat assessment dict with keys
When multiple seasons are resolved (multi-year range and/or two
seasons per year), a wrapper dict with |
Notes
Earth Engine-backed sources ('agera_5', 'era_5', 'chirps_v2+chirts',
'chirps_v3_daily_rnl', and the default 'auto' chain) require a one-time
earthengine authenticate and a Google Cloud project ID, supplied via
the GCP_PROJECT_ID environment variable or the ee_project_id
argument. The 'nasa_power' source needs no credentials.
Progress messages are printed to stdout during data fetching. Use
climate_toolkit.calculate_hazards.hazards.print_hazard_results to
pretty-print the returned dict.
Examples:
Maize near Nairobi for the 2020 long-rains season, explicit window:
>>> import climate_toolkit as ct
>>> result = ct.evaluate_hazards(
... crop_name='maize',
... location_coord=(-1.286, 36.817),
... date_from='2020-01-01',
... date_to='2020-12-31',
... season_start='2020-03-01',
... season_end='2020-06-30',
... source='agera_5',
... )
>>> result['hazard_evaluation']['NTx35']['status']
'no_stress'
Period comparison¶
Available as climate_toolkit.compare_climate_periods.
climate_toolkit.compare_periods.periods.compare ¶
compare(
location,
baseline_start,
baseline_end,
focal_year,
source,
fixed_season=None,
precip_source=None,
temp_source=None,
crop_name=None,
livestock_type=DEFAULT_LIVESTOCK_TYPE,
livestock_climate_profile=DEFAULT_LIVESTOCK_CLIMATE_PROFILE,
livestock_elevation_override_m=None,
calendar_source=None,
calendar_system="rf",
spei_scale_months=None,
spei_fit="ub-pwm",
spei_ref_start=None,
spei_ref_end=None,
spi_scale_months=None,
spi_fit="ub-pwm",
spi_ref_start=None,
spi_ref_end=None,
workers=1,
)
Compare a focal year's climate against a multi-year baseline climatology.
Runs the full climate-statistics workflow twice for a single point
location -- once for the baseline period (baseline_start to
baseline_end) and once for the focal year -- then differences the
four main report sections between the two runs:
raw_climate_summary: per-variable daily statistics (Mean/Min/Max/Std for rainfall, temperature, etc.).overall_statistics: period totals and derived metrics, with baseline values annualised (divided by the number of baseline years) so they are comparable to a single year.season_statistics: growing-season metrics, aggregated across baseline seasons (per fixed-season window whenfixed_seasonis given, otherwise across auto-detected seasons).annual_summary: annual rainfall and humid-year status (focal year vs the baseline average / humid-year frequency).
Each diffed metric reports the focal value, the baseline value (or
baseline average), the absolute difference diff and, where
meaningful, the percent change pct. Supplementary comparison
blocks for xclim reference indices and (when requested) SPEI/SPI are
also included.
Most sources are fetched via Google Earth Engine and require prior
Earth Engine authentication plus a Google Cloud project id in the
GCP_PROJECT_ID environment variable (GOOGLE_CLOUD_PROJECT or
EE_PROJECT_ID also work). The nasa_power source needs no
authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
tuple of float
|
|
required |
baseline_start
|
int
|
First year of the baseline period (inclusive). |
required |
baseline_end
|
int
|
Last year of the baseline period (inclusive). Must be greater
than or equal to |
required |
focal_year
|
int
|
Single year to compare against the baseline climatology. |
required |
source
|
str
|
Climate data source. One of |
required |
fixed_season
|
str
|
Season window(s) as |
None
|
precip_source
|
str
|
Precipitation source when |
None
|
temp_source
|
str
|
Temperature source when |
None
|
crop_name
|
str
|
Crop name used when requesting external crop-calendar presets
(e.g. GGCMI) via |
None
|
livestock_type
|
str
|
Livestock THI (temperature-humidity index) heat-stress profile,
e.g. |
DEFAULT_LIVESTOCK_TYPE
|
livestock_climate_profile
|
str
|
THI climate context: |
DEFAULT_LIVESTOCK_CLIMATE_PROFILE
|
livestock_elevation_override_m
|
float
|
Site elevation in metres above sea level, overriding the automatically fetched elevation used for THI climate-context selection. Default None. |
None
|
calendar_source
|
str
|
Crop-calendar preset source to use when automatic season
detection is not reliable. Currently only |
None
|
calendar_system
|
str
|
Crop-calendar system when |
'rf'
|
spei_scale_months
|
int
|
SPEI accumulation scale in months (e.g. 3, 6, 12). When given, SPEI is computed and compared alongside the other statistics. Default None (SPEI skipped). |
None
|
spei_fit
|
str
|
SPEI distribution-fitting method: |
'ub-pwm'
|
spei_ref_start
|
str
|
SPEI reference-period start date as |
None
|
spei_ref_end
|
str
|
SPEI reference-period end date as |
None
|
spi_scale_months
|
int
|
SPI accumulation scale in months. When given, SPI is computed and compared alongside the other statistics. Default None (SPI skipped). |
None
|
spi_fit
|
str
|
SPI distribution-fitting method: |
'ub-pwm'
|
spi_ref_start
|
str
|
SPI reference-period start date as |
None
|
spi_ref_end
|
str
|
SPI reference-period end date as |
None
|
workers
|
int
|
Number of parallel workers for chunked Earth Engine data fetches. Default 1. |
1
|
Returns:
| Type | Description |
|---|---|
dict
|
On success, a dictionary with:
On failure, a dictionary with an |
Examples:
Compare 2023 against a 1991-2020 baseline for Nairobi, Kenya, using
AgERA-5 with a GGCMI maize calendar (requires Earth Engine
authentication and GCP_PROJECT_ID set):
>>> import climate_toolkit as ct
>>> result = ct.compare_climate_periods(
... location=(-1.286, 36.817),
... baseline_start=1991,
... baseline_end=2020,
... focal_year=2023,
... source="agera_5",
... crop_name="maize",
... calendar_source="ggcmi",
... )
>>> if "error" not in result:
... rain = result["annual_summary"]["annual_rain_mm"]
... print(rain["focal"], rain["baseline_avg"], rain["pct"])