Пример #1
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))
Пример #2
0
    def testMinMax(self):

        # Scalar
        xouter = MV2.outerproduct(MV2.arange(10), MV2.arange(10))
        maxval = MV2.maximum(xouter)
        minval = MV2.minimum(xouter)
        self.assertEqual(maxval, 81)
        self.assertEqual(minval, 0)

        # Do an elementwise maximum
        xmax = MV2.maximum(self.u_file, self.v_file)
        xmin = MV2.minimum(self.u_file, self.v_file)
        self.assertTrue(((xmax - self.u_file) >= 0).all())
        self.assertTrue(((xmax - self.v_file) >= 0).all())
        self.assertTrue(((xmin - self.u_file) <= 0).all())
        self.assertTrue(((xmin - self.v_file) <= 0).all())

        # Reduce along axes
        slicer = [Ellipsis for i in range(len(self.u_file.shape))]
        for axis_index, axis_length in enumerate(self.u_file.shape):
            amax = MV2.maximum.reduce(self.u_file, axis=axis_index)
            amin = MV2.minimum.reduce(self.u_file, axis=axis_index)
            s = list(slicer)
            for i in range(axis_length):
                s[axis_index] = i
                ind_slice = self.u_file.subSlice(*s, squeeze=True)
                self.assertTrue(((ind_slice - amax) <= 0).all())
                self.assertTrue(((ind_slice - amin) >= 0).all())

        t1 = MV2.TransientVariable([1., 2.])
        t2 = MV2.TransientVariable([1., 10.])
        t3 = MV2.minimum.outer(t1, t2)
        self.assertTrue(MV2.allequal(t3, [[1, 1], [1, 2]]))
        t3 = MV2.maximum.outer(t1, t2)
        self.assertTrue(MV2.allequal(t3, [[1, 10], [2, 10]]))
Пример #3
0
 def testArrayGeneration(self):
     xones = MV2.ones(self.other_u_file.shape,
                      numpy.float32,
                      axes=self.other_u_file.getAxisList(),
                      attributes=self.other_u_file.attributes,
                      id=self.other_u_file.id)
     self.assertTrue(MV2.allequal(xones, 1))
     self.assertEqual(xones.shape, self.other_u_file.shape)
     xzeros = MV2.zeros(self.u_file.shape,
                        dtype=numpy.float,
                        axes=self.u_file.getAxisList(),
                        attributes=self.u_file.attributes,
                        id=self.u_file.id)
     self.assertTrue(MV2.allequal(xzeros, 0))
     self.assertEqual(xzeros.shape, self.u_file.shape)
Пример #4
0
 def testPowAndSqrt(self):
     vel = MV2.sqrt(self.u_file**2 + self.v_file**2)
     vel.id = 'velocity'
     vel2 = MV2.sqrt(self.u_transient**2 + self.v_transient**2)
     vel2.id = 'velocity'
     vel2.units = self.u_file.units
     self.assertTrue(MV2.allequal(vel, vel2))
Пример #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))
Пример #6
0
 def testBroadcasting(self):
     # Broadcast
     v_transient2 = self.v_transient[0]
     broadcasted = self.u_transient - v_transient2
     self.assertTrue(
         MV2.allequal(
             self.u_transient[2],
             broadcasted[2] +
             v_transient2))
Пример #7
0
 def testAverage(self):
     xav = MV2.average(self.ones, axis=1)
     self.assertTrue(MV2.allequal(xav, 1))
     xav2 = MV2.average(self.u_file)
     xav3 = MV2.average(self.u_transient)
     xav4, wav4 = MV2.average(
         self.u_transient, weights=MV2.ones(
             self.u_transient.shape, numpy.float), returned=1)
     a = MV2.arange(5)
     b = 2 ** a
     av, wav = MV2.average(b, weights=a, returned=1)
     self.assertEqual(av, 9.8)
     self.assertEqual(wav, 10)
