Esempio n. 1
0
    def test_read_metadata(self):
        meta = self.file.read_metadata()
        for k in CSV_META_TEMPLATE.keys():
            assert k in meta.keys()

        assert meta['lc_2010'].val == meta['lc_2005'].val == meta[
            'lc_2000'].val == 130
        assert meta['climate_KG'].val == 'Cfa'

        assert self.file.check_metadata(filter_meta_dict={
            'lc_2005': 130,
            'climate_KG': 'Cfa',
            'sand_fraction': 36.
        })

        sats = meta['saturation']
        assert len(sats) == 2
        assert sats[0].val == 0.46
        assert sats[1].depth == Depth(0.3, 1.)

        sands = meta['sand_fraction']
        assert len(sands) == 2
        assert sands[1].val == 29.
        assert sands[0].depth == Depth(0, 0.3)

        assert len(meta['silt_fraction']) == len(meta['clay_fraction']) == 2
        assert len(meta['organic_carbon']) == 3
        assert meta['organic_carbon'][0].val == 1.27
        assert meta['organic_carbon'][0].depth == Depth(0, 0.3)
        assert meta['organic_carbon'][1].depth == Depth(0.3, 1)

        # todo: why is there a value for this depth even?
        assert meta['organic_carbon'][2].depth == Depth(-99.9, -99.9)
        assert meta['organic_carbon'][2].val == 0.59
Esempio n. 2
0
    def test_read_metadata(self):
        meta = self.file.read_metadata()
        for k in CSV_META_TEMPLATE.keys():
            assert k in meta.keys()

        assert meta["lc_2010"].val == meta["lc_2005"].val == meta[
            "lc_2000"].val == 130
        assert meta["climate_KG"].val == "Cfa"

        assert self.file.check_metadata(filter_meta_dict={
            "lc_2005": 130,
            "climate_KG": "Cfa",
            "sand_fraction": 36.0,
        })

        sats = meta["saturation"]
        assert len(sats) == 2
        assert sats[0].val == 0.46
        assert sats[1].depth == Depth(0.3, 1.0)

        sands = meta["sand_fraction"]
        assert len(sands) == 2
        assert sands[1].val == 29.0
        assert sands[0].depth == Depth(0, 0.3)

        assert len(meta["silt_fraction"]) == len(meta["clay_fraction"]) == 2
        assert len(meta["organic_carbon"]) == 3
        assert meta["organic_carbon"][0].val == 1.27
        assert meta["organic_carbon"][0].depth == Depth(0, 0.3)
        assert meta["organic_carbon"][1].depth == Depth(0.3, 1)

        # todo: why is there a value for this depth even?
        assert meta["organic_carbon"][2].depth == Depth(-99.9, -99.9)
        assert meta["organic_carbon"][2].val == 0.59
Esempio n. 3
0
 def test_eval(self):
     assert self.sensor.eval("soil_moisture", Depth(0, 5))
     assert self.sensor.eval("soil_moisture", (0, 0.21),
                             check_only_sensor_depth_from=True)
     assert self.sensor.eval("soil_moisture",
                             Depth(0, 0.05),
                             check_only_sensor_depth_from=True)
     # fails because of varname:
     assert not self.sensor.eval("wrongname", Depth(0, 0.21))
     # fails because of depth
     assert not self.sensor.eval("soil_moisture", [0, 0.05],
                                 check_only_sensor_depth_from=False)
Esempio n. 4
0
    def setUp(self) -> None:
        vars = [
            MetaVar('first', 1, Depth(0, 1)),
            MetaVar('second', 0),
            MetaVar('neg', -99, Depth(-0.25, -1)),
            MetaVar('dup', '3rd', Depth(1, 3))
        ]

        self.dat = MetaData(vars)

        self.other = MetaData(
            [MetaVar('dup', '3rd', Depth(2, 4)),
             MetaVar('4', 4)])
Esempio n. 5
0
    def setUp(self) -> None:
        vars = [
            MetaVar("first", 1, Depth(0, 1)),
            MetaVar("second", 0),
            MetaVar("neg", -99, Depth(-0.25, -1)),
            MetaVar("dup", "3rd", Depth(1, 3)),
        ]

        self.dat = MetaData(vars)

        self.other = MetaData(
            [MetaVar("dup", "3rd", Depth(2, 4)),
             MetaVar("4", 4)])
