diff --git a/Changelog.rst b/Changelog.rst index bf3854c47..b0d2dfde4 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -1,8 +1,10 @@ Version NEXTVERSION --------------- +---------------- **2025-??-??** +* New keyword parameter to `cfdm.Field.dump`: ``data`` + (https://github.com/NCAS-CMS/cfdm/issues/345) * New dependency: ``distributed>=2025.5.1`` ---- diff --git a/cfdm/field.py b/cfdm/field.py index 40c02326b..5a486c2b3 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,22 @@ 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 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 + display: `bool`, optional If False then return the description as a string. By default the description is printed. @@ -1839,12 +1855,27 @@ 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: + # 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(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.