Пример #8
0
def loop(potential,potential_reg,c2,w3,region):
    nmax        = potential.shape[0]
    c3          = MV2.not_equal(w3,0.)
    c           = MV2.logical_and(c2,c3)
    thisturn    = MV2.ones(c.shape)
    for i in range(nmax):
        c1                  = MV2.logical_or(MV2.equal(potential_reg[i],region),MV2.equal(potential[i],-999))
        c2                  = MV2.logical_and(c,c1)
        c2                  = MV2.logical_and(c2,thisturn)
        potential_reg[i]    = MV2.where(c2,region,potential_reg[i])
        thisturn            = MV2.where(c2,0,thisturn)
        c1                  = MV2.logical_and(c2,MV2.equal(potential[i],-999))
        c2                  = MV2.logical_and(c2,MV2.not_equal(potential[i],-999))
        potential[i]        = MV2.where(c1,w3,potential[i])
        potential[i]        = MV2.where(c2,potential[i]+w3,potential[i])
    ## Ultimate test to see if more would be needed !
    if not MV2.allequal(MV2.logical_and(c,thisturn),0):
        raise 'OOOPS WE COULD USE MORE REGIONS BUDDY !'
    return
Пример #9
0
 def testNegAbs(self):
     x11 = -self.other_u_file
     x12 = MV2.absolute(self.u_file)
     self.assertTrue(MV2.allequal(x11 + x12[0], 0))
Пример #10
0
 def testAdditionSubtraction(self):
     x1 = self.other_u_file + 1.0
     x2 = 1.0 - self.u_file
     self.assertTrue(MV2.allequal(x1 + x2[0], 2.0))
Пример #11
0
 def testWhere(self):
     xouter = MV2.outerproduct(MV2.arange(3), MV2.arange(3))
     xwhere = MV2.where(MV2.greater(xouter, 3), xouter, -123)
     self.assertEqual(xwhere[-1, -1], 4)
     xwhere.mask = xwhere == 4
     self.assertTrue(MV2.allequal(xwhere, -123))
Пример #12
0
ud = d["u"]
vd = d["v"]
udat = ud[:]
vdat = vd[:]
ulat = ud.getLatitude()
if not isinstance(udat, TV):
    markError("Slice does not return TV")
f = cdms2.open(os.path.join(pth, "u_2000.nc"))
uf = f["u"]

vel = MV2.sqrt(ud * ud + vd * vd)
vel.id = "velocity"
vel2 = MV2.sqrt(udat * udat + vdat * vdat)
vel2.id = "velocity"
vel2.units = ud.units
if not MV2.allequal(vel, vel2):
    markError("Slice operators do not compare")

