Example #1
0
    def test_create_flags_variable_1D(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_vector_variable = DatasetUtil.create_flags_variable([5], meanings, dim_names=["dim1"],
                                                                         attributes={"standard_name": "std"})

        self.assertIsNotNone(flags_vector_variable)
        self.assertEqual(Variable, type(flags_vector_variable))
        self.assertEqual((5,), flags_vector_variable.shape)
        self.assertEqual(np.uint8, flags_vector_variable.dtype)
        self.assertEqual(flags_vector_variable.attrs['flag_masks'], masks)
        self.assertEqual(flags_vector_variable.attrs['flag_meanings'], meanings_txt)
        self.assertEqual(0, flags_vector_variable[2])
        self.assertEqual("std", flags_vector_variable.attrs["standard_name"])
        self.assertEqual(("dim1",), flags_vector_variable.dims)
Example #2
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 = 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