Check attribute content against netCDF's constraints before saving

Đang mở
#7,329 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
66/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
data

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
730
Fork
323
Merge trung bình
3 ngày 21 giờ
Pull request đã merge (30 ngày)
16

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của SciTools/iris

Tất cả issue của SciTools/iris

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.