Пример #1
0
def average_engine(x, wts):
    """
    The work horse that does the averaging! This is specific to averageing.
    We will always be dealing with the first dimension.....and x is already in
    the order we will average.
    1-d wts are always numpy arrays or 'equal' Multi-dimensional arrays are
    always MV2's
    """
    #
    __DEBUG__ = 0
    #
    if x is None: return None
    if wts is None: return None
    #
    shx = numpy.ma.shape(x)
    if __DEBUG__: print '\tInside average_engine.'
    if __DEBUG__: print '\tIncoming data of shape ', shx
    #
    if MV2.isMaskedVariable(wts) or isinstance(wts, numpy.ndarray):
        y, return_wts = MV2.average(x, weights=wts, returned=1, axis=0)
        return y, return_wts
    elif wts in ['equal', 'unweighted']:
        y, return_wts = MV2.average(x, returned=1, axis=0)
        return y, return_wts
    else:
        raise AveragerError, 'wts is an unknown type in average_engine'
        return None
Пример #2
0
def average_engine(x, wts):
    """
    The work horse that does the averaging! This is specific to averageing.
    We will always be dealing with the first dimension.....and x is already in
    the order we will average.
    1-d wts are always numpy arrays or 'equal' Multi-dimensional arrays are
    always MV2's
    """
    #
    __DEBUG__ = 0
    #
    if x is None: return None
    if wts is None: return None
    #
    shx = numpy.ma.shape(x)
    if __DEBUG__: print '\tInside average_engine.'
    if __DEBUG__: print '\tIncoming data of shape ', shx
    #
    if MV2.isMaskedVariable(wts) or isinstance(wts, numpy.ndarray):
        y, return_wts = MV2.average(x, weights=wts, returned=1, axis=0)
        return y, return_wts
    elif wts in ['equal','unweighted']:
        y, return_wts = MV2.average(x, returned=1, axis=0)
        return y, return_wts
    else:
        raise AveragerError, 'wts is an unknown type in average_engine'
        return None
Пример #3
0
def sum_engine(x, wts):
    """
    The work horse that does the summing ! This is specific to summing.
    We will always be dealing with the first dimension.....and x is already in
    the order we will sum.
    1-d wts are always numpy arrays or 'equal' Multi-dimensional arrays are
    always MV2's
    
    Inputs:
        x   : the input array (can be MV2 or MA)
        wts : the input weight array (numpy array or MV2 or 'equal')
        
    Returned:
        y          : The weighted sum (summed over the first dimension)
        return_wts : The sum of weights (summed over the first dimension)
    """
    #
    __DEBUG__ = 0
    #
    if x is None: return None
    if wts is None: return None
    #
    shx = numpy.ma.shape(x)
    #
    if __DEBUG__: print '\tInside sum_engine.'
    if __DEBUG__: print '\tIncoming data of shape ', shx
    #
    if MV2.isMaskedVariable(wts) or isinstance(wts, numpy.ndarray):
        #
        # wts is an MV2 or numpy array
        #
        if __DEBUG__:
            print '\t********** Weight is an MV2 or numpy array! **********'
        #
        xavg, return_wts = MV2.average(x, weights=wts, returned=1, axis=0)
        y = xavg * return_wts
        return y, return_wts
    elif wts in ['equal', 'unweighted']:
        #
        # Equal weights
        #
        if __DEBUG__: print '\t********** Weight is Equal! **********'
        xavg, return_wts = MV2.average(x, returned=1, axis=0)
        y = xavg * return_wts
        return y, return_wts
        # end of if action == 'sum':
    else:
        raise AveragerError, 'wts is an unknown type in sum_engine'
        return None
