def test_create_variable_3D_int_fill_value(self): array_variable = DatasetUtil.create_variable([7, 8, 3], np.int8, fill_value=1) self.assertIsNotNone(array_variable) self.assertEqual(Variable, type(array_variable)) self.assertEqual((7, 8, 3), array_variable.shape) self.assertEqual(np.int8, array_variable.dtype) self.assertEqual(1, array_variable[2, 4, 2])
def test_create_variable_1D_int_fill_value(self): vector_variable = DatasetUtil.create_variable([5], np.int8, fill_value=1) self.assertIsNotNone(vector_variable) self.assertEqual(Variable, type(vector_variable)) self.assertEqual((5,), vector_variable.shape) self.assertEqual(np.int8, vector_variable.dtype) self.assertEqual(1, vector_variable[2])
def test_create_variable_1D_int_dims(self): vector_variable = DatasetUtil.create_variable([5], np.int8, dim_names=["dim1"]) self.assertIsNotNone(vector_variable) self.assertEqual(Variable, type(vector_variable)) self.assertEqual((5,), vector_variable.shape) self.assertEqual(np.int8, vector_variable.dtype) self.assertEqual(-127, vector_variable[2]) self.assertEqual(("dim1",), vector_variable.dims)
def test_create_variable_3D_int_attributes(self): array_variable = DatasetUtil.create_variable([7, 8, 3], np.int8, attributes={"standard_name": "std"}) self.assertIsNotNone(array_variable) self.assertEqual(Variable, type(array_variable)) self.assertEqual((7, 8, 3), array_variable.shape) self.assertEqual(np.int8, array_variable.dtype) self.assertEqual(-127, array_variable[2, 4, 2]) self.assertEqual("std", array_variable.attrs["standard_name"])
def test_create_variable_3D_int_dims(self): array_variable = DatasetUtil.create_variable([7, 8, 3], np.int8, dim_names=["dim1", "dim2", "dim3"]) self.assertIsNotNone(array_variable) self.assertEqual(Variable, type(array_variable)) self.assertEqual((7, 8, 3), array_variable.shape) self.assertEqual(np.int8, array_variable.dtype) self.assertEqual(-127, array_variable[2, 4, 2]) self.assertEqual(("dim1", "dim2", "dim3",), array_variable.dims)
def test_create_variable_1D_int_attributes(self): vector_variable = DatasetUtil.create_variable([5], np.int8, attributes={"standard_name": "std"}) self.assertIsNotNone(vector_variable) self.assertEqual(Variable, type(vector_variable)) self.assertEqual((5,), vector_variable.shape) self.assertEqual(np.int8, vector_variable.dtype) self.assertEqual(-127, vector_variable[2]) self.assertEqual("std", vector_variable.attrs["standard_name"])
def test_add_encoding(self): vector_variable = DatasetUtil.create_variable([5], np.int8) DatasetUtil.add_encoding(vector_variable, np.int32, scale_factor=10, offset=23, fill_value=11, chunksizes=12) self.assertIsNotNone(vector_variable) self.assertEqual(Variable, type(vector_variable)) self.assertEqual((5,), vector_variable.shape) self.assertEqual(np.int8, vector_variable.dtype) self.assertEqual(-127, vector_variable[2]) self.assertEqual(np.int32, vector_variable.encoding["dtype"]) self.assertEqual(10, vector_variable.encoding["scale_factor"]) self.assertEqual(23, vector_variable.encoding["add_offset"]) self.assertEqual(11, vector_variable.encoding["_FillValue"]) self.assertEqual(12, vector_variable.encoding["chunksizes"])
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