Skip to content

Commit

Permalink
Merge pull request #335 from blaylockbk/accessors-to_180-and-to_360
Browse files Browse the repository at this point in the history
add accessors to_180 and to_360
  • Loading branch information
blaylockbk authored Jul 1, 2024
2 parents 2dde811 + ee83468 commit 4d9b6d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions herbie/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ def center(self):
self._center = (float(lon.mean()), float(lat.mean()))
return self._center

def to_180(self):
"""Wrap longitude coordinates as range [-180,180]."""
ds = self._obj
ds["longitude"] = (ds["longitude"] + 180) % 360 - 180
return ds

def to_360(self):
"""Wrap longitude coordinates as range [0,360]."""
ds = self._obj
ds["longitude"] = (ds["longitude"] - 360) % 360
return ds


@functools.cached_property
def crs(self):
"""
Expand Down
11 changes: 10 additions & 1 deletion tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from herbie import Herbie

import xarray as xr

def test_crs():
H = Herbie(
Expand All @@ -16,6 +16,15 @@ def test_crs():
crs = ds.herbie.crs
assert crs

def test_to_180():
z = xr.Dataset({"longitude": [0, 90, 180, 270, 360]})
z = z.herbie.to_180()
assert all(z["longitude"] == [0, 90, -180, -90, 0])

def test_to_360():
z = xr.Dataset({"longitude": [-90, -180, 0, 90, 180]})
z = z.herbie.to_360()
assert all(z["longitude"] == [270, 180, 0, 90, 180])

def test_polygon():
H = Herbie(
Expand Down

0 comments on commit 4d9b6d4

Please sign in to comment.