Esempio n. 6
0
    def test_best_meta(self):
        self.dat.merge(self.other, inplace=True)

        assert len(self.dat) == 6

        # no depths overlap
        best_meta_9_10 = self.dat.best_meta_for_depth(Depth(9, 10))
        assert sorted(best_meta_9_10.keys()) == sorted(['second', '4'])

        # all depths overlap
        best_meta_inf = self.dat.best_meta_for_depth(Depth(-np.inf, np.inf))
        assert len(best_meta_inf) == len(self.dat) - 1  # one duplicate removed
        assert sorted(best_meta_inf.keys()) == sorted(
            ['second', '4', 'dup', 'first', 'neg'])
        # both valzes for dup where equally good, so the first was kept
        assert best_meta_inf['dup'].depth.start == 1
        assert best_meta_inf['dup'].depth.end == 3

        # all but one dup and neg depth overlaps
        best_meta_015 = self.dat.best_meta_for_depth(Depth(0, 1.5))
        assert len(best_meta_015) == len(self.dat) - 2
        assert best_meta_015['dup'].depth.start == 1
        assert best_meta_015['dup'].depth.end == 3

        # both duplicate depths overlap, but one more --> keep second, drop neg
        best_meta_231 = self.dat.best_meta_for_depth(Depth(2, 3.1))
        assert len(best_meta_231) == len(
            self.dat) - 3  # one duplicate and first and neg
        assert best_meta_231['dup'].depth.start == 2
        assert best_meta_231['dup'].depth.end == 4

        # both duplicate depths overlap, equally good -> keep first
        best_meta_23 = self.dat.best_meta_for_depth(Depth(2, 3))
        assert len(best_meta_23) == len(
            self.dat) - 3  # one dup and first and neg
        assert best_meta_23['dup'].depth.start == 1.
        assert best_meta_23['dup'].depth.end == 3.

        # one matches perfectly
        best_meta_13 = self.dat.best_meta_for_depth(Depth(1, 3))
        assert len(best_meta_13) == len(self.dat) - 2  # one dup only, no neg
        assert best_meta_13['dup'].depth.start == 1.
        assert best_meta_13['dup'].depth.end == 3.

        # check with negative
        best_meta_neg = self.dat.best_meta_for_depth(Depth(-0.5, 2.))
        # one dup was outside depth and is dropped, rest remains
        assert sorted(best_meta_neg.keys()) == sorted(
            ['first', 'second', 'dup', '4', 'neg'])
        assert best_meta_neg['dup'].depth.start == 1.
        assert best_meta_neg['dup'].depth.end == 3.

        # check with negative
        best_meta_only_neg = self.dat.best_meta_for_depth(Depth(-0.5, -1.))
        # only keep meta without depths and for neg depth
        assert sorted(best_meta_only_neg.keys()) == sorted(
            ['second', 'neg', '4'])
        assert best_meta_only_neg['neg'].depth.start == -0.25
        assert best_meta_only_neg['neg'].depth.end == -1
Esempio n. 7
0
 def test_sensor_attributes(self):
     """
     Test sensor attributes.
     """
     assert self.sensor.instrument == "Cosmic-ray-Probe"
     assert self.sensor.variable == "soil_moisture"
     assert self.sensor.depth == Depth(0, 0.21)
Esempio n. 8
0
    def test_MetaVar(self):
        var = MetaVar('myvar', 1.1, Depth(0, 1))
        assert str(var) == "myvar (0.0 to 1.0 [m]): 1.1"
        assert tuple(var) == ('myvar', 1.1, 0, 1)
        assert var == var

        nvar = MetaVar('negmyvar', 1.1, Depth(0, -1))
        assert str(nvar) == "negmyvar (0.0 to -1.0 [m]): 1.1"
        assert tuple(nvar) == ('negmyvar', 1.1, -0, -1)
        assert nvar != var

        other = MetaVar('other', 99)
        assert str(other) == "other (no depth): 99"
        assert tuple(other) == ('other', 99, None, None)

        assert other != var
