Check attribute content against netCDF's constraints before saving

Aperta
#7,329 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
66/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
data

Direzione di ricerca

Read the netCDF saver and lib/iris/fileformats/netcdf/_attribute_handlers.py to locate where cube attributes are passed to setncattr; the issue does not name the saver file or a test. Check the existing tests for netCDF attribute saving, then add coverage for unsupported multidimensional values and errors that identify the attribute and variable. Done means invalid content fails before writing with a clear error, while valid attributes still save.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Feature: NetCDF + CF-conventions Type: Bug

(posted by Claude Opus)

Raised from @pp-mo's review of the Zarr I/O design spec: https://github.com/SciTools/iris/pull/7292#discussion_r4155016083

The problem

netCDF constrains what an attribute may hold: a scalar, a string, or a one-dimensional array. Iris does not check this before saving. A cube carrying, say, a 2-D array attribute reaches setncattr and fails inside netCDF4 with a message that names neither the attribute nor the cube.

@pp-mo:

For netCDF4 specifically, possible attribute content is further constrained (e.g. only 1-D arrays). We should make explicit checks and raise errors or warnings when saving unsupported content.

What to do

Check before writing, and raise an error naming the attribute, the variable and why the content cannot be stored.

Raise, do not warn-and-stringify. The same review suggested generalising the known-object handlers in lib/iris/fileformats/netcdf/_attribute_handlers.py — STASH codes, cell methods — into "convert anything unrecognised to its string form and emit a warning". @trexfeathers declined that, and the reasoning is worth keeping with the issue:

I confidently see _attribute_handlers as a special case for things where there is an obvious and understood way to represent something as a string. This does not generalise to everything that might not be storable in the file (e.g. someone assigns an object which has a __str__ and __repr__, but neither of which is adequate for reconstructing said object downstream; that would be a very different decision we are making). In Iris we are typically very careful about Cube-to-Cube roundtripping - can I save a Cube to a file then load back a Cube that is the same as the original - and falling back on converting all unsaveable content to a string seems to deviate from that.

The existing handlers convert where the string form is defined and reversible, and implement the reverse direction beside it. str(some_object) has neither property. A warning is also the wrong instrument here: it does not stop the save, so the user gets a file, is told in passing that part of it is now prose, and finds out at load time.

Scope

This is a behaviour change to the netCDF saver, so it is out of scope for the Zarr I/O programme (#6961) — PR 5 of that programme relocates this code without changing it. The Zarr write path already raises for a value it cannot represent, which is the same policy.

Lingua principale
Python
Stelle
730
Fork
323
Merge medio
3g 21h
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SciTools/iris

Tutte le issue di SciTools/iris

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.