test_gris.py 6.95 KB
Newer Older
Carl Schaffer's avatar
Carl Schaffer committed
1
import os
Carl Schaffer's avatar
Carl Schaffer committed
2
import unittest
Carl Schaffer's avatar
Carl Schaffer committed
3
4
from datetime import datetime
from pathlib import Path
Carl Schaffer's avatar
Carl Schaffer committed
5
from tempfile import TemporaryDirectory
Carl Schaffer's avatar
Carl Schaffer committed
6
from unittest.mock import patch
7

8
import numpy as np
Carl Schaffer's avatar
Carl Schaffer committed
9
import pandas as pd
Carl Schaffer's avatar
Carl Schaffer committed
10
from importer_test_data import gris_folder
Carl Schaffer's avatar
Carl Schaffer committed
11
12
13
14
15
from importer_test_data import test_data, hmi_context
from importer_test_data.gris_structure import (
    raw_files,
    folder as structured_gris_folder,
)
Carl Schaffer's avatar
Carl Schaffer committed
16

17
18
19
20
from kis_tools.gris.GrisArchive import GrisArchive
from kis_tools.gris.GrisDay import GrisDay
from kis_tools.gris.GrisFitsFile import GrisFitsFile
from kis_tools.gris.GrisRun import GrisRun
Carl Schaffer's avatar
Carl Schaffer committed
21
22
23
24
25
from kis_tools.gris.calib_settings.parsing import (
    parse_path,
    get_gris_calib_scripts,
    parse_file,
)
Carl Schaffer's avatar
Carl Schaffer committed
26
from kis_tools.gris.coord_correction_ml import get_coords_ml
Carl Schaffer's avatar
Carl Schaffer committed
27
from kis_tools.gris.headers.wcs import reorder_gris_data
Carl Schaffer's avatar
Carl Schaffer committed
28
from kis_tools.util.util import groupby_gris_run
Carl Schaffer's avatar
Carl Schaffer committed
29

30
31
_testdata = "/dat/sdc/testing_data_for_importer/gris/"

Carl Schaffer's avatar
Carl Schaffer committed
32

Carl Schaffer's avatar
Carl Schaffer committed
33
class TestGris(unittest.TestCase):
34
    """Class to hold tests for gris importer"""
Carl Schaffer's avatar
Carl Schaffer committed
35
36

    @classmethod
Carl Schaffer's avatar
Carl Schaffer committed
37
    def setUpClass(cls):
38
        """global set up for tests"""
Carl Schaffer's avatar
Carl Schaffer committed
39

Carl Schaffer's avatar
Carl Schaffer committed
40
        cls.daytarget = gris_folder
Carl Schaffer's avatar
Carl Schaffer committed
41

42
        # setup targets for run-wise testing
Carl Schaffer's avatar
Carl Schaffer committed
43
44
45
46
        files = [str(p) for p in Path(cls.daytarget).glob("level1_split/*")]
        run, cls.runfiles = next(iter(groupby_gris_run(files).items()))
        # setup target for file-wise-testing
        cls.filetarget = cls.runfiles[0]
47

Carl Schaffer's avatar
Carl Schaffer committed
48
    def test_day(self):
49
        """test instatiation of GrisDay """
50
        # test instantiation with valid folder
Carl Schaffer's avatar
Carl Schaffer committed
51
52
53
        with patch(
            "kis_tools.util.util.date_from_fn", return_value=datetime(2014, 4, 26)
        ) as _:
Carl Schaffer's avatar
Carl Schaffer committed
54
            gd = GrisDay(self.daytarget)
Carl Schaffer's avatar
Carl Schaffer committed
55
        self.assertIsInstance(gd, GrisDay, "Instantiation of GrisDay class failed!")
Carl Schaffer's avatar
Carl Schaffer committed
56
        # test instantiation with folder like 20140426_1
57
        fakeday = GrisDay("20140426_1")
Carl Schaffer's avatar
Carl Schaffer committed
58
        self.assertIsInstance(
Carl Schaffer's avatar
Carl Schaffer committed
59
60
61
62
            fakeday,
            GrisDay,
            "Instantiation of GrisDay class failed on fake day!",
        )
Carl Schaffer's avatar
Carl Schaffer committed
63
64
65
66
67
68
69
70
        # test printing of object
        self.assertIsInstance(gd.__str__(), str)
        # test repr of object
        self.assertIsInstance(gd.__repr__(), str)
        # test printing of object
        self.assertIsInstance(fakeday.__str__(), str)
        # test repr of object
        self.assertIsInstance(fakeday.__repr__(), str)
Carl Schaffer's avatar
Carl Schaffer committed
71

Carl Schaffer's avatar
Carl Schaffer committed
72
        # test the count method
73
74
        self.assertGreater(gd.count("*"), 0)
        self.assertEqual(fakeday.count("*"), 0)
Carl Schaffer's avatar
Carl Schaffer committed
75

Carl Schaffer's avatar
Carl Schaffer committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    def test_calib_parsing(self):
        """
        Test the toolkit for parsing gris calibration settings files.
        """

        # Test get_files
        files = [*get_gris_calib_scripts(structured_gris_folder)]
        assert files

        # test file_parsing
        calls = parse_file(files[0])
        assert calls

        # test recursive parsing
        call_df = parse_path(structured_gris_folder)
        assert isinstance(call_df, pd.DataFrame)

        # Try to parse an empty tempdir, ensure that an error is raised
        with self.assertRaises(ValueError):
            with TemporaryDirectory() as td:
                parse_path(td)

98
    def test_gris_fits_file(self):
99
        """test instantiation of GrisFitsFile """
100
        gff = GrisFitsFile(self.filetarget)
