Skip to content

Commit 872160f

Browse files
committed
new version that works for n-dimensional arrays (or several of them)
1 parent a68aeb3 commit 872160f

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5087,28 +5087,23 @@ def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
50875087
dataout[...,i0_shift:] = datain[...,start_idx:i0+start_idx]
50885088
return dataout,lonsout
50895089

5090-
def addcyclic(arrin,lonsin):
5090+
def addcyclic(a,axis=-1):
50915091
"""
5092-
``arrout, lonsout = addcyclic(arrin, lonsin)``
5093-
adds cyclic (wraparound) point in longitude to ``arrin`` and ``lonsin``,
5094-
assumes longitude is the right-most dimension of ``arrin``.
5092+
``arrout, lonsout = addcyclic((arrin, lonsin),axis=-1)``
5093+
adds cyclic (wraparound) points in longitude to one or several arrays,
5094+
(e.g. ``arrin`` and ``lonsin``),
5095+
where ``axis`` sets the dimension longitude is in (default: right-most).
50955096
"""
5096-
nlons = arrin.shape[-1]
5097-
newshape = list(arrin.shape)
5098-
newshape[-1] += 1
5099-
if ma.isMA(arrin):
5100-
arrout = ma.zeros(newshape,arrin.dtype)
5097+
# define function for a single grid array
5098+
def _addcyclic(a):
5099+
aT = np.swapaxes(a,0,axis)
5100+
idx = np.append(np.arange(aT.shape[0]),0)
5101+
return np.swapaxes(aT[idx],axis,0)
5102+
# process eventual list/tuple of arrays
5103+
if isinstance(a,list) or isinstance(a,tuple):
5104+
return map(_addcyclic,a)
51015105
else:
5102-
arrout = np.zeros(newshape,arrin.dtype)
5103-
arrout[...,0:nlons] = arrin[:]
5104-
arrout[...,nlons] = arrin[...,0]
5105-
if ma.isMA(lonsin):
5106-
lonsout = ma.zeros(nlons+1,lonsin.dtype)
5107-
else:
5108-
lonsout = np.zeros(nlons+1,lonsin.dtype)
5109-
lonsout[0:nlons] = lonsin[:]
5110-
lonsout[nlons] = lonsin[-1] + lonsin[1]-lonsin[0]
5111-
return arrout,lonsout
5106+
return _addcyclic(a)
51125107

51135108
def _choosecorners(width,height,**kwargs):
51145109
"""

0 commit comments

Comments
 (0)