From e44cf78cf8aa2f9a2d21ef7475ac7241ce61df8c Mon Sep 17 00:00:00 2001 From: "stephen.worsley" Date: Mon, 18 May 2020 10:43:00 +0100 Subject: [PATCH 1/3] unify saving behaviour of "unknown" and "no_unit" --- lib/iris/fileformats/netcdf.py | 6 +++--- lib/iris/tests/results/netcdf/netcdf_save_no_name.cdl | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/iris/fileformats/netcdf.py b/lib/iris/fileformats/netcdf.py index 4d7ddedc61..83f7dcd07a 100644 --- a/lib/iris/fileformats/netcdf.py +++ b/lib/iris/fileformats/netcdf.py @@ -1760,7 +1760,7 @@ def _inner_create_cf_cellmeasure_or_ancil_variable( # Add the data to the CF-netCDF variable. cf_var[:] = data - if dimensional_metadata.units != "unknown": + if dimensional_metadata.units not in ("no_unit", "unknown"): _setncattr(cf_var, "units", str(dimensional_metadata.units)) if dimensional_metadata.standard_name is not None: @@ -1926,7 +1926,7 @@ def _create_cf_coord_variable(self, cube, dimension_names, coord): # Deal with CF-netCDF units and standard name. standard_name, long_name, units = self._cf_coord_identity(coord) - if units != "unknown": + if units not in ("no_unit", "unknown"): _setncattr(cf_var, "units", units) if standard_name is not None: @@ -2371,7 +2371,7 @@ def store(data, cf_var, fill_value): if cube.long_name: _setncattr(cf_var, "long_name", cube.long_name) - if cube.units != "unknown": + if cube.units not in ("no_unit", "unknown"): _setncattr(cf_var, "units", str(cube.units)) # Add the CF-netCDF calendar attribute. diff --git a/lib/iris/tests/results/netcdf/netcdf_save_no_name.cdl b/lib/iris/tests/results/netcdf/netcdf_save_no_name.cdl index e67316b2f7..e01eb1b31a 100644 --- a/lib/iris/tests/results/netcdf/netcdf_save_no_name.cdl +++ b/lib/iris/tests/results/netcdf/netcdf_save_no_name.cdl @@ -10,7 +10,6 @@ variables: double dim1(dim1) ; dim1:units = "m" ; char unknown_scalar(string6) ; - unknown_scalar:units = "no_unit" ; // global attributes: :Conventions = "CF-1.7" ; From 493913722546e0a03c2a901e0905fab4089589bd Mon Sep 17 00:00:00 2001 From: "stephen.worsley" Date: Mon, 1 Jun 2020 14:31:37 +0100 Subject: [PATCH 2/3] address review comments --- lib/iris/fileformats/netcdf.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/iris/fileformats/netcdf.py b/lib/iris/fileformats/netcdf.py index 83f7dcd07a..041f818336 100644 --- a/lib/iris/fileformats/netcdf.py +++ b/lib/iris/fileformats/netcdf.py @@ -1760,7 +1760,7 @@ def _inner_create_cf_cellmeasure_or_ancil_variable( # Add the data to the CF-netCDF variable. cf_var[:] = data - if dimensional_metadata.units not in ("no_unit", "unknown"): + if not dimensional_metadata.units.is_udunits(): _setncattr(cf_var, "units", str(dimensional_metadata.units)) if dimensional_metadata.standard_name is not None: @@ -1926,7 +1926,7 @@ def _create_cf_coord_variable(self, cube, dimension_names, coord): # Deal with CF-netCDF units and standard name. standard_name, long_name, units = self._cf_coord_identity(coord) - if units not in ("no_unit", "unknown"): + if not units.is_udunits(): _setncattr(cf_var, "units", units) if standard_name is not None: @@ -2371,7 +2371,7 @@ def store(data, cf_var, fill_value): if cube.long_name: _setncattr(cf_var, "long_name", cube.long_name) - if cube.units not in ("no_unit", "unknown"): + if not cube.units.is_udunits(): _setncattr(cf_var, "units", str(cube.units)) # Add the CF-netCDF calendar attribute. From 17255405eb27a4c1fca6f56775025c274bf99abe Mon Sep 17 00:00:00 2001 From: "stephen.worsley" Date: Mon, 1 Jun 2020 14:54:17 +0100 Subject: [PATCH 3/3] fix tests --- lib/iris/fileformats/netcdf.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/iris/fileformats/netcdf.py b/lib/iris/fileformats/netcdf.py index 041f818336..6f17f6cd9d 100644 --- a/lib/iris/fileformats/netcdf.py +++ b/lib/iris/fileformats/netcdf.py @@ -22,6 +22,7 @@ import warnings import dask.array as da +import cf_units import netCDF4 import numpy as np import numpy.ma as ma @@ -1760,7 +1761,7 @@ def _inner_create_cf_cellmeasure_or_ancil_variable( # Add the data to the CF-netCDF variable. cf_var[:] = data - if not dimensional_metadata.units.is_udunits(): + if dimensional_metadata.units.is_udunits(): _setncattr(cf_var, "units", str(dimensional_metadata.units)) if dimensional_metadata.standard_name is not None: @@ -1926,7 +1927,7 @@ def _create_cf_coord_variable(self, cube, dimension_names, coord): # Deal with CF-netCDF units and standard name. standard_name, long_name, units = self._cf_coord_identity(coord) - if not units.is_udunits(): + if cf_units.as_unit(units).is_udunits(): _setncattr(cf_var, "units", units) if standard_name is not None: @@ -2371,7 +2372,7 @@ def store(data, cf_var, fill_value): if cube.long_name: _setncattr(cf_var, "long_name", cube.long_name) - if not cube.units.is_udunits(): + if cube.units.is_udunits(): _setncattr(cf_var, "units", str(cube.units)) # Add the CF-netCDF calendar attribute.