Esempio n. 9
0
 def test_get_sensors(self):
     variable, depth = 'soil_moisture', Depth(0, 0.05)
     sensors = self.station.get_sensors(variable,
                                        depth_from=depth.start,
                                        depth_to=depth.end)
     assert len(sensors) == len([
         s
         for s in self.station.iter_sensors(variable=variable, depth=depth)
     ])
Esempio n. 10
0
 def test_iter_sensors(self):
     """
     Test if sensor1 one is found when iteration over all sensors in 0-0.05 m.
     """
     for sen in self.station.iter_sensors(
             variable='sm',
             depth=Depth(0, 0.05),
             check_only_sensor_depth_from=False):
         assert sen.name == 'sensor1_sm_0.000000_0.050000'
Esempio n. 11
0
    def setUp(self) -> None:
        """
        Setup test data.
        """
        net1 = Network('Net1')
        net1.add_station('station_1_1', 0, 0, 0)
        net1.stations['station_1_1'].add_sensor('sens_1_1_1', 'var1',
                                                Depth(0.5, 1), None)
        net1.stations['station_1_1'].add_sensor('sens_1_1_2', 'var1',
                                                Depth(1, 2), None)

        net2 = Network('Net2')
        net2.add_station('station_2_1', 1, 1, 1)
        net2.stations['station_2_1'].add_sensor('sens_2_1_1', 'var1',
                                                Depth(0.5, 1), None)
        net2.stations['station_2_1'].add_sensor('sens_2_1_2', 'var1',
                                                Depth(1, 2), None)

        self.netcol = NetworkCollection([net1, net2])
Esempio n. 12
0
 def test_get_sensors(self):
     with pytest.deprecated_call():
         variable, depth = "soil_moisture", Depth(0, 0.05)
         sensors = self.station.get_sensors(variable,
                                            depth_from=depth.start,
                                            depth_to=depth.end)
         assert len(sensors) == len([
             s for s in self.station.iter_sensors(variable=variable,
                                                  depth=depth)
         ])
Esempio n. 13
0
    def test_add_sensor(self):
        """
        Test adding sensors.
        """
        name = "sensor3"
        d = Depth(0, 0.1)
        variable = "sm"

        self.station.add_sensor(name, variable, d, None)
        assert self.station.n_sensors == 3
Esempio n. 14
0
    def setUp(self) -> None:
        """
        Setup test data.
        """
        net1 = Network("Net1")
        net1.add_station("station_1_1", 0, 0, 0)
        net1.stations["station_1_1"].add_sensor("sens_1_1_1", "var1",
                                                Depth(0.5, 1), None)
        net1.stations["station_1_1"].add_sensor("sens_1_1_2", "var1",
                                                Depth(1, 2), None)

        net2 = Network("Net2")
        net2.add_station("station_2_1", 1, 1, 1)
        net2.stations["station_2_1"].add_sensor("sens_2_1_1", "var1",
                                                Depth(0.5, 1), None)
        net2.stations["station_2_1"].add_sensor("sens_2_1_2", "var1",
                                                Depth(1, 2), None)

        self.netcol = NetworkCollection([net1, net2])
Esempio n. 15
0
    def setUp(self):
        """
        Setup test data.
        """
        self.station = Station('station1', 0, 0, 0)

        name = 'sensor1'
        d = Depth(0, 0.05)
        variable = 'sm'
        se_name = '{}_{}_{:1.6f}_{:1.6f}'.format(name, variable, d.start,
                                                 d.end)

        self.station.add_sensor(name, variable, d, None)

        assert self.station.sensors[se_name].variable == 'sm'

        name = 'sensor2'
        d = Depth(0, 0.1)
        variable = 'sm2'

        self.station.add_sensor(name, variable, d, None)

        assert self.station.n_sensors == 2
Esempio n. 16
0
    def setUp(self):
        """
        Setup test data.
        """
        self.station = Station("station1", 0, 0, 0)

        name = "sensor1"
        d = Depth(0, 0.05)
        variable = "sm"
        se_name = "{}_{}_{:1.6f}_{:1.6f}".format(name, variable, d.start,
                                                 d.end)

        self.station.add_sensor(name, variable, d, None)

        assert self.station.sensors[se_name].variable == "sm"

        name = "sensor2"
        d = Depth(0, 0.1)
        variable = "sm2"

        self.station.add_sensor(name, variable, d, None)

        assert self.station.n_sensors == 2
