Esempio n. 1
0
 def testChoose(self):
     ct1 = MV2.TransientVariable([1, 1, 2, 0, 1])
     ctr = MV2.choose(ct1, [numpy.ma.masked, 10, 20, 30, 40])
     self.assertTrue(MV2.allclose(ctr, [10, 10, 20, 100, 10]))
     ctx = MV2.TransientVariable([1, 2, 3, 150, 4])
     cty = -MV2.TransientVariable([1, 2, 3, 150, 4])
     ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, 100))
     self.assertTrue(MV2.allclose(ctr, [1, 2, 3, 100, 4]))
     ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, cty))
     self.assertTrue(MV2.allclose(ctr, [1, 2, 3, -150, 4]))
Esempio n. 2
0
 def testTranspose(self):
     ## transpose(a, axes=None)
     # transpose(a, axes=None) reorder dimensions per tuple axes
     xtr = MV2.transpose(self.u_file)
     xtr = numpy.arange(24)
     xtr.shape = (4, 6)
     xtr1 = MV2.transpose(xtr)
     xtr2 = MV2.transpose(MV2.TransientVariable(xtr))
     self.assertTrue(xtr2.shape == (6, 4))
     xtr3 = numpy.transpose(xtr)
     self.assertTrue(MV2.allclose(xtr1, xtr3))
     self.assertTrue(MV2.allclose(xtr2, xtr3))
Esempio n. 3
0
 def testKwargs(self):
     # This test makes sure that kwargs are passed through in each of the
     # types of operation
     arr = numpy.full((3, 3), .65) * [range(i, i + 3) for i in range(3)]
     # Test var_unary_operation
     v = MV2.around(arr, decimals=1)
     valid = numpy.array([
         [0.0, 0.6, 1.3],
         [0.6, 1.3, 2.0],
         [1.3, 2.0, 2.6]
     ])
     self.assertTrue(MV2.allequal(v, valid))
     xs = [1, -1, -1, 1]
     ys = [1, 1, -1, -1]
     angles = [numpy.pi / 4, 3 * numpy.pi /
               4, -3 * numpy.pi / 4, -numpy.pi / 4]
     test_arr = numpy.zeros(4)
     # Test var_binary_operation
     MV2.arctan2(ys, xs, out=test_arr)
     self.assertFalse(MV2.allclose(test_arr, 0))
     self.assertTrue(MV2.allequal(test_arr, angles))
     # Test var_unary_operation_with_axis
     values = numpy.tile(numpy.arange(2), 3)
     self.assertEqual(
         len(MV2.sometrue(values, axis=0, keepdims=True).shape), len(values.shape))
Esempio n. 4
0
    def teestTimes(self):
        fsc = cdms2.open(os.path.join(cdat_info.get_sampledata_path(),'tas_mo_clim.nc'))

        print("Step #0 : Reading data")
        s=self.f('tas',longitude=(0,360,'co'))

        acok=fsc('climseas',longitude=(0,360,'co'))

        print('Test #1 : Test result')

        ac=cdutil.times.JAN.climatology(s)

        self.assertTrue(MV2.allclose(ac[0],acok[0]))

        fsc.close()

        a=cdtime.comptime(1980)
        b=cdtime.comptime(1980,5)

        f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(),'tas_6h.nc'))
        s=f('tas',time=(a,b,'co'),squeeze=1)

        print("Test #2 : 6hourly AND get")
        jans=cdutil.times.JAN(s)

        print("Test #3 : climatology 6h")
        JFMA=cdutil.times.Seasons('JFMA')
        jfma=JFMA.climatology(s)


        #Test reorder
        print("Test #4 : time not first axis")
        jfma=JFMA.climatology(s(order='x...'))
        print("Test 4b: Result ok ?")
        self.assertEqual(jfma.getOrder()[0], 'x')
Esempio n. 5
0
 def testMulDiv(self):
     scalar_mul = self.u_file * 2
     broadcast_mul = scalar_mul * self.other_u_file
     self.assertTrue(
         MV2.allequal(broadcast_mul[0] / self.u_file[0] / self.other_u_file,
                      2))
     scalar_right = 1 / self.u_file[:]
     self.assertTrue(MV2.allclose(scalar_mul * scalar_right, 2))
Esempio n. 6
0
def testDaskArrayFV(c, s, a, b):
    f = cdms2.open(cdat_info.get_sampledata_path() + "/clt.nc")
    dataFV = f["clt"]
    myDaskArray = da.from_array(dataFV, chunks=(1, 46, 72))
    x = c.compute(myDaskArray)
    myResult = yield x
    assert MV2.allclose(myResult, dataFV) == True
    f.close()
Esempio n. 7
0
def testDaskArrayTV(c, s, a, b):
    f = cdms2.open(cdat_info.get_sampledata_path() + "/clt.nc")
    dataTV = f("clt")
    myDaskArray = da.from_array(dataTV, chunks=(1, 46, 72))
    y = c.compute(myDaskArray)
    myTVResult = yield y
    assert MV2.allclose(myTVResult, dataTV) == True
    f.close()
Esempio n. 8
0
 def testTVSerializeDeserialize(self):
     #
     # make sure that JSON is uncomporessed since
     # zlib compress is called by __getstate__()
     #
     TVstate = self.dataTV.__getstate__()
     newvar = cdms2.fromJSON(TVstate)
     self.assertTrue(MV2.allclose(self.dataTV, newvar))
Esempio n. 9
0
    def getValueAt(self, evt):
        variable = self._data['variable']
        x = evt["x"]
        y = evt["y"]
        cursorX = x / self.image_width
        cursorY = 1.0 - (y / self.image_height)
        v = self._file(variable)
        disp, data = self._canvas.animate_info[0]
        data = data[0]
        t = self._canvas.gettemplate(disp.template)
        dx1 = t.data.x1
        dx2 = t.data.x2
        dy1 = t.data.y1
        dy2 = t.data.y2
        #print "x ", x,cursorX,dx1,dx2
        #print "y ", y, cursorY, dy1,dy2
        if (dx1 < cursorX < dx2) and (dy1 < cursorY < dy2):
            X = data.getAxis(-1)
            Y = data.getAxis(-2)
            if (disp.g_type == "isofill"):
                b = self._canvas.getisofill(disp.g_name)
            if MV2.allclose(b.datawc_x1,1.e20):
                X1 = X[0]
                X2 = X[-1]
            else:
                X1 = b.datawc_x1
                X2 = b.datawc_x2
            if MV2.allclose(b.datawc_y1,1.e20):
                Y1 = Y[0]
                Y2 = Y[-1]
            else:
                Y1 = b.datawc_y1
                Y2 = b.datawc_y2

            L = ((cursorX-dx1)/(dx2-dx1) * (X2-X1)) + X1
            SX = slice(*X.mapInterval((L,L,"cob")))
            l = ((cursorY-dy1)/(dy2-dy1) * (Y2-Y1)) + Y1
            SY = slice(*Y.mapInterval((l,l,"cob")))
            myRank = data.rank()

            if myRank > 2:
                return {'value': str(data[...,SY,SX].flat[0])}
            else:
                return {'value': str(data[...,SY,SX])}
        else:
            return ""
