test_gris.py 6.6 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 glob import glob
5
from os.path import isdir, join
6

7
import numpy as np
8
from importer_test_data import test_data
9
10
11
12
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
13
from kis_tools.gris.wcs import reorder_gris_data
Carl Schaffer's avatar
Carl Schaffer committed
14

15
16
_testdata = "/dat/sdc/testing_data_for_importer/gris/"

Carl Schaffer's avatar
Carl Schaffer committed
17

Carl Schaffer's avatar
Carl Schaffer committed
18
class TestGris(unittest.TestCase):
19
    """Class to hold tests for gris importer"""
Carl Schaffer's avatar
Carl Schaffer committed
20
21

    @classmethod
Carl Schaffer's avatar
Carl Schaffer committed
22
    def setUpClass(cls):
23
        """global set up for tests"""
Carl Schaffer's avatar
Carl Schaffer committed
24
25

        # check that testdata directory exists
Carl Schaffer's avatar
Carl Schaffer committed
26
27
        cls.testdata_location = _testdata
        assert isdir(cls.testdata_location)
28
29

        # set up target for day-wise testing
30
31
        pattern = join(cls.testdata_location, "201?????")

Carl Schaffer's avatar
Carl Schaffer committed
32
        daytarget = list(
33
            filter(isdir, glob(pattern))
Carl Schaffer's avatar
Carl Schaffer committed
34
        )[0]
Carl Schaffer's avatar
Carl Schaffer committed
35
        cls.daytarget = os.path.join(cls.testdata_location, daytarget)
Carl Schaffer's avatar
Carl Schaffer committed
36

37
        # setup targets for run-wise testing
Carl Schaffer's avatar
Carl Schaffer committed
38
        files = os.listdir(os.path.join(cls.daytarget, "level1_split"))
39
        run = files[0].split("_")[4]
Carl Schaffer's avatar
Carl Schaffer committed
40
        cls.runfiles = []
41
        for f in files:
42
            if f.split("_")[4] == run:
Carl Schaffer's avatar
Carl Schaffer committed
43
44
                cls.runfiles.append(
                    os.path.join(cls.daytarget, "level1_split", f)
Carl Schaffer's avatar
Carl Schaffer committed
45
                )
Carl Schaffer's avatar
Carl Schaffer committed
46
            if len(cls.runfiles) == 2:
47
                break
Carl Schaffer's avatar
Carl Schaffer committed
48
            # setup target for file-wise-testing
Carl Schaffer's avatar
Carl Schaffer committed
49
            cls.filetarget = cls.runfiles[0]
50

Carl Schaffer's avatar
Carl Schaffer committed
51
    @classmethod
Carl Schaffer's avatar
Carl Schaffer committed
52
    def tearDownClass(cls):
53
        """gobal clean up after all tests """
54
55
56
57
58
        gmc = cls.gmc.db
        assert "sdc" not in gmc.name
        for c in gmc.list_collection_names():
            gmc[c].drop()

Carl Schaffer's avatar
Carl Schaffer committed
59
60
        del cls.gmc
        shutil.rmtree(cls.errdir)
Carl Schaffer's avatar
Carl Schaffer committed
61
62

    def test_day(self):
63
        """test instatiation of GrisDay """
64
        # test instantiation with valid folder
Carl Schaffer's avatar
Carl Schaffer committed
65
        gd = GrisDay(os.path.join(self.testdata_location, self.daytarget))
Carl Schaffer's avatar
Carl Schaffer committed
66
67
68
        self.assertIsInstance(
            gd, GrisDay, "Instantiation of GrisDay class failed!"
        )
Carl Schaffer's avatar
Carl Schaffer committed
69
        # test instantiation with folder like 20140426_1
70
        fakeday = GrisDay("20140426_1")
Carl Schaffer's avatar
Carl Schaffer committed
71
        self.assertIsInstance(
Carl Schaffer's avatar
Carl Schaffer committed
72
73
74
75
            fakeday,
            GrisDay,
            "Instantiation of GrisDay class failed on fake day!",
        )
Carl Schaffer's avatar
Carl Schaffer committed
76
77
78
79
80
81
82
83
        # 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
84

Carl Schaffer's avatar
Carl Schaffer committed
85
        # test the count method
86
87
        self.assertGreater(gd.count("*"), 0)
        self.assertEqual(fakeday.count("*"), 0)
Carl Schaffer's avatar
Carl Schaffer committed
88

89
    def test_gris_fits_file(self):
90
        """test instantiation of GrisFitsFile """
91
        gff = GrisFitsFile(self.filetarget)
Carl Schaffer's avatar
Carl Schaffer committed
92
93
94
        self.assertIsInstance(
            gff, GrisFitsFile, "Instantiation of GrisFitsFile failed"
        )
95
        # test parse function
96
        gff.parse()
Carl Schaffer's avatar
Carl Schaffer committed
97
        gff.parse()
98

99
100
101
102
103
        # 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
104
105
106
        # test make_bson
        self.assertIsInstance(gff.make_bson(), dict)

Carl Schaffer's avatar
Carl Schaffer committed
107
108
109
110
        # check that constructor complains on invalid filename
        with self.assertRaises(AssertionError):
            _ = GrisFitsFile("")

111
112
113
114
        # test repr
        print(repr(gff))
        print(gff)

115
        # test _coords_from_simple_header with file from 2017
116
        gf2 = GrisFitsFile(
117
            test_data.gris.l1_data[0]
118
119
120
121
        )
        gf2.slit_orientation
        gf2.parse()
        _ = gf2.modestring
122
        _ = gf2._coords_from_simple_header
123

124
125
        _ = gff.mu

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

        # test coordinates from translated and untranslated file
        translated_file = self.filetarget
        raw_file = "/dat/sdc/testing_data_for_importer/gris/raw_split_files/gris_20140503_140538_l1p_012_001_0012.fits"

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

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

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

152
        gr = GrisRun(self.runfiles)
Carl Schaffer's avatar
Carl Schaffer committed
153
154
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)

172
173
174
175
        fig, ax = gr.plot_location()
        self.assertEqual(len(ax.patches), 4)
        return fig, ax

176
    def test_gris_archive(self):
177
        """ test GrisArchive"""
178
        ga = GrisArchive(self.testdata_location)
Carl Schaffer's avatar
Carl Schaffer committed
179
180
181
        self.assertIsInstance(
            ga, GrisArchive, "Could not instantiate gris archive class"
        )
182
183
        df = ga.get_df()
        self.assertGreater(len(df), 0)
Carl Schaffer's avatar
Carl Schaffer committed
184

185
    def test_wcs(self):
186
187

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

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

207
208
209
            # check that the maximum (spectral axis) is at third position
            self.assertEqual(2, shape.index(max(shape)))

Carl Schaffer's avatar
Carl Schaffer committed
210

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