Skip to content
Open
Changes from 5 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
23 changes: 23 additions & 0 deletions ceos_alos2/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from ceos_alos2.array import Array





@dataclass(frozen=True)
class Variable:
dims: str | list[str]
Expand All @@ -21,6 +24,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=self._infer_dtype(self.data)))

def __eq__(self, other):
if not isinstance(other, Variable):
Expand All @@ -37,6 +43,23 @@ def __eq__(self, other):
return self.data == other.data
else:
return np.all(self.data == other.data)

@staticmethod
def _infer_dtype(data):
Comment thread
agrouaze marked this conversation as resolved.
Outdated
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''
max_len = max(len(str(x)) for x in data)
return f"S{max_len}" # or 'U' for unicode
Comment thread
agrouaze marked this conversation as resolved.
Outdated
else:
return None # let numpy infer

@property
def ndim(self):
Expand Down
Loading