def test(self, dataset, mask, code=102): if tit is None: raise ModuleNotFoundError("titanlib was not loaded properly") minvals = [] maxvals = [] values = [] for i in range(0, len(mask)): minvals.append(self.minvals[i]) maxvals.append(self.maxvals[i]) values.append(dataset.values[mask[i]]) global_flags = dataset.flags flags = tit.range_check(values, minvals, maxvals) for i in range(0, len(mask)): if global_flags[mask[i]] == 0 and flags[i] == 1: global_flags[mask[i]] = code if self.debug: for i in range(0, len(mask)): print(self.name, i, mask[i], dataset.values[mask[i]], flags[mask[i]], global_flags[mask[i]]) return global_flags
def test(self, dataset, mask, code=108): if tit is None: raise ModuleNotFoundError("titanlib was not loaded properly") fg = ObsOperator(self.operator, self.geo_in, dataset, self.fg_field, max_distance=self.max_distance) fg_vals = fg.get_obs_value() minvals = [] maxvals = [] values = [] for o in range(0, len(mask)): minval = fg_vals[mask[o]] - self.negdiff[o] maxval = fg_vals[mask[o]] + self.posdiff[o] minvals.append(minval) maxvals.append(maxval) values.append(dataset.values[mask[o]]) flags = tit.range_check(values, minvals, maxvals) global_flags = dataset.flags for i in range(0, len(mask)): if fg.is_in_grid(mask[i]): if int(global_flags[mask[i]]) == 0 and int(flags[i]) == 1: global_flags[mask[i]] = code else: global_flags[mask[i]] = 199 if self.debug: for i in range(0, len(mask)): print(self.name, i, mask[i], dataset.lons[mask[i]], dataset.lats[mask[i]], minvals[i], values[i], maxvals[i], flags[i], global_flags[mask[i]], fg_vals[mask[i]]) return global_flags
def test(self, dataset, mask, code=1): minvals = [] maxvals = [] values = [] for i in range(0, len(mask)): minvals.append(self.minvals[i]) maxvals.append(self.maxvals[i]) values.append(dataset.values[mask[i]]) global_flags = dataset.flags status, flags = tit.range_check(values, minvals, maxvals) if not status: raise Exception(self.name + " test failed") for i in range(0, len(mask)): if global_flags[mask[i]] == 0 and flags[i] == 1: global_flags[mask[i]] = code if self.debug: for i in range(0, len(mask)): print(self.name, i, mask[i], dataset.values[mask[i]], flags[mask[i]], global_flags[mask[i]]) return global_flags
def test(self, dataset, mask, code=1): # Only first guess file implemented at the moment geo_in, validtime, fg_field, glafs, gelevs = surfex.read_first_guess_netcdf_file( self.fg_file, self.fg_var) lons = [] lats = [] dx = [] dy = [] for i in range(0, len(mask)): lons.append(dataset.lons[mask[i]]) lats.append(dataset.lats[mask[i]]) dx.append(0.5) dy.append(0.5) settings = { "nam_lonlatval": { "xx": lons, "xy": lats, "xdx": dx, "xdy": dy } } geo_out = surfex.LonLatVal(settings) nn = surfex.NearestNeighbour(geo_in, geo_out, distance_check=False) fg_interpolated_field = nn.interpolate(fg_field) minvals = [] maxvals = [] values = [] for o in range(0, len(mask)): fg = fg_interpolated_field[o] minval = fg - self.negdiff[o] maxval = fg + self.posdiff[o] minvals.append(minval) maxvals.append(maxval) values.append(dataset.values[mask[o]]) status, flags = tit.range_check(values, minvals, maxvals) if not status: raise Exception(self.name + " test failed") global_flags = dataset.flags for i in range(0, len(mask)): if nn.distances[i] < self.max_distance: if global_flags[mask[i]] == 0 and flags[i] == 1: global_flags[mask[i]] = code else: global_flags[mask[i]] = 199 if self.debug: for i in range(0, len(mask)): print(self.name, i, mask[i], dataset.lons[mask[i]], dataset.lats[mask[i]], minvals[i], values[i], maxvals[i], flags[i], global_flags[mask[i]], fg_interpolated_field[i]) return global_flags
def test_simple(self): """Check that the test returns the correct flags""" # Min/max inconsistent with inputs values = [4, 2, 2] min = [5, 1, 0] max = [6, 3, 1] flags = titanlib.range_check(values, min, max) np.testing.assert_array_equal(flags, [1, 0, 1])
def run_check(self, filename): """Check that the test doesn't fail""" parameters, data = load(filename) if parameters['test'] == 'range': flags = titanlib.range_check(data['value'], [parameters['min']], [parameters['max']]) self.assertListEqual(list(flags), data['flag'].tolist()) else: raise NotImplementedError
def test_invalid_input(self): """Check that the test fails""" # Min/max inconsistent with inputs with self.assertRaises(RuntimeError): flags = titanlib.range_check(len3, len2, len2)
def test_valid_input(self): """Check that the test doesn't fail""" self.assertEqual(titanlib.range_check(len1, len1, len1)[0],0) self.assertEqual(titanlib.range_check(len3, len1, len1)[0],0) self.assertEqual(titanlib.range_check(len3, len1, len3)[0],0) self.assertEqual(titanlib.range_check(len3, len1, len1)[0],0)