Пример #4
0
def sum_engine(x, wts):
    """
    The work horse that does the summing ! This is specific to summing.
    We will always be dealing with the first dimension.....and x is already in
    the order we will sum.
    1-d wts are always numpy arrays or 'equal' Multi-dimensional arrays are
    always MV2's
    
    Inputs:
        x   : the input array (can be MV2 or MA)
        wts : the input weight array (numpy array or MV2 or 'equal')
        
    Returned:
        y          : The weighted sum (summed over the first dimension)
        return_wts : The sum of weights (summed over the first dimension)
    """
    #
    __DEBUG__ = 0
    #
    if x is None: return None
    if wts is None: return None
    #
    shx = numpy.ma.shape(x)
    #
    if __DEBUG__: print '\tInside sum_engine.'
    if __DEBUG__: print '\tIncoming data of shape ', shx
    #
    if MV2.isMaskedVariable(wts) or isinstance(wts, numpy.ndarray):
        #
        # wts is an MV2 or numpy array 
        #
        if __DEBUG__: print '\t********** Weight is an MV2 or numpy array! **********'
        #
        xavg, return_wts = MV2.average(x, weights=wts, returned=1, axis=0)
        y = xavg * return_wts
        return y, return_wts
    elif wts in ['equal','unweighted']:
        #
        # Equal weights
        #
        if __DEBUG__: print '\t********** Weight is Equal! **********'
        xavg, return_wts = MV2.average(x, returned=1, axis=0)
        y = xavg * return_wts
        return y, return_wts
        # end of if action == 'sum':
    else:
        raise AveragerError, 'wts is an unknown type in sum_engine'
        return None
Пример #5
0
#            for k in range(fi,li, s):
#             axis[kp] = parent.panelDM.dim[p].new_axis[k].astype(axis.dtype.char)
#             kp += 1
#             if kp == axis_len: break
    else:
        ## Charles changed it from apply varobj.subRegion to varobj, as curvilinear and
        ## generic grid don't work with subRegion
        ##       slab = apply(var.subRegion, tuple(dargs), kpargs)
        ##       print 'getting it',var,tuple(dargs), kpargs
        if var.shape != ():
            slab = apply(var, tuple(dargs), kpargs)
        else:
            slab = var

    #Make sure it's an MV, for 0D var
    if not MV2.isMaskedVariable(slab):
        slab = MV2.array(slab)
    # Make sure to change the Id
    if d_name != None: slab.id = slab.name = d_name

    #
    # record subRegion command
    r = ''
    if from_file == 1:
        var_name = var
        if d_name is not None: var_name = d_name
        s_index = "%s=fid2( '%s', " % (var_name, var)
        s_coord = "%s=fid2( '%s', " % (var_name, var)
    else:
        var_name = var.id
        if new_var is not None: var_name = new_var
