diff --git a/bapsflib/utils/__init__.py b/bapsflib/utils/__init__.py index 151562fb..772c4b8c 100644 --- a/bapsflib/utils/__init__.py +++ b/bapsflib/utils/__init__.py @@ -11,6 +11,25 @@ """ Package of developer utilities. """ -__all__ = ['decorators', 'errors', 'warnings'] +__all__ = ['decorators', 'errors', 'warnings', 'ndarray_to_xarray'] + +import xarray as xr from . import (decorators, errors, warnings) + + +def ndarray_to_xarray(data, arr_name={}): + names = list(data.dtype.names) + names.remove('signal') + coords_dict = {} + for name in names: + if name == 'xyz': + coords_dict["x"] = ("shotnum", data[name][:, 0]) + coords_dict["y"] = ("shotnum", data[name][:, 1]) + coords_dict["z"] = ("shotnum", data[name][:, 2]) + + else: + coords_dict[name] = ("shotnum", data[name]) + + data_array = xr.DataArray(data['signal'], dims=('shotnum', 'time_index'), coords=coords_dict, name=arr_name) + return data_array