diff --git a/tests/test_dataset.py b/tests/test_1001.py similarity index 85% rename from tests/test_dataset.py rename to tests/test_1001.py index e132a53bb964a9db89bac999070c1acebf450318..bbaf2f4896e42a9c2b9ff034db02f4b281655f4d 100644 --- a/tests/test_dataset.py +++ b/tests/test_1001.py @@ -295,6 +295,42 @@ class Create1001TestCase(unittest.TestCase): ict.specialComments.append("They are just examples!") ict.specialComments.append("Adapt as needed.") + # we can just use len of the list to check number of comments + self.assertEqual(len(ict.specialComments), 3) + + # let's define some normal comments... 21 lines + ncom = { + "PI_CONTACT_INFO": "PI1 pi-email@mail.com\nPI2 more-email@what.com", + "PLATFORM": "a platform", + "LOCATION": "somewhere", + "ASSOCIATED_DATA": "met sensor data", + "INSTRUMENT_INFO": "super cool instrument", + "DATA_INFO": f"icartt Python package version: {icartt.__version__}", + "UNCERTAINTY": "not much", + "ULOD_FLAG": "-7777", + "ULOD_VALUE": "N/A", + "LLOD_FLAG": "-8888", + "LLOD_VALUE": "N/A", + "DM_CONTACT_INFO": "datamanager@mail.edu", + "PROJECT_INFO": "the campaign", + "STIPULATIONS_ON_USE": "no", + "OTHER_COMMENTS": "a lot more info\non multiple lines", + "REVISION": ( + "R1\n" + "R1: revised time synchronization.\n" + "R0: initial, preliminary version." + ), + } + for k, v in ncom.items(): + ict.normalComments.keywords[k].append(v) + + # we can check if nlines method of normalComments class works + self.assertEqual(ict.normalComments.nlines, 21) + + ict.normalComments.freeform.append("free comment line 1") + ict.normalComments.freeform.append("free comment line 2") + self.assertEqual(ict.normalComments.nlines, 23) + ict.endDefineMode() # we have not added data yet, so data must be None @@ -311,9 +347,9 @@ class Create1001TestCase(unittest.TestCase): ict.data.addBulk(data) # elements of the time array must be equal to our input - t0 = np.datetime64(datetime.datetime(*now.timetuple()[:3]), 'ns') + t0 = np.datetime64(datetime.datetime(*now.timetuple()[:3]), "ns") for have, want in zip(ict.times, (12.3, 12.6, 13.4, 14.1)): - self.assertEqual(int(have-t0), int(want*10**9)) + self.assertEqual(int(have - t0), int(want * 10**9)) strOut = io.StringIO() @@ -322,51 +358,10 @@ class Create1001TestCase(unittest.TestCase): return True +if __name__ == "__main__": # pragma: no cover + import warnings -class BulkIOTestCase(unittest.TestCase): - # TODO: need to test invalid input - # def testInvalid(self): - # for fn in fns_fail: - # with self.subTest(msg=f"Opening invalid file {str(fn)}"): - # try: - # _ = icartt.Dataset(fn, loadData=True) - # except: # TODO: failure tests could be more specific - # pass - # else: - # self.fail('expected to fail') - - def testOpen(self): - for fn in fns_pass: - with self.subTest(msg=f"Opening test file {str(fn)}"): - ict = icartt.Dataset(fn, loadData=False) - self.assertEqual(type(ict), icartt.Dataset) - - def testReadData(self): - for fn in fns_pass: - with self.subTest(msg=f"Reading data from test file {str(fn)}"): - ict = icartt.Dataset(fn, loadData=True) - self.assertEqual(type(ict), icartt.Dataset) - - def testWriteHeader(self): - for fn in fns_pass: - with self.subTest(msg=f"Writing header for test file {str(fn)}"): - ict = icartt.Dataset(fn, loadData=False) - strIn = open(fn) - strOut = io.StringIO() - ict.writeHeader(f=strOut) - self.assertTrue(compareFiles(fn, strIn, strOut, nlines=ict.nHeader)) - - def testWrite(self): - for fn in fns_pass: - with self.subTest(msg=f"Writing data for test file {str(fn)}"): - ict = icartt.Dataset(fn, loadData=True) - strIn = open(fn) - strOut = io.StringIO() - ict.write(f=strOut) - self.assertTrue(compareFiles(fn, strIn, strOut)) - -# TODO: should also test dataset methods, e.g. .times - -if __name__ == "__main__": # pragma: no cover - unittest.main() + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=UserWarning) + unittest.main() diff --git a/tests/test_bulkIO.py b/tests/test_bulkIO.py new file mode 100644 index 0000000000000000000000000000000000000000..887abbcbfdb7464042f5eeb895c6388dc5777d48 --- /dev/null +++ b/tests/test_bulkIO.py @@ -0,0 +1,119 @@ +import unittest +import pathlib +import io +import icartt + +# working directory, example files +wd = pathlib.Path(__file__).parent +fns_pass = (wd / "example_data").glob("*.ict") +fns_fail = (wd / "example_data" / "will_fail").glob("*.ict") + + +def compareFiles(fn, strIn, strOut, skiplines=0, nlines=-1): # pragma: no cover + strOut.seek(0) + strIn.seek(0) + content_in = strIn.readlines() + content_out = strOut.readlines() + strIn.close() + strOut.close() + + if nlines > 0: + content_in = content_in[skiplines : (skiplines + nlines)] + content_out = content_out[skiplines : (skiplines + nlines)] + else: + content_in = content_in[skiplines:] + content_out = content_out[skiplines:] + + if not len(content_in) == len(content_out): + return False + + for inline, outline in zip(content_in, content_out): + inline = inline.strip().replace(" ", "") + outline = outline.strip().replace(" ", "") + if not inline == outline: + valid_data_line = False + # maybe this is a data line in which we only have different number formatting? + # compare as floats + # try: + insteps = [float(x) for x in inline.split(",")] + outsteps = [float(x) for x in outline.split(",")] + if len(insteps) == len(outsteps): + valid_data_line = True + for i in range(len(insteps)): + valid_data_line = valid_data_line and insteps[i] == outsteps[i] + # except: + # pass + + valid_var_line = False + # try: + insteps = [x.strip() for x in inline.split(",")] + outsteps = [x.strip() for x in outline.split(",")] + if len(insteps) == 2 and len(outsteps) == 3: + valid_var_line = ( + insteps[0] == outsteps[0] + and insteps[1] == outsteps[1] + and insteps[1] == outsteps[2] + ) + # except: + # pass + + if not valid_data_line and not valid_var_line: + print(f"{str(fn)}: line {i:d} differs:") + print(f" input: {inline}") + print(f" output: {outline}") + + return False + + return True + + +class BulkIOTestCase(unittest.TestCase): + # TODO: need to test invalid input + # def testInvalid(self): + # for fn in fns_fail: + # with self.subTest(msg=f"Opening invalid file {str(fn)}"): + # try: + # _ = icartt.Dataset(fn, loadData=True) + # except: # TODO: failure tests could be more specific + # pass + # else: + # self.fail('expected to fail') + + def testOpen(self): + for fn in fns_pass: + with self.subTest(msg=f"Opening test file {str(fn)}"): + ict = icartt.Dataset(fn, loadData=False) + self.assertEqual(type(ict), icartt.Dataset) + + def testReadData(self): + for fn in fns_pass: + with self.subTest(msg=f"Reading data from test file {str(fn)}"): + ict = icartt.Dataset(fn, loadData=True) + self.assertEqual(type(ict), icartt.Dataset) + + def testWriteHeader(self): + for fn in fns_pass: + with self.subTest(msg=f"Writing header for test file {str(fn)}"): + ict = icartt.Dataset(fn, loadData=False) + strIn = open(fn) + strOut = io.StringIO() + ict.writeHeader(f=strOut) + self.assertTrue(compareFiles(fn, strIn, strOut, nlines=ict.nHeader)) + + def testWrite(self): + for fn in fns_pass: + with self.subTest(msg=f"Writing data for test file {str(fn)}"): + ict = icartt.Dataset(fn, loadData=True) + strIn = open(fn) + strOut = io.StringIO() + ict.write(f=strOut) + self.assertTrue(compareFiles(fn, strIn, strOut)) + + +if __name__ == "__main__": # pragma: no cover + + import warnings + + with warnings.catch_warnings(): + warnings.simplefilter("ignore", category=UserWarning) + unittest.main() diff --git a/tests/usage_examples/create_ffi1001.py b/tests/usage_examples/create_ffi1001.py index 21e4b8c89ab2775370ee5c506f68b1710b0cba78..2261599cc13110282735f2fe120c809a74646be7 100644 --- a/tests/usage_examples/create_ffi1001.py +++ b/tests/usage_examples/create_ffi1001.py @@ -1,6 +1,7 @@ +import datetime import icartt -import datetime + ict = icartt.Dataset(format=icartt.Formats.FFI1001) @@ -8,8 +9,8 @@ ict.PIName = 'Knote, Christoph' ict.PIAffiliation = 'Faculty of Medicine, University Augsburg, Germany' ict.dataSourceDescription = 'Example data' ict.missionName = 'MBEES' -ict.dateOfCollection = datetime.datetime.today() -ict.dateOfRevision = datetime.datetime.today() +ict.dateOfCollection = datetime.datetime.utcnow().timetuple()[:3] +ict.dateOfRevision = datetime.datetime.utcnow().timetuple()[:3] ict.dataIntervalCode = [ 0 ] @@ -36,6 +37,11 @@ ict.specialComments.append("Some comments on this dataset:") ict.specialComments.append("They are just examples!") ict.specialComments.append("Adapt as needed.") +ict.normalComments.freeform.append('free comment line 1') +ict.normalComments.freeform.append('free comment line 2') + +# ict.normalComments are all set to N/A if not specified + ict.endDefineMode() # Three ways to add data: @@ -69,4 +75,4 @@ ict.write() # And you could simply write to file: #with open('output.ict', 'w') as f: -# ict.write(f=f) \ No newline at end of file +# ict.write(f=f) diff --git a/tests/usage_examples/create_ffi2110.py b/tests/usage_examples/create_ffi2110.py index 8796a223203f835fe17e79c7da86e35c15e525be..78ca1973e03a751ffc3d0f72b4075cafcb96698c 100644 --- a/tests/usage_examples/create_ffi2110.py +++ b/tests/usage_examples/create_ffi2110.py @@ -1,15 +1,18 @@ -import icartt import datetime + import numpy as np +import icartt + + ict = icartt.Dataset(format=icartt.Formats.FFI2110) ict.PIName = 'Knote, Christoph' ict.PIAffiliation = 'Faculty of Medicine, University Augsburg, Germany' ict.dataSourceDescription = 'Example data' ict.missionName = 'MBEES' -ict.dateOfCollection = datetime.datetime.today() -ict.dateOfRevision = datetime.datetime.today() +ict.dateOfCollection = datetime.datetime.utcnow().timetuple()[:3] +ict.dateOfRevision = datetime.datetime.utcnow().timetuple()[:3] ict.dataIntervalCode = [ 0 ] @@ -83,7 +86,7 @@ ict.data.add( Time_Start = 13.3, nAuxiliary=2, Time_Stop = 13.5, Latitude = 48.3 # then, dependent data can be added: # ibvar, dvar1, dvar2 -data = np.array( [ ( 0, 123, 8.4e4), +data = np.array( [ ( 0, 123, 8.4e4), (100, 122, 9.1e4), (250, 115, 9.3e4), (500, 106, 9.8e4) ] ) @@ -91,7 +94,7 @@ data = np.array( [ ( 0, 123, 8.4e4), ict.data.addBulkDep(12.3, data) # ibvar, dvar1, dvar2 -data = np.array( [ ( 0, 153, 7.3e4), +data = np.array( [ ( 0, 153, 7.3e4), (270, 172, 8.9e4) ] ) ict.data.addBulkDep(13.3, data)