-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0432c2
commit fd9256b
Showing
9 changed files
with
551 additions
and
159 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
import xarray as xr | ||
from typing import Optional | ||
|
||
from h5rdmtoolbox import get_ureg | ||
from h5rdmtoolbox.protocols import H5TbxDataset | ||
from ..wrapper.accessory import Accessory, register_special_dataset | ||
|
||
|
||
class ToUnitsInterface: | ||
def __init__(self, | ||
dataset: H5TbxDataset, | ||
dataset_unit: Optional[str] = None, | ||
**coord_units): | ||
self.dataset = dataset | ||
self.dataset_unit = dataset_unit | ||
self.coord_units = coord_units | ||
|
||
def _convert_units(self, data: xr.DataArray): | ||
assert isinstance(data, xr.DataArray) | ||
assert 'units' in data.attrs, 'No units attribute found in the dataset' | ||
for c, cn in self.coord_units.items(): | ||
assert 'units' in data.coords[c].attrs, f'No units attribute found in the coordinate {c}' | ||
data.coords[c] = data.coords[c].pint.quantify(unit_registry=get_ureg()).pint.to( | ||
self.coord_units[c]).pint.dequantify() | ||
# convert units | ||
if self.dataset_unit is None: | ||
return data | ||
return data.pint.quantify(unit_registry=get_ureg()).pint.to(self.dataset_unit).pint.dequantify() | ||
|
||
def sel(self, method=None, **coords) -> xr.DataArray: | ||
return self._convert_units(self.dataset.sel(method=method, **coords)) | ||
|
||
def isel(self, **indexers) -> xr.DataArray: | ||
return self._convert_units(self.dataset.isel(**indexers)) | ||
|
||
def __getitem__(self, *args, **kwargs): | ||
return self._convert_units(self.dataset.__getitem__(*args, **kwargs)) | ||
|
||
|
||
@register_special_dataset("to_units", "Dataset") | ||
class ToUnitsAccessory(Accessory): | ||
"""Accessor to await selected data to be converted to a new units""" | ||
|
||
@xr.register_dataarray_accessor("to") | ||
class UnitConversionAccessor: | ||
"""Accessor to convert units of data array. It is | ||
also possible to convert its coordinates""" | ||
|
||
def __init__(self, xarray_obj): | ||
self._obj = xarray_obj | ||
|
||
def __call__(self, *args, **kwargs): | ||
new_obj = self._obj.copy() | ||
if len(args) > 0: | ||
for arg in args: | ||
if isinstance(arg, str): | ||
new_obj = new_obj.pint.quantify(unit_registry=get_ureg()).pint.quantify(unit_registry=get_ureg()).pint.to(arg).pint.dequantify() | ||
elif isinstance(arg, dict): | ||
for k, v in arg.items(): | ||
new_obj.coords[k] = self._obj.coords[k].pint.quantify(unit_registry=get_ureg()).pint.to(v).pint.dequantify() | ||
for k, v in kwargs.items(): | ||
new_obj.coords[k] = self._obj.coords[k].pint.quantify(unit_registry=get_ureg()).pint.to(v).pint.dequantify() | ||
return new_obj | ||
def __call__(self, dataset_unit: Optional[str] = None, **coord_units) -> ToUnitsInterface: | ||
return ToUnitsInterface(self._obj, dataset_unit=dataset_unit, **coord_units) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters