From b7466e7359b214bf6260cb38bf12fc936e915a36 Mon Sep 17 00:00:00 2001 From: Mike Kryjak Date: Thu, 7 Aug 2025 11:24:47 +0100 Subject: [PATCH 1/3] Add final_time accessor This returns the final time slice of a dataset or dataarray, whether the data contains multiple time slices or not. This is helpful when working with multiple simulations, some of which may have only been run for one timestep for developer reasons. --- xbout/boutdataarray.py | 10 ++++++++++ xbout/boutdataset.py | 12 ++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/xbout/boutdataarray.py b/xbout/boutdataarray.py index 3e54f402..ba52b89b 100644 --- a/xbout/boutdataarray.py +++ b/xbout/boutdataarray.py @@ -1169,3 +1169,13 @@ def as_cherab_emitter( cylinder_rmax=cyliner_rmax, step=step, ) + + def final_time(self): + """ + Returns the final time in the Dataset whether + it contains a time dimension or not. + """ + if "t" in self.data.sizes: + return self.data.isel(t=-1) + else: + return self.data diff --git a/xbout/boutdataset.py b/xbout/boutdataset.py index 94987b4b..cdca4b8c 100644 --- a/xbout/boutdataset.py +++ b/xbout/boutdataset.py @@ -33,7 +33,6 @@ _split_into_restarts, ) - @xr.register_dataset_accessor("bout") class BoutDatasetAccessor: """ @@ -1355,7 +1354,16 @@ def with_cherab_grid(self): from .cherab import grid return grid.ds_with_cherab_grid(self.data) - + + def final_time(self): + """ + Returns the final time in the Dataset whether + it contains a time dimension or not. + """ + if "t" in self.data.sizes: + return self.data.isel(t=-1) + else: + return self.data def _find_major_vars(data): """ From 7cb84c5706db8807eba7b87b49de1fe0d1dec664 Mon Sep 17 00:00:00 2001 From: mikekryjak Date: Thu, 7 Aug 2025 10:28:41 +0000 Subject: [PATCH 2/3] Apply black formatting --- xbout/boutdataarray.py | 2 +- xbout/boutdataset.py | 4 +++- xbout/xarraybackend.py | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/xbout/boutdataarray.py b/xbout/boutdataarray.py index ba52b89b..a8bf533f 100644 --- a/xbout/boutdataarray.py +++ b/xbout/boutdataarray.py @@ -1169,7 +1169,7 @@ def as_cherab_emitter( cylinder_rmax=cyliner_rmax, step=step, ) - + def final_time(self): """ Returns the final time in the Dataset whether diff --git a/xbout/boutdataset.py b/xbout/boutdataset.py index cdca4b8c..fe31bd12 100644 --- a/xbout/boutdataset.py +++ b/xbout/boutdataset.py @@ -33,6 +33,7 @@ _split_into_restarts, ) + @xr.register_dataset_accessor("bout") class BoutDatasetAccessor: """ @@ -1354,7 +1355,7 @@ def with_cherab_grid(self): from .cherab import grid return grid.ds_with_cherab_grid(self.data) - + def final_time(self): """ Returns the final time in the Dataset whether @@ -1365,6 +1366,7 @@ def final_time(self): else: return self.data + def _find_major_vars(data): """ Splits data into those variables likely to require a lot of storage space diff --git a/xbout/xarraybackend.py b/xbout/xarraybackend.py index 38f4d65c..6a9644c5 100644 --- a/xbout/xarraybackend.py +++ b/xbout/xarraybackend.py @@ -1,6 +1,6 @@ """License: - Distributed under the OSI-approved Apache License, Version 2.0. See - accompanying file Copyright.txt for details. +Distributed under the OSI-approved Apache License, Version 2.0. See +accompanying file Copyright.txt for details. """ from __future__ import annotations From 105321241cb0c856a94e74e9f5a6379a966023c3 Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Wed, 26 Nov 2025 10:37:56 +0000 Subject: [PATCH 3/3] Remove `else` after `return` --- xbout/boutdataarray.py | 3 +-- xbout/boutdataset.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/xbout/boutdataarray.py b/xbout/boutdataarray.py index a8bf533f..c97908df 100644 --- a/xbout/boutdataarray.py +++ b/xbout/boutdataarray.py @@ -1177,5 +1177,4 @@ def final_time(self): """ if "t" in self.data.sizes: return self.data.isel(t=-1) - else: - return self.data + return self.data diff --git a/xbout/boutdataset.py b/xbout/boutdataset.py index fe31bd12..3a662fb2 100644 --- a/xbout/boutdataset.py +++ b/xbout/boutdataset.py @@ -1363,8 +1363,7 @@ def final_time(self): """ if "t" in self.data.sizes: return self.data.isel(t=-1) - else: - return self.data + return self.data def _find_major_vars(data):