Esempio n. 17
0
    def test_metadata_for_depth(self):
        """Check finding best matching metadata for file"""
        allmeta = self.file.read_metadata()

        meta_for_depth = allmeta.best_meta_for_depth(Depth(0.0, 0.21))

        assert allmeta != meta_for_depth

        assert len([k for k in allmeta.keys() if k == "saturation"]) > 1
        assert len([k for k in meta_for_depth.keys()
                    if k == "saturation"]) == 1

        assert (meta_for_depth["saturation"].depth.start ==
                meta_for_depth["sand_fraction"].depth.start == 0.0)
        assert (meta_for_depth["saturation"].depth.end ==
                meta_for_depth["sand_fraction"].depth.end == 0.3)

        assert allmeta["saturation"][0].depth == meta_for_depth[
            "saturation"].depth

        assert allmeta["saturation"][1].depth.start == 0.3
        assert allmeta["saturation"][1].depth.end == 1.0
Esempio n. 18
0
    def setUp(self):
        """
        Setup test data.
        """
        instrument = 'Cosmic-ray-Probe'
        d = Depth(0, 0.21)
        variable = 'soil_moisture'

        root = os.path.join(rpath, "Data_seperate_files_20170810_20180809")
        subpath = os.path.join(
            "COSMOS", "Barrow-ARM",
            "COSMOS_COSMOS_Barrow-ARM_sm_0.000000_0.210000_Cosmic-ray-Probe_20170810_20180809.stm"
        )

        self.sensor = Sensor(instrument,
                             variable,
                             d,
                             filehandler=DataFile(root, subpath))

        name = '{}_{}_{:1.6f}_{:1.6f}'.format(instrument, variable, d.start,
                                              d.end)

        assert self.sensor.name == name
Esempio n. 19
0
    def test_iter_stations(self):
        self.network.add_station("station1", 0, 0, 0)
        self.network.stations["station1"].add_sensor("sens1", "var1",
                                                     Depth(0.5, 1), None)
        self.network.stations["station1"].add_sensor("sens2", "var1",
                                                     Depth(1, 2), None)

        for s in self.network.iter_stations(variable="var1", depth=Depth(0,
                                                                         1)):
            # sensor 1 applies to conditions, therefore station is found.
            assert s.name == "station1"

        self.network.add_station("station2", 1, 1, 1)
        self.network.stations["station2"].add_sensor("sens1", "var1",
                                                     Depth(0, 1), None)
        self.network.stations["station2"].add_sensor("sens2", "var2",
                                                     Depth(1, 2), None)

        for s in self.network.iter_stations(variable="var1",
                                            depth=Depth(0, 0.5)):
            raise ValueError("Found station but shouldn't ..."
                             )  # this should never be reached
Esempio n. 20
0
    def test_iter_stations(self):
        self.network.add_station('station1', 0, 0, 0)
        self.network.stations['station1'].add_sensor('sens1', 'var1',
                                                     Depth(0.5, 1), None)
        self.network.stations['station1'].add_sensor('sens2', 'var1',
                                                     Depth(1, 2), None)

        for s in self.network.iter_stations(variable='var1', depth=Depth(0,
                                                                         1)):
            # sensor 1 applies to conditions, therefore station is found.
            assert s.name == 'station1'

        self.network.add_station('station2', 1, 1, 1)
        self.network.stations['station2'].add_sensor('sens1', 'var1',
                                                     Depth(0, 1), None)
        self.network.stations['station2'].add_sensor('sens2', 'var2',
                                                     Depth(1, 2), None)

        for s in self.network.iter_stations(variable='var1',
                                            depth=Depth(0, 0.5)):
            raise ValueError("Found station but shouldn't ..."
                             )  # this should never be reached
Esempio n. 21
0
 def test_get_depths(self):
     """
     Test deriving the depths of all sensors measuring a variable at station
     """
     assert self.station.get_depths("sm") == [Depth(0, 0.05)]