Compute chunk sizes for this season resampler. This method is used during chunking operations to determine appropriate chunk sizes for the given variable when using this resampler. Parameters ---------- name : Hashable The name of the di
(self, variable: Variable, *, dim: Hashable)
| 1055 | return EncodedGroups(codes=codes, full_index=full_index) |
| 1056 | |
| 1057 | def compute_chunks(self, variable: Variable, *, dim: Hashable) -> tuple[int, ...]: |
| 1058 | """ |
| 1059 | Compute chunk sizes for this season resampler. |
| 1060 | |
| 1061 | This method is used during chunking operations to determine appropriate |
| 1062 | chunk sizes for the given variable when using this resampler. |
| 1063 | |
| 1064 | Parameters |
| 1065 | ---------- |
| 1066 | name : Hashable |
| 1067 | The name of the dimension being chunked. |
| 1068 | variable : Variable |
| 1069 | The variable being chunked. |
| 1070 | |
| 1071 | Returns |
| 1072 | ------- |
| 1073 | tuple[int, ...] |
| 1074 | A tuple of chunk sizes for the dimension. |
| 1075 | """ |
| 1076 | if not _contains_datetime_like_objects(variable): |
| 1077 | raise ValueError( |
| 1078 | f"Computing chunks with {type(self)!r} only supported for datetime variables. " |
| 1079 | f"Received variable with dtype {variable.dtype!r} instead." |
| 1080 | ) |
| 1081 | |
| 1082 | if len("".join(self.seasons)) != 12: |
| 1083 | raise ValueError( |
| 1084 | "Cannot rechunk with a SeasonResampler that does not cover all 12 months. " |
| 1085 | f"Received `seasons={self.seasons!r}`." |
| 1086 | ) |
| 1087 | |
| 1088 | # Create a temporary resampler that ignores drop_incomplete for chunking |
| 1089 | # This prevents data from being silently dropped during chunking |
| 1090 | resampler_for_chunking = type(self)(seasons=self.seasons, drop_incomplete=False) |
| 1091 | |
| 1092 | chunks = ( |
| 1093 | DataArray( |
| 1094 | np.ones(variable.shape, dtype=int), |
| 1095 | dims=(dim,), |
| 1096 | coords={dim: variable}, |
| 1097 | ) |
| 1098 | .resample({dim: resampler_for_chunking}) |
| 1099 | .sum() |
| 1100 | ) |
| 1101 | # When bins (binning) or time periods are missing (resampling) |
| 1102 | # we can end up with NaNs. Drop them. |
| 1103 | if chunks.dtype.kind == "f": |
| 1104 | chunks = chunks.dropna(dim).astype(int) |
| 1105 | chunks_tuple: tuple[int, ...] = tuple(chunks.data.tolist()) |
| 1106 | return chunks_tuple |
| 1107 | |
| 1108 | def reset(self) -> Self: |
| 1109 | return type(self)(seasons=self.seasons, drop_incomplete=self.drop_incomplete) |