From d12d2fe924896fbbbf74d2838c92da0f03d605a3 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 26 May 2020 16:44:07 +0100 Subject: [PATCH 1/2] Fixes #76 --- cfdm/read_write/netcdf/netcdfread.py | 16 +++++-- cfdm/test/test_read_write.py | 64 +++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/cfdm/read_write/netcdf/netcdfread.py b/cfdm/read_write/netcdf/netcdfread.py index a9ec34112b..89f5cb9a98 100644 --- a/cfdm/read_write/netcdf/netcdfread.py +++ b/cfdm/read_write/netcdf/netcdfread.py @@ -675,9 +675,19 @@ def read(self, filename, extra=None, default_version=None, # ------------------------------------------------------------ # Find the CF version for the file # ------------------------------------------------------------ - # DCH ALERT: haven't yet dealt with multiple conventions! TODO - file_version = g['global_attributes'].get('Conventions', '').replace( - 'CF-', '', 1) + Conventions = g['global_attributes'].get('Conventions', '') + + all_conventions = re.split(',', Conventions) + if all_conventions[0] == Conventions: + all_conventions = re.split('\s+', Conventions) + + file_version = None + for c in all_conventions: + if not re.match('^CF-\d', c): + continue + + file_version = re.sub('^CF-', '', c) + if not file_version: if default_version is not None: # Assume the default version provided by the user diff --git a/cfdm/test/test_read_write.py b/cfdm/test/test_read_write.py index 9144ed5e31..de959e2b98 100644 --- a/cfdm/test/test_read_write.py +++ b/cfdm/test/test_read_write.py @@ -52,13 +52,14 @@ def setUp(self): os.path.dirname(os.path.abspath(__file__)), 'string_char.nc') self.test_only = [] - # self.test_only = ['NOTHING!!!!!'] - # self.test_only = ['test_read_CDL'] - # self.test_only = ['test_write_filename'] - # self.test_only = ['test_read_write_unlimited'] - # self.test_only = ['test_read_field'] - # self.test_only = ['test_read_mask'] - # self.test_only = ['test_read_write_format'] + # self.test_only = ['NOTHING!!!!!'] + # self.test_only = ['test_read_CDL'] + # self.test_only = ['test_write_filename'] + # self.test_only = ['test_read_write_unlimited'] + # self.test_only = ['test_read_field'] + # self.test_only = ['test_read_mask'] + # self.test_only = ['test_read_write_format'] + # self.test_only = ['test_read_write_Conventions'] def test_write_filename(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: @@ -359,6 +360,55 @@ def test_read_write_string(self): self.assertTrue(i.equals(j, verbose=3)) #--- End: for + def test_read_write_Conventions(self): + if self.test_only and inspect.stack()[0][3] not in self.test_only: + return + + f = cfdm.read(self.filename)[0] + + version = 'CF-' + cfdm.CF() + other = 'ACDD-1.3' + + for Conventions in (other,): + cfdm.write(f, tmpfile0, Conventions=Conventions) + g = cfdm.read(tmpfile0)[0] + self.assertTrue( + g.get_property('Conventions') == ' '.join([version, other]), + "{!r}, {!r}".format( + g.get_property('Conventions'), Conventions)) + + for Conventions in (version, + '', + ' ', + ',', + ', ', + ): + Conventions = version + cfdm.write(f, tmpfile0, Conventions=Conventions) + g = cfdm.read(tmpfile0)[0] + self.assertTrue(g.get_property('Conventions') == version, + "{!r}, {!r}".format( + g.get_property('Conventions'), + Conventions)) + + for Conventions in ([version], + [version, other], + ): + cfdm.write(f, tmpfile0, Conventions=Conventions) + g = cfdm.read(tmpfile0)[0] + self.assertTrue( + g.get_property('Conventions') == ' '.join(Conventions), + "{!r}, {!r}".format( + g.get_property('Conventions'), Conventions)) + + for Conventions in ([other, version],): + cfdm.write(f, tmpfile0, Conventions=Conventions) + g = cfdm.read(tmpfile0)[0] + self.assertTrue( + g.get_property('Conventions') == ' '.join([version, other]), + "{!r}, {!r}".format( + g.get_property('Conventions'), Conventions)) + #--- End: class if __name__ == "__main__": From 23bf788a49cbe4b859a5a007e17815938b05a562 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 26 May 2020 16:49:52 +0100 Subject: [PATCH 2/2] Changelog --- Changelog.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changelog.rst b/Changelog.rst index b3998b2c5e..e47333d68f 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -11,6 +11,9 @@ version 1.8.4 messages are displayed globally, i.e. to change the project-wide verbosity. * Changed behaviour and default of `verbose` keyword argument when available to a function/method so it interfaces with the new logging functionality. +* Fixed bug the wouldn't allow the reading of a netCDF file which + specifies Conventions other than CF + (https://github.com/NCAS-CMS/cfdm/issues/36). version 1.8.3 -------------