Пример #1
0
    def test_set_spatial_mask(self):
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        self.assertFalse(np.any(mask_var.get_mask()))
        dmap.set_spatial_mask(mask_var)
        self.assertEqual(dmap.get_spatial_mask(), mask_var.name)

        with self.assertRaises(DimensionMapError):
            dmap.set_variable(DMK.SPATIAL_MASK, mask_var)

        # Test custom variables may be used.
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_spatial_mask_variable('a_mask', None, dims)
        attrs = {'please keep me': 'no overwriting'}
        dmap.set_spatial_mask(mask_var, attrs=attrs)
        attrs = dmap.get_attrs(DMK.SPATIAL_MASK)
        self.assertIn('please keep me', attrs)

        # Test default attributes are not added.
        dmap = DimensionMap()
        dmap.set_spatial_mask('foo', default_attrs={'blue': 'world'})
        prop = dmap.get_property(DMK.SPATIAL_MASK)
        self.assertEqual(prop['attrs'], {'blue': 'world'})
Пример #2
0
    def test_set_variable(self):
        var = Variable(name='test', value=[1, 2], dimensions='two')
        dmap = DimensionMap()
        dmap.set_variable(DMK.X, var, dimension='not_two')
        actual = dmap.get_dimension(DMK.X)
        desired = ['not_two']
        self.assertEqual(actual, desired)

        # Test setting with a variable and bounds.
        var = Variable(name='center',
                       value=[1, 2, 3],
                       dtype=float,
                       dimensions='one')
        var.set_extrapolated_bounds('bounds_data', 'bounds')
        dmap = DimensionMap()
        dmap.set_variable(DMK.Y, var)
        self.assertEqual(dmap.get_bounds(DMK.Y), var.bounds.name)
        new_var = Variable(name='new_center',
                           value=[1, 2, 3],
                           dtype=float,
                           dimensions='one')
        dmap.set_variable(DMK.Y, new_var)
        self.assertIsNone(dmap.get_bounds(DMK.Y))

        # Test a dimension position argument is needed if the variable has more than one dimension.
        var = Variable(name='two_dims',
                       value=np.zeros((4, 5)),
                       dimensions=['one', 'two'])
        dmap = DimensionMap()
        with self.assertRaises(DimensionMapError):
            dmap.set_variable(DMK.X, var)
        dmap.set_variable(DMK.X, var, pos=1)
        self.assertEqual(dmap.get_dimension(DMK.X), ['two'])

        # Test a scalar dimension.
        var = Variable(name='scalar_dimension', dimensions=[])
        dmap = DimensionMap()
        dmap.set_variable(DMK.LEVEL, var)
        self.assertEqual(dmap.get_variable(DMK.LEVEL), 'scalar_dimension')
        self.assertIsNone(dmap.get_bounds(DMK.LEVEL))
        self.assertEqual(dmap.get_dimension(DMK.LEVEL), [])

        # Test dimensionless variable.
        var = Variable(name='two_dims',
                       value=np.zeros((4, 5)),
                       dimensions=['one', 'two'])
        dmap = DimensionMap()
        dmap.set_variable(DMK.X, var, dimensionless=True)
        self.assertEqual(dmap.get_dimension(DMK.X), [])
Пример #3
0
    def test_get_topology(self):
        dmap = DimensionMap()
        self.assertEqual(dmap.get_topology(GridAbstraction.POINT), None)

        dmap_poly = dmap.get_topology(GridAbstraction.POLYGON, create=True)
        self.assertEqual(dmap_poly, DimensionMap())

        dmap_poly.set_variable(DMK.X, 'polyx')
        dmap_poly.set_variable(DMK.ELEMENT_NODE_CONNECTIVITY,
                               'element_connectivity')

        dmap_point = dmap.get_topology(GridAbstraction.POINT, create=True)
        dmap_point.set_variable(DMK.Y, 'point_y')
        self.assertEqual(
            dmap.get_topology(GridAbstraction.POINT).get_variable(DMK.Y),
            'point_y')
Пример #4
0
 def test_set_bounds(self):
     dmap = DimensionMap()
     dmap.set_variable(DMK.X, 'lon', bounds='lon_bounds')
     actual = dmap.get_bounds(DMK.X)
     self.assertEqual(actual, 'lon_bounds')
     dmap.set_bounds(DMK.X, None)
     actual = dmap.get_bounds(DMK.X)
     self.assertIsNone(actual)
