|
8 | 8 | _inplace_enabled,
|
9 | 9 | _inplace_enabled_define_and_cleanup,
|
10 | 10 | )
|
11 |
| -from .functions import _DEPRECATION_ERROR_ATTRIBUTE, _DEPRECATION_ERROR_KWARGS |
| 11 | +from .functions import ( |
| 12 | + _DEPRECATION_ERROR_ATTRIBUTE, |
| 13 | + _DEPRECATION_ERROR_KWARGS, |
| 14 | + bounds_combination_mode, |
| 15 | +) |
12 | 16 | from .timeduration import TimeDuration
|
13 | 17 | from .units import Units
|
14 | 18 |
|
@@ -246,6 +250,154 @@ def increasing(self):
|
246 | 250 | """
|
247 | 251 | return self.direction()
|
248 | 252 |
|
| 253 | + @_inplace_enabled(default=False) |
| 254 | + def anchor(self, value, cell=False, parameters=None, inplace=False): |
| 255 | + """Anchor the coordinate values. |
| 256 | +
|
| 257 | + By default, the coordinate values are transformed so that the |
| 258 | + first coordinate is the closest to *value* from above (below) |
| 259 | + for increasing (decreasing) coordinates. |
| 260 | +
|
| 261 | + If the *cell* parameter is True, then the coordinate values |
| 262 | + are transformed so that the first cell either contains |
| 263 | + *value*; or is the closest to cell to *value* from above |
| 264 | + (below) for increasing (decreasing) coordinates. |
| 265 | +
|
| 266 | + .. versionadded:: NEXTVERSION |
| 267 | +
|
| 268 | + .. seealso:: `period`, `roll` |
| 269 | +
|
| 270 | + :Parameters: |
| 271 | +
|
| 272 | + value: scalar array_like |
| 273 | + Anchor the coordinate values for the selected cyclic |
| 274 | + axis to the *value*. May be any numeric scalar object |
| 275 | + that can be converted to a `Data` object (which |
| 276 | + includes `numpy` and `Data` objects). If *value* has |
| 277 | + units then they must be compatible with those of the |
| 278 | + coordinates, otherwise it is assumed to have the same |
| 279 | + units as the coordinates. |
| 280 | +
|
| 281 | + The coordinate values are transformed so the first |
| 282 | + corodinate is the closest to *value* from above (for |
| 283 | + increasing coordinates), or the closest to *value* from |
| 284 | + above (for decreasing coordinates) |
| 285 | +
|
| 286 | + * Increasing coordinates with positive period, P, |
| 287 | + are transformed so that *value* lies in the |
| 288 | + half-open range (L-P, F], where F and L are the |
| 289 | + transformed first and last coordinate values, |
| 290 | + respectively. |
| 291 | +
|
| 292 | + .. |
| 293 | +
|
| 294 | + * Decreasing coordinates with positive period, P, |
| 295 | + are transformed so that *value* lies in the |
| 296 | + half-open range (L+P, F], where F and L are the |
| 297 | + transformed first and last coordinate values, |
| 298 | + respectively. |
| 299 | +
|
| 300 | + *Parameter example:* |
| 301 | + If the original coordinates are ``0, 5, ..., 355`` |
| 302 | + (evenly spaced) and the period is ``360`` then |
| 303 | + ``value=0`` implies transformed coordinates of ``0, |
| 304 | + 5, ..., 355``; ``value=-12`` implies transformed |
| 305 | + coordinates of ``-10, -5, ..., 345``; ``value=380`` |
| 306 | + implies transformed coordinates of ``380, 385, ..., |
| 307 | + 735``. |
| 308 | +
|
| 309 | + *Parameter example:* |
| 310 | + If the original coordinates are ``355, 350, ..., 0`` |
| 311 | + (evenly spaced) and the period is ``360`` then |
| 312 | + ``value=355`` implies transformed coordinates of |
| 313 | + ``355, 350, ..., 0``; ``value=0`` implies |
| 314 | + transformed coordinates of ``0, -5, ..., -355``; |
| 315 | + ``value=392`` implies transformed coordinates of |
| 316 | + ``390, 385, ..., 35``. |
| 317 | +
|
| 318 | + cell: `bool`, optional |
| 319 | + If True, then the coordinate values are transformed so |
| 320 | + that the first cell either contains *value*, or is the |
| 321 | + closest to cell to *value* from above (below) for |
| 322 | + increasing (decreasing) coordinates. |
| 323 | +
|
| 324 | + If False (the default) then the coordinate values are |
| 325 | + transformed so that the first coordinate is the closest |
| 326 | + to *value* from above (below) for increasing |
| 327 | + (decreasing) coordinates. |
| 328 | +
|
| 329 | + parameters: `dict`, optional |
| 330 | + If a `dict` is provided then it will be updated |
| 331 | + in-place with parameters which describe thethe |
| 332 | + anchoring process. |
| 333 | +
|
| 334 | + {{inplace: `bool`, optional}} |
| 335 | +
|
| 336 | + :Returns: |
| 337 | +
|
| 338 | + `{{class}}` or `None` |
| 339 | + The anchored dimension coordinates, or `None` if the |
| 340 | + operation was in-place. |
| 341 | +
|
| 342 | + """ |
| 343 | + d = _inplace_enabled_define_and_cleanup(self) |
| 344 | + |
| 345 | + period = d.period() |
| 346 | + if period is None: |
| 347 | + raise ValueError(f"Cyclic {d!r} has no period") |
| 348 | + |
| 349 | + value = d._Data.asdata(value) |
| 350 | + if not value.Units: |
| 351 | + value = value.override_units(d.Units) |
| 352 | + elif not value.Units.equivalent(d.Units): |
| 353 | + raise ValueError( |
| 354 | + f"Anchor value has incompatible units: {value.Units!r}" |
| 355 | + ) |
| 356 | + |
| 357 | + if cell: |
| 358 | + c = d.upper_bounds.persist() |
| 359 | + else: |
| 360 | + d.persist(inplace=True) |
| 361 | + c = d.get_data(_fill_value=False) |
| 362 | + |
| 363 | + if d.increasing: |
| 364 | + # Adjust value so it's in the range [c[0], c[0]+period) |
| 365 | + n = ((c[0] - value) / period).ceil() |
| 366 | + value1 = value + n * period |
| 367 | + shift = c.size - np.argmax((c - value1 >= 0).array) |
| 368 | + d.roll(0, shift, inplace=True) |
| 369 | + if cell: |
| 370 | + d0 = d[0].upper_bounds |
| 371 | + else: |
| 372 | + d0 = d.get_data(_fill_value=False)[0] |
| 373 | + |
| 374 | + n = ((value - d0) / period).ceil() |
| 375 | + else: |
| 376 | + # Adjust value so it's in the range (c[0]-period, c[0]] |
| 377 | + n = ((c[0] - value) / period).floor() |
| 378 | + value1 = value + n * period |
| 379 | + shift = c.size - np.argmax((value1 - c >= 0).array) |
| 380 | + d.roll(0, shift, inplace=True) |
| 381 | + if cell: |
| 382 | + d0 = d[0].upper_bounds |
| 383 | + else: |
| 384 | + d0 = d.get_data(_fill_value=False)[0] |
| 385 | + |
| 386 | + n = ((value - d0) / period).floor() |
| 387 | + |
| 388 | + n.persist(inplace=True) |
| 389 | + if n: |
| 390 | + nperiod = n * period |
| 391 | + with bounds_combination_mode("OR"): |
| 392 | + d += nperiod |
| 393 | + else: |
| 394 | + nperiod = 0 |
| 395 | + |
| 396 | + if parameters is not None: |
| 397 | + parameters.update({"shift": shift, "nperiod": nperiod}) |
| 398 | + |
| 399 | + return d |
| 400 | + |
249 | 401 | def direction(self):
|
250 | 402 | """Return True if the dimension coordinate values are
|
251 | 403 | increasing, otherwise return False.
|
|
0 commit comments