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 test_eofs(self): pres = np.arange(1, 300) casts = [Cast(pres, zunits="dbar", zname="pres", theta=np.sin(pres*i*np.pi/300)) for i in range(3)] cc = CastCollection(casts) structures, lamb, eofs = cc.eofs("theta") self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof1"])), 0.634719360307) self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof2"])), 0.363733575635) self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof3"])), 0.350665870142) self.assertTrue(np.allclose(lamb, [87.27018523, 40.37800904, 2.02016724])) return
def test_nearest_to(self): casts = [] for i in range(10): casts.append(Cast(pressure=[1, 2, 3], temperature=[5, 6, 7], coordinates=(-30.0 + i, 10.0 - 2*i))) cc = CastCollection(casts) pt = Point((-26.2, 2.1), crs=LonLatWGS84) nearest, dist = cc.nearest_to_point(pt) self.assertEqual(nearest.coordinates.vertex, (-26.0, 2.0)) self.assertAlmostEqual(dist, 24845.9422, places=4) return
def test_nearest_to(self): casts = [] for i in range(10): casts.append( Cast(pressure=[1, 2, 3], temperature=[5, 6, 7], coordinates=(-30.0 + i, 10.0 - 2 * i))) cc = CastCollection(casts) pt = Point((-26.2, 2.1), crs=LonLatWGS84) nearest, dist = cc.nearest_to_point(pt) self.assertEqual(nearest.coordinates.vertex, (-26.0, 2.0)) self.assertAlmostEqual(dist, 24845.9422, places=4) return
def setUp(self): p = np.linspace(1, 999, 500) casts = [] for i in range(10): cast = Cast(pres=p, temp=2 * np.ones_like(p), sal=30 * np.ones_like(p), station=i, val=abs(i - 5), uniq_val=-i**2) casts.append(cast) self.cc = CastCollection(casts) return
def test_defray(self): lengths = np.arange(50, 71) casts = [] for n in lengths: p = np.r_[np.arange(0, 250, 5), np.arange(250, 250 + 5 * (n - 50), 5)] t = np.ones(n) * 10.0 s = np.ones(n) * 34.0 cast = Cast(pres=p, temp=t, sal=s) casts.append(cast) defrayed_casts = CastCollection(casts).defray() for cast in defrayed_casts: self.assertEqual(len(cast), 70) return
def test_eofs(self): pres = np.arange(1, 300) casts = [ Cast(depth=np.arange(1, 300), theta=np.sin(pres * i * np.pi / 300)) for i in range(3) ] cc = CastCollection(casts) structures, lamb, eofs = narwhal.analysis.eofs(cc, key="theta") self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof1"])), 0.634719360307) self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof2"])), 0.363733575635) self.assertAlmostEqual(np.mean(np.abs(structures["theta_eof3"])), 0.350665870142) self.assertTrue( np.allclose(lamb, [87.27018523, 40.37800904, 2.02016724])) return
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
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(self.p, temp=self.temp, sal=self.sal, date=dt) self.ctd = CTDCast(self.p, temp=self.temp, sal=self.sal, date=dt) self.xbt = XBTCast(self.p, temp=self.temp, sal=self.sal, date=dt) self.collection = CastCollection(self.ctd, self.xbt, 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_save_text(self): try: f = StringIO() self.cast.save(f, binary=False) finally: f.close() try: f = StringIO() self.ctd.save(f, binary=False) finally: f.close() try: f = StringIO() self.xbt.save(f, binary=False) finally: f.close() return def test_save_binary(self): try: f = BytesIO() self.cast.save(f) finally: f.close() try: f = BytesIO() self.ctd.save(f) finally: f.close() try: f = BytesIO() self.xbt.save(f) finally: f.close() return def test_save_collection_text(self): try: f = StringIO() self.collection.save(f, binary=False) finally: f.close() return def test_save_collection_binary(self): try: f = BytesIO() self.collection.save(f) finally: f.close() return def test_save_zprimarykey(self): cast = Cast(np.arange(len(self.p)), temp=self.temp, sal=self.sal, primarykey="z", properties={}) f = BytesIO() try: cast.save(f) finally: f.close() return def test_load_text(self): cast = narwhal.read(os.path.join(DATADIR, "reference_cast_test.nwl")) ctd = narwhal.read(os.path.join(DATADIR, "reference_ctd_test.nwl")) xbt = narwhal.read(os.path.join(DATADIR, "reference_xbt_test.nwl")) self.assertEqual(cast, self.cast) self.assertEqual(ctd, self.ctd) self.assertEqual(xbt, self.xbt) return def test_load_binary(self): cast = narwhal.read(os.path.join(DATADIR, "reference_cast_test.nwz")) ctd = narwhal.read(os.path.join(DATADIR, "reference_ctd_test.nwz")) xbt = narwhal.read(os.path.join(DATADIR, "reference_xbt_test.nwz")) self.assertEqual(cast, self.cast) self.assertEqual(ctd, self.ctd) self.assertEqual(xbt, self.xbt) return def test_load_collection_text(self): coll = narwhal.read(os.path.join(DATADIR, "reference_coll_test.nwl")) self.assertEqual(coll, self.collection) return def test_load_collection_binary(self): coll = narwhal.read(os.path.join(DATADIR, "reference_coll_test.nwz")) self.assertEqual(coll, self.collection) return
def test_castswhere_onearg(self): cc = self.cc self.assertEqual(cc.castswhere("station", 5), CastCollection(cc[5])) return