|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from xrlint.node import DataArrayNode |
| 6 | +from xrlint.plugins.core.rules import plugin |
| 7 | +from xrlint.rule import RuleOp, RuleContext |
| 8 | + |
| 9 | + |
| 10 | +FLAG_MEANINGS = "flag_meanings" |
| 11 | +FLAG_VALUES = "flag_values" |
| 12 | +FLAG_MASKS = "flag_masks" |
| 13 | + |
| 14 | + |
| 15 | +@plugin.define_rule( |
| 16 | + "flags", |
| 17 | + version="1.0.0", |
| 18 | + type="suggestion", |
| 19 | + description=( |
| 20 | + "Validate attributes 'flag_values', 'flag_masks' and 'flag_meanings'" |
| 21 | + " that make variables that contain flag values self describing. " |
| 22 | + ), |
| 23 | + docs_url="https://cfconventions.org/cf-conventions/cf-conventions.html#flags", |
| 24 | +) |
| 25 | +class Flags(RuleOp): |
| 26 | + def data_array(self, ctx: RuleContext, node: DataArrayNode): |
| 27 | + flag_values = node.data_array.attrs.get(FLAG_VALUES) |
| 28 | + flag_masks = node.data_array.attrs.get(FLAG_MASKS) |
| 29 | + flag_meanings = node.data_array.attrs.get(FLAG_MEANINGS) |
| 30 | + |
| 31 | + has_values = flag_values is not None |
| 32 | + has_masks = flag_masks is not None |
| 33 | + has_meanings = flag_meanings is not None |
| 34 | + |
| 35 | + flag_count: int | None = None |
| 36 | + |
| 37 | + if has_values: |
| 38 | + flag_count = _validate_flag_values( |
| 39 | + ctx, |
| 40 | + flag_values, |
| 41 | + has_meanings, |
| 42 | + ) |
| 43 | + |
| 44 | + if has_masks: |
| 45 | + flag_count = _validate_flag_masks( |
| 46 | + ctx, |
| 47 | + flag_masks, |
| 48 | + has_meanings, |
| 49 | + flag_count, |
| 50 | + ) |
| 51 | + |
| 52 | + if has_meanings: |
| 53 | + _validate_flag_meanings( |
| 54 | + ctx, |
| 55 | + flag_meanings, |
| 56 | + has_values, |
| 57 | + has_masks, |
| 58 | + flag_count, |
| 59 | + ) |
| 60 | + |
| 61 | + if has_values and has_masks: |
| 62 | + _validate_variable( |
| 63 | + ctx, |
| 64 | + node.data_array.dtype, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def _validate_flag_values( |
| 69 | + ctx: RuleContext, flag_values: Any, has_meanings: bool |
| 70 | +) -> int | None: |
| 71 | + if not has_meanings: |
| 72 | + ctx.report( |
| 73 | + f"Missing attribute {FLAG_MEANINGS!r} to explain" |
| 74 | + f" attribute {FLAG_VALUES!r}" |
| 75 | + ) |
| 76 | + type_ok, flag_count = _check_values(flag_values) |
| 77 | + if not type_ok or flag_count is None: |
| 78 | + ctx.report( |
| 79 | + f"Attribute {FLAG_VALUES!r} must be a" |
| 80 | + " 1-d array of integers with length >= 1." |
| 81 | + ) |
| 82 | + return flag_count |
| 83 | + |
| 84 | + |
| 85 | +def _validate_flag_masks( |
| 86 | + ctx: RuleContext, flag_masks: Any, has_meanings: bool, flag_count: int | None |
| 87 | +) -> int | None: |
| 88 | + if not has_meanings: |
| 89 | + ctx.report( |
| 90 | + f"Missing attribute {FLAG_MEANINGS!r} to explain" |
| 91 | + f" attribute {FLAG_MASKS!r}" |
| 92 | + ) |
| 93 | + type_ok, flag_masks_count = _check_values(flag_masks) |
| 94 | + if not type_ok or flag_masks_count is None: |
| 95 | + ctx.report( |
| 96 | + f"Attribute {FLAG_MASKS!r} must be a" |
| 97 | + " 1-d array of integers with length >= 1." |
| 98 | + ) |
| 99 | + if flag_count is None: |
| 100 | + flag_count = flag_masks_count |
| 101 | + elif flag_masks_count is not None and flag_masks_count != flag_count: |
| 102 | + ctx.report( |
| 103 | + f"Attribute {FLAG_MASKS!r} must have same length" |
| 104 | + f" as attribute {FLAG_VALUES!r}." |
| 105 | + ) |
| 106 | + return flag_count |
| 107 | + |
| 108 | + |
| 109 | +def _validate_flag_meanings( |
| 110 | + ctx: RuleContext, |
| 111 | + flag_meanings: Any, |
| 112 | + has_values: bool, |
| 113 | + has_masks: bool, |
| 114 | + flag_count: int | None, |
| 115 | +): |
| 116 | + if not has_values and not has_masks: |
| 117 | + ctx.report( |
| 118 | + f"Missing attribute {FLAG_VALUES!r} or {FLAG_MASKS!r}" |
| 119 | + f" when attribute {FLAG_MASKS!r} is used." |
| 120 | + ) |
| 121 | + type_ok, flag_meanings_count = _check_meanings(flag_meanings) |
| 122 | + if not type_ok or flag_meanings_count is None: |
| 123 | + ctx.report( |
| 124 | + f"Attribute {FLAG_MASKS!r} must be a space-separated string" |
| 125 | + f" with at least two entries." |
| 126 | + ) |
| 127 | + if ( |
| 128 | + flag_meanings_count is not None |
| 129 | + and flag_count is not None |
| 130 | + and flag_meanings_count != flag_count |
| 131 | + ): |
| 132 | + ctx.report( |
| 133 | + f"Attribute {FLAG_MASKS!r} must have same length" |
| 134 | + f" as attributes {FLAG_VALUES!r} or {FLAG_MEANINGS!r}." |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def _validate_variable(ctx: RuleContext, var_dtype: np.dtype): |
| 139 | + if not np.issubdtype(var_dtype, np.integer): |
| 140 | + ctx.report( |
| 141 | + f"Flags variable should have an integer data type, was {var_dtype!r}" |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +def _check_values(values: Any) -> tuple[bool, int | None]: |
| 146 | + if isinstance(values, (tuple, list)) or ( |
| 147 | + isinstance(values, np.ndarray) and values.ndim == 1 |
| 148 | + ): |
| 149 | + count = len(values) |
| 150 | + return all(isinstance(v, int) for v in values), count if count >= 1 else None |
| 151 | + return False, None |
| 152 | + |
| 153 | + |
| 154 | +def _check_meanings(meanings: Any): |
| 155 | + if isinstance(meanings, str): |
| 156 | + meanings_list = [m.strip() for m in meanings.split(" ")] |
| 157 | + count = len(meanings_list) |
| 158 | + return True, count if count >= 1 else None |
| 159 | + return False, None |
0 commit comments