Example #1
0
 def test_properties(self):
     # Test an empty properties dictionary is created if there are no data variables on the container/parent field.
     gvar = GeometryVariable(name='empty', value=[Point(1, 2)], ugid=20, dimensions='ngeom')
     info = Variable(name='info')
     gvar.parent.add_variable(info)
     sc = SpatialCollection()
     sc.add_field(Field(name='what'), gvar.parent)
     self.assertEqual(len(sc.properties), 1)
Example #2
0
 def test_properties(self):
     # Test an empty properties dictionary is created if there are no data variables on the container/parent field.
     gvar = GeometryVariable(name='empty', value=[Point(1, 2)], ugid=20, dimensions='ngeom')
     info = Variable(name='info')
     gvar.parent.add_variable(info)
     sc = SpatialCollection()
     sc.add_field(Field(name='what'), gvar.parent)
     self.assertEqual(len(sc.properties), 1)
Example #3
0
    def spatial_collection(self):
        poi = self.get_subset_field()
        field1, field2 = self.field1, self.field2

        sc = SpatialCollection()
        for ii in range(poi.geom.shape[0]):
            subset = poi.geom[ii]
            subset_geom = subset.get_value()[0]
            container = poi.get_field_slice({'geom': ii})
            for field in [field1, field2]:
                field.set_abstraction_geom()
                subset_field = field.geom.get_intersects(subset_geom).parent
                sc.add_field(subset_field, container)

        return sc
Example #4
0
    def spatial_collection(self):
        poi = self.get_subset_field()
        field1, field2 = self.field1, self.field2

        sc = SpatialCollection()
        for ii in range(poi.geom.shape[0]):
            subset = poi.geom[ii]
            subset_geom = subset.get_value()[0]
            container = poi.get_field_slice({'geom': ii})
            for field in [field1, field2]:
                field.set_abstraction_geom()
                subset_field = field.geom.get_intersects(subset_geom).parent
                sc.add_field(subset_field, container)

        return sc
Example #5
0
    def test_system_add_variable(self):
        """Test adding variables from spatial collections."""

        # Create a few separate fields.
        variable_names = tuple(['a', 'b', 'c'])
        fields = [self.get_field(variable_name=v) for v in variable_names]
        # Create spatial collections containing those fields.
        scs = []
        for field in fields:
            sc = SpatialCollection()
            sc.add_field(field, None)
            scs.append(sc)

        # Destination spatial collection to add variables to from source spatial collections.
        grow = scs[0]
        # Loop over source fields.
        for idx in range(1, len(scs)):
            # Loop over child fields and spatial containers in the current source spatial collection.
            for field, container in scs[idx].iter_fields(yield_container=True):
                # TODO: This should be adjusted to allow easier selection with empty fields.
                try:
                    # Case when we have spatial containers.
                    grow_field = grow.get_element(field_name=field.name,
                                                  container_ugid=container)
                except KeyError:
                    # Case without spatial containers.
                    grow_field = grow.get_element(field.name)
                # Add data variables to the grow field.
                for dv in field.data_variables:
                    grow_field.add_variable(dv.extract(), is_data=True)

        # Assert all variables are present on the grow field.
        actual = grow.get_element()
        self.assertEqual(get_variable_names(actual.data_variables),
                         variable_names)

        # Write the spatial collection using a converter.
        conv = NcConverter([grow],
                           outdir=self.current_dir_output,
                           prefix='out.nc')
        conv.write()

        # Assert all variables are present.
        rd = RequestDataset(conv.path)
        actual = rd.get()
        self.assertEqual(get_variable_names(actual.data_variables),
                         variable_names)
Example #6
0
    def test_system_add_variable(self):
        """Test adding variables from spatial collections."""

        # Create a few separate fields.
        variable_names = tuple(['a', 'b', 'c'])
        fields = [self.get_field(variable_name=v) for v in variable_names]
        # Create spatial collections containing those fields.
        scs = []
        for field in fields:
            sc = SpatialCollection()
            sc.add_field(field, None)
            scs.append(sc)

        # Destination spatial collection to add variables to from source spatial collections.
        grow = scs[0]
        # Loop over source fields.
        for idx in range(1, len(scs)):
            # Loop over child fields and spatial containers in the current source spatial collection.
            for field, container in scs[idx].iter_fields(yield_container=True):
                # TODO: This should be adjusted to allow easier selection with empty fields.
                try:
                    # Case when we have spatial containers.
                    grow_field = grow.get_element(field_name=field.name, container_ugid=container)
                except KeyError:
                    # Case without spatial containers.
                    grow_field = grow.get_element(field.name)
                # Add data variables to the grow field.
                for dv in field.data_variables:
                    grow_field.add_variable(dv.extract(), is_data=True)

        # Assert all variables are present on the grow field.
        actual = grow.get_element()
        self.assertEqual(get_variable_names(actual.data_variables), variable_names)

        # Write the spatial collection using a converter.
        conv = NcConverter([grow], outdir=self.current_dir_output, prefix='out.nc')
        conv.write()

        # Assert all variables are present.
        rd = RequestDataset(conv.path)
        actual = rd.get()
        self.assertEqual(get_variable_names(actual.data_variables), variable_names)
Example #7
0
    def test_with_eval_function_two_variables(self):
        funcs = [{'func': 'tas_out=tas+tas2', 'ref': EvalFunction}]
        engine = self.get_engine(funcs=funcs, grouping=None)
        rd = self.test_data.get_rd('cancm4_tas')
        rd2 = self.test_data.get_rd('cancm4_tas', kwds={'rename_variable': 'tas2'})
        field = rd.get()
        field2 = rd2.get()
        with orphaned(field2['tas2']):
            field.add_variable(field2['tas2'], is_data=True)
        field = field.get_field_slice({'time': slice(0, 100), 'y': slice(0, 10), 'x': slice(0, 10)})

        desired = SpatialCollection()
        desired.add_field(field, None)
        actual = deepcopy(desired)
        engine.execute(desired)

        tas_out = desired.get_element(variable_name='tas_out').get_value()
        tas = actual.get_element(variable_name='tas').get_value()
        tas2 = actual.get_element(variable_name='tas2').get_value()
        self.assertNumpyAll(tas_out, tas + tas2)
Example #8
0
    def test_with_eval_function_two_variables(self):
        funcs = [{'func': 'tas_out=tas+tas2', 'ref': EvalFunction}]
        engine = self.get_engine(funcs=funcs, grouping=None)
        rd = self.test_data.get_rd('cancm4_tas')
        rd2 = self.test_data.get_rd('cancm4_tas', kwds={'rename_variable': 'tas2'})
        field = rd.get()
        field2 = rd2.get()
        with orphaned(field2['tas2']):
            field.add_variable(field2['tas2'], is_data=True)
        field = field.get_field_slice({'time': slice(0, 100), 'y': slice(0, 10), 'x': slice(0, 10)})

        desired = SpatialCollection()
        desired.add_field(field, None)
        actual = deepcopy(desired)
        engine.execute(desired)

        tas_out = desired.get_element(variable_name='tas_out').get_value()
        tas = actual.get_element(variable_name='tas').get_value()
        tas2 = actual.get_element(variable_name='tas2').get_value()
        self.assertNumpyAll(tas_out, tas + tas2)
Example #9
0
 def _get_initialized_collection_():
     coll = SpatialCollection()
     return coll