Calculate the sum of weights, accounting for missing values
(self, da: T_DataArray, dim: Dims = None)
| 232 | return dot(da, weights, dim=dim) |
| 233 | |
| 234 | def _sum_of_weights(self, da: T_DataArray, dim: Dims = None) -> T_DataArray: |
| 235 | """Calculate the sum of weights, accounting for missing values""" |
| 236 | |
| 237 | # we need to mask data values that are nan; else the weights are wrong |
| 238 | mask = da.notnull() |
| 239 | |
| 240 | # bool -> int, because ``xr.dot([True, True], [True, True])`` -> True |
| 241 | # (and not 2); GH4074 |
| 242 | if self.weights.dtype == bool: |
| 243 | sum_of_weights = self._reduce( |
| 244 | mask, |
| 245 | duck_array_ops.astype(self.weights, dtype=int), |
| 246 | dim=dim, |
| 247 | skipna=False, |
| 248 | ) |
| 249 | else: |
| 250 | sum_of_weights = self._reduce(mask, self.weights, dim=dim, skipna=False) |
| 251 | |
| 252 | # 0-weights are not valid |
| 253 | valid_weights = sum_of_weights != 0.0 |
| 254 | |
| 255 | return sum_of_weights.where(valid_weights) |
| 256 | |
| 257 | def _sum_of_squares( |
| 258 | self, |
no test coverage detected