Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ceos_alos2/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
from ceos_alos2.array import Array


def _infer_dtype(data):
# If obj is a list or lacks a dtype, convert to numpy array
if not data:
return None
first = data[0]
if isinstance(first, int):
return np.int64 # consistent across platforms
elif isinstance(first, float):
return np.float64
elif isinstance(first, str):
# Use fixed-length string to avoid object dtype
# CEOS strings are usually ASCII, e.g., 'ALOS-2''
return np.dtypes.StrDType
else:
return None # let numpy infer


@dataclass(frozen=True)
class Variable:
dims: str | list[str]
Expand All @@ -21,6 +38,9 @@ def __post_init__(self):
if isinstance(self.dims, str):
# normalize, need the hack
super().__setattr__("dims", [self.dims])
if isinstance(self.data, list):
# Infer dtype more carefully, or force appropriate dtype
super().__setattr__("data", np.array(self.data, dtype=_infer_dtype(self.data)))

def __eq__(self, other):
if not isinstance(other, Variable):
Expand Down
Loading