Open pp-mo opened 5 months ago
We could also maybe be strict about expected content, to avoid obvious problems ...
type(el.attributes['x']) == NcAttribute
)
container['x'].name == 'x'
since, especially, it's far too easy to write el.attributes['x'] = val
in mistake for '= NcAttribute('x', val)` But, this approach involves plugging all loopholes for different means of putting things in a container,
such as 'extend', 'update', etc.
That is tricky to ensure if you provide a subclass of 'dict', since you need to be sure what list of operations needs to be modified. Meanwhile, it's easier to be sure of completeness if you subclass collections.MutableMapping
(like iris CubeAttrsDict). But even then, the correctness + of the solution is not obvious -- and the result no longer satisfies isinstance(x, dict)
, and might need extra methods adding.
In any case, strictness + correctness is hard to maintain since the objects are designed for free use.
For example, el.attributes['x'] = attr = NcAttribute('x', val)
, but then you can just attr.name = 'y'
In that view, it makes sense to make it easy to do things 'right', preserving the expected. By which logic, we should provide utilities such as :
x[key].name == key
)var.data.shape == (dims[dimname].size for dimname in var.dimensions)
conclusion :
Although strictly excluded as a goal for the initial release, I still think the 'secondary' usage of ncdata will be useful :
For this there real scope for some convenience and sugar. Some ideas :
ds.is_valid(error_when_not=False)
: checking the consistencies not ensured by the free-and-easy designds.variables['x'].name == 'x'
)el.variables[var.name] = var
-->el.variables.add(var)
ds.variables.rename('x', 'y')
NcData(dimensions=nc_dims(x=3, y=5, t=(2, True)), variables=nc_vars(x=(['x'], int), y=(['y'], int), data=(['t', 'y', 'x'], float))
(or something !)el.ncd_setatt(name, value)
\~=el.attributes[name] = NcAttribute(name, value)
el.ncd_getatt(name)
\~=el.attributes.get('name', NcAttribute('', None)).as_python_value()
Update:
v0.1.1 delivered most of this :
For instance, some actions I needed to adjust a given file output from xarray so that Iris can correctly interpret the coord-system ...
So, how about