Esempio n. 10
0
File: uweb.py Progetto: imclab/uvis
    def getDataValueFromCursor(self, cursorX, cursorY):
        #f=cdms2.open(self._netcdfFile)
        v=self.f(self._variable)
        disp, data = self._canvas.animate_info[0]
        data = data[0]
        t=self._canvas.gettemplate(disp.template)
        dx1 = t.data.x1
        dx2 = t.data.x2
        dy1 = t.data.y1
        dy2 = t.data.y2
        print "cursorX: ", cursorX
        print "cursorY: ", cursorY
        print "dx1", dx1
        print "dx2", dx2
        print "dy1", dy1
        print "dy2",  dy2
        if (dx1 < cursorX < dx2) and (dy1 < cursorY < dy2):
            X = data.getAxis(-1)
            Y = data.getAxis(-2)
            if (disp.g_type == "boxfill"):
                b = self._canvas.getboxfill(disp.g_name)
            if MV2.allclose(b.datawc_x1,1.e20):
                X1 = X[0]
                X2 = X[-1]
            else:
                X1 = b.datawc_x1
                X2 = b.datawc_x2
            if MV2.allclose(b.datawc_y1,1.e20):
                Y1 = Y[0]
                Y2 = Y[-1]
            else:
                Y1 = b.datawc_y1
                Y2 = b.datawc_y2
            print "X1", X1
            print "X2", X2
            print "Y1", Y1
            print "Y2", Y2

            L = ((cursorX-dx1)/(dx2-dx1)*(X2-X1))+X1
            SX = slice(*X.mapInterval((L,L,"cob")))
            l = ((cursorY-dy1)/(dy2-dy1)*(Y2-Y1))+Y1
            SY = slice(*Y.mapInterval((l,l,"cob")))
            return data[...,SY,SX].flat[0]
        else:
          return ""
Esempio n. 11
0
##   weights if given must have a's shape.
##   Denominator is multiplied by 1.0 to prevent truncation for integers.
##   returned governs return of second quantity, the weights.
xav = MV2.average(xones, axis=1)
xav2 = MV2.average(ud)
xav3 = MV2.average(udat)
xav4, wav4 = MV2.average(udat,
                         weights=MV2.ones(udat.shape, numpy.float),
                         returned=1)

## choose(indices, t)
##   Shaped like indices, values t[i] where at indices[i]
##   If t[j] is masked, special treatment to preserve type.
ct1 = MV2.TransientVariable([1, 1, 2, 0, 1])
ctr = MV2.choose(ct1, [numpy.ma.masked, 10, 20, 30, 40])
if not MV2.allclose(ctr, [10, 10, 20, 100, 10]): markError('choose error 1')
ctx = MV2.TransientVariable([1, 2, 3, 150, 4])
cty = -MV2.TransientVariable([1, 2, 3, 150, 4])
ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, 100))
if not MV2.allclose(ctr, [1, 2, 3, 100, 4]): markError('choose error 2')
ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, cty))
if not MV2.allclose(ctr, [1, 2, 3, -150, 4]): markError('choose error 3')

## concatenate(arrays, axis=0, axisid=None, axisattributes=None)
##   Concatenate the arrays along the given axis. Give the extended axis the id and
##   attributes provided - by default, those of the first array.

try:
    xcon = MV2.concatenate((ud, vd))
except:
    markError('Concatenate error')
Esempio n. 12
0
 def testSerializeDeserialize(self):
     result = deserialize(*serialize(self.dataFV))
     self.assertTrue(MV2.allclose(result, self.dataFV))
     result = deserialize(*serialize(self.dataTV))
     self.assertTrue(MV2.allclose(result, self.dataTV))
Esempio n. 13
0
cdms2.setAutoBounds('on')

f = cdms2.open(os.path.join(vcs.prefix, 'sample_data', 'tas_mo.nc'))
fsc = cdms2.open(os.path.join(vcs.prefix, 'sample_data', 'tas_mo_clim.nc'))

print "Step #0 : Reading data"
s = f(var, longitude=(0, 360, 'co'))

acok = fsc('climseas', longitude=(0, 360, 'co'))

print 'Test #1 : Test result'

ac = times.JAN.climatology(s)

assert (MV2.allclose(ac[0], acok[0]))

f.close()
fsc.close()

a = cdtime.comptime(1980)
b = cdtime.comptime(1980, 5)

f = cdms2.open(os.path.join(vcs.prefix, 'sample_data', 'tas_6h.nc'))
s = f(var, time=(a, b, 'co'), squeeze=1)

print "Test #2 : 6hourly AND get"
jans = times.JAN(s)

print "Test #3 : climatology 6h"
JFMA = times.Seasons('JFMA')
Esempio n. 14
0
 def testTVdumps(self):
     TVstate = self.dataTV.dumps()
     newvar = cdms2.fromJSON(TVstate)
     self.assertTrue(MV2.allclose(self.dataTV, newvar))
Esempio n. 15
0
##   If weights are given, result is sum(a*weights)/sum(weights), with
##   all elements masked in a or in weights ignored.
##   weights if given must have a's shape.
##   Denominator is multiplied by 1.0 to prevent truncation for integers.
##   returned governs return of second quantity, the weights.
xav = MV2.average(xones, axis=1)
xav2 = MV2.average(ud)
xav3 = MV2.average(udat)
xav4, wav4 = MV2.average(udat, weights=MV2.ones(udat.shape, numpy.float), returned=1)

## choose(indices, t)
##   Shaped like indices, values t[i] where at indices[i]
##   If t[j] is masked, special treatment to preserve type.
ct1 = MV2.TransientVariable([1, 1, 2, 0, 1])
ctr = MV2.choose(ct1, [numpy.ma.masked, 10, 20, 30, 40])
if not MV2.allclose(ctr, [10, 10, 20, 100, 10]):
    markError("choose error 1")
ctx = MV2.TransientVariable([1, 2, 3, 150, 4])
cty = -MV2.TransientVariable([1, 2, 3, 150, 4])
ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, 100))
if not MV2.allclose(ctr, [1, 2, 3, 100, 4]):
    markError("choose error 2")
ctr = MV2.choose(MV2.greater(ctx, 100), (ctx, cty))
if not MV2.allclose(ctr, [1, 2, 3, -150, 4]):
    markError("choose error 3")

## concatenate(arrays, axis=0, axisid=None, axisattributes=None)
##   Concatenate the arrays along the given axis. Give the extended axis the id and
##   attributes provided - by default, those of the first array.

try:
Esempio n. 16
0
##   If weights are given, result is sum(a*weights)/sum(weights), with
##   all elements masked in a or in weights ignored.
##   weights if given must have a's shape. 
##   Denominator is multiplied by 1.0 to prevent truncation for integers.
##   returned governs return of second quantity, the weights.
xav = MV2.average(xones, axis=1)
xav2 = MV2.average(ud)
xav3 = MV2.average(udat)
xav4, wav4 = MV2.average(udat, weights=MV2.ones(udat.shape, numpy.float), returned=1)