Пример #6
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
Пример #7
0
def _combine_weights(x, weightoptions):
    """
    This takes in the shape of the reordered array, the passed weight options
    and combines the weights to form a weight array of the same shape as x.

    Inputs:
      x             : The input array reordered to be in the order of operations.
      weightoptions : The weight options for the subset of axes of x - in the order of x

    Returned:
      weight_array  : In the same shape as x but assuming that the remaining weights are equal.
    """
    #
    __DEBUG__ = 0
    #
    xsh = x.shape
    if __DEBUG__: print 'Shape of incoming data x ', xsh
    if __DEBUG__: print 'Type of x ', x.__class__
    #
    if isinstance(weightoptions, types.ListType):
        if __DEBUG__: print 'weightoptions is a list'
        #
        # Note the weight options have already gone through checks against the
        # reordered data. So nofurther checks are necessary!
        #
        n_dimstoadd = len(x.shape) - len(weightoptions)
        if __DEBUG__: print 'Dimensions to add = ', n_dimstoadd
        if n_dimstoadd < 0 :
            raise AveragerError, 'Error in weights list - too many weights passed!'
        elif n_dimstoadd > 0:
            init_shape = x.shape[-n_dimstoadd:]
            if __DEBUG__: print 'Initialized shape = ', init_shape
            #
            wt_init = numpy.ones(init_shape, numpy.float)
            #
            dim = len(init_shape) + 1
        else:
            wt_init = None
            dim = 1
        # end of if n_dimstoadd < 0 :
        #
        wgt = weightoptions[-1]
        if wgt in ['equal', 'unweighted']:
            wgt = numpy.ones(xsh[-dim], numpy.float)
        else:
            if __DEBUG__ : print wgt
        # end of if wgt in ['equal', 'unweighted']:
        #
        if __DEBUG__: print 'wgt.shape = ', wgt.shape
        #
        if wt_init is None:
            wt_init = wgt
            if not isinstance(wt_init, numpy.ndarray): raise AveragerError, 'Wrong Weight!!!'
        else:
            if __DEBUG__ : print 'list(init_shape) = ', list(init_shape)
            newshape = list(init_shape)
            newshape.insert(0, wgt.shape[0])
            if __DEBUG__: print 'Pre-loop newshape ', newshape
            wt_init = numpy.resize(wt_init, tuple(newshape))
            if __DEBUG__: print 'Pre-loop wt_init.shape ', wt_init.shape
            wgtsh = list(wgt.shape)
            for nn in range(1, len(wt_init.shape), 1): wgtsh.append(1)
            if __DEBUG__: print 'wgt resized to ', tuple(wgtsh)
            wgt = numpy.resize(wgt, tuple(wgtsh))
            wt_init = wt_init * wgt
        # end of if not wt_init:
        #
        if __DEBUG__: print 'Pre-loop wt_init.shape ', wt_init.shape
        #
        for i in range(len(weightoptions)-2, -1, -1):
            dim_wt = weightoptions[i]
            if dim_wt in ['equal', 'unweighted']:
                dim_wt = numpy.ones(xsh[i], numpy.float)
            # end of if dim_wt in ['equal', 'weighted']:
            if __DEBUG__: print 'At step ', i, dim_wt
            #wt_init = numpy.outer(wt_init, dim_wt)
            newshape = list(wt_init.shape)
            newshape.insert(0, dim_wt.shape[0])
            wt_init = numpy.resize(wt_init, tuple(newshape))
            if __DEBUG__: print 'Shape of wt_init = ', wt_init.shape
            dim_wtsh = list(dim_wt.shape)
            for nn in range(1, len(wt_init.shape), 1): dim_wtsh.append(1)
            if __DEBUG__: print 'dim_wt resized to ', tuple(dim_wtsh)
            dim_wt = numpy.resize(dim_wt, tuple(dim_wtsh))            
            wt_init = wt_init * dim_wt
        # end of for i in range(len(weightoptions)-1, -1, -1):
        #
        if __DEBUG__: print 'wt_init after all dimensions adeed has shape ', wt_init.shape
        if wt_init.shape != xsh: raise AveragerError, 'SOMETHING SCREWY HAPPENED!!'
        #
        if MV2.isMaskedVariable(x): wt_init = MV2.array(wt_init, axes=x.getAxisList())
        weightoptions = [wt_init]
        #
        if __DEBUG__: print 'Are my weight and area_weights the same?', numpy.ma.allclose(weightoptions[0], area_weights(x))
    elif MV2.isMaskedVariable(weightoptions):
        if __DEBUG__: print 'weightoptions is an MV2. Nothing to do?'
    elif numpy.ma.isMaskedArray(V) and not MV2.isMaskedVariable(V):
        if __DEBUG__: print 'weightoptions is an MA. Nothing to do?'
    else:
        if __DEBUG__: print 'weightoptions is something else. Nothing to do?'
    # end of if isinstance(weightoptions, types.ListType):
    #
    return weightoptions
Пример #8
0
def __check_weightoptions(x, axisoptions, weightoptions):
    """
    Checks the weights=options to make sure that it is valid.
    
    Default: 'weighted'
    
    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 or if multiple axes are passed, generate goes ahead and
      generates the full masked area weights.
    - '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.
    """
    #
    # Since the _check_axisoptions was passed, the variable x has already been reordered
    # to the required order..... check it anyway.
    #
    #
    __DEBUG__ = 0
    #
    if __DEBUG__: print 'Axis options entering __check_weightoptions = ', axisoptions
    #
    axisindex = cdms2.order2index(x.getAxisList(), axisoptions)
    if __DEBUG__: print 'This should be 0,1,2... etc. is it? ', axisindex
    #
    axislist = cdms2.orderparse(axisoptions)
    if __DEBUG__: print 'axislist = ', axislist
    #
    if not isinstance(weightoptions, types.ListType):
        #
        # We have either 1 axis only or multiple axes and one MV2 of weights
        #
        if MV2.isMaskedVariable(weightoptions):
            #
            # Weight passed is an MV2. Probably OK
            #
            try:
                # We had required that the weights be of the same rank as the variable to be averaged.
                weightoptions = weightoptions(order=axisoptions)
                #
                # KRISHNA : check this for combinewts!!
                #
