test_coords.py 2.32 KB
Newer Older
Carl Schaffer's avatar
typo    
Carl Schaffer committed
1
# Tests to ensure that coordinate retrieval methods compatible
2
3
4
5
6
7

import tempfile
import unittest
from itertools import combinations

import numpy as np
Carl Schaffer's avatar
Carl Schaffer committed
8
from importer_test_data import gris_structure
9
10
11
12
13
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
14
from kis_tools.gris.headers.wcs import GrisWCSGenerator
15
16
17
18
19
20
21
22
23
24
25
26


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.

        """

Carl Schaffer's avatar
Carl Schaffer committed
27
        coord_precison_arcsec = 0.5
28
29

        # Extract coordinates from cross correlation result file
Carl Schaffer's avatar
Carl Schaffer committed
30
        coord_file = gris_structure.coord_save
Carl Schaffer's avatar
Carl Schaffer committed
31
        coords_corr = readsav(coord_file)["xy_coord"]
32
33
34
        center_corr = coords_corr.mean(axis=-1).mean(axis=-1)

        # Extract coordinates via GrisRun
Carl Schaffer's avatar
Carl Schaffer committed
35
        gd = GrisDay(gris_structure.folder)
36
37
38
39
40
41
        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:
Carl Schaffer's avatar
Carl Schaffer committed
42
43
            gme = GrisMapExtractor(gris_structure.folder, td)
        center_mapgen = gme.get_coords(list(gme.runs)[0])
44
45

        # Loop over different combinations and compare results
Carl Schaffer's avatar
Carl Schaffer committed
46
47
48
49
        combo_it = zip(
            ["GrisRun", "MapGenerator", "Cross_Correlation"],
            [center_gr, center_mapgen, center_corr],
        )
50
51
52
53
54
55
        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}"
Carl Schaffer's avatar
Carl Schaffer committed
56
57
58
59
60
            coords_match = np.allclose(
                diffs, np.zeros(diffs.shape), atol=coord_precison_arcsec
            )
            if not coords_match:
                print(msg)
61
            self.assertTrue(coords_match, msg=msg)
62
63
64

    def test_gris_wcs(self):
        wcs = GrisWCSGenerator(gris_structure.raw_files[0])