Skip to content

Commit

Permalink
doc: add quarto doc for cpm interporation
Browse files Browse the repository at this point in the history
  • Loading branch information
spool committed Oct 2, 2024
1 parent 34939d8 commit 0463ae8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project:
- "R/README.md"
- "R/misc/Identifying_Runs.md"
- "R/comparing-r-and-python/HADs-reprojection/WIP-Comparing-HADs-grids.md"
- "docs/plot-cpm-time-series.qmd"
- "docs/reference"
# - "docs/pipeline.qmd"
- "docs/contributing.md"
Expand Down
52 changes: 52 additions & 0 deletions docs/cpm-projection.qdm
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: "UKCP 2.2 Temporal Interpolation"
format:
html:
code-fold: true
execute:
cache: true
jupyter: python3
---

To align climate projections with measured records, we interpolate five or six extra time points---standard or leap years respectively---per year.

```{python}
#| label: fig-time-series
#| fig-cap: "Raw CPM projection 360 day years"

from typing import Final
from pathlib import Path

from xarray.typing import T_Dataset

from clim_recal.utils.xarray import (
annual_group_xr_time_series,
join_xr_time_series_var,
plot_xarray,
)
from clim_recal.utils.core import climate_data_mount_path
# import numpy as np
# import matplotlib.pyplot as plt

raw_tasmax_run_01: Final[Path] = climate_data_mount_path() / 'Raw/UKCPM2.2/tasmax/01/latest'
tasmax_var

cpm_annual_ts: T_Dataset = join_xr_time_series_var(raw_tasmax_run_01, 'tas')

plot_xarray(
getattr(summarised_year, variable_name),
path=plot_path,
time_stamp=time_stamp,
**kwargs,
)

# r = np.arange(0, 2, 0.01)
# theta = 2 * np.pi * r
# fig, ax = plt.subplots(
# subplot_kw = {'projection': 'polar'}
# )
# ax.plot(theta, r)
# ax.set_rticks([0.5, 1, 1.5, 2])
# ax.grid(True)
# plt.show()
```
29 changes: 16 additions & 13 deletions python/clim_recal/utils/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def join_xr_time_series_var(


def annual_group_xr_time_series(
path: PathLike,
joined_xr_time_series: T_Dataset | PathLike,
variable_name: str,
groupby_method: str = "time.dayofyear",
method_name: str = "median",
Expand All @@ -639,6 +639,8 @@ def annual_group_xr_time_series(
Parameters
----------
xr_time_series
Provide existing `Dataset` to aggregate and plot (otherwise check `path`).
path
Path to collect files to process from, filtered via `regex`.
variable_name
Expand Down Expand Up @@ -672,17 +674,18 @@ def annual_group_xr_time_series(
Data variables:
tasmax (dayofyear) float64 ... 9.2 8.95 8.408 8.747 ... 6.387 8.15 9.132
"""
full_ts: T_DataArray = join_xr_time_series_var(
path=path,
variable_name=variable_name,
method_name=method_name,
time_dim_name=time_dim_name,
regex=regex,
start=start,
stop=stop,
step=step,
)
summarised_year_groups: T_Dataset = full_ts.groupby(groupby_method)
if isinstance(joined_xr_time_series, PathLike):
joined_xr_time_series = join_xr_time_series_var(
path=joined_xr_time_series,
variable_name=variable_name,
method_name=method_name,
time_dim_name=time_dim_name,
regex=regex,
start=start,
stop=stop,
step=step,
)
summarised_year_groups: T_Dataset = joined_xr_time_series.groupby(groupby_method)
summarised_year: T_Dataset = getattr(summarised_year_groups, method_name)()
try:
assert 360 <= summarised_year.sizes["time"] <= 366
Expand All @@ -691,7 +694,7 @@ def annual_group_xr_time_series(
if plot_path:
plot_xarray(
getattr(summarised_year, variable_name),
path=path,
path=plot_path,
time_stamp=time_stamp,
**kwargs,
)
Expand Down

0 comments on commit 0463ae8

Please sign in to comment.