## choose(indices, t) 
##   Shaped like indices, values t[i] where at indices[i]
##   If t[j] is masked, special treatment to preserve type.
ct1 = MV2.TransientVariable([1,1,2,0,1])
ctr = MV2.choose(ct1, [numpy.ma.masked, 10,20,30,40])
if not MV2.allclose(ctr, [10, 10, 20, 100, 10]): markError('choose error 1')
ctx = MV2.TransientVariable([1,2,3,150,4])
cty = -MV2.TransientVariable([1,2,3,150,4])
ctr = MV2.choose(MV2.greater(ctx,100), (ctx, 100))
if not MV2.allclose(ctr, [1,2,3,100,4]): markError('choose error 2')
ctr = MV2.choose(MV2.greater(ctx,100), (ctx, cty))
if not MV2.allclose(ctr, [1,2,3,-150,4]): markError('choose error 3')

## concatenate(arrays, axis=0, axisid=None, axisattributes=None) 
##   Concatenate the arrays along the given axis. Give the extended axis the id and
##   attributes provided - by default, those of the first array.

try:
    xcon = MV2.concatenate((ud,vd))
except:
    markError('Concatenate error')
Esempio n. 17
0
 def testpickle(self):
     newvar = pickle.loads(pickle.dumps(self.dataTV))
     self.assertTrue(MV2.allclose(self.dataTV, newvar))
Esempio n. 18
0
# Third Method: Use the area weights from Charles' function created above
#               but call the averaging only one 1 dimension at a time. Use the average &
#               weights from the first step in the averaging at the second step!.
#
temp_step, temp_wts = averager(x, axis='x', weight=aw, returned=1)
result3 = averager(temp_step, axis='y', weight=temp_wts)

#
# Note that the above way of doing multiple steps is useful when you want the temp_wts
# altered between steps.......

#
# Now check the 3 results....... they will be different by 273.0 (Centigrade to Kelvin)
#
diff1 = result1 - correct
assert MV2.allclose(273.0, diff1)
diff2 = result2 - correct
assert MV2.allclose(273.0, diff1)
diff3 = result3 - correct
assert MV2.allclose(273.0, diff1)
assert MV2.allclose(result1, result2)
assert MV2.allclose(result2, result3)

#
# This test is to verify the action='sum' option
#
tasm_file = os.path.join(cdat_info.get_sampledata_path(), 'tas_cru_1979.nc')

ftasm = cdms2.open(tasm_file)
xtasm = ftasm('tas')
ywt = area_weights(xtasm)
Esempio n. 19
0
import vcs.test.support
bg = 0  # vcs.test.support.bg

nmonths = 5
## Getting about nmonths months worth of data

W = WK.WK()
f = cdms.open(sys.prefix + "/bin/tutorials/WK/data.nc")
s = f("rlut", time=slice(0, nmonths * 30), latitude=(-15, 15))
f.close()
f = cdms.open(sys.prefix + "/bin/tutorials/WK/results.nc")

## Process the data, i.e compute spectral wave number and frequencies
power = W.process(s)
ok = f('power')
if not MV.allclose(power, ok):
    raise Exception, 'Error computing power, wrong values returned'

## Split between Sym and ASym components
## Averages over time if compresstime is True (default)
S, A = W.split(power)  #,compresstime=False,smooth=False)
ok = f('S')
if not MV.allclose(S, ok):
    raise Exception, 'Error computing symetric, wrong values returned'
ok = f('A')
if not MV.allclose(A, ok):
    raise Exception, 'Error computing antisymetric, wrong values returned'

## Now tries to do plot this...
WP = WK.WKPlot()
WP.x.scriptrun(sys.prefix + "/bin/tutorials/WK/colormap.scr")
Esempio n. 20
0
cdms2.setAutoBounds('on')

f   = cdms2.open(os.path.join(cdms2.__path__[0],'..','..','..','..','sample_data','tas_mo.nc'))
fsc = cdms2.open(os.path.join(cdms2.__path__[0],'..','..','..','..','sample_data','tas_mo_clim.nc'))

print "Step #0 : Reading data"
s=f(var,longitude=(0,360,'co'))

acok=fsc('climseas',longitude=(0,360,'co'))

print 'Test #1 : Test result'

ac=times.JAN.climatology(s)

if not(MV2.allclose(ac[0],acok[0])) : raise 'Err answer seems to be wrong we Missing Value free dataset'

f.close()
fsc.close()

a=cdtime.comptime(1980)
b=cdtime.comptime(1980,5)

f = cdms2.open(os.path.join(cdms2.__path__[0],'..','..','..','..','sample_data','tas_6h.nc'))
s=f(var,time=(a,b,'co'),squeeze=1)

print "Test #2 : 6hourly AND get"
jans=times.JAN(s)
try:
    jans=times.JAN(s)
except:
Esempio n. 21
0
cdms2.setAutoBounds('on')

f   = cdms2.open(os.path.join(sys.prefix,'sample_data','tas_mo.nc'))
fsc = cdms2.open(os.path.join(sys.prefix,'sample_data','tas_mo_clim.nc'))

print "Step #0 : Reading data"
s=f(var,longitude=(0,360,'co'))

acok=fsc('climseas',longitude=(0,360,'co'))

print 'Test #1 : Test result'

ac=times.JAN.climatology(s)

assert(MV2.allclose(ac[0],acok[0]))

f.close()
fsc.close()

a=cdtime.comptime(1980)
b=cdtime.comptime(1980,5)

f = cdms2.open(os.path.join(sys.prefix,'sample_data','tas_6h.nc'))
s=f(var,time=(a,b,'co'),squeeze=1)

print "Test #2 : 6hourly AND get"
jans=times.JAN(s)

print "Test #3 : climatology 6h"
JFMA=times.Seasons('JFMA')
Esempio n. 22
0
import vcs.test.support
bg = vcs.test.support.bg

nmonths = 5
## Getting about nmonths months worth of data

W = WK.WK()
f=cdms.open("data.nc")
s=f("rlut",time=slice(0,nmonths*30),latitude=(-15,15))
f.close()
f=cdms.open("results.nc")

## Process the data, i.e compute spectral wave number and frequencies
power = W.process(s)
ok = f('power')
if not MV.allclose(power,ok):
    raise Exception,'Error computing power, wrong values returned'

## Split between Sym and ASym components
## Averages over time if compresstime is True (default)
S,A = W.split(power) #,compresstime=False,smooth=False)
ok = f('S')
if not MV.allclose(S,ok):
    raise Exception,'Error computing symetric, wrong values returned'
ok=f('A')
if not MV.allclose(A,ok):
    raise Exception,'Error computing antisymetric, wrong values returned'

