def test_project_array(self, npload, gsfni, marray): """Test the project_array function. """ in_area_id = random_string(20) out_area_id = random_string(20) data = np.random.standard_normal((3, 1)) utils.parse_area_file.return_value.__getitem__.side_effect = [ "a", "b", "c", "d" ] # test quick self.proj = Projector(in_area_id, out_area_id, mode="quick") self.proj.project_array(data) mpop.projector.image.ImageContainer.assert_called_with(data, "a", fill_value=None) mpop.projector.image.ImageContainer.return_value.\ get_array_from_linesample.assert_called_with( self.proj._cache["row_idx"], self.proj._cache["col_idx"]) marray.assert_called_once_with( mpop.projector.image.ImageContainer.return_value. get_array_from_linesample.return_value, dtype=np.dtype('float64')) # test nearest in_area = MagicMock() out_area = MagicMock() utils.parse_area_file.return_value.__getitem__.side_effect = [ in_area, out_area ] self.proj = Projector(in_area_id, out_area_id, mode="nearest") self.proj.project_array(data) mpop.projector.kd_tree.get_sample_from_neighbour_info.\ assert_called_with('nn', out_area.shape, data, npload.return_value.__getitem__.return_value, npload.return_value.__getitem__.return_value, npload.return_value.__getitem__.return_value, fill_value=None)
def test_init(self, gah, gni, gqla, bil_info): """Creation of coverage. """ # in case of wrong number of arguments self.assertRaises(TypeError, Projector) self.assertRaises(TypeError, Projector, random_string(20)) # in case of string arguments in_area_id = random_string(20) out_area_id = random_string(20) area_type = utils.parse_area_file.return_value.__getitem__.return_value gni.side_effect = [("a", "b", "c", "d")] * 10 self.proj = Projector(in_area_id, out_area_id) self.assertEquals(utils.parse_area_file.call_count, 2) area_file = mpop.projector.get_area_file() utils.parse_area_file.assert_any_call(area_file, in_area_id) utils.parse_area_file.assert_any_call(area_file, out_area_id) self.assertEquals(self.proj.in_area, area_type) self.assertEquals(self.proj.out_area, area_type) # in case of undefined areas mock = MagicMock(side_effect=Exception("raise")) with patch.object(utils, 'parse_area_file', mock): self.assertRaises(Exception, Projector, "raise", random_string(20)) self.assertRaises(Exception, Projector, random_string(20), "raise") # in case of geometry objects as input with patch.object(utils, 'AreaNotFound', Exception): mock = MagicMock( side_effect=[utils.AreaNotFound("raise"), MagicMock()]) with patch.object(utils, 'parse_area_file', mock): in_area = geometry.AreaDefinition() self.proj = Projector(in_area, out_area_id) self.assertEquals(self.proj.in_area, in_area) in_area = geometry.SwathDefinition() utils.parse_area_file.return_value.__getitem__.side_effect = [ AttributeError, out_area_id ] self.proj = Projector(in_area, out_area_id) self.assertEquals(self.proj.in_area, in_area) out_area = geometry.AreaDefinition() utils.parse_area_file.return_value.__getitem__.side_effect = [ in_area_id, AttributeError ] self.proj = Projector(in_area_id, out_area) self.assertEquals(self.proj.out_area, out_area) # in case of lon/lat is input utils.parse_area_file.return_value.__getitem__.side_effect = [ AttributeError, out_area_id ] lonlats = ("great_lons", "even_greater_lats") self.proj = Projector("raise", out_area_id, lonlats) geometry.SwathDefinition.assert_called_with(lons=lonlats[0], lats=lonlats[1]) utils.parse_area_file.return_value.__getitem__.side_effect = None # in case of wrong mode self.assertRaises(ValueError, Projector, random_string(20), random_string(20), mode=random_string(20)) utils.parse_area_file.return_value.__getitem__.side_effect = [ "a", "b", "c", "d" ] gqla.side_effect = [("ridx", "cidx")] # quick mode cache self.proj = Projector(in_area_id, out_area_id, mode="quick") cache = getattr(self.proj, "_cache") self.assertTrue(cache['row_idx'] is not None) self.assertTrue(cache['col_idx'] is not None) # nearest mode cache self.proj = Projector(in_area_id, out_area_id, mode="nearest") cache = getattr(self.proj, "_cache") self.assertTrue(cache['valid_index'] is not None) self.assertTrue(cache['valid_output_index'] is not None) self.assertTrue(cache['index_array'] is not None) # bilinear mode cache bil_info.return_value = (1, 2, 3, 4) def spam(val): return 'adef' with patch.object(mpop.projector, 'get_area_def', spam): self.proj = Projector(in_area_id, out_area_id, mode="bilinear") cache = getattr(self.proj, "_cache") self.assertTrue(cache['bilinear_t'] is not None) self.assertTrue(cache['bilinear_s'] is not None) self.assertTrue(cache['input_idxs'] is not None) self.assertTrue(cache['idx_arr'] is not None)
#outfile = 'black_marble_C1_'+area+'.jpg' #bkg_file = "/data/COALITION2/database/Land/blue_marble/BlackMarble_2016_3km_geo.13500x6750.tif" #area_bkg = get_area_def('world_plat_13500_6750') #outfile = 'black_marble_'+area+'.jpg' # # # # # # area_target = get_area_def(area) projector = Projector(area_bkg, area_target, mode="nearest") background = Image.open(bkg_file) data = background.split() r = projector.project_array(np.array(data[0])) g = projector.project_array(np.array(data[1])) b = projector.project_array(np.array(data[2])) pil_r = Image.fromarray(r) pil_g = Image.fromarray(g) pil_b = Image.fromarray(b) background = Image.merge("RGB", (pil_r, pil_g, pil_b)) background.save(outfile) print("display " + outfile + " &")