Skip to content

Commit

Permalink
add init 1d xarray method
Browse files Browse the repository at this point in the history
  • Loading branch information
kkappler committed Aug 15, 2024
1 parent a98e030 commit 0ee8247
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
43 changes: 43 additions & 0 deletions aurora/time_series/xarray_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Placeholder module for methods manipulating xarray time series
"""

import numpy as np
import xarray as xr
from loguru import logger
from typing import Optional, Union


def handle_nan(X, Y, RR, drop_dim=""):
Expand Down Expand Up @@ -85,3 +87,44 @@ def handle_nan(X, Y, RR, drop_dim=""):
RR = RR.rename(data_var_rm_label_mapper)

return X, Y, RR


def initialize_xrda_1d(
channels: list,
dtype=Optional[type],
value: Optional[Union[complex, float, bool]] = 0,
) -> xr.DataArray:
"""
Returns a 1D xr.DataArray with variable "channel", having values channels named by the input list.
Parameters
----------
channels: list
The channels in the multivariate array
dtype: type
The datatype to initialize the array.
Common cases are complex, float, and bool
value: Union[complex, float, bool]
The default value to assign the array
Returns
-------
xrda: xarray.core.dataarray.DataArray
An xarray container for the channel variances, initialized to zeros.
"""
k = len(channels)
logger.debug(f"Initializing xarray with values {value}")
xrda = xr.DataArray(
np.zeros(k, dtype=dtype),
dims=[
"variable",
],
coords={
"variable": channels,
},
)
if value != 0:
data = value * np.ones(k, dtype=dtype)
xrda.data = data
return xrda
38 changes: 38 additions & 0 deletions tests/time_series/test_xarray_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""
"""

import unittest
from aurora.time_series.xarray_helpers import initialize_xrda_1d


class TestXarrayHelpers(unittest.TestCase):
"""
Test methods in xarray helpers
- may get broken into separate tests if this module grows
"""

@classmethod
def setUpClass(self):
pass

def setUp(self):
pass

def test_initialize_xrda_1d(self):
channels = ["ex", "ey", "hx", "hy", "hz"]
dtype = float
value = -1
tmp = initialize_xrda_1d(channels, dtype=dtype, value=value)
self.assertTrue((tmp.data == value).all())

def test_sometehing_else(self):
"""
Place holder
"""
pass


if __name__ == "__main__":
unittest.main()

0 comments on commit 0ee8247

Please sign in to comment.