## Now tries to do plot this...
WP = WK.WKPlot()
WP.x.scriptrun("colormap.scr")
Esempio n. 23
0
 def testDiagnoal(self):
     xdiag = MV2.TransientVariable([[1, 2, 3], [4, 5, 6]])
     self.assertTrue(MV2.allclose(MV2.diagonal(xdiag, 1), [2, 6]))
Esempio n. 24
0
def averager(V, axis=None, weights=None, action='average', returned=0, weight=None, combinewts=None):
    """
                        Documentation for averager():
                        -----------------------------
The averager() function provides a convenient way of averaging your data giving
you control over the order of operations (i.e which dimensions are averaged
over first) and also the weighting for the different axes. You can pass your
own array of weights for each dimension or use the default (grid) weights or
specify equal weighting. 


Author: Krishna AchutaRao : [email protected]

Returns:
-------
       The average over the specified dimensions.
Usage: 
------
from cdutil import averager
averager( V, axis='axisoptions', weights=weightoptions, action='average',
          returned='0')

Where V is an array. It can be an array of numpy, numpy.ma or MV2 type. In each case
the function returns an array (except when it results in a scalar) of the same
type as V. See examples for more details.

Optional Arguments:
-------------------
axis=axisoptions   
        Restrictions: axisoptions has to be a string
        Default : first dimension in the data you pass to the function.

        You can pass axis='tyx', or '123', or 'x (plev)' etc.  the same way as
        in order= options for variable operations EXCEPT that
        '...'(i.e Ellipses) are not allowed. In the case that V is a numpy or
        numpy.ma array, axis names have no meaning and only axis indexes are valid.


weights=weightoptions
        Default :
                 'weighted' for Transient Variables (MV2s)
                 'unweighted' for numpy.ma or numpy arrays.

        Note that depending on the array being operated on by averager, the
        default weights change!
        
        Weight options are one of 'weighted', 'unweighted',  an array of weights for
        each dimension or a MaskedVariable of the same shape as the data x.

        - 'weighted' means use the grid information to generate weights for
          that dimension.

        - 'unweighted' means use equal weights for all the grid points in that axis. 

        - Also an array of weights (of the same shape as the dimension being
          averaged over or same shape as V) can be passed.

          Additional Notes on 'weighted' option: The weights are generated
          using the bounds for the specified axis. For latitude and Longitude,
          the weights are calculated using the area (see the cdms2 manual
          grid.getWeights() for more details) whereas for the other axes
          weights are the difference between the bounds (when the bounds are
          available). If the bounds are stored in the file being read in, then
          those values are used. Otherwise, bounds are generated as long as
          cdms2.setAutoBounds('on') is set. If cdms2.setAutoBounds() is set to
          'off', then an Error is raised.

action='average' or 'sum'
        Default : 'average'

        You can either return the weighted average or the weighted sum of the
        data by specifying the keyword argument action=
        
returned = 0 or 1
        Default: 0
        
        - 0 implies sum of weights are not returned after averaging operation.
        - 1 implies the sum of weights after the average operation is returned.

combinewts = None, 0 or 1
        Default: None - same as 0
        - 0 implies weights passed for individual axes are not combined into one
          weight array for the full variable V before performing operation.
       - 1 implies weights passed for individual axes are combined into one
         weight array for the full variable before performing average or sum
         operations. One-dimensional weight arrays or key words of 'weighted' or
         'unweighted' must be passed for the axes over which the operation is
         to be performed. Additionally, weights for axes that are not being
         averaged or summed may also bepassed in the order in which they appear.
         If the weights for the other axes are not passed, they  are assumed to
         be equally weighted.

Examples:
---------
        >>> f = cdms2.open('data_file_name')
        >>> averager(f('variable_name'), axis='1')
        # extracts the variable 'variable_name' from f and averages over the 
        # dimension whose position is 1. Since no other options are specified, 
        # defaults kick in i.e weight='weighted' and returned=0

        >>> averager(V, axis='xy', weights=['weighted','unweighted'])
        or
        >>> averager(V, axis='t', weights='unweighted')
        or
        >>> averager(V, axis='x')
        # Default weights option of 'weighted' is implemented
        or
        >>> averager(V, axis='x', weights=mywts) 
        # where mywts is an array of shape (len(xaxis)) or shape(V)
        or
        >>> averager(V, axis='(lon)y', weights=[myxwts, myywts])
        # where myxwts is of shape len(xaxis) and myywts is of shape len(yaxis)
        or
        >>> averager(V, axis='xy', weights=V_wts)
        # where V_wts is a Masked Variable of shape V
        or
        >>> averager(V, axis='x', weights='unweighted', action='sum')
        # will return the equally weighted sum over the x dimension
        or
        >>> ywt = area_weights(y)
        >>> fractional_area = averager(ywt, axis='xy',
                                weights=['unweighted', 'unweighted'], action='sum')
        # is a good way to compute the area fraction that the
        # data y that is non-missing
        
Note:
-----
        When averaging data with missing values, extra care needs to be taken.
        It is recommended that you use the default weights='weighted' option.
        This uses cdutil.area_weights(V) to get the correct weights to
        pass to the averager.
        >>> averager(V, axis='xy', weights='weighted')
        
        The above is equivalent to:
        >>> V_wts = cdutil.area_weights(V)
        >>> result = averager(V, axis='xy', weights=V_wts)
        or
        >>> result = averager(V, axis='xy', weights=cdutil.area_weights(V))

        However, the area_weights function requires that the axis bounds are
        stored or can be calculated (see documentation of area_weights for more
        details). In the case that such weights are not stored with the axis
        specifications (or the user desires to specify weights from another
        source), the use of combinewts option can produce the same results.
        In short, the following two are equivalent:
        >>> xavg_1 = averager(X, axis = 'xy', weights = area_weights(X))
        >>> xavg_2 = averager(X, axis = 'xy', weights = ['weighted', 'weighted', 'weighted'], combinewts=1)

        Where X is a function of x, y and a third dimension such as time or level.
        
        In general, the above can be substituted with arrays of weights where
        the 'weighted' keyword appears.
        """
    __DEBUG__ = 0
    #
    # Check the weight = option. This is done for backward compatibility since
    # weights= is the current default syntax.
    #
    if not weight is None:
        if not weights is None:
            raise AveragerError, \
                  'Error: You cannot set both weight and weights!. weight is obsolete please use weights only !!!'
        else:
            weights = weight
        # end of if not weights in ['generate','weighted'] :
    # end of if not weight is None:
    #
    # Note: Further checking on weights is done later - in the numpy.ma & MV2 sections also.
    #
    # Check the returned option
    #
    if returned not in [0,1]:
        raise AveragerError, \
              'Error: Invalid option for returned. Pass 0 or 1.'
    # end of if returned not in [0,1]:
    #
    # Check the action = options
    #
    if string.upper(action) in ['AVERAGE', 'AVE', 'AVG']:
        action = 'average'
    elif string.upper(action) in ['SUM', 'ADD']:
        action = 'sum'
    else:
        raise AveragerError, 'Error: Invalid option for action. Pass \'average\' or \'sum\''
    # end of if string.upper(action) in ['AVERAGE', 'AVE', 'AVG']:
    #
    # Check the combinewts option
    #
    if not combinewts :
        combinewts = 0
    elif combinewts not in [0, 1]:
         raise AveragerError, \
               "Error: combinewts must be set to 0 or 1"
    # end of if not combinewts :
    # ************************* End of option checking ************************* 
    #
    # Account for MV2, numpy.ma or numpy arrays sent in by users. Return result of same type.
    #
    #
    # Case 1. numpy array
    #         Convert numpy array to numpy.ma and remember it using _NUM_FLAG so you
    #         can convert the answer to numpy array before returning
    #
    if isinstance(V, numpy.ndarray):
        if __DEBUG__: print 'Converting to numpy.ma so I can do an numpy.ma.average or sum'
        V = numpy.ma.array(V)
        _NUM_FLAG = 1
    else:
        _NUM_FLAG = 0
    # end of if isinstance(V, numpy.ndarray):
    #
    #
    # Case 2. Masked Array (numpy.ma)
    #
    if numpy.ma.isMaskedArray(V) and not MV2.isMaskedVariable(V):     
        #
        # The passed array is an numpy.ma
        #
        if __DEBUG__: print 'Entered numpy.ma only....'
        if __DEBUG__: print '!!!!!!Checking weights for numpy.ma', weights
        #
        #
        if isinstance(weights, types.StringType) and weights in ['weighted', 'generate']:
            if __DEBUG__: print 'VOILA!'
            print 'cdutil.averager Warning: \n\tNot operating on a TransientVariable.'
            print '\tChanging default weights to \'unweighted\' (equally weighted)'
            weights = None
        # end of if weights == 'weighted':
        #
        # Check the axis options.
        #
        axis = _check_MA_axisoptions(axis, numpy.ma.rank(V))
        #
        # Now reorder the original MA to the order in which operations need to be done
        #
        newaxorder = []
        for i in axis:
            newaxorder.append(i)
        # end of for i in axis:
        for i in range(len(V.shape)):
            if not i in newaxorder:
                newaxorder.append(i)
            # if not i in newaxorder:
        # end of for i in range(len(numpy.ma.shape(V))):
        #doloop = False
        if newaxorder != range(len(V.shape)):
            x = numpy.ma.transpose(V, newaxorder)
            if __DEBUG__: print 'Reordered shape = ', x.shape
            #osh=list(x.shape)
            #na=len(axis)
            #if n!=x.rank():
            #    nsh=osh[:na] # the axes of operations....
            #    n=1
            #    for m in osh[na:]:
            #        n*=m
            #    nsh.append(n)
            #    x = numpy.ma.reshape(x,nsh)
            #    if n>35000000:
            #        doloop=
        else:
            x = V

        
        # end of if newaxorder != range(len(V.shape)):
        #
        # Check the weight options
        #
        weights = _check_MA_weight_options(weights, x.shape, len(axis))
        #
        #
        if __DEBUG__: print 'Length of axis = ', len(axis)
        if __DEBUG__: print 'Length of weights = ', len(weights)
        #
        # If the user has passed combinewts = 1, then do the combining of weights here
        #
        if combinewts == 1:
            weights = _combine_weights(x, weights)
        # end of if combinewts == 1:
        #
        # Now decide if we need to average or sum
        #
        if action == 'average':
            #
            # The actual averaging.........
            #
            for i in range(len(axis)):
                #
                if __DEBUG__: print 'Averaging axis # = ', i,
                #
                if  isinstance(weights[i] , types.StringType) or (weights[i] is None):
                    pass
                else:
                    if __DEBUG__: print weights[i].shape
                # end of if not isinstance(weights[i] , types.StringType):
                if i > len(weights)-1:
                    if not retwts:
                        raise AveragerError, 'An unknown error occurred (retwts). Report this bug.'
                    else:
                        weights.append(retwts)
                    # end of if not retwts:
                # end of if i > len(weights)-1:
                try:
                    x, retwts = numpy.ma.average(x, weights=weights[i], returned=1, axis=0)
                except:
                    raise AveragerError, 'Some problem with averaging MA'
                #
            # end of for i in range(len(axis)):
        elif action == 'sum':
            #
            # Come to sum function here
            #
            for i in range(len(axis)):
                if __DEBUG__: print 'Summing axis #', i
                if i > len(weights)-1:
                    try:
                        x = numpy.ma.sum(x, returned=0, axis=0)
                        retwts = numpy.ma.sum(retwts, axis=0)
                    except:
                        raise AveragerError, 'Some problem with summing numpy.ma'
                    # end of try:
                else:
                    try:
                        x, retwts = numpy.ma.average(x, weights=weights[i], returned=1, axis=0)
                        x = x * retwts
                    except:
                        raise AveragerError, 'Some problem with summing numpy.ma'
                    # end of try:
                # end of if i > len(filled_wtoptions):            
                if __DEBUG__: print 'Finished Summing axis #', i
            # end of for i in range(N_axes):
        # end of if action == 'sum':
        #
        # If we started out with a numpy array, convert the numpy.ma to numpy
        #
        if _NUM_FLAG:
            if numpy.ma.isMaskedArray(x):
                x = x.filled()
            # end of if numpy.ma.isMaskedArray(x):
            #
            if numpy.ma.isMaskedArray(retwts):
                retwts = retwts.filled()
            # end of if numpy.ma.isMaskedArray(retwts):
        # end of if _NUM_FLAG:
        #
        if returned:
            return x, retwts
        else:
            return x
        # end of if returned:
        #
        return None
    # end of if numpy.ma.isMaskedArray(V):
    #
    #******************************************************************************************
    #
    # Case 3: Masked Variable.
    #
    if weights is None: weights = 'weighted'
    #
    axis_order = []
    if __DEBUG__: print 'Inside averager axis = ', axis
    if axis == None:
        if __DEBUG__: print 'Default axis is the first axis.........'
        axis = V.getOrder()[0]
        axis_order.append(axis)
    else:
        if type(axis)==type(0): axis=str(axis)
        axis_order = _check_axisoptions(V, axis)
        if __DEBUG__: print 'Axes to be addressed in the order ', axis_order
        for an in range(len(axis_order)):
            item = axis_order[an]
            if isinstance(item, types.IntType):
                loc = string.find(axis, str(item))
                if loc != -1:
                    xlist = list(axis)
                    xlist[loc] = V.getOrder()[item]
                    if xlist[loc] == '-': xlist[loc] = '(' + V.getAxis(item).id + ')'
                    if __DEBUG__: print '*** the axisoption is about to be modified. Before mod  = ', axis 
                    axis = string.joinfields(xlist, '')
                    if __DEBUG__: print '*** the axisoption has been modified. It is = ', axis
            # end of if type(item) = type(1):
        # end of for an in range(len(axis_order)):
        if __DEBUG__: print 'NEW! Axes to be addressed in the order ', axis_order
        if axis_order != None:
            if __DEBUG__: print 'axis = ', axis
            V= V(order=axis)
            if __DEBUG__: print '********** I have reordered V= V(order=axis) **********'
        else:
            return None
        # end of if axis_order != None:
    # end of if axis == None:
    #
    if __DEBUG__: print 'Passed axis checks......'
    if __DEBUG__: print 'Axes to be addressed in the order ', axis_order
    #
    # Number of axes to average/sum over = len(axis_order)
    #
    N_axes = len(axis_order)    
    #
    # Parse the weights = options
    #
    if __DEBUG__: print 'Checking weights= options:',weights
    #
    filled_wtoptions = __check_weightoptions(V, axis, weights)
    if __DEBUG__: print 'The weights options are ', filled_wtoptions
    #
    if not isinstance(filled_wtoptions, types.ListType):
        filled_wtoptions = [filled_wtoptions]
    # end of if not isinstance(filled_wtoptions, types.ListType):
    #
    # 
    if __DEBUG__: print 'Length of axis_order = ', N_axes
    if __DEBUG__: print 'Length of filled_wtoptions = ', len(filled_wtoptions)
    #
    # If the user has passed combinewts = 1, then do the combining of weights here
    #
    if combinewts == 1:
        filled_wtoptions = _combine_weights(V, weights)
    # end of if combinewts == 1:
    #
    # Now decide if we need to average or sum
    #
    if __DEBUG__: print 'type(weights) = ', type(weights)
    try:
        if __DEBUG__: print 'Are they equal?', MV2.allclose(weights, area_weights(V,axisoptions))
    except:
        pass
    #
    if action == 'average':
        #
        # Come to averaging function here....
        #
        for i in range(N_axes):
            #
            if __DEBUG__: print 'Averaging axis #', i
            #
            if i > len(filled_wtoptions)-1:
                if sumwts is None:
                    raise AveragerError, 'An unknown error occurred (sumwts). Report this bug.'
                else:
                    filled_wtoptions.append(sumwts)
                # end of if not sumwts:
            # end of if i > len(filled_wtoptions):
            V, sumwts = average_engine(V, filled_wtoptions[i])
            if __DEBUG__: print 'Finished Averaging axis #', i
        # end of for i in range(N_axes):
        if returned == 1:
            return V, sumwts
        else:
            return V
        # end of if returned == 1:
    elif action == 'sum':
        #
        # Come to sum function here
        #
        for i in range(N_axes):
            if __DEBUG__: print 'Summing axis #', i
            if i > len(filled_wtoptions)-1:
                V, dummy_wts = sum_engine(V, 'unweighted')
                sumwts = MV2.sum(sumwts, axis=0)
            else:
                V, sumwts = sum_engine(V, filled_wtoptions[i])
            # end of if i > len(filled_wtoptions):            
            if __DEBUG__: print 'Finished Summing axis #', i
        # end of for i in range(N_axes):
        y = V
        # end of if len(filled_wtoptions) == 1:
        if returned == 1:
            return y, sumwts
        else:
            return y
