def test_kw_property_indexing(self): cast = Cast(pressure=self.p, temp=self.temp, sal=self.sal, name="Cruise station 7") self.assertEqual(cast.p["name"], "Cruise station 7") return
def test_concatenation(self): p = np.arange(1, 1001, 2) temp = 12. * np.exp(-.007 * p) - 14. * np.exp(-0.005 * (p + 100)) + 1.8 sal = -13. * np.exp(-.01 * p) + 34.5 cast2 = Cast(pres=p, temp=temp, sal=sal) cc = self.cast + cast2 self.assertTrue(isinstance(cc, CastCollection)) self.assertEqual(len(cc), 2) return
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 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 ccmeanp(cc): if False in (np.all(cc[0]["pres"] == c["pres"]) for c in cc): raise ValueError("casts must share pressure levels") p = cc[0]["pres"] # shared keys are those in all casts, minus pressure and botdepth sharedkeys = set(cc[0].data.keys()).intersection( *[set(c.data.keys()) for c in cc[1:]]).difference(set(("pres", "botdepth", "time"))) data = dict() for key in sharedkeys: arr = np.vstack([c.data[key] for c in cc]) data[key] = _nanmean(arr, axis=1) return Cast(p, **data)
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_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_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
def ccmeans(cc): """ Calculate a mean Cast along isopycnals from a CastCollection. """ c0 = max(cc, key=lambda c: c.nvalid()) s0 = c0["sigma"] sharedkeys = set(c0.data.keys()).intersection( *[set(c.data.keys()) for c in cc[1:]]).difference(set(("pres", "botdepth", "time"))) nanmask = reduce(lambda a, b: a * b, [c.nanmask() for c in cc]) data = dict() for key in sharedkeys: arr = np.nan * np.empty((len(cc), len(c0["pres"]))) arr[0, :] = c0[key] for j, c in enumerate(cc[1:]): s = np.convolve(c["sigma"], np.ones(3) / 3.0, mode="same") arr[j + 1, :] = np.interp(s0, s, c[key]) data[key] = _nanmean(arr, axis=1) data[key][nanmask] = np.nan return Cast(copy.copy(c0["pres"]), **data)
def test_read_property_using_alias(self): cast = Cast(pressure=self.p, temp=self.temp, sal=self.sal, time="late") self.assertEqual(cast.p["time"], "late") return
def test_add_property_using_alias(self): cast = Cast(pres=self.p, temp=self.temp, sal=self.sal) cast.p["comment"] = "performed bottle cast #23" self.assertEqual(cast.properties["comment"][-2:], "23") return