예제 #1
0
    def test_neg_depth(self):
        other = Depth(0.0, -0.05)
        assert str(other) == "0.0 to -0.05 [m]"

        assert self.d != other
        assert other.encloses(self.d) == False
        assert other.enclosed(self.d) == False
        assert self.d.across0 == other.across0 == False
        assert self.d.is_profile == other.is_profile == True

        assert other.perc_overlap(self.d) == 0.0
예제 #2
0
파일: components.py 프로젝트: lugeo17/ismn
    def eval(self,
             variable=None,
             depth=None,
             filter_meta_dict=None,
             check_only_sensor_depth_from=False):
        """
        Evaluate whether the sensor complies with the passed metadata
        requirements.

        Parameters
        ----------
        variable : str, optional (default: None)
            Check if the variable name matches, e.g. soil_moisture
        depth : Depth or list or tuple, optional (default: None)
            Check if the passed depth encloses the sensor depth.
            A list/tuple must contain 2 values where the first is the depth start
            and the second is the end. Start must be closer to 0 than end (or equal).
            A negative depth range is above the surface.
        filter_meta_dict : dict, optional (default: None)
            Additional metadata keys and values for which the file list is filtered
            e.g. {'lc_2010': [10, 130]} or
                 {'climate_KG': 'Dwa', 'lc_2010': [10, 130] }
            to filter for a multiple landcover classes and a climate class.
        check_only_sensor_depth_from : bool, optional (default: False)
            Ignores the sensors depth_to value and only checks if depth_from of
            the sensor is in the passed depth (e.g. for cosmic ray probes).

        Returns
        -------
        flag : bool
            Indicates weather metadata for this Sensor matches with the passed
            requirements.
        """
        if isinstance(depth, (list, tuple)):
            depth = Depth(depth[0], depth[1])

        if depth is None:
            depth = Depth(-np.inf, np.inf)

        flag = False

        if check_only_sensor_depth_from:
            d = Depth(self.depth.start, self.depth.start)
        else:
            d = self.depth

        if (variable in [None, self.variable]) and depth.encloses(d):
            flag = True

        if flag and filter_meta_dict:
            if self.filehandler is None:
                warnings.warn("No filehandle found, can't filter by metadata.")
            else:
                # checks also if the metadata in file matches
                flag = self.filehandler.check_metadata(
                    variable,
                    allowed_depth=depth,
                    filter_meta_dict=filter_meta_dict,
                    check_only_sensor_depth_from=check_only_sensor_depth_from)

        return flag
예제 #3
0
class DepthTest(unittest.TestCase):
    def setUp(self):
        """
        Setup test data.
        """
        self.d = Depth(0, 0.05)
        assert str(self.d) == "0.0 to 0.05 [m]"
        assert tuple(self.d) == (0.0, 0.05)

    def test_neg_depth(self):
        other = Depth(0.0, -0.05)
        assert str(other) == "0.0 to -0.05 [m]"

        assert self.d != other
        assert other.encloses(self.d) == False
        assert other.enclosed(self.d) == False
        assert self.d.across0 == other.across0 == False
        assert self.d.is_profile == other.is_profile == True

        assert other.perc_overlap(self.d) == 0.0

    def test_attributes(self):
        """
        Test depth attributes.
        """
        assert self.d.start == 0
        assert self.d.end == 0.05

    def test_is_profile(self):
        """
        Test if depth represents a profile.
        """
        assert self.d.is_profile

    def test_equal(self):
        """
        Test depth equality.
        """
        other = Depth(0, 0.05)
        assert self.d == other

    def test_invalid(self):
        try:
            Depth(0.5, 0.1)
            raise AssertionError
        except DepthError:
            pass
        try:
            Depth(-0.5, -0.1)
            raise AssertionError
        except DepthError:
            pass

    def test_enclose(self):
        """
        Test if other depth encloses depth.
        """
        other = Depth(0, 0.05)
        assert self.d.encloses(other)
        assert other.enclosed(self.d)

        other = Depth(0, 0.1)
        assert not self.d.encloses(other)
        assert not other.enclosed(self.d)
        assert other.across0 == False

        other = Depth(-0.1, -0.2)
        assert not self.d.encloses(other)
        assert not self.d.enclosed(other)
        assert other.across0 == False

    def test_perc_overlap(self):
        other = Depth(0.05, 0.1)
        assert other.across0 == False
        assert self.d.overlap(other) == True
        assert self.d.perc_overlap(other) == 0.0

        other = Depth(0.03, 0.05)
        assert other.across0 == False
        assert self.d.overlap(other) == True
        assert self.d.perc_overlap(other) == round(0.02 / 0.05, 7)

        other = Depth(0, 0.05)
        assert other.across0 == False
        assert self.d.overlap(other) == True
        assert self.d.perc_overlap(other) == 1.0

        other = Depth(-0.01, -0.05)
        assert other.across0 == False
        assert self.d.overlap(other) == False
        assert self.d.perc_overlap(other) == -1

        other = Depth(-0.01, 0.01)
        assert other.across0 == True
        assert self.d.overlap(other) == True
        assert self.d.perc_overlap(other) == round(0.01 / 0.06, 7)