Skip to content

Commit 6f46217

Browse files
committed
change shiftdata so 1 and 2 points work, add tests.. (#213)
1 parent f70b00c commit 6f46217

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,10 +4822,15 @@ def shiftdata(self,lonsin,datain=None,lon_0=None):
48224822
nlons = len(lonsin)
48234823
lonsin = np.where(lonsin > lon_0+180, lonsin-360 ,lonsin)
48244824
lonsin = np.where(lonsin < lon_0-180, lonsin+360 ,lonsin)
4825-
londiff = np.abs(lonsin[0:-1]-lonsin[1:])
4826-
londiff_sort = np.sort(londiff)
4827-
thresh = 360.-londiff_sort[-2]
4828-
itemindex = len(lonsin)-np.where(londiff>=thresh)[0]
4825+
4826+
if nlons > 1:
4827+
londiff = np.abs(lonsin[0:-1]-lonsin[1:])
4828+
londiff_sort = np.sort(londiff)
4829+
thresh = 360.-londiff_sort[-2] if nlons > 2 else 360.0 - londiff_sort[-1]
4830+
itemindex = len(lonsin)-np.where(londiff>=thresh)[0]
4831+
else:
4832+
itemindex = 0
4833+
48294834
if itemindex:
48304835
# check to see if cyclic (wraparound) point included
48314836
# if so, remove it.

lib/mpl_toolkits/basemap/test.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def make_array(self):
1313
u = np.ones((len(lat), len(lon)))
1414
v = np.zeros((len(lat), len(lon)))
1515
return u,v,lat,lon
16-
16+
1717
def test_cylindrical(self):
1818
# Cylindrical case
1919
B = Basemap()
@@ -22,7 +22,7 @@ def test_cylindrical(self):
2222
# Check that the vectors are identical.
2323
assert_almost_equal(ru, u)
2424
assert_almost_equal(rv, v)
25-
25+
2626
def test_nan(self):
2727
B = Basemap()
2828
u,v,lat,lon=self.make_array()
@@ -32,12 +32,12 @@ def test_nan(self):
3232
assert not np.isnan(ru).any()
3333
assert_almost_equal(u, ru)
3434
assert_almost_equal(v, rv)
35-
35+
3636
def test_npstere(self):
3737
# NP Stereographic case
3838
B=Basemap(projection='npstere', boundinglat=50., lon_0=0.)
3939
u,v,lat,lon=self.make_array()
40-
v = np.ones((len(lat), len(lon)))
40+
v = np.ones((len(lat), len(lon)))
4141
ru, rv = B.rotate_vector(u,v, lon, lat)
4242
assert_almost_equal(ru[2, :],[1,-1,-1,1], 6)
4343
assert_almost_equal(rv[2, :],[1,1,-1,-1], 6)
@@ -96,6 +96,35 @@ def test_no_cyc2(self):
9696
assert (grid==gridout).all()
9797

9898

99+
class TestShiftdata(TestCase):
100+
101+
def test_2_points_should_work(self):
102+
"""
103+
Shiftdata should work with 2 points
104+
"""
105+
bm = Basemap(llcrnrlon=0, llcrnrlat=-80, urcrnrlon=360, urcrnrlat=80, projection='mill')
106+
107+
lons_expected = [10, 15, 20]
108+
lonsout = bm.shiftdata(lons_expected)
109+
assert_almost_equal(lons_expected, lonsout)
110+
111+
lonsout_expected = bm.shiftdata([10, 361, 362])
112+
lonsout = bm.shiftdata([10, 361])
113+
assert_almost_equal(lonsout_expected[:len(lonsout)], lonsout)
114+
115+
def test_1_point_should_work(self):
116+
bm = Basemap(llcrnrlon=0, llcrnrlat=-80, urcrnrlon=360, urcrnrlat=80, projection='mill')
117+
118+
# should not fail
119+
lonsout = bm.shiftdata([361])
120+
assert_almost_equal(lonsout, [1.0,])
121+
122+
lonsout = bm.shiftdata([10])
123+
assert_almost_equal(lonsout, [10.0,])
124+
125+
126+
127+
99128
def test():
100129
"""
101130
Run some tests.

0 commit comments

Comments
 (0)