##                 if x.shape != weightoptions.shape:
##                     raise AveragerError, \
##                           'Error: Shape of weight array does not match shape of data array'
##                 # end of if x.shape != weightoptions.shape:
                if __DEBUG__: print '... definitely OK'
            except:
                raise AveragerError, \
                      'Error: Did not find the axes requested in the Masked variable.'
        elif len(axislist) == 1:
            #
            # Only one axis to reduce over....'
            # figure out if it is an array (numpy or numpy.ma) or 'equal' or 'generate' or something else
            #
            if __DEBUG__: print 'I have only 1 axis and 1 option'
            weightoptions = __check_each_weight_option(x, axislist[0], axisindex[0], weightoptions)
        else:
            #
            # More than 1 axis to reduce over, and not an MV2 of the same dimensionality as x!
            #
            if weightoptions in ['generate','weighted']:
                weightoptions = area_weights(x,axisoptions)
            else:
                # Cannot do this because 'generate' was not passed with multiple axes.
                raise AveragerError, 'Error: Multiple axes passed without weights to match'
            # end of if weightoptions == 'generate':
    else:
        #
        # I have a list of weightoptions i.e more than 1 axis in axisoption
        #
        #
        # We have multiple axes to deal with each with a weight....
        #
        for i in range(len(axislist)):
            weightoptions[i] = __check_each_weight_option(x, axislist[i], axisindex[i], weightoptions[i])
        # end of for i in range(len(axislist)):
        #
        if len(axislist) < len(weightoptions):
            for j in range(len(axislist), len(weightoptions), 1):
                weightoptions[j] = __check_each_weight_option(x, None, j, weightoptions[j])
        # end of if len(axislist) < len(weightoptions):
    # end of if not isinstance(weightoptions, types.ListType):
    #
    if __DEBUG__: print 'Successful with __check_weightoptions'
    #
    return weightoptions
Пример #9
0
 def testIsMasked(self):
     self.assertFalse(MV2.isMaskedVariable(numpy.ones(self.ones.shape)))
     self.assertTrue(MV2.isMaskedVariable(self.ones))
