|
| 1 | +import math |
| 2 | + |
| 3 | +from xrlint.node import DataArrayNode |
| 4 | +from xrlint.plugins.xcube.plugin import plugin |
| 5 | +from xrlint.rule import RuleContext, RuleOp |
| 6 | +from xrlint.util.schema import schema |
| 7 | + |
| 8 | +DEFAULT_LIMIT = 5 |
| 9 | + |
| 10 | + |
| 11 | +@plugin.define_rule( |
| 12 | + "no-chunked-coords", |
| 13 | + version="1.0.0", |
| 14 | + type="problem", |
| 15 | + description=( |
| 16 | + "Coordinate variables should not be chunked." |
| 17 | + " Can be used to identify performance issues, where chunked coordinates" |
| 18 | + " can cause slow opening if datasets due to the many chunk-fetching" |
| 19 | + " requests made to (remote) filesystems with low bandwidth." |
| 20 | + " You can use the `limit` parameter to specify an acceptable number " |
| 21 | + f" of chunks. Its default is {DEFAULT_LIMIT}." |
| 22 | + ), |
| 23 | + schema=schema( |
| 24 | + "object", |
| 25 | + properties=dict( |
| 26 | + limit=schema( |
| 27 | + "integer", |
| 28 | + minimum=0, |
| 29 | + default=DEFAULT_LIMIT, |
| 30 | + title="Acceptable number of chunks", |
| 31 | + ) |
| 32 | + ), |
| 33 | + ), |
| 34 | +) |
| 35 | +class NoChunkedCoords(RuleOp): |
| 36 | + def __init__(self, limit: int = DEFAULT_LIMIT): |
| 37 | + self.limit = limit |
| 38 | + |
| 39 | + def data_array(self, ctx: RuleContext, node: DataArrayNode): |
| 40 | + if node.name not in ctx.dataset.coords or node.data_array.ndim != 1: |
| 41 | + return |
| 42 | + |
| 43 | + chunks = node.data_array.encoding.get("chunks") |
| 44 | + if isinstance(chunks, (list, tuple)) and len(chunks) == 1: |
| 45 | + num_chunks = math.ceil(node.data_array.size / chunks[0]) |
| 46 | + if num_chunks > self.limit: |
| 47 | + ctx.report( |
| 48 | + f"Number of chunks exceeds limit: {num_chunks} > {self.limit}.", |
| 49 | + suggestions=["Combine chunks into a one or more larger ones."], |
| 50 | + ) |
0 commit comments