Skip to content

Commit 9d8191f

Browse files
author
Jeff Whitaker
committed
Merge pull request #147 from j08lue/master
new suggestion for addcyclic
2 parents 8862c27 + c6c1b90 commit 9d8191f

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

Changelog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version 1.0.8 (not yet released)
22
--------------------------------
33
* don't assume grid is regular with adding cyclic point in addcyclic.
4-
New kwarg 'cyclic_dimension' added, with a default of 360. Partially
4+
New kwarg 'cyclic' added, with a default of 360. Partially
55
addresses issue 139.
66
* fix for coastline drawing glitch (issue 123).
77
* update shapefile.py to version 1.2.0 from pyshp.googlecode.com. Add back

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ Jeff Whitaker <[email protected]>
108108

109109
##Thanks
110110

111-
Special thanks to John Hunter, Andrew Straw, Eric Firing, Rob Hetland, Scott Sinclair, Ivan Lima, Erik Andersen, Michael Hearne, Jesper Larsen, Ryan May, David Huard, Mauro Cavalcanti, Chris Murphy, Pierre Gerard-Marchant, Christoph Gohlke, Eric Bruning, Stephane Raynaud, Tom Loredo, Patrick Marsh, Phil Elson, and Henry Hammond for valuable contributions.
111+
Special thanks to John Hunter, Andrew Straw, Eric Firing, Rob Hetland, Scott Sinclair, Ivan Lima, Erik Andersen, Michael Hearne, Jesper Larsen, Ryan May, David Huard, Mauro Cavalcanti, Jonas Bluethgen, Chris Murphy, Pierre Gerard-Marchant, Christoph Gohlke, Eric Bruning, Stephane Raynaud, Tom Loredo, Patrick Marsh, Phil Elson, and Henry Hammond for valuable contributions.

lib/mpl_toolkits/basemap/__init__.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5087,32 +5087,49 @@ 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,cyclic_length=360):
5090+
def addcyclic(*arr,**kwargs):
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``.
5095-
If length of cyclic dimension is not 360 (degrees), set with kwarg
5096-
``cyclic_length``.
5092+
Adds cyclic (wraparound) points in longitude to one or several arrays,
5093+
the last array being longitudes in degrees. E.g.
5094+
5095+
``data1out, data2out, lonsout = addcyclic(data1,data2,lons)``
5096+
5097+
============== ====================================================
5098+
Keywords Description
5099+
============== ====================================================
5100+
axis the dimension longitude is in (default right-most)
5101+
cyclic width of periodic domain (default 360)
5102+
============== ====================================================
50975103
"""
5098-
nlons = arrin.shape[-1]
5099-
newshape = list(arrin.shape)
5100-
newshape[-1] += 1
5101-
if ma.isMA(arrin):
5102-
arrout = ma.zeros(newshape,arrin.dtype)
5103-
else:
5104-
arrout = np.zeros(newshape,arrin.dtype)
5105-
arrout[...,0:nlons] = arrin[:]
5106-
arrout[...,nlons] = arrin[...,0]
5107-
if ma.isMA(lonsin):
5108-
lonsout = ma.zeros(nlons+1,lonsin.dtype)
5104+
# get (default) keyword arguments
5105+
axis = kwargs.get('axis',-1)
5106+
cyclic = kwargs.get('cyclic',360)
5107+
# define functions
5108+
def _addcyclic(a):
5109+
"""addcyclic function for a single data array"""
5110+
npsel = np.ma if np.ma.is_masked(a) else np
5111+
slicer = [slice(None)] * np.ndim(a)
5112+
try:
5113+
slicer[axis] = slice(0, 1)
5114+
except IndexError:
5115+
raise ValueError('The specified axis does not correspond to an '
5116+
'array dimension.')
5117+
return npsel.concatenate((a,a[slicer]),axis=axis)
5118+
def _addcyclic_lon(a):
5119+
"""addcyclic function for a single longitude array"""
5120+
# select the right numpy functions
5121+
npsel = np.ma if np.ma.is_masked(a) else np
5122+
# get cyclic longitudes
5123+
clon = (np.take(a,[0],axis=axis)
5124+
+ cyclic * np.sign(np.diff(np.take(a,[0,-1],axis=axis),axis=axis)))
5125+
# ensure the values do not exceed cyclic
5126+
clonmod = npsel.where(clon<=cyclic,clon,np.mod(clon,cyclic))
5127+
return npsel.concatenate((a,clonmod),axis=axis)
5128+
# process array(s)
5129+
if len(arr) == 1:
5130+
return _addcyclic_lon(arr[-1])
51095131
else:
5110-
lonsout = np.zeros(nlons+1,lonsin.dtype)
5111-
lonsout[0:nlons] = lonsin[:]
5112-
# this assumes a regular grid (in longitude)
5113-
#lonsout[nlons] = lonsin[-1] + lonsin[1]-lonsin[0]
5114-
# the version below is valid for irregular grids.
5115-
lonsout[nlons] = lonsin[-1] + cyclic_length % (lonsin[-1]-lonsin[0])
5132+
return map(_addcyclic,arr[:-1]) + [_addcyclic_lon(arr[-1])]
51165133

51175134
def _choosecorners(width,height,**kwargs):
51185135
"""

0 commit comments

Comments
 (0)