def test_add_buoyancy_freq_squared(self): # This is a fairly lousy test, merely ensuring that an N^2 field was # calculated, and that it's not wildly different than the direct # calculation. p = np.arange(10) t = 20.0 * 0.2 * p s = 30.0 * 0.25 * p x = [-20.0 for _ in p] y = [50.0 for _ in p] sa = gsw.sa_from_sp(s, p, x, y) ct = gsw.ct_from_t(sa, t, p) rho = np.asarray(gsw.rho(sa, ct, p)) cast = CTDCast(p, s, t, coordinates=(-20, 50), density=rho) cast.add_depth() cast.add_Nsquared(depthkey="depth") # Calculate the buoyancy frequency directly z = cast["depth"].values drhodz = -np.r_[rho[1]-rho[0], rho[2:]-rho[:-2], rho[-1]-rho[-2]] / \ np.r_[z[1]-z[0], z[2:]-z[:-2], z[-1]-z[-2]] N2_direct = -9.81 / rho * drhodz self.assertTrue( np.mean(np.abs(cast["N2"][1:] - N2_direct[1:])) < 0.0004) 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 self.cast = CTDCast(p, sal, temp) 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 test_three_sources_constant(self): ans = np.array([0.3, 0.2, 0.5]) sources = [(34.0, 2.0), (34.5, 7.0), (34.6, 5.0)] s = np.array(sources) x = np.ones(10, dtype=np.float64) sal = x * np.dot(ans, s[:, 0]) tmp = x * np.dot(ans, s[:, 1]) c = CTDCast(np.arange(10), sal, tmp) (chi1, chi2, chi3) = narwhal.analysis.water_fractions(c, sources) self.assertTrue(np.allclose(chi1, 0.3 * np.ones(10))) self.assertTrue(np.allclose(chi2, 0.2 * np.ones(10))) self.assertTrue(np.allclose(chi3, 0.5 * np.ones(10))) return
def test_add_density(self): p = np.arange(10) t = 20.0 * 0.2 * p s = 30.0 * 0.25 * p x = [-20.0 for _ in p] y = [50.0 for _ in p] sa = gsw.sa_from_sp(s, p, x, y) ct = gsw.ct_from_t(sa, t, p) rho = gsw.rho(sa, ct, p) cast = CTDCast(p, s, t, coordinates=(-20, 50)) cast.add_density() self.assertTrue(np.allclose(rho, cast["density"])) return
def test_three_sources_varying(self): ans_chi1 = np.linspace(0.2, 0.35, 10) ans_chi2 = np.linspace(0.6, 0.1, 10) ans_chi3 = 1.0 - (ans_chi1 + ans_chi2) ans = np.c_[ans_chi1, ans_chi2, ans_chi3] sources = [(34.0, 2.0), (34.5, 7.0), (34.6, 5.0)] s = np.array(sources) x = np.ones(10, dtype=np.float64) sal = x * np.dot(ans, s[:, 0]) tmp = x * np.dot(ans, s[:, 1]) c = CTDCast(np.arange(10), sal, tmp) (chi1, chi2, chi3) = narwhal.analysis.water_fractions(c, sources) self.assertTrue(np.allclose(chi1, ans_chi1)) self.assertTrue(np.allclose(chi2, ans_chi2)) self.assertTrue(np.allclose(chi3, ans_chi3)) return
def test_four_sources_varying(self): ans_chi1 = np.linspace(0.2, 0.35, 10) ans_chi2 = np.linspace(0.6, 0.1, 10) ans_chi3 = np.linspace(0.05, 0.12, 10) ans_chi4 = 1.0 - (ans_chi1 + ans_chi2 + ans_chi3) ans = np.c_[ans_chi1, ans_chi2, ans_chi3, ans_chi4] sources = [(34.0, 2.0, 280.0), (34.5, 70.0, 250.0), (34.6, 5.0, 330.0), (33.9, 18.0, 390.0)] s = np.array(sources) x = np.ones(10, dtype=np.float64) sal = x * np.dot(ans, s[:, 0]) tmp = x * np.dot(ans, s[:, 1]) oxy = x * np.dot(ans, s[:, 2]) c = CTDCast(np.arange(10), sal, tmp, oxygen=oxy) (chi1, chi2, chi3, chi4) = narwhal.analysis.water_fractions( c, sources, tracers=["salinity", "temperature", "oxygen"]) self.assertTrue(np.allclose(chi1, ans_chi1)) self.assertTrue(np.allclose(chi2, ans_chi2)) self.assertTrue(np.allclose(chi3, ans_chi3)) self.assertTrue(np.allclose(chi4, ans_chi4)) return