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 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_default_array_3D_int_fill_value(self): default_array = DatasetUtil.create_default_array([7, 8, 3], np.int8, fill_value=1) self.assertIsNotNone(default_array) self.assertEqual(DataArray, type(default_array)) self.assertEqual((7, 8, 3), default_array.shape) self.assertEqual(np.int8, default_array.dtype) self.assertEqual(1, default_array[2, 4, 2])
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_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_default_array_3D_int_dims(self): default_array = DatasetUtil.create_default_array([7, 8, 3], np.int8, dim_names=["dim1", "dim2", "dim3"]) self.assertIsNotNone(default_array) self.assertEqual(DataArray, type(default_array)) self.assertEqual((7, 8, 3), default_array.shape) self.assertEqual(np.int8, default_array.dtype) self.assertEqual(-127, default_array[2, 4, 2]) self.assertEqual(("dim1", "dim2", "dim3",), default_array.dims)
def test_create_default_array_1D_int(self): default_array = DatasetUtil.create_default_array([5], np.int8) self.assertIsNotNone(default_array) self.assertEqual(DataArray, type(default_array)) self.assertEqual((5,), default_array.shape) self.assertEqual(np.int8, default_array.dtype) self.assertEqual(-127, default_array[2])
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_get_default_fill_value(self): self.assertEqual(-127, DatasetUtil.get_default_fill_value(np.int8)) self.assertEqual(-32767, DatasetUtil.get_default_fill_value(np.int16)) self.assertEqual(np.uint16(-1), DatasetUtil.get_default_fill_value(np.uint16)) self.assertEqual(-2147483647, DatasetUtil.get_default_fill_value(np.int32)) self.assertEqual(-9223372036854775806, DatasetUtil.get_default_fill_value(np.int64)) self.assertEqual(np.float32(9.96921E36), DatasetUtil.get_default_fill_value(np.float32)) self.assertEqual(9.969209968386869E36, DatasetUtil.get_default_fill_value(np.float64))
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)
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
def test_return_flags_dtype_32(self): data_type = DatasetUtil.return_flags_dtype(32) self.assertEqual(data_type, np.uint32)
def test_return_flags_dtype_16(self): data_type = DatasetUtil.return_flags_dtype(16) self.assertEqual(data_type, np.uint16)