Пример #10
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
Пример #11
0
def _combine_weights(x, weightoptions):
    """
    This takes in the shape of the reordered array, the passed weight options
    and combines the weights to form a weight array of the same shape as x.

    Inputs:
      x             : The input array reordered to be in the order of operations.
      weightoptions : The weight options for the subset of axes of x - in the order of x

    Returned:
      weight_array  : In the same shape as x but assuming that the remaining weights are equal.
    """
    #
    __DEBUG__ = 0
    #
    xsh = x.shape
    if __DEBUG__: print 'Shape of incoming data x ', xsh
    if __DEBUG__: print 'Type of x ', x.__class__
    #
    if isinstance(weightoptions, types.ListType):
        if __DEBUG__: print 'weightoptions is a list'
        #
        # Note the weight options have already gone through checks against the
        # reordered data. So nofurther checks are necessary!
        #
        n_dimstoadd = len(x.shape) - len(weightoptions)
        if __DEBUG__: print 'Dimensions to add = ', n_dimstoadd
        if n_dimstoadd < 0:
            raise AveragerError, 'Error in weights list - too many weights passed!'
        elif n_dimstoadd > 0:
            init_shape = x.shape[-n_dimstoadd:]
            if __DEBUG__: print 'Initialized shape = ', init_shape
            #
            wt_init = numpy.ones(init_shape, numpy.float)
            #
            dim = len(init_shape) + 1
        else:
            wt_init = None
            dim = 1
        # end of if n_dimstoadd < 0 :
        #
        wgt = weightoptions[-1]
        if wgt in ['equal', 'unweighted']:
            wgt = numpy.ones(xsh[-dim], numpy.float)
        else:
            if __DEBUG__: print wgt
        # end of if wgt in ['equal', 'unweighted']:
        #
        if __DEBUG__: print 'wgt.shape = ', wgt.shape
        #
        if wt_init is None:
            wt_init = wgt
            if not isinstance(wt_init, numpy.ndarray):
                raise AveragerError, 'Wrong Weight!!!'
        else:
            if __DEBUG__: print 'list(init_shape) = ', list(init_shape)
            newshape = list(init_shape)
            newshape.insert(0, wgt.shape[0])
            if __DEBUG__: print 'Pre-loop newshape ', newshape
            wt_init = numpy.resize(wt_init, tuple(newshape))
            if __DEBUG__: print 'Pre-loop wt_init.shape ', wt_init.shape
            wgtsh = list(wgt.shape)
            for nn in range(1, len(wt_init.shape), 1):
                wgtsh.append(1)
            if __DEBUG__: print 'wgt resized to ', tuple(wgtsh)
            wgt = numpy.resize(wgt, tuple(wgtsh))
            wt_init = wt_init * wgt
        # end of if not wt_init:
        #
        if __DEBUG__: print 'Pre-loop wt_init.shape ', wt_init.shape
        #
        for i in range(len(weightoptions) - 2, -1, -1):
            dim_wt = weightoptions[i]
            if dim_wt in ['equal', 'unweighted']:
                dim_wt = numpy.ones(xsh[i], numpy.float)
            # end of if dim_wt in ['equal', 'weighted']:
            if __DEBUG__: print 'At step ', i, dim_wt
            #wt_init = numpy.outer(wt_init, dim_wt)
            newshape = list(wt_init.shape)
            newshape.insert(0, dim_wt.shape[0])
            wt_init = numpy.resize(wt_init, tuple(newshape))
            if __DEBUG__: print 'Shape of wt_init = ', wt_init.shape
            dim_wtsh = list(dim_wt.shape)
            for nn in range(1, len(wt_init.shape), 1):
                dim_wtsh.append(1)
            if __DEBUG__: print 'dim_wt resized to ', tuple(dim_wtsh)
            dim_wt = numpy.resize(dim_wt, tuple(dim_wtsh))
            wt_init = wt_init * dim_wt
        # end of for i in range(len(weightoptions)-1, -1, -1):
        #
        if __DEBUG__:
            print 'wt_init after all dimensions adeed has shape ', wt_init.shape
        if wt_init.shape != xsh:
            raise AveragerError, 'SOMETHING SCREWY HAPPENED!!'
        #
        if MV2.isMaskedVariable(x):
            wt_init = MV2.array(wt_init, axes=x.getAxisList())
        weightoptions = [wt_init]
        #
        if __DEBUG__:
            print 'Are my weight and area_weights the same?', numpy.ma.allclose(
                weightoptions[0], area_weights(x))
    elif MV2.isMaskedVariable(weightoptions):
        if __DEBUG__: print 'weightoptions is an MV2. Nothing to do?'
    elif numpy.ma.isMaskedArray(V) and not MV2.isMaskedVariable(V):
        if __DEBUG__: print 'weightoptions is an MA. Nothing to do?'
    else:
        if __DEBUG__: print 'weightoptions is something else. Nothing to do?'
    # end of if isinstance(weightoptions, types.ListType):
    #
    return weightoptions
Пример #12
0
def __check_weightoptions(x, axisoptions, weightoptions):
    """
    Checks the weights=options to make sure that it is valid.
    
    Default: 'weighted'
    
    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 or if multiple axes are passed, generate goes ahead and
      generates the full masked area weights.
    - '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.
    """
    #
    # Since the _check_axisoptions was passed, the variable x has already been reordered
    # to the required order..... check it anyway.
    #
    #
    __DEBUG__ = 0
    #
    if __DEBUG__:
        print 'Axis options entering __check_weightoptions = ', axisoptions
    #
    axisindex = cdms2.order2index(x.getAxisList(), axisoptions)
    if __DEBUG__: print 'This should be 0,1,2... etc. is it? ', axisindex
    #
    axislist = cdms2.orderparse(axisoptions)
    if __DEBUG__: print 'axislist = ', axislist
    #
    if not isinstance(weightoptions, types.ListType):
        #
        # We have either 1 axis only or multiple axes and one MV2 of weights
        #
        if MV2.isMaskedVariable(weightoptions):
            #
            # Weight passed is an MV2. Probably OK
            #
            try:
                # We had required that the weights be of the same rank as the variable to be averaged.
                weightoptions = weightoptions(order=axisoptions)
                #
                # KRISHNA : check this for combinewts!!
                #
                ##                 if x.shape != weightoptions.shape:
                ##                     raise AveragerError, \
                ##                           'Error: Shape of weight array does not match shape of data array'
                ##                 # end of if x.shape != weightoptions.shape:
                if __DEBUG__: print '... definitely OK'
            except:
                raise AveragerError, \
                      'Error: Did not find the axes requested in the Masked variable.'
        elif len(axislist) == 1:
            #
            # Only one axis to reduce over....'
            # figure out if it is an array (numpy or numpy.ma) or 'equal' or 'generate' or something else
            #
            if __DEBUG__: print 'I have only 1 axis and 1 option'
            weightoptions = __check_each_weight_option(x, axislist[0],
                                                       axisindex[0],
                                                       weightoptions)
        else:
            #
            # More than 1 axis to reduce over, and not an MV2 of the same dimensionality as x!
            #
            if weightoptions in ['generate', 'weighted']:
                weightoptions = area_weights(x, axisoptions)
            else:
                # Cannot do this because 'generate' was not passed with multiple axes.
                raise AveragerError, 'Error: Multiple axes passed without weights to match'
            # end of if weightoptions == 'generate':
    else:
        #
        # I have a list of weightoptions i.e more than 1 axis in axisoption
        #
        #
        # We have multiple axes to deal with each with a weight....
        #
        for i in range(len(axislist)):
            weightoptions[i] = __check_each_weight_option(
                x, axislist[i], axisindex[i], weightoptions[i])
        # end of for i in range(len(axislist)):
        #
        if len(axislist) < len(weightoptions):
            for j in range(len(axislist), len(weightoptions), 1):
                weightoptions[j] = __check_each_weight_option(
                    x, None, j, weightoptions[j])
        # end of if len(axislist) < len(weightoptions):
    # end of if not isinstance(weightoptions, types.ListType):
    #
    if __DEBUG__: print 'Successful with __check_weightoptions'
    #
    return weightoptions
