test_gris.py 6.04 KB
Newer Older
Carl Schaffer's avatar
Carl Schaffer committed
1
import os
Carl Schaffer's avatar
Carl Schaffer committed
2
import shutil
Carl Schaffer's avatar
Carl Schaffer committed
3
import unittest
Carl Schaffer's avatar
Carl Schaffer committed
4
from datetime import datetime
Carl Schaffer's avatar
Carl Schaffer committed
5
from glob import glob
6
from os.path import isdir, join
Carl Schaffer's avatar
Carl Schaffer committed
7
8
from pathlib import Path
from unittest.mock import patch
9

10
import numpy as np
Carl Schaffer's avatar
Carl Schaffer committed
11
12
13
14
from importer_test_data import test_data, hmi_context
from importer_test_data import gris_folder
from importer_test_data.gris_structure import raw_files

15
16
17
18
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
19
from kis_tools.gris.wcs import reorder_gris_data
Carl Schaffer's avatar
Carl Schaffer committed
20
from kis_tools.util.util import gris_run_number, groupby_gris_run
Carl Schaffer's avatar
Carl Schaffer committed
21

22
23
_testdata = "/dat/sdc/testing_data_for_importer/gris/"

Carl Schaffer's avatar
Carl Schaffer committed
24

Carl Schaffer's avatar
Carl Schaffer committed
25
class TestGris(unittest.TestCase):
26
    """Class to hold tests for gris importer"""
Carl Schaffer's avatar
Carl Schaffer committed
27
28

    @classmethod
Carl Schaffer's avatar
Carl Schaffer committed
29
    def setUpClass(cls):
30
        """global set up for tests"""
Carl Schaffer's avatar
Carl Schaffer committed
31

Carl Schaffer's avatar
Carl Schaffer committed
32
        cls.daytarget = gris_folder
Carl Schaffer's avatar
Carl Schaffer committed
33

34
        # setup targets for run-wise testing
Carl Schaffer's avatar
Carl Schaffer committed
35
36
37
38
        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]
39

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

Carl Schaffer's avatar
Carl Schaffer committed
64
        # test the count method
65
66
        self.assertGreater(gd.count("*"), 0)
        self.assertEqual(fakeday.count("*"), 0)
Carl Schaffer's avatar
Carl Schaffer committed
67

68
    def test_gris_fits_file(self):
69
        """test instantiation of GrisFitsFile """
70
        gff = GrisFitsFile(self.filetarget)
Carl Schaffer's avatar
Carl Schaffer committed
71
        self.assertIsInstance(gff, GrisFitsFile, "Instantiation of GrisFitsFile failed")
72
        # test parse function
73
        gff.parse()
Carl Schaffer's avatar
Carl Schaffer committed
74
        gff.parse()
75

76
77
78
79
80
        # 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
81
82
83
        # test make_bson
        self.assertIsInstance(gff.make_bson(), dict)

Carl Schaffer's avatar
Carl Schaffer committed
84
85
86
87
        # check that constructor complains on invalid filename
        with self.assertRaises(AssertionError):
            _ = GrisFitsFile("")

88
89
90
91
        # test repr
        print(repr(gff))
        print(gff)

92
        # test _coords_from_simple_header with file from 2017
Carl Schaffer's avatar
Carl Schaffer committed
93
        gf2 = GrisFitsFile(test_data.gris.l1_data[0])
94
95
96
        gf2.slit_orientation
        gf2.parse()
        _ = gf2.modestring
97
        _ = gf2._coords_from_simple_header
98

99
        # test coordinates from WCS
100
101
102
103
        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
104
        raw_file = raw_files[0]
105
106
107
108

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

110
    def test_gris_run(self):
111
        """test instantiation of GrisRun """
112
113
        myfiles = [GrisFitsFile(f) for f in self.runfiles]
        gr = GrisRun(myfiles)
114
        self.assertIsInstance(
Carl Schaffer's avatar
Carl Schaffer committed
115
116
            gr, GrisRun, "Instantiation of GrisRun from GrisFitsFiles failed"
        )
Carl Schaffer's avatar
Carl Schaffer committed
117
118

        # run @property functions once to test
Carl Schaffer's avatar
Carl Schaffer committed
119
120
121
122
123
        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
124

125
        gr = GrisRun(self.runfiles)
Carl Schaffer's avatar
Carl Schaffer committed
126
        self.assertIsInstance(gr, GrisRun, "Instantiation of GrisRun from Paths failed")
Carl Schaffer's avatar
Carl Schaffer committed
127

128
        gr.parse()
Carl Schaffer's avatar
Carl Schaffer committed
129

130
131
132
133
134
135
        # test printing of object
        self.assertIsInstance(gr.__str__(), str)

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

136
137
138
        # test was aborted
        self.assertTrue(gr.was_aborted)

139
        # test observers
Carl Schaffer's avatar
Carl Schaffer committed
140
        obs = gr.observers
141
142
        self.assertTrue(obs)

Carl Schaffer's avatar
Carl Schaffer committed
143
144
145
        with patch(
            "kis_tools.util.locplot.get_hmi_continuum", return_value=hmi_context[0]
        ) as patched:
Carl Schaffer's avatar
Carl Schaffer committed
146
147
148
            fig, ax = gr.plot_location()
            self.assertEqual(len(ax.patches), 4)
            return fig, ax
149

150
    def test_gris_archive(self):
151
        """ test GrisArchive"""
Carl Schaffer's avatar
Carl Schaffer committed
152
        ga = GrisArchive(Path(self.daytarget).parent)
Carl Schaffer's avatar
Carl Schaffer committed
153
154
155
        self.assertIsInstance(
            ga, GrisArchive, "Could not instantiate gris archive class"
        )
156
157
        df = ga.get_df()
        self.assertGreater(len(df), 0)
Carl Schaffer's avatar
Carl Schaffer committed
158

159
    def test_wcs(self):
160
161

        # Test reordering of data
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        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])

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

181
182
183
            # check that the maximum (spectral axis) is at third position
            self.assertEqual(2, shape.index(max(shape)))

Carl Schaffer's avatar
Carl Schaffer committed
184

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