Skip to content

Commit e4b4b74

Browse files
Merge pull request #63 from epochpic/improve-drop-variables
Improve drop variables
2 parents ef66bc9 + e0015cd commit e4b4b74

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/sdf_xarray/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,18 @@ def acquire_context(self, needs_lock=True):
253253
def load(self): # noqa: PLR0912, PLR0915
254254
# Drop any requested variables
255255
if self.drop_variables:
256+
# Build a mapping from underscored names to real variable names
257+
name_map = {_rename_with_underscore(var): var for var in self.ds.variables}
258+
256259
for variable in self.drop_variables:
257-
# TODO: nicer error handling
258-
self.ds.variables.pop(variable)
260+
key = _rename_with_underscore(variable)
261+
original_name = name_map.get(key)
262+
263+
if original_name is None:
264+
raise KeyError(
265+
f"Variable '{variable}' not found (interpreted as '{key}')."
266+
)
267+
self.ds.variables.pop(original_name)
259268

260269
# These two dicts are global metadata about the run or file
261270
attrs = {**self.ds.header, **self.ds.run_info}
@@ -434,7 +443,7 @@ def guess_can_open(self, filename_or_obj):
434443

435444
description = "Use .sdf files in Xarray"
436445

437-
url = "https://epochpic.github.io/documentation/visualising_output/python.html"
446+
url = "https://epochpic.github.io/documentation/visualising_output/python_beam.html"
438447

439448

440449
class SDFPreprocess:

tests/test_basic.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,44 @@ def test_3d_distribution_function():
185185
with xr.open_dataset(EXAMPLE_3D_DIST_FN / "0000.sdf") as df:
186186
distribution_function = "dist_fn_x_px_py_Electron"
187187
assert df[distribution_function].shape == (16, 20, 20)
188+
189+
190+
def test_drop_variables():
191+
with xr.open_dataset(
192+
EXAMPLE_FILES_DIR / "0000.sdf", drop_variables=["Electric_Field_Ex"]
193+
) as df:
194+
assert "Electric_Field_Ex" not in df
195+
196+
197+
def test_drop_variables_multiple():
198+
with xr.open_dataset(
199+
EXAMPLE_FILES_DIR / "0000.sdf",
200+
drop_variables=["Electric_Field_Ex", "Electric_Field_Ey"],
201+
) as df:
202+
assert "Electric_Field_Ex" not in df
203+
assert "Electric_Field_Ey" not in df
204+
205+
206+
def test_drop_variables_original():
207+
with xr.open_dataset(
208+
EXAMPLE_FILES_DIR / "0000.sdf",
209+
drop_variables=["Electric_Field/Ex", "Electric_Field/Ey"],
210+
) as df:
211+
assert "Electric_Field_Ex" not in df
212+
assert "Electric_Field_Ey" not in df
213+
214+
215+
def test_drop_variables_mixed():
216+
with xr.open_dataset(
217+
EXAMPLE_FILES_DIR / "0000.sdf",
218+
drop_variables=["Electric_Field/Ex", "Electric_Field_Ey"],
219+
) as df:
220+
assert "Electric_Field_Ex" not in df
221+
assert "Electric_Field_Ey" not in df
222+
223+
224+
def test_erroring_drop_variables():
225+
with pytest.raises(KeyError):
226+
xr.open_dataset(
227+
EXAMPLE_FILES_DIR / "0000.sdf", drop_variables=["Electric_Field/E"]
228+
)

0 commit comments

Comments
 (0)