def test_get_flags_mask_and(self):

        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [2, 3],
            meanings,
            dim_names=["dim1", "dim2"],
            attributes={"standard_name": "std"})

        ds["flags"] = flags_vector_variable
        ds["flags"] = DatasetUtil.set_flag(ds["flags"], "flag4")
        ds["flags"][0, 1] = DatasetUtil.set_flag(ds["flags"][0, 1], "flag5")
        ds["flags"][1, 1] = DatasetUtil.set_flag(ds["flags"][1, 1], "flag2")
        ds["flags"][1, 1] = DatasetUtil.set_flag(ds["flags"][1, 1], "flag7")

        flags_mask = DatasetUtil.get_flags_mask_and(ds["flags"],
                                                    flags=["flag2", "flag7"])

        expected_flags_mask = np.array(
            [[False, False, False], [False, True, False]], dtype=bool)

        np.testing.assert_array_almost_equal(flags_mask, expected_flags_mask)
    def test_unpack_flags(self):

        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        masks = [1, 2, 4, 8, 16, 32, 64, 128]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [2, 3],
            meanings,
            dim_names=["dim1", "dim2"],
            attributes={"standard_name": "std"})

        ds["flags"] = flags_vector_variable
        ds["flags"][0, 0] = ds["flags"][0, 0] | 8

        empty = np.zeros((2, 3), bool)
        flag4 = np.zeros((2, 3), bool)
        flag4[0, 0] = True

        flags = DatasetUtil.unpack_flags(ds["flags"])

        self.assertTrue((flags["flag1"].data == empty).all())
        self.assertTrue((flags["flag2"].data == empty).all())
        self.assertTrue((flags["flag3"].data == empty).all())
        self.assertTrue((flags["flag4"].data == flag4).all())
        self.assertTrue((flags["flag5"].data == empty).all())
        self.assertTrue((flags["flag6"].data == empty).all())
        self.assertTrue((flags["flag7"].data == empty).all())
        self.assertTrue((flags["flag8"].data == empty).all())
Esempio n. 3
0
    def add_variables(ds, variables_dict, dim_sizes_dict):
        """
        Adds defined variables dataset

        :type ds: xarray.Dataset
        :param ds: dataset

        :type variables_dict: dict
        :type variables_dict: dictionary defining variables

        :type dim_sizes_dict: dict
        :param dim_sizes_dict: entry per dataset dimension with value of size as int

        :return: dataset with defined variables
        :rtype: xarray.Dataset
        """

        du = DatasetUtil()

        for variable_name in variables_dict.keys():

            variable_attrs = variables_dict[variable_name]

            # Check variable definition
            TemplateUtil._check_variable_definition(variable_name, variable_attrs)

            # Unpack variable attributes
            dtype = variable_attrs["dtype"]
            dim_names = variable_attrs["dim"]
            attributes = deepcopy(variable_attrs["attributes"]) if "attributes" in variable_attrs else None

            # Determine variable shape from dims
            try:
                dim_sizes = TemplateUtil._return_variable_shape(dim_names, dim_sizes_dict)
            except KeyError:
                raise KeyError("Dim Name Error - Variable " + variable_name + " defined with dim not in dim_sizes_dict")

            # Create variable and add to dataset
            if dtype == "flag":
                flag_meanings = attributes.pop("flag_meanings")
                variable = du.create_flags_variable(dim_sizes, meanings=flag_meanings,
                                                    dim_names=dim_names, attributes=attributes)

            else:
                variable = du.create_variable(dim_sizes, dim_names=dim_names,
                                              dtype=dtype, attributes=attributes)

                if "encoding" in variable_attrs:
                    du.add_encoding(variable, **variable_attrs["encoding"])

            ds[variable_name] = variable

        return ds
    def test_check_flag_set_2d(self):
        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5],
            meanings,
            dim_names=["dim1"],
            attributes={"standard_name": "std"})
        ds["flags"] = flags_vector_variable

        self.assertRaises(ValueError, DatasetUtil.check_flag_set, ds["flags"],
                          "flag6")
    def test_check_flag_set_false(self):
        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5],
            meanings,
            dim_names=["dim1"],
            attributes={"standard_name": "std"})
        ds["flags"] = flags_vector_variable
        ds["flags"][3] = ds["flags"][3] | 8

        flag_set = DatasetUtil.check_flag_set(ds["flags"][3], "flag6")

        self.assertFalse(flag_set)
    def test_set_flag_error_if_set(self):
        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5],
            meanings,
            dim_names=["dim1"],
            attributes={"standard_name": "std"})
        ds["flags"] = flags_vector_variable
        ds["flags"][3] = ds["flags"][3] | 8

        self.assertRaises(ValueError,
                          DatasetUtil.set_flag,
                          ds["flags"],
                          "flag4",
                          error_if_set=True)
    def test_set_flag(self):

        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5, 4],
            meanings,
            dim_names=["dim1", "dim2"],
            attributes={"standard_name": "std"})
        ds["flags"] = flags_vector_variable

        ds["flags"] = DatasetUtil.set_flag(ds["flags"], "flag4")

        flags = np.full(ds["flags"].shape, 0 | 8)

        self.assertTrue((ds["flags"].data == flags).all())
    def test_get_set_flags(self):

        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5],
            meanings,
            dim_names=["dim1"],
            attributes={"standard_name": "std"})
        ds["flags"] = flags_vector_variable
        ds["flags"][3] = ds["flags"][3] | 8
        ds["flags"][3] = ds["flags"][3] | 32

        set_flags = DatasetUtil.get_set_flags(ds["flags"][3])

        self.assertCountEqual(set_flags, ["flag4", "flag6"])
    def test__get_flag_encoding(self):

        ds = Dataset()
        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        masks = [1, 2, 4, 8, 16, 32, 64, 128]
        flags_vector_variable = DatasetUtil.create_flags_variable(
            [5],
            meanings,
            dim_names=["dim1"],
            attributes={"standard_name": "std"})

        ds["flags"] = flags_vector_variable

        meanings_out, masks_out = DatasetUtil._get_flag_encoding(ds["flags"])

        self.assertCountEqual(meanings, meanings_out)
        self.assertCountEqual(masks, masks_out)
    def test_create_flags_variable_3D(self):

        meanings = [
            "flag1", "flag2", "flag3", "flag4", "flag5", "flag6", "flag7",
            "flag8"
        ]
        meanings_txt = "flag1 flag2 flag3 flag4 flag5 flag6 flag7 flag8"
        masks = "1, 2, 4, 8, 16, 32, 64, 128"
        flags_array_variable = DatasetUtil.create_flags_variable(
            [7, 8, 3],
            meanings,
            dim_names=["dim1", "dim2", "dim3"],
            attributes={"standard_name": "std"})

        self.assertIsNotNone(flags_array_variable)
        self.assertEqual(Variable, type(flags_array_variable))
        self.assertEqual((7, 8, 3), flags_array_variable.shape)
        self.assertEqual(np.uint8, flags_array_variable.dtype)
        self.assertEqual(flags_array_variable.attrs['flag_masks'], masks)
        self.assertEqual(flags_array_variable.attrs['flag_meanings'],
                         meanings_txt)
        self.assertEqual(0, flags_array_variable[2, 4, 2])
        self.assertEqual("std", flags_array_variable.attrs["standard_name"])
        self.assertEqual(("dim1", "dim2", "dim3"), flags_array_variable.dims)