Пример #5
0
    def create_dimension_map(self, group_metadata, **kwargs):
        """
        Create a dimension map for a group from its metadata.

        :param dict group_metadata: Group metadata to use when creating the dimension map.
        :rtype: :class:`~ocgis.DimensionMap`
        """
        ret = DimensionMap()
        ret.set_driver(self.key)
        return ret
Пример #6
0
    def test_set_spatial_mask(self):
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_grid_mask_variable('a_mask', None, dims)
        self.assertFalse(np.any(mask_var.get_mask()))
        dmap.set_spatial_mask(mask_var)
        self.assertEqual(dmap.get_spatial_mask(), mask_var.name)

        with self.assertRaises(DimensionMapError):
            dmap.set_variable(DMK.SPATIAL_MASK, mask_var)

        # Test custom variables may be used.
        dmap = DimensionMap()
        dims = Dimension('x', 3), Dimension('y', 7)
        mask_var = create_grid_mask_variable('a_mask', None, dims)
        attrs = {'please keep me': 'no overwriting'}
        dmap.set_spatial_mask(mask_var, attrs=attrs)
        attrs = dmap.get_attrs(DMK.SPATIAL_MASK)
        self.assertIn('please keep me', attrs)
Пример #7
0
    def test(self):
        dmap = DimensionMap()

        _ = dmap.get_variable('x')
        _ = dmap.get_dimension('x')
        _ = dmap.get_attrs('x')
        _ = dmap.get_bounds('x')

        dmap.set_variable('y',
                          'latitude',
                          dimension='ache',
                          bounds='lat_bounds')

        _ = dmap.get_crs()

        dmap.set_crs('latitude_longitude')

        desired = {
            'crs': {
                'variable': 'latitude_longitude'
            },
            'x': {
                'attrs': {},
                'bounds': None,
                'dimension': [],
                'variable': None
            },
            'y': {
                'attrs': {
                    'axis': 'Y'
                },
                'bounds': 'lat_bounds',
                'dimension': ['ache'],
                'variable': 'latitude'
            }
        }

        self.assertDictEqual(dmap._storage, desired)
Пример #8
0
 def test_set_driver(self):
     dmap = DimensionMap()
     dmap.set_driver(DriverNetcdfUGRID)
     self.assertEqual(dmap.get_driver(), DriverNetcdfUGRID.key)
Пример #9
0
    def test_get_group(self):
        dmap = DimensionMap()
        dmap.set_crs('who')
        actual = dmap.get_group(None)
        self.assertEqual(dmap.as_dict(), actual.as_dict())

        dmap = DimensionMap()
        dmap.set_crs('what')
        with self.assertRaises(DimensionMapError):
            dmap.get_group(['level_1'])

        level_1 = DimensionMap()
        level_1.set_variable(DMK.LEVEL, 'level_1')
        dmap.set_group('level_1', level_1)
        desired = {
            'crs': {
                'variable': 'what'
            },
            'groups': {
                'level_1': {
                    'level': {
                        'attrs': {},
                        'bounds': None,
                        'dimension': [],
                        'variable': 'level_1'
                    }
                }
            }
        }
        actual = dmap.as_dict()
        self.assertEqual(actual, desired)

        level_1_1 = DimensionMap()
        level_1_1.set_variable(DMK.X, 'longitude')
        level_1.set_group('level_1_1', level_1_1)
        desired = {
            'crs': {
                'variable': 'what'
            },
            'groups': {
                'level_1': {
                    'groups': {
                        'level_1_1': {
                            'x': {
                                'attrs': {},
                                'bounds': None,
                                'dimension': [],
                                'variable': 'longitude'
                            }
                        }
                    },
                    'level': {
                        'attrs': {},
                        'bounds': None,
                        'dimension': [],
                        'variable': 'level_1'
                    }
                }
            }
        }
        actual = dmap.as_dict()
        self.assertEqual(actual, desired)
Пример #10
0
 def test_get_driver(self):
     dmap = DimensionMap()
     self.assertEqual(dmap.get_driver(), DriverNetcdfCF.key)
Пример #11
0
 def get_dimension_map(self, group_metadata):
     """:rtype: :class:`ocgis.DimensionMap`"""
     return DimensionMap()