import os import shutil import unittest from datetime import datetime from glob import glob from os.path import isdir, join from pathlib import Path from unittest.mock import patch import numpy as np 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 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 from kis_tools.gris.wcs import reorder_gris_data from kis_tools.util.util import gris_run_number, groupby_gris_run _testdata = "/dat/sdc/testing_data_for_importer/gris/" class TestGris(unittest.TestCase): """Class to hold tests for gris importer""" @classmethod def setUpClass(cls): """global set up for tests""" cls.daytarget = gris_folder # setup targets for run-wise testing 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] def test_day(self): """test instatiation of GrisDay """ # test instantiation with valid folder with patch( "kis_tools.util.util.date_from_fn", return_value=datetime(2014, 4, 26) ) as _: gd = GrisDay(self.daytarget) self.assertIsInstance(gd, GrisDay, "Instantiation of GrisDay class failed!") # test instantiation with folder like 20140426_1 fakeday = GrisDay("20140426_1") self.assertIsInstance( fakeday, GrisDay, "Instantiation of GrisDay class failed on fake day!", ) # 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) # test the count method self.assertGreater(gd.count("*"), 0) self.assertEqual(fakeday.count("*"), 0) def test_gris_fits_file(self): """test instantiation of GrisFitsFile """ gff = GrisFitsFile(self.filetarget) self.assertIsInstance(gff, GrisFitsFile, "Instantiation of GrisFitsFile failed") # test parse function gff.parse() gff.parse() # test printing of object self.assertIsInstance(gff.__str__(), str) # test repr of object self.assertIsInstance(gff.__repr__(), str) # test make_bson self.assertIsInstance(gff.make_bson(), dict) # check that constructor complains on invalid filename with self.assertRaises(AssertionError): _ = GrisFitsFile("") # test repr print(repr(gff)) print(gff) # test _coords_from_simple_header with file from 2017 gf2 = GrisFitsFile(test_data.gris.l1_data[0]) gf2.slit_orientation gf2.parse() _ = gf2.modestring _ = gf2._coords_from_simple_header # test coordinates from WCS self.assertEqual(len(gf2._coords_from_wcs.shape), 2) # test coordinates from translated and untranslated file translated_file = self.filetarget raw_file = raw_files[0] for f in [translated_file, raw_file]: coords = GrisFitsFile(f).coords self.assertTrue(coords) def test_gris_run(self): """test instantiation of GrisRun """ myfiles = [GrisFitsFile(f) for f in self.runfiles] gr = GrisRun(myfiles) self.assertIsInstance( gr, GrisRun, "Instantiation of GrisRun from GrisFitsFiles failed" ) # run @property functions once to test 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) gr = GrisRun(self.runfiles) self.assertIsInstance(gr, GrisRun, "Instantiation of GrisRun from Paths failed") gr.parse() # test printing of object self.assertIsInstance(gr.__str__(), str) # test repr of object self.assertIsInstance(gr.__repr__(), str) # test was aborted self.assertTrue(gr.was_aborted) # test observers obs = gr.observers self.assertTrue(obs) with patch( "kis_tools.util.locplot.get_hmi_continuum", return_value=hmi_context[0] ) as patched: fig, ax = gr.plot_location() self.assertEqual(len(ax.patches), 4) return fig, ax def test_gris_archive(self): """ test GrisArchive""" ga = GrisArchive(Path(self.daytarget).parent) self.assertIsInstance( ga, GrisArchive, "Could not instantiate gris archive class" ) df = ga.get_df() self.assertGreater(len(df), 0) def test_wcs(self): # Test reordering of data 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]) # check that reversed data is the reverse of normal data self.assertEqual(reordered.shape[::-1], reordered_rev.shape) # check that the maximum (spectral axis) is at third position self.assertEqual(2, shape.index(max(shape))) if __name__ == "__main__": with open(os.devnull, "w") as log: unittest.main()