def _get_result(self, primary_arg):
        ds = primary_arg

        score = 0
        messages = []

        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])
        fname = os.path.basename(ds.filepath())

        fn_score, msg = vocabs.check_file_name(fname, keys=self.kwargs["order"],
                                               delimiter=self.kwargs["delimiter"],
                                               extension=self.kwargs["extension"])
        score += fn_score
        if fn_score < (self.out_of / 3.):
            # a third of the marks are for the file name check
            messages.extend(msg)

        # Check global attributes one-by-one
        items = os.path.splitext(fname)[0].split(self.kwargs["delimiter"])

        for i, attr in enumerate(self.kwargs["order"]):
            if attr.startswith('regex:') or attr in self.kwargs["ignore_attr_checks"]:
                # Case 1: we do not have the attribute name - so cannot check
                # Case 2: instructed to not perform this check
                continue
            res, msg = vocabs.check_global_attribute_value(ds, attr, items[i],
                                                           property="raw_name")
            score += res

            if res < 2:
                messages.extend(msg)

        return Result(self.level, (score, self.out_of),
                      self.get_short_name(), messages)
Ejemplo n.º 2
0
    def _get_result(self, primary_arg):
        ds = primary_arg
        score = 0
        self.out_of = 1
        messages = []

        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])

        var_id = self.kwargs["var_id"]
        if var_id in ds.variables:
            array = ds[var_id][:]
            result = vocabs.check_array_matches_terms(
                array, self.kwargs["pyessv_namespace"])

            if result:
                score += 1
            else:
                messages.append(self.get_messages()[score])

        else:
            messages.append(
                "Variable '{}' not found in the file so cannot perform other checks."
                .format(var_id))

        return Result(self.level, (score, self.out_of), self.get_short_name(),
                      messages)
    def _get_result(self, primary_arg):
        ds = primary_arg
        var_id = self.kwargs["var_id"]

        messages = []
        score = 0

        # Check the variable exists first
        if var_id not in ds.variables:
            messages = [self.get_messages()[score]]
            return Result(self.level, (score, self.out_of),
                          self.get_short_name(), messages)

        score += 1

        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])
        expected_values = vocabs.get_value("coordinate:{}".format(var_id),
                                           "data")["value"]

        actual_values = ds.variables[var_id][:]

        # Cast to a list if not iterable
        if not hasattr(actual_values, "__len__"):
            actual_values = [actual_values]

        if list(expected_values) == list(actual_values):
            score += 1
        else:
            messages = [self.get_messages()[score]]

        return Result(self.level, (score, self.out_of), self.get_short_name(),
                      messages)
    def _get_result(self, primary_arg):

        ds = primary_arg
        var_id = self.kwargs["var_id"]

        messages = []
        score = 0

        # Check the variable exists first
        if var_id not in ds.variables:
            messages = [self.get_messages()[score]]
            return Result(self.level, (score, self.out_of),
                          self.get_short_name(), messages)

        score += 1

        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])
        expected_length = vocabs.get_value("coordinate:{}".format(var_id),
                                           "data")["length"]

        actual_length = len(ds.variables[var_id][:])

        if expected_length == actual_length:
            score += 1
        else:
            messages = [self.get_messages()[score]]

        return Result(self.level, (score, self.out_of), self.get_short_name(),
                      messages)
    def _get_result(self, primary_arg):
        ds = primary_arg
        score = 0
        var_id = self._get_var_id(ds)

        # Check the variable first (will match if `var_id` is None from previous call)
        if var_id not in ds.variables:
            messages = self.get_messages()[:1]
            return Result(self.level, (score, self.out_of),
                          self.get_short_name(), messages)

        # Work out the overall 'out of' value based on number of attributes
        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])
        lookup = ":".join([self.kwargs["pyessv_namespace"], var_id])
        expected_attr_dict = vocabs.get_value(lookup, "data")

        self.out_of = 1 + len(expected_attr_dict) * 2

        score += 1
        variable = ds.variables[var_id]
        messages = []

        # Check the variable attributes one-by-one
        for attr, expected_value in expected_attr_dict.items():

            # Check items to ignore
            ignores = self.kwargs["ignores"]

            if ignores and attr in ignores:
                self.out_of -= 2
                continue

            KNOWN_IGNORE_VALUES = ("<derived from file>",)

            if expected_value in KNOWN_IGNORE_VALUES:
                self.out_of -= 2
                continue

            if attr not in variable.ncattrs():
                messages.append("Required variable attribute '{}' is not present for "
                                "variable: '{}'.".format(attr, var_id))
            else:
                score += 1
                # Check the value of attribute
                check = nc_util.check_nc_attribute(variable, attr, expected_value)
                if check:
                    score += 1
                else:
                    messages.append(u"Required variable attribute '{}' has incorrect value ('{}') "
                                    u"for variable: '{}'. Value should be: '{}'.".format(attr,
                                                                                        getattr(variable, attr), var_id,
                                                                                        expected_value))

        return Result(self.level, (score, self.out_of),
                      self.get_short_name(), messages)
    def _get_result(self, primary_arg):
        ds = primary_arg
        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])

        score = vocabs.check_global_attribute(ds, self.kwargs["attribute"], vocab_lookup=self.kwargs["vocab_lookup"])
        messages = []

        if score < self.out_of:
            messages.append(self.get_messages()[score])

        return Result(self.level, (score, self.out_of),
                      self.get_short_name(), messages)
    def _get_result(self, primary_arg):
        ds = primary_arg
        dim_id = self.kwargs["dim_id"]
        ignore_coord_var_check = util._parse_boolean(self.kwargs["ignore_coord_var_check"])

        score = 0
        messages = []

        if dim_id in ds.dimensions:
            score += 1
            self.out_of = 1
        else:
            messages = [self.get_messages()[score],
                        "Cannot look up coordinate variable because dimension does not exist.",
                        "Cannot assess coordinate variable properties because dimension does not exist."]
            self.out_of = len(messages)

            # Now return because all other checks are irrelevant
            return Result(self.level, (score, self.out_of), self.get_short_name(), messages)

        # Now test coordinate variable using look-up in vocabularies
        # First, work out the overall 'out of' value based on number of attributes
        vocabs = ESSVocabs(*self.vocabulary_ref.split(":")[:2])
        lookup = ":".join([self.kwargs["pyessv_namespace"], dim_id])
        expected_attr_dict = vocabs.get_value(lookup, "data")

        # Check length if needed
        if "length" in expected_attr_dict:
            req_length = expected_attr_dict["length"]

            # If expected length is <i> or <n> etc then dimension length does
            # not matter, so give +1 without checking
            skip_check = req_length.startswith("<") and req_length.endswith(">")
            if skip_check or int(req_length) == ds.dimensions[dim_id].size:
                score += 1
            else:
                messages.append("Dimension '{}' does not have required length: {}.".format(dim_id, req_length))

            self.out_of += 1

        # Ignore coordinate variable check if instructed to
        if ignore_coord_var_check:
            pass
        # Check coordinate variable exists for dimension
        elif dim_id in ds.variables:
            score += 1
            self.out_of += 1

            variable = ds.variables[dim_id]

            # Check the coordinate variable attributes one-by-one
            for attr, expected_value in expected_attr_dict.items():
                # Length has already been checked - so ignore here
                if attr == "length": continue

                self.out_of += 1

                if attr not in variable.ncattrs():
                    messages.append("Required variable attribute '{}' is not present for "
                                    "coorinate variable: '{}'.".format(attr, dim_id))
                else:
                    score += 1
                    self.out_of += 1

                    # Check the value of attribute
                    check = nc_util.check_nc_attribute(variable, attr, expected_value)
                    if check:
                        score += 1
                    else:
                        messages.append("Required variable attribute '{}' has incorrect value ('{}') "
                                        "for variable: '{}'. Value should be: '{}'.".format(attr,
                                        getattr(variable, attr), dim_id, expected_value))
        # If coordinate variable not found
        else:
            messages.append("Coordinate variable for dimension not found: {}.".format(dim_id))
            self.out_of += 1

        return Result(self.level, (score, self.out_of), self.get_short_name(), messages)