@@ -5087,28 +5087,23 @@ def shiftgrid(lon0,datain,lonsin,start=True,cyclic=360.0):
5087
5087
dataout [...,i0_shift :] = datain [...,start_idx :i0 + start_idx ]
5088
5088
return dataout ,lonsout
5089
5089
5090
- def addcyclic (arrin , lonsin ):
5090
+ def addcyclic (a , axis = - 1 ):
5091
5091
"""
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).
5095
5096
"""
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 )
5101
5105
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 )
5112
5107
5113
5108
def _choosecorners (width ,height ,** kwargs ):
5114
5109
"""
0 commit comments