def units_convertible(units1, units2, reftimeistime=True): """Return True if a Unit representing the string units1 can be converted to a Unit representing the string units2, else False.""" try: u1 = Unit(units1) u2 = Unit(units2) except ValueError: return False return u1.is_convertible(units2)
def _check_variable_attrs(dataset, var_name, required_attributes=None): ''' Convenience method to check a variable attributes based on the expected_vars dict ''' score = 0 out_of = 0 messages = [] if var_name not in dataset.variables: # No need to check the attrs if the variable doesn't exist return (score, out_of, messages) var = dataset.variables[var_name] # Get the expected attrs to check check_attrs = required_attributes or required_var_attrs.get(var_name, {}) var_attrs = set(var.ncattrs()) for attr in check_attrs: if attr == 'dtype': # dtype check is special, see above continue out_of += 1 score += 1 # Check if the attribute is present if attr not in var_attrs: messages.append('Variable {} must contain attribute: {}' ''.format(var_name, attr)) score -= 1 continue # Attribute exists, let's check if there was a value we need to compare against if check_attrs[attr] is not None: if getattr(var, attr) != check_attrs[attr]: # No match, this may be an error, but first an exception for units if attr == 'units': msg = ('Variable {} units attribute must be ' 'convertible to {}'.format(var_name, check_attrs[attr])) try: cur_unit = Unit(var.units) comp_unit = Unit(check_attrs[attr]) if not cur_unit.is_convertible(comp_unit): messages.append(msg) score -= 1 except ValueError: messages.append(msg) score -= 1 else: messages.append( 'Variable {} attribute {} must be {}'.format( var_name, attr, check_attrs[attr])) score -= 1 else: # Final check to make sure the attribute isn't an empty string try: # try stripping whitespace, and return an error if empty att_strip = getattr(var, attr).strip() if not att_strip: messages.append('Variable {} attribute {} is empty' ''.format(var_name, attr)) score -= 1 except AttributeError: pass return (score, out_of, messages)
def _check_variable_attrs(dataset, var_name, required_attributes=None): ''' Convenience method to check a variable attributes based on the expected_vars dict ''' score = 0 out_of = 0 messages = [] if var_name not in dataset.variables: # No need to check the attrs if the variable doesn't exist return (score, out_of, messages) var = dataset.variables[var_name] # Get the expected attrs to check check_attrs = required_attributes or required_var_attrs.get(var_name, {}) var_attrs = set(var.ncattrs()) for attr in check_attrs: if attr == 'dtype': # dtype check is special, see above continue out_of += 1 score += 1 # Check if the attribute is present if attr not in var_attrs: messages.append('Variable {} must contain attribute: {}' ''.format(var_name, attr)) score -= 1 continue # Attribute exists, let's check if there was a value we need to compare against if check_attrs[attr] is not None: if getattr(var, attr) != check_attrs[attr]: # No match, this may be an error, but first an exception for units if attr == 'units': msg = ('Variable {} units attribute must be ' 'convertible to {}'.format(var_name, check_attrs[attr])) try: cur_unit = Unit(var.units) comp_unit = Unit(check_attrs[attr]) if not cur_unit.is_convertible(comp_unit): messages.append(msg) score -= 1 except ValueError: messages.append(msg) score -= 1 else: messages.append('Variable {} attribute {} must be {}'.format(var_name, attr, check_attrs[attr])) score -= 1 else: # Final check to make sure the attribute isn't an empty string try: # try stripping whitespace, and return an error if empty att_strip = getattr(var, attr).strip() if not att_strip: messages.append('Variable {} attribute {} is empty' ''.format(var_name, attr)) score -= 1 except AttributeError: pass return (score, out_of, messages)