Skip to content

Commit 74b35e4

Browse files
bzahAbel Aoun
andauthored
FIX: #70 scalar parsing (#71)
* FIX: #70 scalar parsing --------- Co-authored-by: Abel Aoun <[email protected]>
1 parent d2ee816 commit 74b35e4

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
0.5.0 (unreleased)
22
==================
33

4-
**Breaking changes**
4+
Breaking changes
5+
^^^^^^^^^^^^^^^^
6+
57
- Nested group handling:
68
Before this version, all groups were read, but conflicting variable names in-between groups would shadow data. Now, similarly to xarray ``open_dataset``, ``open_ncml`` accepts an optional ``group`` argument to specify which group should be read. When ``group`` is not specified, it defaults to the root group. Additionally ``group`` can be set to ``'*'`` so that every group is read and the hierarchy is flattened. In the event of conflicting variable/dimension names across groups, the conflicting name will be modified by appending ``'__n'`` where n is incremented.
79
- Enums are no longer transformed into CF flag_values and flag_meanings attributes, instead they are stored in the ``encoding["dtype"].metadata`` of their respective variable. This is aligned with what is done on xarray v2024.01.0
10+
- [fix] scalar attributes that are not strings are no longer wrapped in tuples of length 1.
811

912
0.4.0 (2024-01-08)
1013
==================
@@ -13,7 +16,7 @@
1316
- Update XSD schema and dataclasses to latest version from netcdf-java to add support
1417
for unsigned types. By @bzah
1518
- Add support for scalar variables. By @Bzah
16-
- [fix] empty attributes now are parsed into an empty string instead of crashing the parser. By @Bzah
19+
- [fix] empty attributes are now parsed into an empty string instead of crashing the parser. By @Bzah
1720

1821
0.3.1 (2023-11-10)
1922
==================

tests/data/testDoubleAttr.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">
3+
<attribute name="toto" type="double" value="42.42" />
4+
<attribute name="comment" value="" />
5+
</netcdf>

tests/test_parser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ def test_flatten_groups__sub_groups():
403403
assert ds['a_var__2'].size == 22
404404

405405

406+
def test_read_non_str_attribute():
407+
ds = xncml.open_ncml(data / 'testDoubleAttr.xml')
408+
assert ds.attrs['toto'] == 42.42
409+
410+
406411
# --- #
407412
def check_dimension(ds):
408413
assert len(ds['lat']) == 3

xncml/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,10 @@ def cast(obj: Attribute) -> tuple | str:
779779
if value:
780780
if obj.type in [DataType.STRING, DataType.STRING_1]:
781781
return value
782-
783782
sep = obj.separator or ' '
784783
values = value.split(sep)
784+
if len(values) == 1:
785+
return nctype(obj.type)(values[0])
785786
return tuple(map(nctype(obj.type), values))
786787
return ''
787788

0 commit comments

Comments
 (0)