diff --git a/obztak/utils/ortho.py b/obztak/utils/ortho.py index e95365f..ccc82e9 100644 --- a/obztak/utils/ortho.py +++ b/obztak/utils/ortho.py @@ -90,11 +90,59 @@ def proj(self,lon,lat): return x, y @staticmethod - def roll(ra,dec): - idx = np.abs(ra - 180).argmin() - if (ra[idx]<180) and (ra[idx+1]>180): idx += 1 - elif (ra[idx]>180) and (ra[idx+1]<180): idx += 1 - return np.roll(ra,-idx), np.roll(dec,-idx) + def wrap_index(lon, lat, wrap=180.): + """ Find the index where the array wraps. + """ + # No wrap: ignore + if wrap is None: return None + + lon = np.atleast_1d(lon) + lat = np.atleast_1d(lat) + + # No array: ignore + if len(lon)==1 or len(lat)==1: return None + + # Map [0,360) + lon = np.mod(lon,360) + wrap = np.mod(wrap,360) + + # Find the index of the entry closest to the wrap angle + idx = np.abs(lon - wrap).argmin() + # First or last index: ignore + if idx == 0 or idx+1 == len(lon): return None + # Value exactly equals wrap, choose next value + elif (lon[idx] == wrap): idx += 1 + # Wrap angle sandwiched + elif (lon[idx]wrap): idx += 1 + elif (lon[idx]wrap): idx += 0 + elif (lon[idx]>wrap) and (lon[idx+1]wrap) and (lon[idx-1]