コード例 #1
0
ファイル: test_variable.py プロジェクト: HydroLogic/ocgis
    def test_get_empty_like(self):
        kwargs = dict(name='tas', alias='tas2', units='celsius', meta={'foo': 5}, uid=5, data='foo', did=5)
        value = np.array([1, 2, 3, 4, 5])
        value = np.ma.array(value, mask=[False, True, False, True, False])
        kwargs['value'] = value
        kwargs['attrs'] = OrderedDict(foo=5)
        var = Variable(**kwargs)

        for shape in [None, (2, 2)]:
            new_var = var.get_empty_like(shape=shape)
            self.assertDictEqual(new_var.attrs, kwargs['attrs'])
            new_var.attrs['hi'] = 'wow'
            self.assertNotEqual(new_var.attrs, kwargs['attrs'])
            self.assertEqual(new_var.uid, var.uid)
            if shape is None:
                actual = np.ma.array(np.zeros(5), dtype=var.dtype, fill_value=var.fill_value, mask=value.mask)
            else:
                actual = np.ma.array(np.zeros((2, 2)), dtype=var.dtype, fill_value=var.fill_value, mask=False)
            self.assertNumpyAll(actual, new_var.value)
            # the meta dictionary should be deepcopied
            new_var.meta['hi'] = 'there'
            self.assertDictEqual(var.meta, {'foo': 5})