def test_base_lon_wrapping(self): lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. with catch_warnings() as w1: base_def1 = geometry.BaseDefinition(lons1, lats) self.assertFalse( len(w1) != 0, 'Got warning <%s>, but was not expecting one' % w1) lons2 = np.where(lons1 < 0, lons1 + 360, lons1) with catch_warnings() as w2: base_def2 = geometry.BaseDefinition(lons2, lats) self.assertFalse( len(w2) != 1, 'Failed to trigger a warning on longitude wrapping') self.assertFalse(('-180:+180' not in str(w2[0].message)), 'Failed to trigger correct warning about longitude wrapping') self.assertFalse( base_def1 != base_def2, 'longitude wrapping to [-180:+180] did not work') with catch_warnings() as w3: base_def3 = geometry.BaseDefinition(None, None) self.assertFalse( len(w3) != 0, 'Got warning <%s>, but was not expecting one' % w3) self.assert_raises(ValueError, base_def3.get_lonlats)
def test_base_type(self): lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. # Test dtype is preserved without longitude wrapping basedef = geometry.BaseDefinition(lons1, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons1.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons1.dtype, lons.dtype, )) lons1_ints = lons1.astype('int') basedef = geometry.BaseDefinition(lons1_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons1_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons1_ints.dtype, lons.dtype, )) # Test dtype is preserved with automatic longitude wrapping lons2 = np.where(lons1 < 0, lons1 + 360, lons1) with catch_warnings() as w: basedef = geometry.BaseDefinition(lons2, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons2.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons2.dtype, lons.dtype, )) lons2_ints = lons2.astype('int') with catch_warnings() as w: basedef = geometry.BaseDefinition(lons2_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual( lons.dtype, lons2_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % ( lons2_ints.dtype, lons.dtype, ))
def test_gauss_uncert(self): sigma = utils.fwhm2sigma(41627.730557884883) with catch_warnings() as w: res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, self.tgrid, 100000, sigma, with_uncert=True) self.assertTrue(len(w) > 0, 'Failed to create neighbour warning') self.assertTrue((any('Searching' in str(_w.message) for _w in w)), 'Failed to create correct neighbour warning') expected_res = 2.20206560694 expected_stddev = 0.707115076173 expected_count = 3 self.assertAlmostEqual( res[0], expected_res, 5, 'Failed to calculate gaussian weighting with uncertainty') self.assertAlmostEqual( stddev[0], expected_stddev, 5, 'Failed to calculate uncertainty for gaussian weighting') self.assertEqual( count[0], expected_count, 'Wrong data point count for gaussian weighting with uncertainty')
def test_custom_multi(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return numpy.cos(dist) ** 2 data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data_multi, self.area_def, 50000, [wf1, wf2, wf3], segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.842980746 self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel custom resampling failed')
def test_custom_multi(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return np.cos(dist)**2 data = np.fromfunction(lambda y, x: (y + x) * 10**-6, (5000, 100)) lons = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = np.column_stack( (data.ravel(), data.ravel(), data.ravel())) with catch_warnings(UserWarning) as w: res = kd_tree.resample_custom(swath_def, data_multi, self.area_def, 50000, [wf1, wf2, wf3], segments=1) self.assertFalse(len(w) != 1) self.assertFalse('Possible more' not in str(w[0].message)) cross_sum = res.sum() expected = 1461.8428378742638 self.assertAlmostEqual(cross_sum, expected)
def test_self_map_multi(self): data = np.column_stack((self.tb37v, self.tb37v, self.tb37v)) swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) with catch_warnings() as w: res = kd_tree.resample_gauss( swath_def, data, swath_def, radius_of_influence=70000, sigmas=[56500, 56500, 56500] ) self.assertFalse(len(w) != 1, "Failed to create neighbour radius warning") self.assertFalse( ("Possible more" not in str(w[0].message)), "Failed to create correct neighbour radius warning" ) if sys.platform == "darwin": # OSX seems to get slightly different results for `_spatial_mp.Cartesian` truth_value = 668848.144817 else: truth_value = 668848.082208 self.assertAlmostEqual( res[:, 0].sum() / 100.0, truth_value, 1, msg="Failed self mapping swath multi for channel 1" ) self.assertAlmostEqual( res[:, 1].sum() / 100.0, truth_value, 1, msg="Failed self mapping swath multi for channel 2" ) self.assertAlmostEqual( res[:, 2].sum() / 100.0, truth_value, 1, msg="Failed self mapping swath multi for channel 3" )
def test_custom_multi(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return numpy.cos(dist)**2 data = numpy.fromfunction(lambda y, x: (y + x) * 10**-6, (5000, 100)) lons = numpy.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack( (data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data_multi, self.area_def, 50000, [wf1, wf2, wf3], segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse( ('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.842980746 self.assertAlmostEqual( cross_sum, expected, msg='Swath multi channel custom resampling failed')
def test_custom(self): def wf(dist): return 1 - dist / 100000.0 data = np.fromfunction(lambda y, x: (y + x) * 10**-5, (5000, 100)) lons = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings(UserWarning) as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) # PyProj proj/CRS and "more than 8 neighbours" are warned about self.assertFalse(len(w) > 2) neighbour_warn = False for warn in w: if 'Possible more' in str(warn.message): neighbour_warn = True break self.assertTrue(neighbour_warn) if len(w) == 2: proj_crs_warn = False for warn in w: if 'important projection information' in str(warn.message): proj_crs_warn = True break self.assertTrue(proj_crs_warn) cross_sum = res.sum() expected = 4872.8100347930776 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_multi_uncert(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: # The assertion below checks if there is only one warning raised # and whether it contains a specific message from pyresample # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings # that cause to fail, so we ignore the unrelated warnings. res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], segments=1, with_uncert=True) self.assertTrue(len(w) >= 1) self.assertTrue( any(['Possible more' in str(x.message) for x in w])) cross_sum = res.sum() cross_sum_counts = counts.sum() expected = 1461.8429990248171 expected_stddev = [0.44621800779801657, 0.44363137712896705, 0.43861019464274459] expected_counts = 4934802.0 self.assertTrue(res.shape == stddev.shape and stddev.shape == counts.shape and counts.shape == (800, 800, 3)) self.assertAlmostEqual(cross_sum, expected) for i, e_stddev in enumerate(expected_stddev): cross_sum_stddev = stddev[:, :, i].sum() self.assertAlmostEqual(cross_sum_stddev, e_stddev) self.assertAlmostEqual(cross_sum_counts, expected_counts)
def test_custom_multi(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return np.cos(dist) ** 2 data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = np.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = np.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings(UserWarning) as w: res = kd_tree.resample_custom(swath_def, data_multi, self.area_def, 50000, [wf1, wf2, wf3], segments=1) self.assertFalse(len(w) != 1) self.assertFalse('Possible more' not in str(w[0].message)) cross_sum = res.sum() expected = 1461.8428378742638 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_multi_uncert(self): data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = np.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = np.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings(UserWarning) as w: # The assertion below checks if there is only one warning raised # and whether it contains a specific message from pyresample # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings # that cause to fail, so we ignore the unrelated warnings. res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], segments=1, with_uncert=True) self.assertTrue(len(w) >= 1) self.assertTrue( any(['Possible more' in str(x.message) for x in w])) cross_sum = res.sum() cross_sum_counts = counts.sum() expected = 1461.8429990248171 expected_stddev = [0.44621800779801657, 0.44363137712896705, 0.43861019464274459] expected_counts = 4934802.0 self.assertTrue(res.shape == stddev.shape and stddev.shape == counts.shape and counts.shape == (800, 800, 3)) self.assertAlmostEqual(cross_sum, expected) for i, e_stddev in enumerate(expected_stddev): cross_sum_stddev = stddev[:, :, i].sum() self.assertAlmostEqual(cross_sum_stddev, e_stddev) self.assertAlmostEqual(cross_sum_counts, expected_counts)
def test_custom(self): def wf(dist): return 1 - dist / 100000.0 data = numpy.fromfunction(lambda y, x: (y + x) * 10**-5, (5000, 100)) lons = numpy.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse( ('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050729 self.assertAlmostEqual(cross_sum, expected, msg='Swath custom resampling failed')
def test_gauss_multi_mp_segments(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10**-6, (5000, 100)) lons = numpy.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack( (data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], nprocs=2, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse( ('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.84313918 self.assertAlmostEqual( cross_sum, expected, msg='Swath multi channel segments resampling gauss failed')
def test_gauss_base(self): with catch_warnings(UserWarning) as w: res = kd_tree.resample_gauss(self.tswath, self.tdata.ravel(), self.tgrid, 50000, 25000, reduce_data=False, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Searching' not in str(w[0].message))) self.assertAlmostEqual(res[0], 2.2020729, 5)
def test_gauss_base(self): with catch_warnings() as w: res = kd_tree.resample_gauss(self.tswath, self.tdata.ravel(), self.tgrid, 50000, 25000, reduce_data=False, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Searching' not in str(w[0].message))) self.assertAlmostEqual(res[0], 2.2020729, 5)
def test_custom_base(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings(UserWarning) as w: res = kd_tree.resample_custom(self.tswath, self.tdata.ravel(), self.tgrid, 50000, wf, reduce_data=False, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Searching' not in str(w[0].message))) self.assertAlmostEqual(res[0], 2.4356757, 5)
def test_gauss_base(self): with catch_warnings() as w: res = kd_tree.resample_gauss(self.tswath, self.tdata.ravel(), self.tgrid, 50000, 25000, reduce_data=False, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour warning') self.assertFalse(('Searching' not in str( w[0].message)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.2020729, 5, 'Failed to calculate gaussian weighting')
def test_custom_base(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings() as w: res = kd_tree.resample_custom(self.tswath, self.tdata.ravel(), self.tgrid, 50000, wf, reduce_data=False, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Searching' not in str(w[0].message))) self.assertAlmostEqual(res[0], 2.4356757, 5)
def test_custom_multi_from_sample(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return np.cos(dist)**2 data = np.fromfunction(lambda y, x: (y + x) * 10**-6, (5000, 100)) lons = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = np.column_stack( (data.ravel(), data.ravel(), data.ravel())) with catch_warnings(UserWarning) as w: valid_input_index, valid_output_index, index_array, distance_array = \ kd_tree.get_neighbour_info(swath_def, self.area_def, 50000, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) res = kd_tree.get_sample_from_neighbour_info( 'custom', (800, 800), data_multi, valid_input_index, valid_output_index, index_array, distance_array, weight_funcs=[wf1, wf2, wf3]) cross_sum = res.sum() expected = 1461.8428378742638 self.assertAlmostEqual(cross_sum, expected) res = kd_tree.get_sample_from_neighbour_info( 'custom', (800, 800), data_multi, valid_input_index, valid_output_index, index_array, distance_array, weight_funcs=[wf1, wf2, wf3]) # Look for error where input data has been manipulated cross_sum = res.sum() expected = 1461.8428378742638 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_multi_uncert(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10**-6, (5000, 100)) lons = numpy.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack( (data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: # The assertion below checks if there is only one warning raised # and whether it contains a specific message from pyresample # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings # that cause to fail, so we ignore the unrelated warnings. res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], segments=1, with_uncert=True) self.assertTrue( len(w) >= 1, 'Failed to create neighbour radius warning') self.assertTrue( any(['Possible more' in str(x.message) for x in w]), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() cross_sum_counts = counts.sum() expected = 1461.84313918 expected_stddev = [0.446193170875, 0.443606880035, 0.438586349519] expected_counts = 4934802.0 self.assertTrue(res.shape == stddev.shape and stddev.shape == counts.shape and counts.shape == (800, 800, 3)) self.assertAlmostEqual( cross_sum, expected, msg='Swath multi channel resampling gauss failed on data') for i, e_stddev in enumerate(expected_stddev): cross_sum_stddev = stddev[:, :, i].sum() print(cross_sum_stddev, e_stddev) self.assertAlmostEqual( cross_sum_stddev, e_stddev, msg= 'Swath multi channel resampling gauss failed on stddev (channel {})' .format(i)) self.assertAlmostEqual( cross_sum_counts, expected_counts, msg='Swath multi channel resampling gauss failed on counts')
def test_custom_multi_from_sample(self): def wf1(dist): return 1 - dist / 100000.0 def wf2(dist): return 1 def wf3(dist): return numpy.cos(dist) ** 2 data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: valid_input_index, valid_output_index, index_array, distance_array = \ kd_tree.get_neighbour_info(swath_def, self.area_def, 50000, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') res = kd_tree.get_sample_from_neighbour_info('custom', (800, 800), data_multi, valid_input_index, valid_output_index, index_array, distance_array, weight_funcs=[wf1, wf2, wf3]) cross_sum = res.sum() expected = 1461.842980746 self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel custom resampling from neighbour info failed 1') res = kd_tree.get_sample_from_neighbour_info('custom', (800, 800), data_multi, valid_input_index, valid_output_index, index_array, distance_array, weight_funcs=[wf1, wf2, wf3]) # Look for error where input data has been manipulated cross_sum = res.sum() expected = 1461.842980746 self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel custom resampling from neighbour info failed 2')
def test_custom_uncert(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings() as w: res, stddev, counts = kd_tree.resample_custom(self.tswath, self.tdata, self.tgrid, 100000, wf, with_uncert=True) self.assertTrue(len(w) > 0) self.assertTrue((any('Searching' in str(_w.message) for _w in w))) self.assertAlmostEqual(res[0], 2.32193149, 5) self.assertAlmostEqual(stddev[0], 0.81817972, 5) self.assertEqual(counts[0], 3)
def test_custom_uncert(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings(UserWarning) as w: res, stddev, counts = kd_tree.resample_custom(self.tswath, self.tdata, self.tgrid, 100000, wf, with_uncert=True) self.assertTrue(len(w) > 0) self.assertTrue((any('Searching' in str(_w.message) for _w in w))) self.assertAlmostEqual(res[0], 2.32193149, 5) self.assertAlmostEqual(stddev[0], 0.81817972, 5) self.assertEqual(counts[0], 3)
def test_custom_base(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings() as w: res = kd_tree.resample_custom(self.tswath, self.tdata.ravel(), self.tgrid, 50000, wf, reduce_data=False, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour warning') self.assertFalse(('Searching' not in str( w[0].message)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.4356757, 5, 'Failed to calculate custom weighting')
def test_gauss_uncert(self): sigma = utils.fwhm2sigma(41627.730557884883) with catch_warnings() as w: res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, self.tgrid, 100000, sigma, with_uncert=True) self.assertTrue(len(w) > 0) self.assertTrue((any('Searching' in str(_w.message) for _w in w))) expected_res = 2.20206560694 expected_stddev = 0.707115076173 expected_count = 3 self.assertAlmostEqual(res[0], expected_res, 5) self.assertAlmostEqual(stddev[0], expected_stddev, 5) self.assertEqual(count[0], expected_count)
def test_base_type(self): lons1 = np.arange(-135., +135, 50.) lats = np.ones_like(lons1) * 70. # Test dtype is preserved without longitude wrapping basedef = geometry.BaseDefinition(lons1, lats) lons, _ = basedef.get_lonlats() self.assertEqual(lons.dtype, lons1.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % (lons1.dtype, lons.dtype,)) lons1_ints = lons1.astype('int') basedef = geometry.BaseDefinition(lons1_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual(lons.dtype, lons1_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % (lons1_ints.dtype, lons.dtype,)) # Test dtype is preserved with automatic longitude wrapping lons2 = np.where(lons1 < 0, lons1 + 360, lons1) with catch_warnings() as w: basedef = geometry.BaseDefinition(lons2, lats) lons, _ = basedef.get_lonlats() self.assertEqual(lons.dtype, lons2.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % (lons2.dtype, lons.dtype,)) lons2_ints = lons2.astype('int') with catch_warnings() as w: basedef = geometry.BaseDefinition(lons2_ints, lats) lons, _ = basedef.get_lonlats() self.assertEqual(lons.dtype, lons2_ints.dtype, "BaseDefinition did not maintain dtype of longitudes (in:%s out:%s)" % (lons2_ints.dtype, lons.dtype,))
def test_gauss_uncert(self): sigma = utils.fwhm2sigma(41627.730557884883) with catch_warnings(UserWarning) as w: res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, self.tgrid, 100000, sigma, with_uncert=True) self.assertTrue(len(w) > 0) self.assertTrue((any('Searching' in str(_w.message) for _w in w))) expected_res = 2.20206560694 expected_stddev = 0.707115076173 expected_count = 3 self.assertAlmostEqual(res[0], expected_res, 5) self.assertAlmostEqual(stddev[0], expected_stddev, 5) self.assertEqual(count[0], expected_count)
def test_gauss_fwhm(self): data = np.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100)) lons = np.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings(UserWarning) as w: res = kd_tree.resample_gauss(swath_def, data.ravel(), self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 4872.8100353517921 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_fwhm(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data.ravel(), self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 4872.8100353517921 self.assertAlmostEqual(cross_sum, expected)
def test_self_map(self): swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def, radius_of_influence=70000, sigmas=56500) self.assertEqual(len(w), 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') # only compare the whole number as different OSes and versions of numpy # can produce slightly different results truth_value = 668848.0 self.assertAlmostEqual(res.sum() / 100., truth_value, 0, msg='Failed self mapping swath for 1 channel')
def test_gauss_multi(self): data = np.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = np.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = np.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings(UserWarning) as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 1461.8429990248171 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_multi(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 1461.8429990248171 self.assertAlmostEqual(cross_sum, expected)
def test_custom(self): def wf(dist): return 1 - dist / 100000.0 data = np.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100)) lons = np.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings(UserWarning) as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 4872.8100347930776 self.assertAlmostEqual(cross_sum, expected)
def test_gauss_fwhm(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data.ravel(), self.area_def, 50000, utils.fwhm2sigma(41627.730557884883), segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050892 self.assertAlmostEqual(cross_sum, expected, msg='Swath resampling gauss failed')
def test_custom_uncert(self): def wf(dist): return 1 - dist / 100000.0 with catch_warnings() as w: res, stddev, counts = kd_tree.resample_custom(self.tswath, self.tdata, self.tgrid, 100000, wf, with_uncert=True) self.assertTrue( len(w) > 0, 'Failed to create neighbour warning') self.assertTrue((any('Searching' in str(_w.message) for _w in w)), 'Failed to create correct neighbour warning') self.assertAlmostEqual(res[0], 2.32193149, 5, 'Failed to calculate custom weighting with uncertainty') self.assertAlmostEqual(stddev[0], 0.81817972, 5, 'Failed to calculate custom for gaussian weighting') self.assertEqual( counts[0], 3, 'Wrong data point count for custom weighting with uncertainty')
def test_self_map_multi(self): data = np.column_stack((self.tb37v, self.tb37v, self.tb37v)) swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data, swath_def, radius_of_influence=70000, sigmas=[56500, 56500, 56500]) self.assertEqual(len(w), 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') truth_value = 668848.0 self.assertAlmostEqual(res[:, 0].sum() / 100., truth_value, 0, msg='Failed self mapping swath multi for channel 1') self.assertAlmostEqual(res[:, 1].sum() / 100., truth_value, 0, msg='Failed self mapping swath multi for channel 2') self.assertAlmostEqual(res[:, 2].sum() / 100., truth_value, 0, msg='Failed self mapping swath multi for channel 3')
def test_swath_wrap(self): lons1 = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats1 = np.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) lons1 += 180. with catch_warnings() as w1: swath_def = geometry.BaseDefinition(lons1, lats1) self.assertFalse( len(w1) != 1, 'Failed to trigger a warning on longitude wrapping') self.assertFalse(('-180:+180' not in str(w1[0].message)), 'Failed to trigger correct warning about longitude wrapping') lons2, lats2 = swath_def.get_lonlats() self.assertTrue(id(lons1) != id(lons2), msg='Caching of swath coordinates failed with longitude wrapping') self.assertTrue(lons2.min() > -180 and lons2.max() < 180, 'Wrapping of longitudes failed for SwathDefinition')
def test_gauss_multi(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [25000, 15000, 10000], segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 1461.84313918 self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel resampling gauss failed')
def test_gauss_uncert(self): sigma = utils.fwhm2sigma(41627.730557884883) with catch_warnings() as w: res, stddev, count = kd_tree.resample_gauss(self.tswath, self.tdata, self.tgrid, 100000, sigma, with_uncert=True) self.assertTrue( len(w) > 0, 'Failed to create neighbour warning') self.assertTrue((any('Searching' in str(_w.message) for _w in w)), 'Failed to create correct neighbour warning') expected_res = 2.20206560694 expected_stddev = 0.707115076173 expected_count = 3 self.assertAlmostEqual(res[0], expected_res, 5, 'Failed to calculate gaussian weighting with uncertainty') self.assertAlmostEqual(stddev[0], expected_stddev, 5, 'Failed to calculate uncertainty for gaussian weighting') self.assertEqual( count[0], expected_count, 'Wrong data point count for gaussian weighting with uncertainty')
def test_custom(self): def wf(dist): return 1 - dist / 100000.0 data = np.fromfunction(lambda y, x: (y + x) * 10**-5, (5000, 100)) lons = np.fromfunction(lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = np.fromfunction(lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) self.assertFalse(len(w) != 1) self.assertFalse(('Possible more' not in str(w[0].message))) cross_sum = res.sum() expected = 4872.8100347930776 self.assertAlmostEqual(cross_sum, expected)
def test_custom(self): def wf(dist): return 1 - dist / 100000.0 data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -5, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) with catch_warnings() as w: res = kd_tree.resample_custom(swath_def, data.ravel(), self.area_def, 50000, wf, segments=1) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse(('Possible more' not in str( w[0].message)), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() expected = 4872.81050729 self.assertAlmostEqual(cross_sum, expected, msg='Swath custom resampling failed')
def test_self_map(self): swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, self.tb37v.copy(), swath_def, radius_of_influence=70000, sigmas=56500) self.assertFalse( len(w) != 1, 'Failed to create neighbour radius warning') self.assertFalse( ('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning') if sys.platform == 'darwin': # OSX seems to get slightly different results for `_spatial_mp.Cartesian` truth_value = 668848.144817 else: truth_value = 668848.082208 self.assertAlmostEqual(res.sum() / 100., truth_value, 1, msg='Failed self mapping swath for 1 channel')
def test_object_type_with_warnings(self, area_def_stere_source, area_def_stere_target, input_data): """Test that providing certain input data causes a warning.""" resampler = KDTreeNearestXarrayResampler(area_def_stere_source, area_def_stere_target) with catch_warnings( PerformanceWarning) as w, assert_maximum_dask_computes(1): res = resampler.resample(input_data) assert type(res) is type(input_data) _check_common_metadata( res, isinstance(area_def_stere_target, AreaDefinition)) is_data_arr_dask = isinstance(input_data, xr.DataArray) and isinstance( input_data.data, da.Array) is_dask_based = isinstance(input_data, da.Array) or is_data_arr_dask if is_dask_based: assert not w else: assert_warnings_contain(w, "will be converted") res = np.array(res) cross_sum = np.nansum(res) expected = 952386.0 # same as 'test_nearest_area_2d_to_area_1n_no_roi' assert cross_sum == expected assert res.shape == resampler.target_geo_def.shape
def test_gauss_multi_uncert(self): data = numpy.fromfunction(lambda y, x: (y + x) * 10 ** -6, (5000, 100)) lons = numpy.fromfunction( lambda y, x: 3 + (10.0 / 100) * x, (5000, 100)) lats = numpy.fromfunction( lambda y, x: 75 - (50.0 / 5000) * y, (5000, 100)) swath_def = geometry.SwathDefinition(lons=lons, lats=lats) data_multi = numpy.column_stack((data.ravel(), data.ravel(), data.ravel())) with catch_warnings() as w: # The assertion below checks if there is only one warning raised # and whether it contains a specific message from pyresample # On python 2.7.9+ the resample_gauss method raises multiple deprecation warnings # that cause to fail, so we ignore the unrelated warnings. res, stddev, counts = kd_tree.resample_gauss(swath_def, data_multi, self.area_def, 50000, [ 25000, 15000, 10000], segments=1, with_uncert=True) self.assertTrue( len(w) >= 1, 'Failed to create neighbour radius warning') self.assertTrue(any(['Possible more' in str( x.message) for x in w]), 'Failed to create correct neighbour radius warning') cross_sum = res.sum() cross_sum_stddev = stddev.sum() cross_sum_counts = counts.sum() expected = 1461.84313918 expected_stddev = 0.446204424799 expected_counts = 4934802.0 self.assertTrue(res.shape == stddev.shape and stddev.shape == counts.shape and counts.shape == (800, 800, 3)) self.assertAlmostEqual(cross_sum, expected, msg='Swath multi channel resampling gauss failed on data') self.assertAlmostEqual(cross_sum_stddev, expected_stddev, msg='Swath multi channel resampling gauss failed on stddev') self.assertAlmostEqual(cross_sum_counts, expected_counts, msg='Swath multi channel resampling gauss failed on counts')
def test_self_map_multi(self): data = np.column_stack((self.tb37v, self.tb37v, self.tb37v)) swath_def = geometry.SwathDefinition(lons=self.lons, lats=self.lats) with catch_warnings() as w: res = kd_tree.resample_gauss(swath_def, data, swath_def, radius_of_influence=70000, sigmas=[56500, 56500, 56500]) self.assertEqual(len(w), 1, 'Failed to create neighbour radius warning') self.assertFalse( ('Possible more' not in str(w[0].message)), 'Failed to create correct neighbour radius warning') if sys.platform == 'darwin': # OSX seems to get slightly different results for `_spatial_mp.Cartesian` truth_value = 668848.144817 else: truth_value = 668848.082208 self.assertAlmostEqual( res[:, 0].sum() / 100., truth_value, 1, msg='Failed self mapping swath multi for channel 1') self.assertAlmostEqual( res[:, 1].sum() / 100., truth_value, 1, msg='Failed self mapping swath multi for channel 2') self.assertAlmostEqual( res[:, 2].sum() / 100., truth_value, 1, msg='Failed self mapping swath multi for channel 3')