Skip to content

Commit 8554f6e

Browse files
committed
Merge branch 'main' of https://github.com/comet-toolkit/obsarray into small_fixes
2 parents cf7f7fa + 251cae1 commit 8554f6e

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

docs/content/user/unc_accessor.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,20 @@ A component of uncertainty can be simply be deleted as,
181181
.. ipython:: python
182182
:okwarning:
183183
184-
del ds.unc["temperature"]["u_sys_temperature"]
184+
del ds.unc["temperature"]["u_str_temperature"]
185185
186186
# Check uncertainties
187187
ds.unc["temperature"].keys()
188188
189+
Renaming Variables
190+
------------------
189191

192+
The storage of uncertainty information is underpinned by variable attributes, which include referencing other variables (for example, which variables are the uncertainties associated with a particular observation variable). Because of this it is important, if renaming uncertainty variables, to use **obsarray**'s renaming functionality. This renames the uncertainty variable and safely updates attribute variable references. This is done as follows:
193+
194+
195+
.. ipython:: python
196+
:okwarning:
197+
198+
print(ds.unc["temperature"])
199+
ds = ds.unc["temperature"]["u_ran_temperature"].rename("u_noise")
200+
print(ds.unc["temperature"])

obsarray/test/test_unc_accessor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ def test_expand_slice_first(self):
535535
sli = self.ds.unc["temperature"]["u_ran_temperature"]._expand_sli((0,))
536536
self.assertEqual((0, slice(None), slice(None)), sli)
537537

538+
def test_rename(self):
539+
ds = self.ds.unc["temperature"]["u_ran_temperature"].rename("test")
540+
self.assertTrue("test" in ds.keys())
541+
self.assertTrue("u_ran_temperature" not in ds.keys())
542+
self.assertCountEqual(
543+
ds["temperature"].attrs["unc_comps"],
544+
["test", "u_str_temperature", "u_sys_temperature"],
545+
)
546+
538547
def test_err_corr(self):
539548

540549
expected_err_corr = [

obsarray/unc_accessor.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ def _expand_sli(self, sli: Optional[tuple] = None) -> tuple:
9292

9393
return out_sli
9494

95+
def rename(self, name) -> xr.Dataset:
96+
"""
97+
Renames uncertainty variable and safely updates variable name references in obs variable metadata
98+
:param name: desired name
99+
:returns: dataset with safely renamed variable
100+
"""
101+
self._obj = self._obj.rename({self._unc_var_name: name})
102+
self._obj[self._var_name].attrs["unc_comps"] = [
103+
v.replace(self._unc_var_name, name)
104+
for v in self._obj[self._var_name].attrs["unc_comps"]
105+
]
106+
107+
return self._obj
108+
95109
@property
96110
def err_corr(self) -> List[Tuple[Union[str, List[str]], BaseErrCorrForm]]:
97111
"""
@@ -105,7 +119,9 @@ def err_corr(self) -> List[Tuple[Union[str, List[str]], BaseErrCorrForm]]:
105119
# Find dimensions in variable slice
106120
sli_dims = [
107121
dim
108-
for dim, idx in zip(self._obj.dims.keys(), self._sli) # Due to hit a FutureDeprecation warning
122+
for dim, idx in zip(
123+
self._obj.dims.keys(), self._sli
124+
) # Due to hit a FutureDeprecation warning
109125
if not isinstance(idx, int)
110126
]
111127

0 commit comments

Comments
 (0)