Esempio n. 25
0
# Third Method: Use the area weights from Charles' function created above
#               but call the averaging only one 1 dimension at a time. Use the average &
#               weights from the first step in the averaging at the second step!.
#
temp_step, temp_wts = averager(x, axis='x', weight=aw, returned=1)
result3 = averager(temp_step, axis='y', weight=temp_wts)

#
# Note that the above way of doing multiple steps is useful when you want the temp_wts
# altered between steps.......

#
# Now check the 3 results....... they will be different by 273.0 (Centigrade to Kelvin)
#
diff1 = result1 - correct
assert MV2.allclose(273.0, diff1)
diff2 = result2 - correct
assert MV2.allclose(273.0, diff1)
diff3 = result3 - correct
assert MV2.allclose(273.0, diff1)
assert MV2.allclose(result1, result2)
assert MV2.allclose(result2, result3)

#
# This test is to verify the action='sum' option
#
tasm_file = os.path.join(cdms2.__path__[0],'..','..','..','..','sample_data','tas_cru_1979.nc')

ftasm = cdms2.open(tasm_file)
xtasm = ftasm('tas')
ywt = area_weights(xtasm)
Esempio n. 26
0
def averager(V,
             axis=None,
             weights=None,
             action='average',
             returned=0,
             weight=None,
             combinewts=None):
    """
                        Documentation for averager():
                        -----------------------------
The averager() function provides a convenient way of averaging your data giving
you control over the order of operations (i.e which dimensions are averaged
over first) and also the weighting for the different axes. You can pass your
own array of weights for each dimension or use the default (grid) weights or
specify equal weighting. 


Author: Krishna AchutaRao : [email protected]

Returns:
-------
       The average over the specified dimensions.
Usage: 
------
from cdutil import averager
averager( V, axis='axisoptions', weights=weightoptions, action='average',
          returned='0')

Where V is an array. It can be an array of numpy, numpy.ma or MV2 type. In each case
the function returns an array (except when it results in a scalar) of the same
type as V. See examples for more details.

Optional Arguments:
-------------------
axis=axisoptions   
        Restrictions: axisoptions has to be a string
        Default : first dimension in the data you pass to the function.

        You can pass axis='tyx', or '123', or 'x (plev)' etc.  the same way as
        in order= options for variable operations EXCEPT that
        '...'(i.e Ellipses) are not allowed. In the case that V is a numpy or
        numpy.ma array, axis names have no meaning and only axis indexes are valid.


weights=weightoptions
        Default :
                 'weighted' for Transient Variables (MV2s)
                 'unweighted' for numpy.ma or numpy arrays.

        Note that depending on the array being operated on by averager, the
        default weights change!
        
        Weight options are one of 'weighted', 'unweighted',  an array of weights for
        each dimension or a MaskedVariable of the same shape as the data x.

        - 'weighted' means use the grid information to generate weights for
          that dimension.

        - 'unweighted' means use equal weights for all the grid points in that axis. 

        - Also an array of weights (of the same shape as the dimension being
          averaged over or same shape as V) can be passed.

          Additional Notes on 'weighted' option: The weights are generated
          using the bounds for the specified axis. For latitude and Longitude,
          the weights are calculated using the area (see the cdms2 manual
          grid.getWeights() for more details) whereas for the other axes
          weights are the difference between the bounds (when the bounds are
          available). If the bounds are stored in the file being read in, then
          those values are used. Otherwise, bounds are generated as long as
          cdms2.setAutoBounds('on') is set. If cdms2.setAutoBounds() is set to
          'off', then an Error is raised.

action='average' or 'sum'
        Default : 'average'

        You can either return the weighted average or the weighted sum of the
        data by specifying the keyword argument action=
        
returned = 0 or 1
        Default: 0
        
        - 0 implies sum of weights are not returned after averaging operation.
        - 1 implies the sum of weights after the average operation is returned.

combinewts = None, 0 or 1
        Default: None - same as 0
        - 0 implies weights passed for individual axes are not combined into one
          weight array for the full variable V before performing operation.
       - 1 implies weights passed for individual axes are combined into one
         weight array for the full variable before performing average or sum
         operations. One-dimensional weight arrays or key words of 'weighted' or
         'unweighted' must be passed for the axes over which the operation is
         to be performed. Additionally, weights for axes that are not being
         averaged or summed may also bepassed in the order in which they appear.
         If the weights for the other axes are not passed, they  are assumed to
         be equally weighted.

Examples:
---------
        >>> f = cdms2.open('data_file_name')
        >>> averager(f('variable_name'), axis='1')
        # extracts the variable 'variable_name' from f and averages over the 
        # dimension whose position is 1. Since no other options are specified, 
        # defaults kick in i.e weight='weighted' and returned=0

        >>> averager(V, axis='xy', weights=['weighted','unweighted'])
        or
        >>> averager(V, axis='t', weights='unweighted')
        or
        >>> averager(V, axis='x')
        # Default weights option of 'weighted' is implemented
        or
        >>> averager(V, axis='x', weights=mywts) 
        # where mywts is an array of shape (len(xaxis)) or shape(V)
        or
        >>> averager(V, axis='(lon)y', weights=[myxwts, myywts])
        # where myxwts is of shape len(xaxis) and myywts is of shape len(yaxis)
        or
        >>> averager(V, axis='xy', weights=V_wts)
        # where V_wts is a Masked Variable of shape V
        or
        >>> averager(V, axis='x', weights='unweighted', action='sum')
        # will return the equally weighted sum over the x dimension
        or
        >>> ywt = area_weights(y)
        >>> fractional_area = averager(ywt, axis='xy',
                                weights=['unweighted', 'unweighted'], action='sum')
        # is a good way to compute the area fraction that the
        # data y that is non-missing
        
Note:
-----
        When averaging data with missing values, extra care needs to be taken.
        It is recommended that you use the default weights='weighted' option.
        This uses cdutil.area_weights(V) to get the correct weights to
        pass to the averager.
        >>> averager(V, axis='xy', weights='weighted')
        
        The above is equivalent to:
        >>> V_wts = cdutil.area_weights(V)
        >>> result = averager(V, axis='xy', weights=V_wts)
        or
        >>> result = averager(V, axis='xy', weights=cdutil.area_weights(V))

        However, the area_weights function requires that the axis bounds are
        stored or can be calculated (see documentation of area_weights for more
        details). In the case that such weights are not stored with the axis
        specifications (or the user desires to specify weights from another
        source), the use of combinewts option can produce the same results.
        In short, the following two are equivalent:
        >>> xavg_1 = averager(X, axis = 'xy', weights = area_weights(X))
        >>> xavg_2 = averager(X, axis = 'xy', weights = ['weighted', 'weighted', 'weighted'], combinewts=1)

        Where X is a function of x, y and a third dimension such as time or level.
        
        In general, the above can be substituted with arrays of weights where
        the 'weighted' keyword appears.
        """
    __DEBUG__ = 0
    #
    # Check the weight = option. This is done for backward compatibility since
    # weights= is the current default syntax.
    #
    if not weight is None:
        if not weights is None:
            raise AveragerError, \
                  'Error: You cannot set both weight and weights!. weight is obsolete please use weights only !!!'
        else:
            weights = weight
        # end of if not weights in ['generate','weighted'] :
    # end of if not weight is None:
    #
    # Note: Further checking on weights is done later - in the numpy.ma & MV2 sections also.
    #
    # Check the returned option
    #
    if returned not in [0, 1]:
        raise AveragerError, \
              'Error: Invalid option for returned. Pass 0 or 1.'
    # end of if returned not in [0,1]:
    #
    # Check the action = options
    #
    if string.upper(action) in ['AVERAGE', 'AVE', 'AVG']:
        action = 'average'
    elif string.upper(action) in ['SUM', 'ADD']:
        action = 'sum'
    else:
        raise AveragerError, 'Error: Invalid option for action. Pass \'average\' or \'sum\''
    # end of if string.upper(action) in ['AVERAGE', 'AVE', 'AVG']:
    #
    # Check the combinewts option
    #
    if not combinewts:
        combinewts = 0
    elif combinewts not in [0, 1]:
        raise AveragerError, \
              "Error: combinewts must be set to 0 or 1"
    # end of if not combinewts :
    # ************************* End of option checking *************************
    #
    # Account for MV2, numpy.ma or numpy arrays sent in by users. Return result of same type.
    #
    #
    # Case 1. numpy array
    #         Convert numpy array to numpy.ma and remember it using _NUM_FLAG so you
    #         can convert the answer to numpy array before returning
    #
    if isinstance(V, numpy.ndarray):
        if __DEBUG__:
            print 'Converting to numpy.ma so I can do an numpy.ma.average or sum'
        V = numpy.ma.array(V)
        _NUM_FLAG = 1
    else:
        _NUM_FLAG = 0
    # end of if isinstance(V, numpy.ndarray):
    #
    #
    # Case 2. Masked Array (numpy.ma)
    #
    if numpy.ma.isMaskedArray(V) and not MV2.isMaskedVariable(V):
        #
        # The passed array is an numpy.ma
        #
        if __DEBUG__: print 'Entered numpy.ma only....'
        if __DEBUG__: print '!!!!!!Checking weights for numpy.ma', weights
        #
        #
        if isinstance(weights, types.StringType) and weights in [
                'weighted', 'generate'
        ]:
            if __DEBUG__: print 'VOILA!'
            print 'cdutil.averager Warning: \n\tNot operating on a TransientVariable.'
            print '\tChanging default weights to \'unweighted\' (equally weighted)'
            weights = None
        # end of if weights == 'weighted':
        #
        # Check the axis options.
        #
        axis = _check_MA_axisoptions(axis, numpy.ma.rank(V))
        #
        # Now reorder the original MA to the order in which operations need to be done
        #
        newaxorder = []
        for i in axis:
            newaxorder.append(i)
        # end of for i in axis:
        for i in range(len(V.shape)):
            if not i in newaxorder:
                newaxorder.append(i)
            # if not i in newaxorder:
        # end of for i in range(len(numpy.ma.shape(V))):
        #doloop = False
        if newaxorder != range(len(V.shape)):
            x = numpy.ma.transpose(V, newaxorder)
            if __DEBUG__: print 'Reordered shape = ', x.shape
            #osh=list(x.shape)
            #na=len(axis)
            #if n!=x.rank():
            #    nsh=osh[:na] # the axes of operations....
            #    n=1
            #    for m in osh[na:]:
            #        n*=m
            #    nsh.append(n)
            #    x = numpy.ma.reshape(x,nsh)
            #    if n>35000000:
            #        doloop=
        else:
            x = V

        # end of if newaxorder != range(len(V.shape)):
        #
        # Check the weight options
        #
        weights = _check_MA_weight_options(weights, x.shape, len(axis))
        #
        #
        if __DEBUG__: print 'Length of axis = ', len(axis)
        if __DEBUG__: print 'Length of weights = ', len(weights)
        #
        # If the user has passed combinewts = 1, then do the combining of weights here
        #
        if combinewts == 1:
            weights = _combine_weights(x, weights)
        # end of if combinewts == 1:
        #
        # Now decide if we need to average or sum
        #
        if action == 'average':
            #
            # The actual averaging.........
            #
            for i in range(len(axis)):
                #
                if __DEBUG__: print 'Averaging axis # = ', i,
                #
                if isinstance(weights[i],
                              types.StringType) or (weights[i] is None):
                    pass
                else:
                    if __DEBUG__: print weights[i].shape
                # end of if not isinstance(weights[i] , types.StringType):
                if i > len(weights) - 1:
                    if not retwts:
                        raise AveragerError, 'An unknown error occurred (retwts). Report this bug.'
                    else:
                        weights.append(retwts)
                    # end of if not retwts:
                # end of if i > len(weights)-1:
                try:
                    x, retwts = numpy.ma.average(x,
                                                 weights=weights[i],
                                                 returned=1,
                                                 axis=0)
                except:
                    raise AveragerError, 'Some problem with averaging MA'
                #
            # end of for i in range(len(axis)):
        elif action == 'sum':
            #
            # Come to sum function here
            #
            for i in range(len(axis)):
                if __DEBUG__: print 'Summing axis #', i
                if i > len(weights) - 1:
                    try:
                        x = numpy.ma.sum(x, returned=0, axis=0)
                        retwts = numpy.ma.sum(retwts, axis=0)
                    except:
                        raise AveragerError, 'Some problem with summing numpy.ma'
                    # end of try:
                else:
                    try:
                        x, retwts = numpy.ma.average(x,
                                                     weights=weights[i],
                                                     returned=1,
                                                     axis=0)
                        x = x * retwts
                    except:
                        raise AveragerError, 'Some problem with summing numpy.ma'
                    # end of try:
                # end of if i > len(filled_wtoptions):
                if __DEBUG__: print 'Finished Summing axis #', i
            # end of for i in range(N_axes):
        # end of if action == 'sum':
        #
        # If we started out with a numpy array, convert the numpy.ma to numpy
        #
        if _NUM_FLAG:
            if numpy.ma.isMaskedArray(x):
                x = x.filled()
            # end of if numpy.ma.isMaskedArray(x):
            #
            if numpy.ma.isMaskedArray(retwts):
                retwts = retwts.filled()
            # end of if numpy.ma.isMaskedArray(retwts):
        # end of if _NUM_FLAG:
        #
        if returned:
            return x, retwts
        else:
            return x
        # end of if returned:
        #
        return None
    # end of if numpy.ma.isMaskedArray(V):
    #
    #******************************************************************************************
    #
    # Case 3: Masked Variable.
    #
    if weights is None: weights = 'weighted'
    #
    axis_order = []
    if __DEBUG__: print 'Inside averager axis = ', axis
    if axis == None:
        if __DEBUG__: print 'Default axis is the first axis.........'
        axis = V.getOrder()[0]
        axis_order.append(axis)
    else:
        if type(axis) == type(0): axis = str(axis)
        axis_order = _check_axisoptions(V, axis)
        if __DEBUG__: print 'Axes to be addressed in the order ', axis_order
        for an in range(len(axis_order)):
            item = axis_order[an]
            if isinstance(item, types.IntType):
                loc = string.find(axis, str(item))
                if loc != -1:
                    xlist = list(axis)
                    xlist[loc] = V.getOrder()[item]
                    if xlist[loc] == '-':
                        xlist[loc] = '(' + V.getAxis(item).id + ')'
                    if __DEBUG__:
                        print '*** the axisoption is about to be modified. Before mod  = ', axis
                    axis = string.joinfields(xlist, '')
                    if __DEBUG__:
                        print '*** the axisoption has been modified. It is = ', axis
            # end of if type(item) = type(1):
        # end of for an in range(len(axis_order)):
        if __DEBUG__:
            print 'NEW! Axes to be addressed in the order ', axis_order
        if axis_order != None:
            if __DEBUG__: print 'axis = ', axis
            V = V(order=axis)
            if __DEBUG__:
                print '********** I have reordered V= V(order=axis) **********'
        else:
            return None
        # end of if axis_order != None:
    # end of if axis == None:
    #
    if __DEBUG__: print 'Passed axis checks......'
    if __DEBUG__: print 'Axes to be addressed in the order ', axis_order
    #
    # Number of axes to average/sum over = len(axis_order)
    #
    N_axes = len(axis_order)
    #
    # Parse the weights = options
    #
    if __DEBUG__: print 'Checking weights= options:', weights
    #
    filled_wtoptions = __check_weightoptions(V, axis, weights)
    if __DEBUG__: print 'The weights options are ', filled_wtoptions
    #
    if not isinstance(filled_wtoptions, types.ListType):
        filled_wtoptions = [filled_wtoptions]
    # end of if not isinstance(filled_wtoptions, types.ListType):
    #
    #
    if __DEBUG__: print 'Length of axis_order = ', N_axes
    if __DEBUG__: print 'Length of filled_wtoptions = ', len(filled_wtoptions)
    #
    # If the user has passed combinewts = 1, then do the combining of weights here
    #
    if combinewts == 1:
        filled_wtoptions = _combine_weights(V, weights)
    # end of if combinewts == 1:
    #
    # Now decide if we need to average or sum
    #
    if __DEBUG__: print 'type(weights) = ', type(weights)
    try:
        if __DEBUG__:
            print 'Are they equal?', MV2.allclose(weights,
                                                  area_weights(V, axisoptions))
    except:
        pass
    #
    if action == 'average':
        #
        # Come to averaging function here....
        #
        for i in range(N_axes):
            #
            if __DEBUG__: print 'Averaging axis #', i
            #
            if i > len(filled_wtoptions) - 1:
                if sumwts is None:
                    raise AveragerError, 'An unknown error occurred (sumwts). Report this bug.'
                else:
                    filled_wtoptions.append(sumwts)
                # end of if not sumwts:
            # end of if i > len(filled_wtoptions):
            V, sumwts = average_engine(V, filled_wtoptions[i])
            if __DEBUG__: print 'Finished Averaging axis #', i
        # end of for i in range(N_axes):
        if returned == 1:
            return V, sumwts
        else:
            return V
        # end of if returned == 1:
    elif action == 'sum':
        #
        # Come to sum function here
        #
        for i in range(N_axes):
            if __DEBUG__: print 'Summing axis #', i
            if i > len(filled_wtoptions) - 1:
                V, dummy_wts = sum_engine(V, 'unweighted')
                sumwts = MV2.sum(sumwts, axis=0)
            else:
                V, sumwts = sum_engine(V, filled_wtoptions[i])
            # end of if i > len(filled_wtoptions):
            if __DEBUG__: print 'Finished Summing axis #', i
        # end of for i in range(N_axes):
        y = V
        # end of if len(filled_wtoptions) == 1:
        if returned == 1:
            return y, sumwts
        else:
            return y