Пример #13
0
def __check_each_weight_option(x, ax, index, wtopt):
    """
    This does the checking of individual weight options. It fills in the weights
    if 'generate' is an option used i.e if getWeights is needed.
    """
    #
    __DEBUG__ = 0
    #
    if __DEBUG__:        print 'Inside __check_each_weight_option, index = ', \
index, 'axis = ', ax, ' wtopt = ', wtopt
    #
    # Check the types etc.....
    #
    if MV2.isMaskedVariable(wtopt):
        #
        # wtopt is an MV2
        #
        if __DEBUG__: print 'I have a Masked Variable of shape', wtopt.shape
        #
        try:
            if __DEBUG__:
                print '****Order of axes in wtopt (originally) = ', MV2.getorder(
                    wtopt)
            wtopt = wtopt(order=ax)
            if __DEBUG__:
                print '****Order of axes in wtopt    (finally) = ', MV2.getorder(
                    wtopt)
        except:
            raise AveragerError, 'The MV2 passed does not have an axis matching ' + str(
                ax)
        # end of try:
        if numpy.ma.shape(wtopt(order=ax)[:])[0] != len(x.getAxis(index)[:]):
            # We have an MV2 whose length is not the same as that of the corresponding axis for x
            raise AveragerError, \
                  'The Masked Variable passed does not match the length of axis ' + str(ax)
        # end of if numpy.ma.shape(wtopt(order=ax)[:])[0] != len(x.getAxis(index)[:]):
        if len(numpy.ma.shape(wtopt)) == 1:
            #
            # Coerce the MV2 to numpy type..... only if the wtopt is 1-d!!
            #
            if __DEBUG__: print 'Returning a numeric array from MV2!!'
            wtopt = numpy.ma.filled(wtopt)
        # end of if len(numpy.ma.shape(wtopt)) == 1:
    elif numpy.ma.isMA(wtopt):
        #
        # wtopt is an numpy.ma
        #
        if __DEBUG__:
            print 'I have a Masked Array of rank', numpy.ma.rank(wtopt)
        #
        if numpy.ma.rank(wtopt) > 1:
            raise AveragerError, \
                 'Error: The Masked Array of more than 1 dimension lacks sufficient information'
        # end of if numpy.ma.rank(wtopt) > 1:
        if numpy.ma.shape(wtopt)[0] != len(x.getAxis(index)[:]):
            raise AveragerError, 'Error: Axis is of length %d, weights passed of length %d.' %\
            (len(x.getAxis(index)[:]), numpy.ma.shape(wtopt)[0])
        # end of if numpy.ma.shape(wtopt)[0] != len(x.getAxis(index)[:]):
        #
        # Coerce the numpy.ma to numpy type.....
        #
        if __DEBUG__: print 'Returning a numeric array from numpy.ma!!'
        wtopt = wtopt.filled()
    elif isinstance(wtopt, numpy.ndarray):
        #
        # wtopt is a numpy Array
        #
        if __DEBUG__:
            print 'I have a numpy Array of rank', len(numpy.shape(wtopt))
        #
        if len(numpy.shape(wtopt)) > 1:
            raise AveragerError, \
            'Error: The numpy Array of more than 1 dimension lacks sufficient information'
        if numpy.shape(wtopt)[0] != len(x.getAxis(index)[:]):
            raise AveragerError, 'Error: Axis is of length %d, weights passed of length %d' % \
            (len(x.getAxis(index)[:]), numpy.shape(wtopt)[0])
    elif isinstance(wtopt, types.StringType):
        #
        # wtopt is a string
        #
        if __DEBUG__: print 'I have a string =', wtopt
        #
        if wtopt.lower() in ['equal', 'unweighted']:
            if __DEBUG__: print 'Equal weights specified'
            wtopt = 'unweighted'
        elif wtopt.lower() in ['generate', 'weighted']:
            # NOTE: THIS FUNCTION CAN BE CHANGED WHEN BOB PUTS THE FUNCTION getAxisWeights() INTO cdms2
            if __DEBUG__: print 'GENERATE weights specified'
            wtopt = __myGetAxisWeights(x, index)
            if __DEBUG__: print wtopt
        else:
            raise AveragerError, 'Error: Unrecognised string option specified'
    # end of if MV2.isMaskedVariable(wtopt):
    return wtopt
