Skip to content

Commit c8ded4e

Browse files
committed
issue #213, fix and tests for the 2d case
1 parent 6f46217 commit c8ded4e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4777,10 +4777,15 @@ def shiftdata(self,lonsin,datain=None,lon_0=None):
47774777
lonsin1 = lonsin[0,:]
47784778
lonsin1 = np.where(lonsin1 > lon_0+180, lonsin1-360 ,lonsin1)
47794779
lonsin1 = np.where(lonsin1 < lon_0-180, lonsin1+360 ,lonsin1)
4780-
londiff = np.abs(lonsin1[0:-1]-lonsin1[1:])
4781-
londiff_sort = np.sort(londiff)
4782-
thresh = 360.-londiff_sort[-2]
4783-
itemindex = nlons-np.where(londiff>=thresh)[0]
4780+
if nlons > 1:
4781+
londiff = np.abs(lonsin1[0:-1]-lonsin1[1:])
4782+
londiff_sort = np.sort(londiff)
4783+
thresh = 360.-londiff_sort[-2] if nlons > 2 else 360.-londiff_sort[-1]
4784+
itemindex = nlons-np.where(londiff>=thresh)[0]
4785+
else:
4786+
lonsin[0, :] = lonsin1
4787+
itemindex = 0
4788+
47844789
# if no shift necessary, itemindex will be
47854790
# empty, so don't do anything
47864791
if itemindex:

lib/mpl_toolkits/basemap/test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ def test_no_cyc2(self):
9898

9999
class TestShiftdata(TestCase):
100100

101+
def _get_2d_lons(self, lons1d):
102+
"""
103+
Generate a 2d grid
104+
"""
105+
lats = [10, ] * len(lons1d)
106+
return np.meshgrid(lons1d, lats)[0]
107+
101108
def test_2_points_should_work(self):
102109
"""
103110
Shiftdata should work with 2 points
@@ -122,7 +129,24 @@ def test_1_point_should_work(self):
122129
lonsout = bm.shiftdata([10])
123130
assert_almost_equal(lonsout, [10.0,])
124131

132+
lonsin = np.array([361.0])
133+
lonsin.shape = (1, 1)
134+
lonsout = bm.shiftdata(lonsin)
135+
assert_almost_equal(lonsout.squeeze(), [1.0,])
136+
137+
def test_less_than_n_by_3_points_should_work(self):
138+
bm = Basemap(llcrnrlon=0, llcrnrlat=-80, urcrnrlon=360, urcrnrlat=80, projection='mill')
139+
lons_expected = self._get_2d_lons([10, 15, 20])
140+
141+
# nothing should change
142+
lonsout = bm.shiftdata(lons_expected)
143+
assert_almost_equal(lons_expected, lonsout)
125144

145+
# shift n x 3 and n x 2 grids and compare results over overlapping region
146+
lonsin = self._get_2d_lons([10, 361, 362])
147+
lonsout_expected = bm.shiftdata(lonsin)[:, :2]
148+
lonsout = bm.shiftdata(lonsin[:, :2])
149+
assert_almost_equal(lonsout_expected, lonsout)
126150

127151

128152
def test():

0 commit comments

Comments
 (0)