Source code for geoh5py.data.text_data
# Copyright (c) 2024 Mira Geoscience Ltd.
#
# This file is part of geoh5py.
#
# geoh5py is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# geoh5py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with geoh5py. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
import json
import numpy as np
from .data import Data
from .primitive_type_enum import PrimitiveTypeEnum
[docs]
class TextData(Data):
_values: np.ndarray | str | None
@property
def nan_value(self):
"""
Nan-Data-Value to be used in arrays
"""
return ""
[docs]
@classmethod
def primitive_type(cls) -> PrimitiveTypeEnum:
return PrimitiveTypeEnum.TEXT
@property
def values(self) -> np.ndarray | str | None:
"""
:obj:`str` Text value.
"""
if (getattr(self, "_values", None) is None) and self.on_file:
values = self.workspace.fetch_values(self)
if isinstance(values, np.ndarray) and values.dtype == object:
values = np.array(
[v.decode("utf-8") if isinstance(v, bytes) else v for v in values]
)
if isinstance(values, (np.ndarray, str, type(None))):
self._values = values
return self._values
@values.setter
def values(self, values: np.ndarray | str | None):
if isinstance(values, bytes):
values = values.decode()
if isinstance(values, np.ndarray) and values.dtype == object:
values = values.astype(str)
if (not isinstance(values, (str, type(None), np.ndarray))) or (
isinstance(values, np.ndarray) and values.dtype.kind not in ["U", "S"]
):
raise ValueError(
f"Input 'values' for {self} must be of type {np.ndarray} str or None."
)
self._values = values
self.workspace.update_attribute(self, "values")
[docs]
class MultiTextData(Data):
_values: np.ndarray | str | None
[docs]
@classmethod
def primitive_type(cls) -> PrimitiveTypeEnum:
return PrimitiveTypeEnum.MULTI_TEXT
@property
def values(self) -> np.ndarray | str | None:
"""
:obj:`str` Text value.
"""
if (getattr(self, "_values", None) is None) and self.on_file:
values = self.workspace.fetch_values(self)
if isinstance(values, (np.ndarray, str, type(None))):
self._values = values
return self._values
@values.setter
def values(self, values: np.ndarray | str | None):
self._values = values
if not isinstance(values, (np.ndarray, str, type(None))):
raise ValueError(
f"Input 'values' for {self} must be of type {np.ndarray} str or None."
)
self.workspace.update_attribute(self, "values")