class IOTests(unittest.TestCase): def setUp(self): p = np.arange(1, 1001, 2) temp = 10. * np.exp(-.008*p) - 15. * np.exp(-0.005*(p+100)) + 2. sal = -14. * np.exp(-.01*p) + 34. self.p = p self.temp = temp self.sal = sal dt = datetime.datetime(1993, 8, 18, 14, 42, 36) self.cast = Cast(pres=self.p, temp=self.temp, sal=self.sal, date=dt) self.ctd = CTDCast(self.p, self.sal, self.temp, date=dt) self.collection = CastCollection(self.ctd, self.ctd) return def assertFilesEqual(self, f1, f2): f1.seek(0) f2.seek(0) s1 = f1.read() s2 = f2.read() self.assertEqual(s1, s2) return def test_fromdict_cast(self): d = {"type": "cast", "properties": {"coordinates": (-10.0, 54.0), "date": "2015-04-17 15:03 UTC", "notes": "CTD touched bottom"}, "data": {"depth": np.arange(1.0, 500.0, 2.0), "temperature": np.linspace(8.0, 4.0, 250), "salinity": np.linspace(32.1, 34.4, 250)}} cast = narwhal.cast.fromdict(d) self.assertEqual(cast.p["coordinates"], (-10.0, 54.0)) self.assertEqual(cast.p["date"], datetime.datetime(2015, 4, 17, 15, 3, tzinfo=dateutil.tz.tzutc())) self.assertEqual(cast.p["notes"], "CTD touched bottom") self.assertTrue((cast["depth"] == d["data"]["depth"]).all()) self.assertTrue((cast["temperature"] == d["data"]["temperature"]).all()) self.assertTrue((cast["salinity"] == d["data"]["salinity"]).all()) return def test_iojson_text(self): self.cast.save_json(os.path.join(DATADIR, "json_cast.nwl"), binary=False) cast = narwhal.load_json(os.path.join(DATADIR, "json_cast.nwl")) self.assertTrue(np.all(cast["pres"] == self.cast["pres"])) self.assertTrue(np.all(cast["temp"] == self.cast["temp"])) self.assertTrue(np.all(cast["sal"] == self.cast["sal"])) self.assertEqual(cast.p["date"], self.cast.p["date"]) return def test_save_json_text(self): try: f = StringIO() self.cast.save_json(f, binary=False) finally: f.close() return def test_save_json_binary(self): try: f = BytesIO() self.cast.save_json(f) finally: f.close() return def test_save_collection_json_text(self): try: f = StringIO() self.collection.save_json(f, binary=False) finally: f.close() return def test_save_collection_json_binary(self): try: f = BytesIO() self.collection.save_json(f) finally: f.close() return