Carl Schaffer's avatar
Carl Schaffer committed
101
        self.assertIsInstance(gff, GrisFitsFile, "Instantiation of GrisFitsFile failed")
102
        # test parse function
103
104
        gff.parse()

105
106
107
108
109
        # test printing of object
        self.assertIsInstance(gff.__str__(), str)
        # test repr of object
        self.assertIsInstance(gff.__repr__(), str)

Carl Schaffer's avatar
Carl Schaffer committed
110
111
112
        # test make_bson
        self.assertIsInstance(gff.make_bson(), dict)

Carl Schaffer's avatar
Carl Schaffer committed
113
114
115
116
        # check that constructor complains on invalid filename
        with self.assertRaises(AssertionError):
            _ = GrisFitsFile("")

117
118
119
120
        # test repr
        print(repr(gff))
        print(gff)

121
        # test _coords_from_simple_header with file from 2017
Carl Schaffer's avatar
Carl Schaffer committed
122
        gf2 = GrisFitsFile(test_data.gris.l1_data[0])
123
124
        gf2.slit_orientation
        gf2.parse()
Carl Schaffer's avatar
Carl Schaffer committed
125
        gf2.telescope_centering
126
        _ = gf2.modestring
127
        _ = gf2._coords_from_simple_header
128

129
        # test coordinates from WCS
130
131
        self.assertEqual(len(gf2._coords_from_wcs.shape), 2)

Carl Schaffer's avatar
Carl Schaffer committed
132
133
134
        # test coods via ml:
        _ = get_coords_ml(gf2)

135
136
        # test coordinates from translated and untranslated file
        translated_file = self.filetarget
Carl Schaffer's avatar
Carl Schaffer committed
137
        raw_file = raw_files[0]
138
139
140
141

        for f in [translated_file, raw_file]:
            coords = GrisFitsFile(f).coords
            self.assertTrue(coords)
142

143
    def test_gris_run(self):
144
        """test instantiation of GrisRun """
145
146
        myfiles = [GrisFitsFile(f) for f in self.runfiles]
        gr = GrisRun(myfiles)
147
        self.assertIsInstance(
Carl Schaffer's avatar
Carl Schaffer committed
148
149
            gr, GrisRun, "Instantiation of GrisRun from GrisFitsFiles failed"
        )
Carl Schaffer's avatar
Carl Schaffer committed
150
151

        # run @property functions once to test
Carl Schaffer's avatar
Carl Schaffer committed
152
153
154
155
156
        self.assertIsNotNone(gr.files_l0)
        self.assertIsNotNone(gr.files_l1)
        self.assertIsNotNone(gr.files_l1_split)
        self.assertIsNotNone(gr.files_l2)
        self.assertIsNotNone(gr.map_saves)
Carl Schaffer's avatar
Carl Schaffer committed
157

158
        gr = GrisRun(self.runfiles)
Carl Schaffer's avatar
Carl Schaffer committed
159
        self.assertIsInstance(gr, GrisRun, "Instantiation of GrisRun from Paths failed")
Carl Schaffer's avatar
Carl Schaffer committed
160

161
        gr.parse()
Carl Schaffer's avatar
Carl Schaffer committed
162

163
164
165
166
167
168
        # test printing of object
        self.assertIsInstance(gr.__str__(), str)

        # test repr of object
        self.assertIsInstance(gr.__repr__(), str)

169
170
171
        # test was aborted
        self.assertTrue(gr.was_aborted)

172
        # test observers
Carl Schaffer's avatar
Carl Schaffer committed
173
        obs = gr.observers
174
175
        self.assertTrue(obs)

Carl Schaffer's avatar
Carl Schaffer committed
176
177
178
        with patch(
            "kis_tools.util.locplot.get_hmi_continuum", return_value=hmi_context[0]
        ) as patched:
Carl Schaffer's avatar
Carl Schaffer committed
179
180
181
            fig, ax = gr.plot_location()
            self.assertEqual(len(ax.patches), 4)
            return fig, ax
182

183
    def test_gris_archive(self):
184
        """ test GrisArchive"""
Carl Schaffer's avatar
Carl Schaffer committed
185
        ga = GrisArchive(Path(self.daytarget).parent)
Carl Schaffer's avatar
Carl Schaffer committed
186
187
188
        self.assertIsInstance(
            ga, GrisArchive, "Could not instantiate gris archive class"
        )
189
190
        df = ga.get_df()
        self.assertGreater(len(df), 0)
Carl Schaffer's avatar
Carl Schaffer committed
191

192
    def test_wcs(self):
193
194

        # Test reordering of data
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
        test_shapes = [
            (4, 166, 1010),  # polarimetric  data
            (493, 1010),  # spectroscopic data
        ]
        for shape in test_shapes:
            data = np.zeros(shape)
            reordered = reorder_gris_data(data, reverse_axes=False)
            reordered_rev = reorder_gris_data(data, reverse_axes=True)

            # check that something has been done
            self.assertNotEqual(data.shape, reordered.shape)

            # check the correct order of spatial axes
            shape = reordered.shape
            self.assertLess(shape[0], shape[1])

211
212
213
            # check that reversed data is the reverse of normal data
            self.assertEqual(reordered.shape[::-1], reordered_rev.shape)

214
215
216
            # check that the maximum (spectral axis) is at third position
            self.assertEqual(2, shape.index(max(shape)))

Carl Schaffer's avatar
Carl Schaffer committed
217

Carl Schaffer's avatar
Carl Schaffer committed
218
if __name__ == "__main__":
219
    with open(os.devnull, "w") as log:
Carl Schaffer's avatar
Carl Schaffer committed
220
        unittest.main()