Пример #14
0
#            for k in range(fi,li, s):
#             axis[kp] = parent.panelDM.dim[p].new_axis[k].astype(axis.dtype.char)
#             kp += 1
#             if kp == axis_len: break
   else:
       ## Charles changed it from apply varobj.subRegion to varobj, as curvilinear and
       ## generic grid don't work with subRegion
##       slab = apply(var.subRegion, tuple(dargs), kpargs)
##       print 'getting it',var,tuple(dargs), kpargs
      if var.shape!=():
        slab = apply(var, tuple(dargs), kpargs)
      else:
          slab=var

   #Make sure it's an MV, for 0D var
   if not MV2.isMaskedVariable(slab):
       slab=MV2.array(slab)
   # Make sure to change the Id
   if d_name != None: slab.id = slab.name = d_name

   #
   # record subRegion command
   r=''
   if from_file == 1:
      var_name = var
      if d_name is not None: var_name = d_name
      s_index = "%s=fid2( '%s', " % ( var_name, var )
      s_coord = "%s=fid2( '%s', " % ( var_name, var )
   else:
      var_name = var.id
      if new_var is not None: var_name = new_var
Пример #15
0
def __check_each_weight_option(x, ax, index, wtopt):
    """
    This does the checking of individual weight options. It fills in the weights
    if 'generate' is an option used i.e if getWeights is needed.
    """
    #
    __DEBUG__ = 0
    #
    if __DEBUG__: print 'Inside __check_each_weight_option, index = ', \
       index, 'axis = ', ax, ' wtopt = ', wtopt
    #
    # Check the types etc.....
    #
    if MV2.isMaskedVariable(wtopt):
        #
        # wtopt is an MV2
        #
        if __DEBUG__: print 'I have a Masked Variable of shape', wtopt.shape
        #
        try:
            if __DEBUG__: print '****Order of axes in wtopt (originally) = ', MV2.getorder(wtopt)
            wtopt = wtopt(order=ax)
            if __DEBUG__: print '****Order of axes in wtopt    (finally) = ', MV2.getorder(wtopt)
        except:
            raise AveragerError, 'The MV2 passed does not have an axis matching '+str(ax)
        # end of try:
        if numpy.ma.shape(wtopt(order=ax)[:])[0] != len(x.getAxis(index)[:]):
            # We have an MV2 whose length is not the same as that of the corresponding axis for x
            raise AveragerError, \
                  'The Masked Variable passed does not match the length of axis ' + str(ax)
        # end of if numpy.ma.shape(wtopt(order=ax)[:])[0] != len(x.getAxis(index)[:]):
        if len(numpy.ma.shape(wtopt)) == 1:
            #
            # Coerce the MV2 to numpy type..... only if the wtopt is 1-d!!
            #
            if __DEBUG__: print 'Returning a numeric array from MV2!!'
            wtopt = numpy.ma.filled(wtopt)
        # end of if len(numpy.ma.shape(wtopt)) == 1:
    elif numpy.ma.isMA(wtopt):
        #
        # wtopt is an numpy.ma
        #
        if __DEBUG__: print 'I have a Masked Array of rank', numpy.ma.rank(wtopt)
        #
        if numpy.ma.rank(wtopt) > 1:
            raise AveragerError, \
                 'Error: The Masked Array of more than 1 dimension lacks sufficient information'
        # end of if numpy.ma.rank(wtopt) > 1:
        if numpy.ma.shape(wtopt)[0] != len(x.getAxis(index)[:]):
            raise AveragerError, 'Error: Axis is of length %d, weights passed of length %d.' %\
            (len(x.getAxis(index)[:]), numpy.ma.shape(wtopt)[0])
        # end of if numpy.ma.shape(wtopt)[0] != len(x.getAxis(index)[:]):
        #
        # Coerce the numpy.ma to numpy type.....
        #
        if __DEBUG__: print 'Returning a numeric array from numpy.ma!!'
        wtopt = wtopt.filled()
    elif isinstance(wtopt, numpy.ndarray):
        #
        # wtopt is a numpy Array
        #
        if __DEBUG__: print 'I have a numpy Array of rank', len(numpy.shape(wtopt))
        #
        if len(numpy.shape(wtopt)) > 1:
            raise AveragerError, \
            'Error: The numpy Array of more than 1 dimension lacks sufficient information'
        if numpy.shape(wtopt)[0] != len(x.getAxis(index)[:]):
            raise AveragerError, 'Error: Axis is of length %d, weights passed of length %d' % \
            (len(x.getAxis(index)[:]), numpy.shape(wtopt)[0])
    elif isinstance(wtopt, types.StringType):
        #
        # wtopt is a string
        #
        if __DEBUG__: print 'I have a string =', wtopt
        #
        if wtopt.lower() in ['equal','unweighted']:
            if __DEBUG__: print 'Equal weights specified'
            wtopt = 'unweighted'
        elif wtopt.lower() in ['generate','weighted']:
            # NOTE: THIS FUNCTION CAN BE CHANGED WHEN BOB PUTS THE FUNCTION getAxisWeights() INTO cdms2
            if __DEBUG__: print 'GENERATE weights specified'            
            wtopt = __myGetAxisWeights(x, index)
            if __DEBUG__: print wtopt
        else:
            raise AveragerError, 'Error: Unrecognised string option specified'
    # end of if MV2.isMaskedVariable(wtopt):
    return wtopt
