test_gris.py 6.83 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.headers.wcs import reorder_gris_data
Carl Schaffer's avatar
Carl Schaffer committed
27
from kis_tools.util.util import groupby_gris_run
Carl Schaffer's avatar
Carl Schaffer committed
28

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

Carl Schaffer's avatar
Carl Schaffer committed
31

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

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

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

41
        # setup targets for run-wise testing
Carl Schaffer's avatar
Carl Schaffer committed
42
43
44
45
        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]
46

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

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

Carl Schaffer's avatar
Carl Schaffer committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    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)

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

104
105
106
107
108
        # 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
109
110
111
        # test make_bson
        self.assertIsInstance(gff.make_bson(), dict)

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

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

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

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

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

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

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

        # run @property functions once to test
Carl Schaffer's avatar
Carl Schaffer committed
148
149
150
151
152
        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
153

154
        gr = GrisRun(self.runfiles)
Carl Schaffer's avatar
Carl Schaffer committed
155
        self.assertIsInstance(gr, GrisRun, "Instantiation of GrisRun from Paths failed")
Carl Schaffer's avatar
Carl Schaffer committed
156

157
        gr.parse()
Carl Schaffer's avatar
Carl Schaffer committed
158

159
160
161
162
163
164
        # test printing of object
        self.assertIsInstance(gr.__str__(), str)

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

165
166
167
        # test was aborted
        self.assertTrue(gr.was_aborted)

168
        # test observers
Carl Schaffer's avatar
Carl Schaffer committed
169
        obs = gr.observers
170
171
        self.assertTrue(obs)

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

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

188
    def test_wcs(self):
189
190

        # Test reordering of data
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        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])

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

210
211
212
            # check that the maximum (spectral axis) is at third position
            self.assertEqual(2, shape.index(max(shape)))

Carl Schaffer's avatar
Carl Schaffer committed
213

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