Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36 Fix functionality for multiple Conventions, and when Conventions does not mention CF #37

Merged
merged 2 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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