示例#1
0
 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
示例#2
0
 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
示例#3
0
 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
示例#4
0
 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
示例#5
0
 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
示例#6
0
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)
示例#7
0
 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
示例#8
0
    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
示例#9
0
    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
示例#10
0
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)
示例#11
0
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
示例#12
0
 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
示例#13
0
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
示例#14
0
 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
示例#15
0
 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