From 7b3ca85ff688247a11794de7715b76e47debeb62 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Fri, 25 Jul 2025 13:04:22 +0100 Subject: [PATCH 1/3] Field.dump data keyword --- Changelog.rst | 10 ++++++++++ cfdm/field.py | 23 +++++++++++++++++++---- cfdm/test/test_Field.py | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index fa6a0a9b9..b7d0eccca 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -1,3 +1,13 @@ +Version NEXTVERSION +---------------- + +**2025-??-??** + +* New keyword parameter to `cfdm.Field.dump`: ``data`` + (https://github.com/NCAS-CMS/cfdm/issues/345) + +---- + Version 1.12.2.0 ---------------- diff --git a/cfdm/field.py b/cfdm/field.py index 40c02326b..185466917 100644 --- a/cfdm/field.py +++ b/cfdm/field.py @@ -1787,7 +1787,7 @@ def creation_commands( return out @_display_or_return - def dump(self, display=True, _level=0, _title=None): + def dump(self, data=True, display=True, _level=0, _title=None): """A full description of the field construct. Returns a description of all properties, including those of @@ -1798,6 +1798,18 @@ def dump(self, display=True, _level=0, _title=None): :Parameters: + data: `bool`, optional + If True (the default) then display the first and last + Field data values. This can take a long time if the + data need an expensive computation (possibly including + a slow read from local or remote disk), in which case + setting *data* to False will not display these values, + thereby avoiding the computational cost. Note that + when the first and last values are displayed, they are + cached for fast future retrieval. + + .. versionadded:: NEXTVERSION + display: `bool`, optional If False then return the description as a string. By default the description is printed. @@ -1839,12 +1851,15 @@ def dump(self, display=True, _level=0, _title=None): string.append(self._dump_properties(_level=_level)) # Data - data = self.get_data(None) - if data is not None: + d = self.get_data(None) + if d is not None: x = [axis_to_name[axis] for axis in self.get_data_axes(default=())] + x = f"{indent0}Data({', '.join(x)})" + if data: + x += f" = {d}" string.append("") - string.append(f"{indent0}Data({', '.join(x)}) = {data}") + string.append(x) string.append("") # Quantization diff --git a/cfdm/test/test_Field.py b/cfdm/test/test_Field.py index dc3cf475c..e77513d52 100644 --- a/cfdm/test/test_Field.py +++ b/cfdm/test/test_Field.py @@ -58,6 +58,7 @@ def test_Field__repr__str__dump_construct_type(self): repr(f) str(f) self.assertIsInstance(f.dump(display=False), str) + self.assertIsInstance(f.dump(data=False, display=False), str) self.assertEqual(f.construct_type, "field") # Test when any construct which can have data in fact has no data. From e6679960d4a69274ba79b68be6e768bdcd7105e8 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 29 Jul 2025 08:45:22 +0100 Subject: [PATCH 2/3] Typo Co-authored-by: Sadie L. Bartholomew --- cfdm/field.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfdm/field.py b/cfdm/field.py index 185466917..252c3b78a 100644 --- a/cfdm/field.py +++ b/cfdm/field.py @@ -1801,7 +1801,7 @@ def dump(self, data=True, display=True, _level=0, _title=None): data: `bool`, optional If True (the default) then display the first and last Field data values. This can take a long time if the - data need an expensive computation (possibly including + data needs an expensive computation (possibly including a slow read from local or remote disk), in which case setting *data* to False will not display these values, thereby avoiding the computational cost. Note that From e42e09cd35b88740ddc178e58a7e4aae430af6c8 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 29 Jul 2025 09:58:19 +0100 Subject: [PATCH 3/3] include units when data=False --- cfdm/field.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cfdm/field.py b/cfdm/field.py index 252c3b78a..5a486c2b3 100644 --- a/cfdm/field.py +++ b/cfdm/field.py @@ -1801,12 +1801,16 @@ def dump(self, data=True, display=True, _level=0, _title=None): data: `bool`, optional If True (the default) then display the first and last Field data values. This can take a long time if the - data needs an expensive computation (possibly including - a slow read from local or remote disk), in which case - setting *data* to False will not display these values, - thereby avoiding the computational cost. Note that - when the first and last values are displayed, they are - cached for fast future retrieval. + data needs an expensive computation (possibly + including a slow read from local or remote disk), in + which case setting *data* to False will not display + these values, thereby avoiding the computational + cost. This only applies to the Field's data - the + first and last values of data arrays stored in + metadata constructs are always displayed. + + Note that when the first and last values are + displayed, they are cached for fast future retrieval. .. versionadded:: NEXTVERSION @@ -1856,7 +1860,19 @@ def dump(self, data=True, display=True, _level=0, _title=None): x = [axis_to_name[axis] for axis in self.get_data_axes(default=())] x = f"{indent0}Data({', '.join(x)})" if data: + # Show selected data values x += f" = {d}" + else: + # Don't show any data values + units = d.Units + if units.isreftime: + calendar = getattr(units, "calendar", None) + if calendar is not None: + x += f" {calendar}" + else: + units = getattr(units, "units", None) + if units is not None: + x += f" {units}" string.append("") string.append(x)