Skip to content

Advanced Analysis Methods

Advanced analysis methods for weather and air quality, including temporal resampling, climatology, and Kolmogorov-Zurbenko (KZ) filters.

Advanced analysis methods for weather and air quality (Aero Protocol Compliant).

climatology(data, freq='season', method='mean', dim='time')

Compute climatological statistics (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data with a time-like coordinate. freq : str, optional Climatology frequency ('season', 'month', 'dayofyear', 'hour'). Default is 'season'. method : str, optional Statistical method to apply ('mean', 'std', 'min', 'max', 'median'). Default is 'mean'. dim : str, optional Dimension along which to compute climatology. Default is 'time'.

Returns

Union[xr.DataArray, xr.Dataset] Climatological statistics.

Examples

import xarray as xr import pandas as pd import numpy as np times = pd.date_range("2020-01-01", periods=365*2, freq="D") da = xr.DataArray(np.random.rand(730), coords={"time": times}, dims="time") seasonal_climo = climatology(da, freq="season", method="mean")

Source code in src/monet_stats/analysis.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def climatology(
    data: Union[xr.DataArray, xr.Dataset],
    freq: str = "season",
    method: str = "mean",
    dim: str = "time",
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute climatological statistics (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data with a time-like coordinate.
    freq : str, optional
        Climatology frequency ('season', 'month', 'dayofyear', 'hour').
        Default is 'season'.
    method : str, optional
        Statistical method to apply ('mean', 'std', 'min', 'max', 'median').
        Default is 'mean'.
    dim : str, optional
        Dimension along which to compute climatology. Default is 'time'.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Climatological statistics.

    Examples
    --------
    >>> import xarray as xr
    >>> import pandas as pd
    >>> import numpy as np
    >>> times = pd.date_range("2020-01-01", periods=365*2, freq="D")
    >>> da = xr.DataArray(np.random.rand(730), coords={"time": times}, dims="time")
    >>> seasonal_climo = climatology(da, freq="season", method="mean")
    """
    group = f"{dim}.{freq}"
    # Use native xarray groupby methods for better performance and Dask integration
    grouped = data.groupby(group)
    if hasattr(grouped, method):
        result = getattr(grouped, method)(dim=dim)
    else:
        # Fallback for methods not directly implemented on GroupBy
        result = grouped.reduce(getattr(np, f"nan{method}" if "nan" not in method else method), dim=dim)

    return _update_history(result, f"Climatology ({freq}) using {method}")

diurnal_cycle(data, method='mean', dim='time')

Compute the diurnal cycle (average hourly profile) (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data with a time-like coordinate. method : str, optional Statistical method to apply ('mean', 'median', 'std'). Default is 'mean'. dim : str, optional Dimension along which to compute the cycle. Default is 'time'.

Returns

Union[xr.DataArray, xr.Dataset] Diurnal cycle (24 values, one for each hour).

Examples

import xarray as xr import pandas as pd import numpy as np times = pd.date_range("2020-01-01", periods=24*10, freq="h") da = xr.DataArray(np.random.rand(240), coords={"time": times}, dims="time") cycle = diurnal_cycle(da, method="mean")

Source code in src/monet_stats/analysis.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def diurnal_cycle(
    data: Union[xr.DataArray, xr.Dataset],
    method: str = "mean",
    dim: str = "time",
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute the diurnal cycle (average hourly profile) (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data with a time-like coordinate.
    method : str, optional
        Statistical method to apply ('mean', 'median', 'std'). Default is 'mean'.
    dim : str, optional
        Dimension along which to compute the cycle. Default is 'time'.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Diurnal cycle (24 values, one for each hour).

    Examples
    --------
    >>> import xarray as xr
    >>> import pandas as pd
    >>> import numpy as np
    >>> times = pd.date_range("2020-01-01", periods=24*10, freq="h")
    >>> da = xr.DataArray(np.random.rand(240), coords={"time": times}, dims="time")
    >>> cycle = diurnal_cycle(da, method="mean")
    """
    return climatology(data, freq="hour", method=method, dim=dim)

exceedance_count(data, threshold, dim='time')

Count exceedances of a threshold (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. threshold : float Value above which an exceedance is counted. dim : str, optional Dimension along which to count exceedances. Default is 'time'.

Returns

Union[xr.DataArray, xr.Dataset] Number of exceedances.

Examples

import xarray as xr da = xr.DataArray([1, 5, 2, 6, 3]) exceedance_count(da, threshold=4) array(2)

Source code in src/monet_stats/analysis.py
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
def exceedance_count(
    data: Union[xr.DataArray, xr.Dataset],
    threshold: float,
    dim: str = "time",
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Count exceedances of a threshold (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data.
    threshold : float
        Value above which an exceedance is counted.
    dim : str, optional
        Dimension along which to count exceedances. Default is 'time'.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Number of exceedances.

    Examples
    --------
    >>> import xarray as xr
    >>> da = xr.DataArray([1, 5, 2, 6, 3])
    >>> exceedance_count(da, threshold=4)
    <xarray.DataArray ()>
    array(2)
    """
    res = (data > threshold).sum(dim=dim)
    return _update_history(res, f"Exceedance count (threshold={threshold})")

fft_analysis(data, dim='time', output='psd')

Perform Fast Fourier Transform (FFT) analysis (Aero Protocol).

Parameters

data : xarray.DataArray Input data. dim : str, optional Dimension along which to perform FFT. Default is 'time'. output : str, optional Type of output to return: - 'psd': Power Spectral Density (magnitude squared of FFT). - 'magnitude': Magnitude of FFT. - 'complex': Complex FFT results. Default is 'psd'.

Returns

xarray.DataArray FFT results. The coordinate for 'dim' is replaced by frequency indices.

Examples

import xarray as xr import numpy as np t = np.linspace(0, 10, 100) signal = np.sin(2 * np.pi * 1.5 * t) # 1.5 Hz signal da = xr.DataArray(signal, coords={"time": t}, dims="time") psd = fft_analysis(da, dim="time", output="psd")

Source code in src/monet_stats/analysis.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
def fft_analysis(
    data: xr.DataArray,
    dim: str = "time",
    output: str = "psd",
) -> xr.DataArray:
    """
    Perform Fast Fourier Transform (FFT) analysis (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray
        Input data.
    dim : str, optional
        Dimension along which to perform FFT. Default is 'time'.
    output : str, optional
        Type of output to return:
        - 'psd': Power Spectral Density (magnitude squared of FFT).
        - 'magnitude': Magnitude of FFT.
        - 'complex': Complex FFT results.
        Default is 'psd'.

    Returns
    -------
    xarray.DataArray
        FFT results. The coordinate for 'dim' is replaced by frequency indices.

    Examples
    --------
    >>> import xarray as xr
    >>> import numpy as np
    >>> t = np.linspace(0, 10, 100)
    >>> signal = np.sin(2 * np.pi * 1.5 * t)  # 1.5 Hz signal
    >>> da = xr.DataArray(signal, coords={"time": t}, dims="time")
    >>> psd = fft_analysis(da, dim="time", output="psd")
    """

    def _fft_wrapper(x):
        return np.fft.fft(x, axis=-1)

    # Core dimensions for apply_ufunc must be a single chunk if using dask
    if hasattr(data.data, "chunks"):
        data = data.chunk({dim: -1})

    res_complex = xr.apply_ufunc(
        _fft_wrapper,
        data,
        input_core_dims=[[dim]],
        output_core_dims=[[dim]],
        dask="parallelized",
        output_dtypes=[np.complex128],
    )

    if output == "complex":
        res = res_complex
    elif output == "magnitude":
        res = np.abs(res_complex)
    elif output == "psd":
        res = np.abs(res_complex) ** 2
    else:
        raise ValueError(f"Unknown output type: {output}")

    # Update coordinate to frequency index
    n = data.sizes[dim]
    res = res.assign_coords({dim: np.arange(n)})

    return _update_history(res, f"FFT analysis (output={output})")

kz_filter(data, m, k, dim='time', axis=-1)

Kolmogorov-Zurbenko (KZ) filter (Aero Protocol).

The KZ filter is a low-pass filter implemented as k iterations of a moving average of window size m.

Parameters

data : xarray.DataArray, xarray.Dataset, or numpy.ndarray Input data. m : int Window size for the moving average (must be an odd integer for symmetry). k : int Number of iterations. dim : str, optional Dimension along which to apply the filter (xarray only). Default is 'time'. axis : int, optional Axis along which to apply the filter (numpy only). Default is -1.

Returns

Union[xr.DataArray, xr.Dataset, np.ndarray] Filtered data.

Notes

The KZ filter is widely used in air quality analysis to separate different time scales in a time series (e.g., seasonal, long-term, and short-term).

Examples

import numpy as np x = np.random.rand(100) filtered = kz_filter(x, m=5, k=3)

Source code in src/monet_stats/analysis.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def kz_filter(
    data: Union[xr.DataArray, xr.Dataset, np.ndarray],
    m: int,
    k: int,
    dim: str = "time",
    axis: int = -1,
) -> Union[xr.DataArray, xr.Dataset, np.ndarray]:
    """
    Kolmogorov-Zurbenko (KZ) filter (Aero Protocol).

    The KZ filter is a low-pass filter implemented as k iterations of a moving
    average of window size m.

    Parameters
    ----------
    data : xarray.DataArray, xarray.Dataset, or numpy.ndarray
        Input data.
    m : int
        Window size for the moving average (must be an odd integer for symmetry).
    k : int
        Number of iterations.
    dim : str, optional
        Dimension along which to apply the filter (xarray only). Default is 'time'.
    axis : int, optional
        Axis along which to apply the filter (numpy only). Default is -1.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset, np.ndarray]
        Filtered data.

    Notes
    -----
    The KZ filter is widely used in air quality analysis to separate different
    time scales in a time series (e.g., seasonal, long-term, and short-term).

    Examples
    --------
    >>> import numpy as np
    >>> x = np.random.rand(100)
    >>> filtered = kz_filter(x, m=5, k=3)
    """
    if m % 2 == 0:
        warnings.warn("KZ filter window size m should ideally be odd for symmetry.", stacklevel=2)

    if not isinstance(data, (xr.DataArray, xr.Dataset)):
        # Handle numpy array by wrapping it in a DataArray to ensure consistency
        is_numpy = True
        # Create dummy dimensions
        dims = [f"dim_{i}" for i in range(np.asanyarray(data).ndim)]
        res = xr.DataArray(data, dims=dims)
        target_dim = dims[axis]
    else:
        is_numpy = False
        res = data
        target_dim = dim

    for _ in range(k):
        res = res.rolling({target_dim: m}, center=True).mean()

    if is_numpy:
        return res.values
    return _update_history(res, f"KZ filter (m={m}, k={k})")

mda8(data, dim='time', min_periods=6, center=False)

Compute Maximum Daily 8-hour Average (MDA8) (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. Must have hourly frequency. dim : str, optional Dimension along which to compute. Default is 'time'. min_periods : int, optional Minimum number of observations for the 8-hour rolling mean. Default is 6. center : bool, optional Whether to center the 8-hour rolling window. Regulatory MDA8 (e.g., EPA) typically uses a non-centered (trailing) window. Default is False.

Returns

Union[xr.DataArray, xr.Dataset] MDA8 values (one per day).

Examples

import xarray as xr import pandas as pd import numpy as np times = pd.date_range("2020-01-01", periods=24*5, freq="h") da = xr.DataArray(np.random.rand(120), coords={"time": times}, dims="time") ozone_mda8 = mda8(da)

Source code in src/monet_stats/analysis.py
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
def mda8(
    data: Union[xr.DataArray, xr.Dataset],
    dim: str = "time",
    min_periods: int = 6,
    center: bool = False,
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute Maximum Daily 8-hour Average (MDA8) (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data. Must have hourly frequency.
    dim : str, optional
        Dimension along which to compute. Default is 'time'.
    min_periods : int, optional
        Minimum number of observations for the 8-hour rolling mean. Default is 6.
    center : bool, optional
        Whether to center the 8-hour rolling window. Regulatory MDA8 (e.g., EPA)
        typically uses a non-centered (trailing) window. Default is False.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        MDA8 values (one per day).

    Examples
    --------
    >>> import xarray as xr
    >>> import pandas as pd
    >>> import numpy as np
    >>> times = pd.date_range("2020-01-01", periods=24*5, freq="h")
    >>> da = xr.DataArray(np.random.rand(120), coords={"time": times}, dims="time")
    >>> ozone_mda8 = mda8(da)
    """
    rolling_8h = rolling_mean_8h(data, dim=dim, min_periods=min_periods, center=center)
    # Group by day and take maximum
    res = rolling_8h.resample({dim: "D"}).max()
    return _update_history(res, "MDA8 (Maximum Daily 8-hour Average)")

peak_timing(data, dim='time')

Identify the coordinate value of the maximum (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. dim : str, optional Dimension along which to find the peak. Default is 'time'.

Returns

Union[xr.DataArray, xr.Dataset] Coordinate values where the maximum occurs.

Examples

import xarray as xr import pandas as pd times = pd.date_range("2020-01-01", periods=24, freq="h") da = xr.DataArray(np.random.rand(24), coords={"time": times}, dims="time") peak_hour = peak_timing(da, dim="time")

Source code in src/monet_stats/analysis.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
def peak_timing(
    data: Union[xr.DataArray, xr.Dataset],
    dim: str = "time",
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Identify the coordinate value of the maximum (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data.
    dim : str, optional
        Dimension along which to find the peak. Default is 'time'.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Coordinate values where the maximum occurs.

    Examples
    --------
    >>> import xarray as xr
    >>> import pandas as pd
    >>> times = pd.date_range("2020-01-01", periods=24, freq="h")
    >>> da = xr.DataArray(np.random.rand(24), coords={"time": times}, dims="time")
    >>> peak_hour = peak_timing(da, dim="time")
    """
    # Ensure dimension is in a single chunk for Dask-backed arrays
    if hasattr(data.data, "chunks"):
        data = data.chunk({dim: -1})

    # idxmax returns the coordinate of the maximum
    res = data.idxmax(dim=dim)
    return _update_history(res, f"Peak timing along {dim}")

percentile(data, q, dim='time', **kwargs)

Compute percentiles (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. q : float or list of float Percentile(s) to compute (0-100). dim : str, optional Dimension(s) over which to compute percentiles. Default is 'time'. **kwargs : Any Additional keyword arguments passed to xarray.quantile.

Returns

Union[xr.DataArray, xr.Dataset] Computed percentiles.

Source code in src/monet_stats/analysis.py
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
def percentile(
    data: Union[xr.DataArray, xr.Dataset],
    q: Union[float, list, np.ndarray],
    dim: str = "time",
    **kwargs: Any,
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute percentiles (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data.
    q : float or list of float
        Percentile(s) to compute (0-100).
    dim : str, optional
        Dimension(s) over which to compute percentiles. Default is 'time'.
    **kwargs : Any
        Additional keyword arguments passed to xarray.quantile.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Computed percentiles.
    """
    # Ensure dimension is in a single chunk for Dask-backed arrays
    if hasattr(data.data, "chunks"):
        data = data.chunk({dim: -1})

    # xarray uses 0-1 for quantile, so divide by 100
    res = data.quantile(np.asanyarray(q) / 100.0, dim=dim, **kwargs)
    return _update_history(res, f"Percentile (q={q})")

power_spectrum(data, dim='time', fs=1.0, window='hann', nperseg=None, **kwargs)

Compute power spectrum using Welch's method (Aero Protocol).

Welch's method computes an estimate of the power spectral density by dividing the data into overlapping segments, computing a periodogram for each segment and averaging the results.

Parameters

data : xarray.DataArray Input data. dim : str, optional Dimension along which to compute the spectrum. Default is 'time'. fs : float, optional Sampling frequency. Default is 1.0. window : str, optional Desired window to use. Default is 'hann'. nperseg : int, optional Length of each segment. Default is None (256). **kwargs : Any Additional keyword arguments passed to scipy.signal.welch.

Returns

xarray.DataArray Power spectral density. The 'dim' dimension is replaced by 'frequency'.

Examples

import xarray as xr import numpy as np t = np.linspace(0, 100, 1000) signal = np.sin(2 * np.pi * 0.1 * t) + np.random.randn(1000) * 2 da = xr.DataArray(signal, coords={"time": t}, dims="time") psd = power_spectrum(da, dim="time", fs=10.0)

Source code in src/monet_stats/analysis.py
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
def power_spectrum(
    data: xr.DataArray,
    dim: str = "time",
    fs: float = 1.0,
    window: str = "hann",
    nperseg: Optional[int] = None,
    **kwargs: Any,
) -> xr.DataArray:
    """
    Compute power spectrum using Welch's method (Aero Protocol).

    Welch's method computes an estimate of the power spectral density by
    dividing the data into overlapping segments, computing a periodogram for
    each segment and averaging the results.

    Parameters
    ----------
    data : xarray.DataArray
        Input data.
    dim : str, optional
        Dimension along which to compute the spectrum. Default is 'time'.
    fs : float, optional
        Sampling frequency. Default is 1.0.
    window : str, optional
        Desired window to use. Default is 'hann'.
    nperseg : int, optional
        Length of each segment. Default is None (256).
    **kwargs : Any
        Additional keyword arguments passed to scipy.signal.welch.

    Returns
    -------
    xarray.DataArray
        Power spectral density. The 'dim' dimension is replaced by 'frequency'.

    Examples
    --------
    >>> import xarray as xr
    >>> import numpy as np
    >>> t = np.linspace(0, 100, 1000)
    >>> signal = np.sin(2 * np.pi * 0.1 * t) + np.random.randn(1000) * 2
    >>> da = xr.DataArray(signal, coords={"time": t}, dims="time")
    >>> psd = power_spectrum(da, dim="time", fs=10.0)
    """
    from scipy.signal import welch

    # Core dimensions for apply_ufunc must be a single chunk if using dask
    if hasattr(data.data, "chunks"):
        data = data.chunk({dim: -1})

    def _welch_wrapper(x, fs, window, nperseg, **kwargs):
        f, psd = welch(x, fs=fs, window=window, nperseg=nperseg, axis=-1, **kwargs)
        return psd

    # Get the number of frequency bins to set output_core_dims size
    # For real FFT, it's nperseg // 2 + 1
    if nperseg is None:
        nperseg = min(data.sizes[dim], 256)

    n_freq = nperseg // 2 + 1

    res = xr.apply_ufunc(
        _welch_wrapper,
        data,
        input_core_dims=[[dim]],
        output_core_dims=[["frequency"]],
        kwargs={"fs": fs, "window": window, "nperseg": nperseg, **kwargs},
        dask="parallelized",
        output_dtypes=[data.dtype],
        dask_gufunc_kwargs={"output_sizes": {"frequency": n_freq}},
    )

    # Assign frequency coordinates
    freqs = np.fft.rfftfreq(nperseg, d=1.0 / fs)
    res = res.assign_coords(frequency=freqs)

    return _update_history(res, "Power spectrum (Welch method)")

resample_data(data, freq='MS', method='mean', dim='time', **kwargs)

Resample data to a new temporal frequency (Aero Protocol).

Parameters

data : xarray.DataArray, xarray.Dataset, pandas.Series, or pandas.DataFrame Input data with a time-like index or coordinate. freq : str, optional Resampling frequency (e.g., 'MS' for monthly start, 'W' for weekly, 'D' for daily). Default is 'MS'. method : str, optional Statistical method to apply ('mean', 'sum', 'min', 'max', 'std', 'median'). Default is 'mean'. dim : str, optional Dimension along which to resample (xarray only). Default is 'time'. **kwargs : Any Additional keyword arguments passed to the resample method.

Returns

Union[xr.DataArray, xr.Dataset, pd.Series, pd.DataFrame] Resampled data.

Examples

import xarray as xr import pandas as pd import numpy as np times = pd.date_range("2020-01-01", periods=100, freq="D") da = xr.DataArray(np.random.rand(100), coords={"time": times}, dims="time") monthly_mean = resample_data(da, freq="MS", method="mean")

Source code in src/monet_stats/analysis.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def resample_data(
    data: Union[xr.DataArray, xr.Dataset, pd.Series, pd.DataFrame],
    freq: str = "MS",
    method: str = "mean",
    dim: str = "time",
    **kwargs: Any,
) -> Union[xr.DataArray, xr.Dataset, pd.Series, pd.DataFrame]:
    """
    Resample data to a new temporal frequency (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray, xarray.Dataset, pandas.Series, or pandas.DataFrame
        Input data with a time-like index or coordinate.
    freq : str, optional
        Resampling frequency (e.g., 'MS' for monthly start, 'W' for weekly, 'D' for daily).
        Default is 'MS'.
    method : str, optional
        Statistical method to apply ('mean', 'sum', 'min', 'max', 'std', 'median').
        Default is 'mean'.
    dim : str, optional
        Dimension along which to resample (xarray only). Default is 'time'.
    **kwargs : Any
        Additional keyword arguments passed to the resample method.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset, pd.Series, pd.DataFrame]
        Resampled data.

    Examples
    --------
    >>> import xarray as xr
    >>> import pandas as pd
    >>> import numpy as np
    >>> times = pd.date_range("2020-01-01", periods=100, freq="D")
    >>> da = xr.DataArray(np.random.rand(100), coords={"time": times}, dims="time")
    >>> monthly_mean = resample_data(da, freq="MS", method="mean")
    """
    if isinstance(data, (xr.DataArray, xr.Dataset)):
        resampled = data.resample({dim: freq}, **kwargs)
        result = getattr(resampled, method)()
        return _update_history(result, f"Resampled to {freq} using {method}")
    elif isinstance(data, (pd.Series, pd.DataFrame)):
        result = data.resample(freq, **kwargs).agg(method)
        return result
    else:
        raise TypeError("data must be an xarray or pandas object with a time index")

rolling_mean_24h(data, dim='time', min_periods=18, center=True)

Compute rolling 24-hour mean (commonly for PM2.5) (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. Must have hourly frequency. dim : str, optional Dimension along which to compute the mean. Default is 'time'. min_periods : int, optional Minimum number of observations in window required to have a value. Default is 18 (75% of 24 hours). center : bool, optional If True, set the labels at the center of the window. Default is True.

Returns

Union[xr.DataArray, xr.Dataset] Rolling 24-hour mean.

Source code in src/monet_stats/analysis.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def rolling_mean_24h(
    data: Union[xr.DataArray, xr.Dataset],
    dim: str = "time",
    min_periods: int = 18,
    center: bool = True,
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute rolling 24-hour mean (commonly for PM2.5) (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data. Must have hourly frequency.
    dim : str, optional
        Dimension along which to compute the mean. Default is 'time'.
    min_periods : int, optional
        Minimum number of observations in window required to have a value.
        Default is 18 (75% of 24 hours).
    center : bool, optional
        If True, set the labels at the center of the window. Default is True.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Rolling 24-hour mean.
    """
    res = data.rolling({dim: 24}, min_periods=min_periods, center=center).mean()
    return _update_history(res, "Rolling 24-hour mean")

rolling_mean_8h(data, dim='time', min_periods=6, center=True)

Compute rolling 8-hour mean (commonly for Ozone) (Aero Protocol).

Parameters

data : xarray.DataArray or xarray.Dataset Input data. Must have hourly frequency. dim : str, optional Dimension along which to compute the mean. Default is 'time'. min_periods : int, optional Minimum number of observations in window required to have a value. Default is 6 (75% of 8 hours). center : bool, optional If True, set the labels at the center of the window. Default is True.

Returns

Union[xr.DataArray, xr.Dataset] Rolling 8-hour mean.

Source code in src/monet_stats/analysis.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def rolling_mean_8h(
    data: Union[xr.DataArray, xr.Dataset],
    dim: str = "time",
    min_periods: int = 6,
    center: bool = True,
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute rolling 8-hour mean (commonly for Ozone) (Aero Protocol).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data. Must have hourly frequency.
    dim : str, optional
        Dimension along which to compute the mean. Default is 'time'.
    min_periods : int, optional
        Minimum number of observations in window required to have a value.
        Default is 6 (75% of 8 hours).
    center : bool, optional
        If True, set the labels at the center of the window. Default is True.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Rolling 8-hour mean.
    """
    res = data.rolling({dim: 8}, min_periods=min_periods, center=center).mean()
    return _update_history(res, "Rolling 8-hour mean")

weighted_spatial_mean(data, lat_dim='lat', lon_dim='lon')

Compute area-weighted spatial mean for lat/lon grids (Aero Protocol).

Assumes a regular lat/lon grid where weights are proportional to cos(lat).

Parameters

data : xarray.DataArray or xarray.Dataset Input data with latitude and longitude coordinates. lat_dim : str, optional Name of the latitude dimension. Default is 'lat'. lon_dim : str, optional Name of the longitude longitude. Default is 'lon'.

Returns

Union[xr.DataArray, xr.Dataset] Area-weighted spatial mean.

Examples

import xarray as xr import numpy as np lats = np.arange(-90, 91, 1) lons = np.arange(-180, 181, 1) da = xr.DataArray(np.ones((len(lats), len(lons))), ... coords={"lat": lats, "lon": lons}, ... dims=("lat", "lon")) spatial_mean = weighted_spatial_mean(da)

Source code in src/monet_stats/analysis.py
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
def weighted_spatial_mean(
    data: Union[xr.DataArray, xr.Dataset],
    lat_dim: str = "lat",
    lon_dim: str = "lon",
) -> Union[xr.DataArray, xr.Dataset]:
    """
    Compute area-weighted spatial mean for lat/lon grids (Aero Protocol).

    Assumes a regular lat/lon grid where weights are proportional to cos(lat).

    Parameters
    ----------
    data : xarray.DataArray or xarray.Dataset
        Input data with latitude and longitude coordinates.
    lat_dim : str, optional
        Name of the latitude dimension. Default is 'lat'.
    lon_dim : str, optional
        Name of the longitude longitude. Default is 'lon'.

    Returns
    -------
    Union[xr.DataArray, xr.Dataset]
        Area-weighted spatial mean.

    Examples
    --------
    >>> import xarray as xr
    >>> import numpy as np
    >>> lats = np.arange(-90, 91, 1)
    >>> lons = np.arange(-180, 181, 1)
    >>> da = xr.DataArray(np.ones((len(lats), len(lons))),
    ...                   coords={"lat": lats, "lon": lons},
    ...                   dims=("lat", "lon"))
    >>> spatial_mean = weighted_spatial_mean(da)
    """
    weights = np.cos(np.deg2rad(data[lat_dim]))
    weights.name = "weights"
    weighted_data = data.weighted(weights)
    res = weighted_data.mean(dim=(lat_dim, lon_dim))
    return _update_history(res, "Weighted spatial mean")