Skip to content

Commit

Permalink
Merge pull request #37 from davidhassell/Multiple_Conventions
Browse files Browse the repository at this point in the history
Fixes #36 Fix functionality for multiple Conventions, and when Conventions does not mention CF
  • Loading branch information
davidhassell authored May 29, 2020
2 parents c1392ba + 23bf788 commit 8af3d81
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
16 changes: 13 additions & 3 deletions cfdm/read_write/netcdf/netcdfread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 57 additions & 7 deletions cfdm/test/test_read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 8af3d81

Please sign in to comment.