import os import shutil import unittest from glob import glob from os.path import isdir, join import numpy as np from importer_test_data import test_data 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 _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""" # check that testdata directory exists cls.testdata_location = _testdata assert isdir(cls.testdata_location) # set up target for day-wise testing pattern = join(cls.testdata_location, "201?????") daytarget = list( filter(isdir, glob(pattern)) )[0] cls.daytarget = os.path.join(cls.testdata_location, daytarget) # setup targets for run-wise testing files = os.listdir(os.path.join(cls.daytarget, "level1_split")) run = files[0].split("_")[4] cls.runfiles = [] for f in files: if f.split("_")[4] == run: cls.runfiles.append( os.path.join(cls.daytarget, "level1_split", f) ) if len(cls.runfiles) == 2: break # setup target for file-wise-testing cls.filetarget = cls.runfiles[0] @classmethod def tearDownClass(cls): """gobal clean up after all tests """ gmc = cls.gmc.db assert "sdc" not in gmc.name for c in gmc.list_collection_names(): gmc[c].drop() del cls.gmc shutil.rmtree(cls.errdir) def test_day(self): """test instatiation of GrisDay """ # test instantiation with valid folder gd = GrisDay(os.path.join(self.testdata_location, 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 _ = gff.mu # 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 = "/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) 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) fig, ax = gr.plot_location() self.assertEqual(len(ax.patches), 4) return fig, ax def test_gris_archive(self): """ test GrisArchive""" ga = GrisArchive(self.testdata_location) 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()