Skip to content

Commit

Permalink
Merge pull request #231 from dstansby/squeeze-att-data
Browse files Browse the repository at this point in the history
Squeeze attribute data
  • Loading branch information
dstansby committed Sep 15, 2023
2 parents dd379ec + 40254fc commit 73974a7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cdflib/cdfread.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union

import numpy as np
import numpy.typing as npt

import cdflib.epochs as epoch
from cdflib._gzip_wrapper import gzip_inflate
Expand All @@ -23,6 +24,7 @@
VDRInfo,
)
from cdflib.s3 import S3object
from cdflib.utils import _squeeze_or_scalar

__all__ = ["CDF"]

Expand Down Expand Up @@ -1770,14 +1772,16 @@ def _get_attdata(self, adr_info: ADRInfo, entry_num: int, num_entry: int, first_
data_type = self._datatype_token(aedr_info.data_type)

num_items = aedr_info.num_elements
data = aedr_info.entry
data: Union[str, npt.NDArray] = aedr_info.entry
if isinstance(data, str):
if aedr_info.num_strings is not None:
num_strings = aedr_info.num_strings
num_items = num_strings
if num_strings > 1 and isinstance(aedr_info.entry, str):
data = np.array(aedr_info.entry.split("\\N "))
return AttData(item_size, data_type, num_items, data)
return AttData(item_size, data_type, num_items, data)
else:
return AttData(item_size, data_type, num_items, _squeeze_or_scalar(data))
else:
position = next_aedr

Expand Down
9 changes: 9 additions & 0 deletions cdflib/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from numbers import Number
from typing import Union

import numpy as np
import numpy.typing as npt


def _squeeze_or_scalar(arr: npt.ArrayLike) -> Union[npt.NDArray, Number]:
arr = np.squeeze(arr)
if arr.ndim == 0:
return arr.item()
else:
return arr


def _squeeze_or_scalar_real(arr: npt.ArrayLike) -> Union[npt.NDArray, float]:
arr = np.squeeze(arr)
if arr.ndim == 0:
Expand Down
2 changes: 1 addition & 1 deletion cdflib/xarray/cdf_to_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _convert_cdf_time_types(
for att in atts:
data_type = atts[att].Data_Type
data = atts[att].Data
if len(data) == 0 or data_type not in ("CDF_EPOCH", "CDF_EPOCH16", "CDF_TIME_TT2000"):
if data_type not in ("CDF_EPOCH", "CDF_EPOCH16", "CDF_TIME_TT2000"):
new_atts[att] = data
else:
if to_datetime:
Expand Down
6 changes: 4 additions & 2 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Changelog
=========

Unreleased
==========
1.2.0
=====
- Attribute data with a single value is now returned as a Python scalar instead of
a numpy array.
- Added missing changelog entries for 1.1.1 and 1.1.2.

1.1.2
Expand Down
8 changes: 4 additions & 4 deletions tests/test_cdfwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_checksum_compressed(tmp_path):
np.testing.assert_equal(var, v)

att = reader.attget("Attribute1", entry=0)
assert att.Data == [1]
assert att.Data == 1

att = reader.attget("Attribute2", entry=0)
assert att.Data == "500"
Expand Down Expand Up @@ -265,7 +265,7 @@ def test_create_zvariables_with_attributes(tmp_path):

# Test CDF info
att = reader.attget("Attribute1", entry=0)
assert att.Data == [1]
assert att.Data == 1

att = reader.attget("Attribute2", entry=1)
assert att.Data == "1000"
Expand Down Expand Up @@ -300,7 +300,7 @@ def test_create_zvariables_then_attributes(tmp_path):

# Test CDF info
att = reader.attget("Attribute1", entry=0)
assert att.Data == [1]
assert att.Data == 1

att = reader.attget("Attribute2", entry=1)
att.Data == "1000"
Expand Down Expand Up @@ -567,7 +567,7 @@ def test_create_2d_r_and_z_variables(tmp_path):
np.testing.assert_equal(var, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

att = reader.attget("Attribute1", entry="Variable2")
assert att.Data == [2]
assert att.Data == 2

att = reader.attget("Attribute2", entry="Variable2")
assert att.Data == "1000"

0 comments on commit 73974a7

Please sign in to comment.