Convert this Coarsen object to a DataArray or Dataset, where the coarsening dimension is split or reshaped to two new dimensions. Parameters ---------- window_dim: mapping A mapping from existing dimension name to new dimension names.
(
self,
window_dim=None,
keep_attrs=None,
**window_dim_kwargs,
)
| 1112 | return f"{self.__class__.__name__} [{attrs}]" |
| 1113 | |
| 1114 | def construct( |
| 1115 | self, |
| 1116 | window_dim=None, |
| 1117 | keep_attrs=None, |
| 1118 | **window_dim_kwargs, |
| 1119 | ) -> T_Xarray: |
| 1120 | """ |
| 1121 | Convert this Coarsen object to a DataArray or Dataset, |
| 1122 | where the coarsening dimension is split or reshaped to two |
| 1123 | new dimensions. |
| 1124 | |
| 1125 | Parameters |
| 1126 | ---------- |
| 1127 | window_dim: mapping |
| 1128 | A mapping from existing dimension name to new dimension names. |
| 1129 | The size of the second dimension will be the length of the |
| 1130 | coarsening window. |
| 1131 | keep_attrs: bool, optional |
| 1132 | Preserve attributes if True |
| 1133 | **window_dim_kwargs : {dim: new_name, ...} |
| 1134 | The keyword arguments form of ``window_dim``. |
| 1135 | |
| 1136 | Returns |
| 1137 | ------- |
| 1138 | Dataset or DataArray with reshaped dimensions |
| 1139 | |
| 1140 | Examples |
| 1141 | -------- |
| 1142 | >>> da = xr.DataArray(np.arange(24), dims="time") |
| 1143 | >>> da.coarsen(time=12).construct(time=("year", "month")) |
| 1144 | <xarray.DataArray (year: 2, month: 12)> Size: 192B |
| 1145 | array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], |
| 1146 | [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) |
| 1147 | Dimensions without coordinates: year, month |
| 1148 | |
| 1149 | See Also |
| 1150 | -------- |
| 1151 | DataArrayRolling.construct |
| 1152 | DatasetRolling.construct |
| 1153 | """ |
| 1154 | |
| 1155 | from xarray.core.dataarray import DataArray |
| 1156 | from xarray.core.dataset import Dataset |
| 1157 | |
| 1158 | window_dim = either_dict_or_kwargs( |
| 1159 | window_dim, window_dim_kwargs, "Coarsen.construct" |
| 1160 | ) |
| 1161 | if not window_dim: |
| 1162 | raise ValueError( |
| 1163 | "Either window_dim or window_dim_kwargs need to be specified." |
| 1164 | ) |
| 1165 | |
| 1166 | bad_new_dims = tuple( |
| 1167 | win |
| 1168 | for win, dims in window_dim.items() |
| 1169 | if len(dims) != 2 or isinstance(dims, str) |
| 1170 | ) |
| 1171 | if bad_new_dims: |