Climatology & Anomalies¶
Compute climatologies and seasonal cycles, then form anomalies by removing them. For a field \(x(t)\) with climatology \(\bar{x}(\tau)\) over the cycle phase \(\tau\) (e.g. day-of-year), the anomaly is
The *_smoothed variants apply a harmonic / rolling smoother to
\(\bar{x}(\tau)\) before subtraction.
Operators¶
CalculateClimatology
¶
Bases: Operator
Compute a climatology grouped at freq from the time axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
str
|
Climatology grouping — e.g. |
'day'
|
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
|
A dataset of the per-group climatological means, with |
|
|
replaced by the grouping dimension. |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/operators.py
CalculateClimatologySmoothed
¶
Bases: Operator
Compute a day-of-year climatology smoothed with a rolling window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window
|
int
|
Width (in days) of the centred rolling-mean smoothing applied around the day-of-year cycle. |
60
|
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
|
A smoothed day-of-year climatology dataset. |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/operators.py
RemoveClimatology
¶
Bases: Operator
Subtract a precomputed climatology to form anomalies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
climatology
|
Climatology dataset (e.g. from
:class: |
required | |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
|
The anomaly dataset (input minus the matched climatology). |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/operators.py
AddClimatology
¶
Bases: Operator
Add a climatology back to anomalies (inverse of :class:RemoveClimatology).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
climatology
|
Climatology dataset to add. |
required | |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
|
The reconstructed dataset (anomaly plus climatology). |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/operators.py
RemoveMean
¶
Bases: Operator
Subtract the mean over dim (a cheap anomaly without a climatology).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
str | Sequence[str]
|
Dimension or dimensions to average over before subtracting. |
required |
Returns:
| Type | Description |
|---|---|
|
The input dataset with the per- |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/operators.py
Functional primitives (Layer 0)¶
These pure functions back the operators above; each takes xr.DataArray/xr.Dataset and a dim: argument.
calculate_climatology
¶
calculate_climatology(data: DataArray | Dataset, freq: str = 'day', time: str = 'time') -> DataArray | Dataset
Compute the climatology by grouping time at a given frequency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
Input DataArray or Dataset with a |
required |
freq
|
str
|
One of |
'day'
|
time
|
str
|
Name of the time coordinate (default |
'time'
|
Returns:
| Type | Description |
|---|---|
DataArray | Dataset
|
Climatology, same container type as |
DataArray | Dataset
|
replaced by the grouping dimension |
DataArray | Dataset
|
( |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
calculate_climatology_season
¶
Climatology grouped by meteorological season (DJF/MAM/JJA/SON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
Input DataArray or Dataset with a |
required |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
DataArray | Dataset
|
Climatology with |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
calculate_climatology_smoothed
¶
calculate_climatology_smoothed(data: DataArray | Dataset, window: int = 60, time: str = 'time') -> DataArray | Dataset
Daily climatology smoothed with a circular rolling window.
Pads the climatology using wrap mode so the smoothing doesn't bias the January / December edges, then applies a centered rolling mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
Input DataArray or Dataset with a |
required |
window
|
int
|
Length of the rolling window in days (must be even so
that the half-window |
60
|
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
DataArray | Dataset
|
Smoothed day-of-year climatology. |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
calculate_anomaly
¶
calculate_anomaly(data: DataArray | Dataset, freq: str = 'day', time: str = 'time') -> DataArray | Dataset
Convenience: compute climatology at freq and subtract it.
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
calculate_anomaly_smoothed
¶
calculate_anomaly_smoothed(data: DataArray | Dataset, window: int = 60, time: str = 'time') -> DataArray | Dataset
Convenience: smoothed daily climatology then subtract it.
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
remove_climatology
¶
remove_climatology(data: DataArray | Dataset, climatology: DataArray | Dataset, time: str = 'time') -> DataArray | Dataset
Subtract a precomputed climatology from data.
The grouping frequency is inferred from climatology: it must
carry exactly one of the dimensions dayofyear, month, or
year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
Input with a |
required |
climatology
|
DataArray | Dataset
|
Output of :func: |
required |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
DataArray | Dataset
|
Anomalies |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
add_climatology
¶
add_climatology(data: DataArray | Dataset, climatology: DataArray | Dataset, time: str = 'time') -> DataArray | Dataset
Add a climatology back onto data (inverse of
:func:remove_climatology).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataArray | Dataset
|
Anomaly field with a |
required |
climatology
|
DataArray | Dataset
|
Climatology computed via :func: |
required |
time
|
str
|
Name of the time coordinate. |
'time'
|
Returns:
| Type | Description |
|---|---|
DataArray | Dataset
|
Reconstructed field |
Source code in packages/xrtoolz-ops/src/xrops/anomaly/_src/detrend.py
remove_mean
¶
Subtract the mean over dims (per-variable, NaN-aware).
Useful for cheap anomaly fields when no climatology is available
(e.g. spatial mean removal per timestep, or zonal-mean removal).
Distinct from :func:calculate_anomaly, which subtracts a
time-grouped climatology.