# Tests to ensure that coordinate retrieval methods compatibel import tempfile import unittest from itertools import combinations from pathlib import Path import numpy as np from scipy.io import readsav from kis_tools.gris.GrisDay import GrisDay from kis_tools.gris.GrisMapExtractor import GrisMapExtractor from kis_tools.gris.GrisRun import GrisRun class TestCoords(unittest.TestCase): # how well aligned should the coordinates be? def test_coords(self): """There were different methods for retrieving the coordinates from l1 files as well as the cross-correlation results. THe tests below test that all methods return center values within a certain precision of one another. """ coord_precison_arcsec = .5 test_folder = '/dat/sdc/testing_data_for_importer/coordinate_correction/coordinate_testdata/20140426' # Extract coordinates from cross correlation result file coord_file = Path(test_folder).rglob('*coord_mu*').__next__() coords_corr = readsav(coord_file)['xy_coord'] center_corr = coords_corr.mean(axis=-1).mean(axis=-1) # Extract coordinates via GrisRun gd = GrisDay(test_folder) gr: GrisRun = gd.runs[0] coords_gr = gr.calculate_bounding_box() center_gr = np.array([np.mean(coords_gr[:2]), np.mean(coords_gr[2:])]) # Extract coordinates via MapExtractor with tempfile.TemporaryDirectory() as td: gme = GrisMapExtractor(test_folder, td) map_gen_coords = gme.get_coords(list(gme.runs)[0]) center_mapgen = np.array([map_gen_coords[0].value, map_gen_coords[1].value]) # Loop over different combinations and compare results combo_it = zip(['GrisRun', 'MapGenerator', 'Cross_Correlation'], [center_gr, center_mapgen, center_corr]) for set_a, set_b in combinations(combo_it, 2): name_a, data_a = set_a name_b, data_b = set_b diffs = data_a - data_b msg = f"Coordinates do not match within {coord_precison_arcsec} arcsec:\ \n {name_a}:{data_a} vs. {name_b}: {data_b}" coords_match = np.allclose(diffs, np.zeros(diffs.shape), atol=coord_precison_arcsec) if not coords_match: print(msg) self.assertTrue(coords_match, msg=msg)