Пример #16
0
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')

## isMaskedVariable(x)
##   Is x a masked variable, that is, an instance of AbstractVariable?
im1 = MV2.isMaskedVariable(xones)
im2 = MV2.isMaskedVariable(xmasked)

## outerproduct(a, b)
##   outerproduct(a,b) = {a[i]*b[j]}, has shape (len(a),len(b))
xouter = MV2.outerproduct(MV2.arange(16.), MV2.arange(32.))
lat = uf.getLatitude()
lon = uf.getLongitude()
xouter.setAxis(0, lat)
xouter.setAxis(1, lon)
xouter.setAxisList([lat, lon])  # Equivalent

## masked_equal(x, value)
##   masked_equal(x, value) = x masked where x == value
##   For floating point consider masked_values(x, value) instead.
Пример #17
0
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')

## isMaskedVariable(x) 
##   Is x a masked variable, that is, an instance of AbstractVariable?
im1 = MV2.isMaskedVariable(xones)
im2 = MV2.isMaskedVariable(xmasked)

## outerproduct(a, b) 
##   outerproduct(a,b) = {a[i]*b[j]}, has shape (len(a),len(b))
xouter = MV2.outerproduct(MV2.arange(16.),MV2.arange(32.))
lat = uf.getLatitude()
lon = uf.getLongitude()
xouter.setAxis(0,lat)
xouter.setAxis(1,lon)
xouter.setAxisList([lat,lon])           # Equivalent

## masked_equal(x, value) 
##   masked_equal(x, value) = x masked where x == value
##   For floating point consider masked_values(x, value) instead.