def compute_flags(self, lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype): self.check = QualityCheck(lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype) return self.check.check_quality()
def setUp(self): lswt = numpy.ones((4, 4)) * 293 visible = numpy.ones((4, 4)) * 3.0 nir = numpy.ones((4, 4)) * 0.25 bt3 = numpy.ones((4, 4)) * 294 lower_bt = numpy.ones((4, 4)) * 295 upper_bt = numpy.ones((4, 4)) * 294 sat_za = numpy.ones((4, 4)) * 20 sun_za = numpy.ones((4, 4)) * 40 rel_az = numpy.ones((4, 4)) * 100 lwm = numpy.ones((4, 4)) cmsk = numpy.zeros((4, 4)) self.check = QualityCheck(lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, 4, 4, 'AVHRR')
def test_spatial_stddev(self): in_data = [[5, 2, 3, 1], [5, 2, 3, 1], [5, 2, 3, 1], [5, 2, 3, 1]] in_data = numpy.array(in_data, dtype=numpy.float32) in_data[0, 0:2] = numpy.nan stdv = QualityCheck.spatial_stddev(in_data, 3) testing.assert_allclose( numpy.array([[numpy.nan, numpy.nan, numpy.nan, 0.99999994], [numpy.nan, numpy.nan, numpy.nan, 0.99999994], [1.5, 1.32287566, 0.8660254, 0.99999994], [1.5, 1.32287566, 0.8660254, 0.99999994]]), stdv.tolist())
class MonoWindowAlgo: def __init__(self, a0, a1, needs_lut): self.needs_lut = needs_lut if not needs_lut: self.a0 = a0 self.a1 = a1 else: season = numpy.array(range( 0, 4), dtype=numpy.float64) # spring, summer, autumn, winter height = numpy.array([400, 800, 1000]) # height in meter zenith = numpy.array(range( 5, 56, 5), dtype=numpy.float64) # 5 - 55deg in 5deg steps dimensions = [season, height, zenith] mono_coeff = Mono() values = numpy.array(mono_coeff.values, dtype=numpy.float64) self.lut = LookupTable(values, dimensions, 2) self.check = None def get_season(self, start_date): utils = Utils() return utils.get_season(start_date) def compute_lswt(self, data, season, height, zenith_data): if not self.needs_lut: return self.a0 * data + self.a1 else: lswt = numpy.zeros(numpy.shape(data)) lswt[numpy.where(numpy.isnan(data))] = Float.NaN season = season[numpy.where(~numpy.isnan(data))] height = height[numpy.where(~numpy.isnan(data))] zenith_data = zenith_data[numpy.where(~numpy.isnan(data))] size = numpy.shape(season) if size != (0, ): if len(size) == 2: (shape_x, shape_y) = size result = self.lut.getValues([ season[::2, ::2], height[::2, ::2], zenith_data[::2, ::2] ]) [a0, a1] = result a0 = numpy.repeat(numpy.repeat(a0, 2, axis=0), 2, axis=1) a1 = numpy.repeat(numpy.repeat(a1, 2, axis=0), 2, axis=1) a0 = a0[:shape_x, :shape_y] a1 = a1[:shape_x, :shape_y] elif len(size) == 1: (shape_x, ) = size result = self.lut.getValues( [season[::2], height[::2], zenith_data[::2]]) [a0, a1] = result a0 = numpy.repeat(a0, 2) a1 = numpy.repeat(a1, 2) a0 = a0[:shape_x] a1 = a1[:shape_x] lswt[numpy.where(~numpy.isnan( data))] = a0 * data[numpy.where(~numpy.isnan(data))] + a1 return lswt def compute_flags(self, lswt, visible, nir, bt, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype): self.check = QualityCheck(lswt, visible, nir, None, bt, None, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype) return self.check.check_quality() def compute_quality_index(self, flags): return self.check.get_quality_mask(flags)
class SplitWindowAlgo: def __init__(self, a0, a1, a2, a3, needs_lut): if needs_lut: self.has_lut = True season = numpy.array(range( 0, 4), dtype=numpy.float64) # spring, summer, autumn, winter height = numpy.array([400, 800, 1000]) # height in meter zenith = numpy.array(range( 5, 56, 5), dtype=numpy.float64) # 5 - 55deg in 5deg steps dimensions = [season, height, zenith] # TODO needs to be loaded from file! values = numpy.tile([3.079654, 0.988938, 1.867209, -0.434251], len(season) * len(zenith) * len(height)) self.lut = LookupTable(values, dimensions, 4) else: self.has_lut = False self.a0 = a0 self.a1 = a1 self.a2 = a2 self.a3 = a3 self.check = None def get_season(self, start_date): utils = Utils() return utils.get_season(start_date) def compute_lswt_lut(self, lower_data, upper_data, zenith_data, season, height): lswt = numpy.zeros(numpy.shape(lower_data)) lswt[numpy.where( numpy.logical_or(numpy.isnan(lower_data), numpy.isnan(upper_data)))] = Float.NaN season = season[numpy.where( numpy.logical_and(~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))] zenith_data = zenith_data[numpy.where( numpy.logical_and(~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))] height = height[numpy.where( numpy.logical_and(~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))] size = numpy.shape(season) if size != (0, ): [self.a0, self.a1, self.a2, self.a3] = self.lut.getValues([season, height, zenith_data]) lswt[numpy.where( numpy.logical_and( ~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))] = self.compute_lswt( lower_data[numpy.where( numpy.logical_and(~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))], upper_data[numpy.where( numpy.logical_and(~numpy.isnan(lower_data), ~numpy.isnan(upper_data)))], zenith_data) return lswt def compute_lswt(self, lower_data, upper_data, zenith_data): lswt = self.a0 + self.a1 * lower_data + self.a2 * (lower_data - upper_data) + \ self.a3 * (1 - 1 / (numpy.cos(zenith_data * (numpy.math.pi / 180.0)))) * (lower_data - upper_data) return lswt def compute_flags(self, lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype): self.check = QualityCheck(lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, height, width, sattype) return self.check.check_quality() def compute_quality_index(self, flags): return self.check.get_quality_mask(flags)
class TestLSWTQualityCheck(unittest.TestCase): def setUp(self): lswt = numpy.ones((4, 4)) * 293 visible = numpy.ones((4, 4)) * 3.0 nir = numpy.ones((4, 4)) * 0.25 bt3 = numpy.ones((4, 4)) * 294 lower_bt = numpy.ones((4, 4)) * 295 upper_bt = numpy.ones((4, 4)) * 294 sat_za = numpy.ones((4, 4)) * 20 sun_za = numpy.ones((4, 4)) * 40 rel_az = numpy.ones((4, 4)) * 100 lwm = numpy.ones((4, 4)) cmsk = numpy.zeros((4, 4)) self.check = QualityCheck(lswt, visible, nir, bt3, lower_bt, upper_bt, sat_za, sun_za, rel_az, lwm, cmsk, 4, 4, 'AVHRR') def test_spatial_stddev(self): in_data = [[5, 2, 3, 1], [5, 2, 3, 1], [5, 2, 3, 1], [5, 2, 3, 1]] in_data = numpy.array(in_data, dtype=numpy.float32) in_data[0, 0:2] = numpy.nan stdv = QualityCheck.spatial_stddev(in_data, 3) testing.assert_allclose( numpy.array([[numpy.nan, numpy.nan, numpy.nan, 0.99999994], [numpy.nan, numpy.nan, numpy.nan, 0.99999994], [1.5, 1.32287566, 0.8660254, 0.99999994], [1.5, 1.32287566, 0.8660254, 0.99999994]]), stdv.tolist()) def test_get_quality_flag_Q8(self): # 0: 0000 0000 0000 flag = numpy.ones(1) * 0 result = self.check.get_quality_mask(flag) self.assertEqual(8, result[0], 'Quality level should be 8 for 0') def test_get_quality_flag_Q7(self): # 1024: 0100 0000 0000 flag = numpy.ones(1) * 1024 result = self.check.get_quality_mask(flag) self.assertEqual(7, result[0], 'Quality level should be 7 for 1024') def test_get_quality_flag_Q6(self): # 512: 0010 0000 0000 flag = numpy.ones(1) * 512 result = self.check.get_quality_mask(flag) self.assertEqual(6, result[0], 'Quality level should be 6 for 512') def test_get_quality_flag_Q5(self): # 2048: 1000 0000 0000 flag = numpy.ones(1) * 2048 result = self.check.get_quality_mask(flag) self.assertEqual(5, result[0], 'Quality level should be 5 for 2048') def test_get_quality_flag_Q4(self): # 3072: 1100 0000 0000 flag = numpy.ones(1) * 3072 result = self.check.get_quality_mask(flag) self.assertEqual(4, result[0], 'Quality level should be 4 for 3072') def test_get_quality_flag_Q3(self): # 2560: 1010 0000 0000 flag = numpy.ones(1) * 2560 result = self.check.get_quality_mask(flag) self.assertEqual(3, result[0], 'Quality level should be 3 for 2560') def test_get_quality_flag_Q2(self): # 1536: 0110 0000 0000 # 3584: 1110 0000 0000 for i in [1536, 3584]: flag = numpy.ones(1) * i result = self.check.get_quality_mask(flag) self.assertEqual(2, result[0], 'Quality level should be 2 for ' + str(i)) def test_get_quality_flag_Q1(self): # 256: 0001 0000 0000 # 768: 0011 0000 0000 # 1280: 0101 0000 0000 # 1792: 0111 0000 0000 # 2304: 1001 0000 0000 # 2816: 1011 0000 0000 # 3328: 1101 0000 0000 # 3840: 1111 0000 0000 for i in [256, 768, 1280, 1792, 2304, 2816, 3328, 3840]: flag = numpy.ones(1) * i result = self.check.get_quality_mask(flag) self.assertEqual(1, result[0], 'Quality level should be 1 for ' + str(i)) # 3584: 1110 0000 0000 # 0: 0000 0000 0000 # 4095: 1111 1111 1111 for i in [3584, 0, 4095]: flag = numpy.ones(1) * i result = self.check.get_quality_mask(flag) self.assertNotEqual(1, result[0], 'Quality level should NOT be 1 for ' + str(i)) def test_get_quality_flag_Q0(self): # 255: 00000 1111 1111 # everything below should be quality level 0 for i in range(1, 256): flag = numpy.ones(1) * i result = self.check.get_quality_mask(flag) self.assertEqual(0, result[0], 'Quality level should be 0 for ' + str(i)) # 3855: 1111 0000 1111 -> also Q0 # 257: 0001 0000 0001 flag = numpy.ones(1) * 3855 result = self.check.get_quality_mask(flag) self.assertEqual(0, result[0], 'Quality level should be 0 for 3855') # 256: 0001 0000 0000 -> not Q0 flag = numpy.ones(1) * 256 result = self.check.get_quality_mask(flag) self.assertNotEqual(0, result[0], 'Quality level should NOT be 0 for 256')