CLIMATE-392 - Shift lat/lon grid during Dataset creation - When creating a Dataset object with a lon grid that ranges from 0-360 the values are now shifted to fall within [-180, 180).
Project: http://git-wip-us.apache.org/repos/asf/climate/repo Commit: http://git-wip-us.apache.org/repos/asf/climate/commit/b75633b1 Tree: http://git-wip-us.apache.org/repos/asf/climate/tree/b75633b1 Diff: http://git-wip-us.apache.org/repos/asf/climate/diff/b75633b1 Branch: refs/heads/master Commit: b75633b191840daef0780cb417fb8dd1a46525f2 Parents: 0e65170 Author: Michael Joyce <[email protected]> Authored: Fri Apr 4 17:34:25 2014 -0700 Committer: Michael Joyce <[email protected]> Committed: Fri Apr 4 17:34:25 2014 -0700 ---------------------------------------------------------------------- ocw/dataset.py | 12 ++++++++++++ ocw/tests/test_dataset.py | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/climate/blob/b75633b1/ocw/dataset.py ---------------------------------------------------------------------- diff --git a/ocw/dataset.py b/ocw/dataset.py index 24f7220..f0b61a9 100644 --- a/ocw/dataset.py +++ b/ocw/dataset.py @@ -26,6 +26,8 @@ import numpy import logging import datetime as dt +from mpl_toolkits.basemap import shiftgrid + logger = logging.getLogger(__name__) class Dataset: @@ -60,6 +62,16 @@ class Dataset: self.variable = variable self.name = name + # We want our lon. values to be [-180, 180). If they're out of this + # grid then we need to shift them so they're not! Check the Basemap + # docs for additional information. + # http://matplotlib.org/basemap/api/basemap_api.html + if self.lons.max() > 180: + self.values, self.lons = shiftgrid(180, + self.values, + self.lons, + start=False) + def spatial_boundaries(self): '''Calculate the spatial boundaries. http://git-wip-us.apache.org/repos/asf/climate/blob/b75633b1/ocw/tests/test_dataset.py ---------------------------------------------------------------------- diff --git a/ocw/tests/test_dataset.py b/ocw/tests/test_dataset.py index d11aa90..4dd57f6 100644 --- a/ocw/tests/test_dataset.py +++ b/ocw/tests/test_dataset.py @@ -88,7 +88,15 @@ class TestInvalidDatasetInit(unittest.TestCase): with self.assertRaises(ValueError): Dataset(self.lat, self.lon, self.time, self.values_in_wrong_order) - + def test_lons_values_incorrectly_gridded(self): + times = np.array([dt.datetime(2000, x, 1) for x in range(1, 13)]) + lats = np.array(range(-30, 30)) + bad_lons = np.array(range(360)) + flat_array = np.array(range(len(times) * len(lats) * len(bad_lons))) + values = flat_array.reshape(len(times), len(lats), len(bad_lons)) + + ds = Dataset(lats, bad_lons, times, values) + self.assertTrue(np.array_equal(ds.lons, range(-180, 180))) class TestDatasetFunctions(unittest.TestCase): def setUp(self):
