|
58 | 58 | local_to_coordinates, |
59 | 59 | reverse_index, |
60 | 60 | wrap_longitude, |
| 61 | + window_index, |
61 | 62 | ) |
62 | 63 | from ..poly import ( |
63 | 64 | bbox_intersection, |
@@ -574,51 +575,58 @@ def __iter__(self): |
574 | 575 | for obs in self.obs: |
575 | 576 | yield obs |
576 | 577 |
|
577 | | - def iter_on(self, xname, bins=None): |
| 578 | + def iter_on(self, xname, window=None, bins=None): |
578 | 579 | """ |
579 | 580 | Yield observation group for each bin. |
580 | 581 |
|
581 | 582 | :param str,array xname: |
| 583 | + :param float,None window: if defined we use a moving window with value like half window |
582 | 584 | :param array bins: bounds of each bin |
583 | 585 | :yield array,float,float: index in self, lower bound, upper bound |
584 | 586 |
|
585 | 587 | .. minigallery:: py_eddy_tracker.EddiesObservations.iter_on |
586 | 588 | """ |
587 | 589 | x = self.parse_varname(xname) |
588 | | - d = x[1:] - x[:-1] |
589 | | - if bins is None: |
590 | | - bins = arange(x.min(), x.max() + 2) |
591 | | - elif not isinstance(bins, ndarray): |
592 | | - bins = array(bins) |
593 | | - nb_bins = len(bins) - 1 |
594 | | - |
595 | | - # Not monotonous |
596 | | - if (d < 0).any(): |
597 | | - # If bins cover a small part of value |
598 | | - test, translate, x = iter_mode_reduce(x, bins) |
599 | | - # convert value in bins number |
600 | | - i = numba_digitize(x, bins) - 1 |
601 | | - # Order by bins |
602 | | - i_sort = i.argsort() |
603 | | - # If in reduced mode we will translate i_sort in full array index |
604 | | - i_sort_ = translate[i_sort] if test else i_sort |
605 | | - # Bound for each bins in sorting view |
606 | | - i0, i1, _ = build_index(i[i_sort]) |
607 | | - m = ~(i0 == i1) |
608 | | - i0, i1 = i0[m], i1[m] |
609 | | - for i0_, i1_ in zip(i0, i1): |
610 | | - i_bins = i[i_sort[i0_]] |
611 | | - if i_bins == -1 or i_bins == nb_bins: |
612 | | - continue |
613 | | - yield i_sort_[i0_:i1_], bins[i_bins], bins[i_bins + 1] |
| 590 | + if window is not None: |
| 591 | + x0 = arange(x.min(), x.max()) if bins is None else array(bins) |
| 592 | + i_ordered, first_index, last_index = window_index(x, x0, window) |
| 593 | + for x_, i0, i1 in zip(x0, first_index, last_index): |
| 594 | + yield i_ordered[i0: i1], x_ - window, x_ + window |
614 | 595 | else: |
615 | | - i = numba_digitize(x, bins) - 1 |
616 | | - i0, i1, _ = build_index(i) |
617 | | - m = ~(i0 == i1) |
618 | | - i0, i1 = i0[m], i1[m] |
619 | | - for i0_, i1_ in zip(i0, i1): |
620 | | - i_bins = i[i0_] |
621 | | - yield slice(i0_, i1_), bins[i_bins], bins[i_bins + 1] |
| 596 | + d = x[1:] - x[:-1] |
| 597 | + if bins is None: |
| 598 | + bins = arange(x.min(), x.max() + 2) |
| 599 | + elif not isinstance(bins, ndarray): |
| 600 | + bins = array(bins) |
| 601 | + nb_bins = len(bins) - 1 |
| 602 | + |
| 603 | + # Not monotonous |
| 604 | + if (d < 0).any(): |
| 605 | + # If bins cover a small part of value |
| 606 | + test, translate, x = iter_mode_reduce(x, bins) |
| 607 | + # convert value in bins number |
| 608 | + i = numba_digitize(x, bins) - 1 |
| 609 | + # Order by bins |
| 610 | + i_sort = i.argsort() |
| 611 | + # If in reduced mode we will translate i_sort in full array index |
| 612 | + i_sort_ = translate[i_sort] if test else i_sort |
| 613 | + # Bound for each bins in sorting view |
| 614 | + i0, i1, _ = build_index(i[i_sort]) |
| 615 | + m = ~(i0 == i1) |
| 616 | + i0, i1 = i0[m], i1[m] |
| 617 | + for i0_, i1_ in zip(i0, i1): |
| 618 | + i_bins = i[i_sort[i0_]] |
| 619 | + if i_bins == -1 or i_bins == nb_bins: |
| 620 | + continue |
| 621 | + yield i_sort_[i0_:i1_], bins[i_bins], bins[i_bins + 1] |
| 622 | + else: |
| 623 | + i = numba_digitize(x, bins) - 1 |
| 624 | + i0, i1, _ = build_index(i) |
| 625 | + m = ~(i0 == i1) |
| 626 | + i0, i1 = i0[m], i1[m] |
| 627 | + for i0_, i1_ in zip(i0, i1): |
| 628 | + i_bins = i[i0_] |
| 629 | + yield slice(i0_, i1_), bins[i_bins], bins[i_bins + 1] |
622 | 630 |
|
623 | 631 | def align_on(self, other, var_name="time", all_ref=False, **kwargs): |
624 | 632 | """ |
@@ -2420,6 +2428,9 @@ def create_particles(self, step, intern=True): |
2420 | 2428 | xname, yname = self.intern(intern) |
2421 | 2429 | return create_meshed_particles(self[xname], self[yname], step) |
2422 | 2430 |
|
| 2431 | + def empty_dataset(self): |
| 2432 | + return self.new_like(self, 0) |
| 2433 | + |
2423 | 2434 |
|
2424 | 2435 | @njit(cache=True) |
2425 | 2436 | def grid_count_(grid, i, j): |
|
0 commit comments