x1 = uf + 1.0
x2 = 1.0 - ud
x11 = -uf
x12 = MV2.absolute(ud)
x3 = uf + x2
x4 = 1.0 + ud
x5 = uf - 1
x6 = ud * uf
x7 = ud / x2
x8 = 1 / uf
x9 = 3 * ud
x10 = uf ** 3
x13 = MV2.add.reduce(uf)
Пример #13
0
       landsea maks on target grid
    """
    cdat_info.pingPCMDIdb("cdat","cdutil.generateLandSeaMask")
    if cdms2.isVariable(target):
        target = target.getGrid()
        if target is None:
            raise Exception,"Error target data passed do not have  a grid"
    if not isinstance(target,cdms2.grid.TransientRectGrid):
        raise Exception, "Error: target grid must be rectilinear"

    if source is None:
        source = cdms2.open(os.path.join(sys.prefix,'share','cdutil','navy_land.nc'))('sftlf')
        
    try:
        navy_frac_t = source.regrid(target,regridTool='regrid2')
    except Exception,err:
        raise "error, cannot regrid source data to target, got error message: %s" % err
    
    mask = MV2.greater(navy_frac_t,.5).astype('i') # First guess, anything greater than 50% is land
    UL,UC,UR,ML,MR,LL,LC,LR = create_surrounds(navy_frac_t)
    cont = True
    i=0
    while cont:
        mask2 = improve(mask,navy_frac_t,threshold_1,threshold_2,UL,UC,UR,ML,MR,LL,LC,LR,regridTool=regridTool)
        if MV2.allequal(mask2,mask) or i>25: # shouldn't be more than 10 at max, 25 is way safe
            cont=False
        mask=mask2
        i+=1
    mask.id='sftlf'
    return mask
Пример #14
0
 def testMaskedArray(self):
     xmarray = MV2.masked_array(self.u_file, mask=self.u_file > .01)
     self.assertEqual(len(xmarray.getAxisList()),
                      len(self.u_file.getAxisList()))
     self.assertTrue(MV2.allequal(xmarray.mask, self.u_file > .01))
Пример #15
0
 def testCount(self):
     xouter = MV2.outerproduct(MV2.arange(5.), [1] * 10)
     masked = MV2.masked_outside(xouter, 1, 3)
     self.assertEqual(MV2.count(masked), 30)
     self.assertTrue(MV2.allequal(MV2.count(masked, 0), 3))
     self.assertTrue((MV2.count(masked, 1) == [0, 10, 10, 10, 0]).all())
Пример #16
0
 def testMaskingFunctions(self):
     xouter = MV2.outerproduct(MV2.arange(5.), [1] * 10)
     masked = MV2.masked_greater(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[2:], True))
     self.assertTrue(MV2.allequal(masked.mask[:2], False))
     masked = MV2.masked_greater_equal(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[1:], True))
     self.assertTrue(MV2.allequal(masked.mask[:1], False))
     masked = MV2.masked_less(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[:1], True))
     self.assertTrue(MV2.allequal(masked.mask[1:], False))
     masked = MV2.masked_less_equal(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[:2], True))
     self.assertTrue(MV2.allequal(masked.mask[2:], False))
     masked = MV2.masked_not_equal(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[1], False))
     self.assertTrue(MV2.allequal(masked.mask[0], True))
     self.assertTrue(MV2.allequal(masked.mask[2:], True))
     masked = MV2.masked_equal(xouter, 1)
     self.assertTrue(MV2.allequal(masked.mask[1], True))
     self.assertTrue(MV2.allequal(masked.mask[0], False))
     self.assertTrue(MV2.allequal(masked.mask[2:], False))
     masked = MV2.masked_outside(xouter, 1, 3)
     self.assertTrue(MV2.allequal(masked.mask[0:1], True))
     self.assertTrue(MV2.allequal(masked.mask[1:4], False))
     self.assertTrue(MV2.allequal(masked.mask[4:], True))
     masked = MV2.masked_where(
         MV2.logical_or(MV2.greater(xouter, 3), MV2.less(xouter, 2)),
         xouter)
     self.assertTrue(MV2.allequal(masked.mask[0:2], True))
     self.assertTrue(MV2.allequal(masked.mask[2:4], False))
     self.assertTrue(MV2.allequal(masked.mask[4:], True))
Пример #17
0
 def testOuterproduct(self):
     xouter = MV2.outerproduct(MV2.arange(3.), MV2.arange(5.))
     self.assertEqual(xouter.shape, (3, 5))
     for i in range(3):
         self.assertTrue(MV2.allequal(xouter[i], i * xouter[1]))
Пример #18
0
d = cdms2.open(os.path.join(get_sample_data_dir(),'test.xml'))
ud = d['u']
vd = d['v']
udat = ud[:]
vdat = vd[:]
ulat = ud.getLatitude()
if not isinstance(udat, TV): markError('Slice does not return TV')
f = cdms2.open(os.path.join(get_sample_data_dir(),'u_2000.nc'))
uf = f['u']

vel = MV2.sqrt(ud*ud + vd*vd)
vel.id = 'velocity'
vel2 = MV2.sqrt(udat*udat + vdat*vdat)
vel2.id = 'velocity'
vel2.units = ud.units
if not MV2.allequal(vel,vel2): markError('Slice operators do not compare')

x1 = uf+1.0
x2 = 1.0-ud
x11 = -uf
x12 = MV2.absolute(ud)
x3 = uf+x2
x4 = 1.0+ud
x5 = uf-1
x6 = ud*uf
x7 = ud/x2
x8=1/uf
x9 = 3*ud
x10=uf**3
x13 = MV2.add.reduce(uf)
x14 = MV2.add.reduce(ud)
Пример #19
0
def generateLandSeaMask(target,
                        source=None,
                        threshold_1=.2,
                        threshold_2=.3,
                        regridTool='regrid2'):
    """
    Generates a best guess mask on any rectilinear grid, using the method described in `PCMDI's report #58`_

    .. _PCMDI's report #58: http://www-pcmdi.llnl.gov/publications/pdf/58.pdf

    :param target: either a MV2 object with a grid, or a cdms2 grid (rectilinear grid only)
    :type target: MV2 or cdms2

    :param source: A fractional (0.0 to 1.0) land sea mask, where 1 means all land
    :type source: float

    :param threshold_1: criteria 1 for detecting cells with possible increment see report for detail
                        difference threshold
    :type threshold_1: float

    :param threshold_2: criteria 2 for detecting cells with possible increment see report for detail
                        water/land content threshold
    :type threshold_2: float

    :param regridTool: which cdms2 regridder tool to use, default is regrid2
    :type regridTool:

    :returns: landsea mask on target grid
    """
    if cdms2.isVariable(target):
        target = target.getGrid()
        if target is None:
            raise Exception("Error target data passed do not have  a grid")
    if not isinstance(target, cdms2.grid.TransientRectGrid):
        raise Exception("Error: target grid must be rectilinear")

    if source is None:
        source = cdms2.open(os.path.join(egg_path, 'navy_land.nc'))('sftlf')

    try:
        # print("REGRIDDING WITH SOURCE:", source)
        navy_frac_t = source.regrid(target, regridTool='regrid2')
    except Exception as err:
        raise Exception(
            "error, cannot regrid source data to target, got error message: %s"
            % err)

    # First guess, anything greater than 50% is land
    mask = MV2.greater(navy_frac_t, .5).astype('i')
    UL, UC, UR, ML, MR, LL, LC, LR = create_surrounds(navy_frac_t)
    cont = True
    i = 0
    while cont:
        mask2 = improve(mask,
                        navy_frac_t,
                        threshold_1,
                        threshold_2,
                        UL,
                        UC,
                        UR,
                        ML,
                        MR,
                        LL,
                        LC,
                        LR,
                        regridTool=regridTool)
        if MV2.allequal(
                mask2, mask
        ) or i > 25:  # shouldn't be more than 10 at max, 25 is way safe
            cont = False
        mask = mask2.astype("i")
        i += 1
    mask.id = 'sftlf'
    return mask
Пример #20
0
 def testProduct(self):
     xprod = MV2.product(self.u_file)
     partial_prod = MV2.product(self.u_file[1:])
     self.assertTrue(MV2.allequal(xprod / partial_prod, self.u_file[0]))
Пример #21
0
    except Exception, err:
        raise "error, cannot regrid source data to target, got error message: %s" % err

    mask = MV2.greater(navy_frac_t, .5).astype(
        'i')  # First guess, anything greater than 50% is land
    UL, UC, UR, ML, MR, LL, LC, LR = create_surrounds(navy_frac_t)
    cont = True
    i = 0
    while cont:
        mask2 = improve(mask,
                        navy_frac_t,
                        threshold_1,
                        threshold_2,
                        UL,
                        UC,
                        UR,
                        ML,
                        MR,
                        LL,
                        LC,
                        LR,
                        regridTool=regridTool)
        if MV2.allequal(
                mask2, mask
        ) or i > 25:  # shouldn't be more than 10 at max, 25 is way safe
            cont = False
        mask = mask2
        i += 1
    mask.id = 'sftlf'
    return mask
Пример #22
0
 def testSum(self):
     ones = MV2.ones((1, 2, 3))
     self.assertEqual(MV2.sum(ones), 6)
     self.assertTrue(MV2.allequal(MV2.sum(ones, axis=2), 3))
Пример #23
0
d = cdms2.open(os.path.join(pth, 'test.xml'))
ud = d['u']
vd = d['v']
udat = ud[:]
vdat = vd[:]
ulat = ud.getLatitude()
if not isinstance(udat, TV): markError('Slice does not return TV')
f = cdms2.open(os.path.join(pth, 'u_2000.nc'))
uf = f['u']

vel = MV2.sqrt(ud * ud + vd * vd)
vel.id = 'velocity'
vel2 = MV2.sqrt(udat * udat + vdat * vdat)
vel2.id = 'velocity'
vel2.units = ud.units
if not MV2.allequal(vel, vel2): markError('Slice operators do not compare')

x1 = uf + 1.0
x2 = 1.0 - ud
x11 = -uf
x12 = MV2.absolute(ud)
x3 = uf + x2
x4 = 1.0 + ud
x5 = uf - 1
x6 = ud * uf
x7 = ud / x2
x8 = 1 / uf
x9 = 3 * ud
x10 = uf**3
x13 = MV2.add.reduce(uf)
x14 = MV2.add.reduce(ud)
Пример #24
0
 def testTake(self):
     t = MV2.take(self.u_file, [2, 4, 6], axis=2)
     self.assertTrue(MV2.allequal(t[:, :, 0], self.u_file[:, :, 2]))
     self.assertTrue(MV2.allequal(t[:, :, 1], self.u_file[:, :, 4]))
     self.assertTrue(MV2.allequal(t[:, :, 2], self.u_file[:, :, 6]))