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

Fix bug with aux coords with scalefactor/add_offset #2975

Merged
merged 5 commits into from
Mar 15, 2018
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
4 changes: 2 additions & 2 deletions lib/iris/fileformats/_pyke_rules/fc_rules_cf.krb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2017, Met Office
# (C) British Crown Copyright 2010 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -1624,7 +1624,7 @@ fc_extras
attr_units = get_attr_units(cf_coord_var, attributes)

def cf_var_as_array(cf_var):
dtype = cf_var.dtype
dtype = iris.fileformats.netcdf._get_actual_dtype(cf_var)
fill_value = getattr(cf_var.cf_data, '_FillValue',
netCDF4.default_fillvals[dtype.str[1:]])
proxy = iris.fileformats.netcdf.NetCDFDataProxy(
Expand Down
13 changes: 9 additions & 4 deletions lib/iris/fileformats/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,21 +490,26 @@ def _set_attributes(attributes, key, value):
attributes[str(key)] = value


def _load_cube(engine, cf, cf_var, filename):
"""Create the cube associated with the CF-netCDF data variable."""
def _get_actual_dtype(cf_var):
# Figure out what the eventual data type will be after any scale/offset
# transforms.
dummy_data = np.zeros(1, dtype=cf_var.dtype)
if hasattr(cf_var, 'scale_factor'):
dummy_data = cf_var.scale_factor * dummy_data
if hasattr(cf_var, 'add_offset'):
dummy_data = cf_var.add_offset + dummy_data
return dummy_data.dtype


def _load_cube(engine, cf, cf_var, filename):
"""Create the cube associated with the CF-netCDF data variable."""
dtype = _get_actual_dtype(cf_var)

# Create cube with deferred data, but no metadata
fill_value = getattr(cf_var.cf_data, '_FillValue',
netCDF4.default_fillvals[cf_var.dtype.str[1:]])
proxy = NetCDFDataProxy(cf_var.shape, dummy_data.dtype,
filename, cf_var.cf_name, fill_value)
proxy = NetCDFDataProxy(cf_var.shape, dtype, filename, cf_var.cf_name,
fill_value)
data = as_lazy_data(proxy)
cube = iris.cube.Cube(data)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2014 - 2017, Met Office
# (C) British Crown Copyright 2014 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -30,6 +30,7 @@
import numpy as np

from iris.coords import AuxCoord
from iris.fileformats.cf import CFVariable
from iris.fileformats._pyke_rules.compiled_krb.fc_rules_cf_fc import \
build_auxiliary_coordinate
from iris.tests import mock
Expand All @@ -40,8 +41,10 @@ def setUp(self):
# Create coordinate cf variables and pyke engine.
points = np.arange(6).reshape(2, 3)
self.cf_coord_var = mock.Mock(
spec=CFVariable,
dimensions=('foo', 'bar'),
cf_name='wibble',
cf_data=mock.Mock(),
standard_name=None,
long_name='wibble',
units='m',
Expand Down Expand Up @@ -73,8 +76,10 @@ def test_slowest_varying_vertex_dim(self):
# Create the bounds cf variable.
bounds = np.arange(24).reshape(4, 2, 3)
self.cf_bounds_var = mock.Mock(
spec=CFVariable,
dimensions=('nv', 'foo', 'bar'),
cf_name='wibble_bnds',
cf_data=mock.Mock(),
shape=bounds.shape,
dtype=bounds.dtype,
__getitem__=lambda self, key: bounds[key])
Expand Down Expand Up @@ -112,8 +117,10 @@ def test_slowest_varying_vertex_dim(self):
def test_fastest_varying_vertex_dim(self):
bounds = np.arange(24).reshape(2, 3, 4)
self.cf_bounds_var = mock.Mock(
spec=CFVariable,
dimensions=('foo', 'bar', 'nv'),
cf_name='wibble_bnds',
cf_data=mock.Mock(),
shape=bounds.shape,
dtype=bounds.dtype,
__getitem__=lambda self, key: bounds[key])
Expand Down Expand Up @@ -149,8 +156,10 @@ def test_fastest_with_different_dim_names(self):
# this should still work because the vertex dim is the fastest varying.
bounds = np.arange(24).reshape(2, 3, 4)
self.cf_bounds_var = mock.Mock(
spec=CFVariable,
dimensions=('x', 'y', 'nv'),
cf_name='wibble_bnds',
cf_data=mock.Mock(),
shape=bounds.shape,
dtype=bounds.dtype,
__getitem__=lambda self, key: bounds[key])
Expand Down Expand Up @@ -181,5 +190,65 @@ def test_fastest_with_different_dim_names(self):
expected_list)


class TestDtype(tests.IrisTest):
def setUp(self):
# Create coordinate cf variables and pyke engine.
points = np.arange(6).reshape(2, 3)
self.cf_coord_var = mock.Mock(
spec=CFVariable,
dimensions=('foo', 'bar'),
cf_name='wibble',
cf_data=mock.Mock(),
standard_name=None,
long_name='wibble',
units='m',
shape=points.shape,
dtype=points.dtype,
__getitem__=lambda self, key: points[key])

self.engine = mock.Mock(
cube=mock.Mock(),
cf_var=mock.Mock(dimensions=('foo', 'bar')),
filename='DUMMY',
provides=dict(coordinates=[]))

def patched__getitem__(proxy_self, keys):
if proxy_self.variable_name == self.cf_coord_var.cf_name:
return self.cf_coord_var[keys]
raise RuntimeError()

self.deferred_load_patch = mock.patch(
'iris.fileformats.netcdf.NetCDFDataProxy.__getitem__',
new=patched__getitem__)

def test_scale_factor_add_offset_int(self):
self.cf_coord_var.scale_factor = 3
self.cf_coord_var.add_offset = 5

with self.deferred_load_patch:
build_auxiliary_coordinate(self.engine, self.cf_coord_var)

coord, _ = self.engine.provides['coordinates'][0]
self.assertEqual(coord.dtype.kind, 'i')

def test_scale_factor_float(self):
self.cf_coord_var.scale_factor = 3.

with self.deferred_load_patch:
build_auxiliary_coordinate(self.engine, self.cf_coord_var)

coord, _ = self.engine.provides['coordinates'][0]
self.assertEqual(coord.dtype.kind, 'f')

def test_add_offset_float(self):
self.cf_coord_var.add_offset = 5.

with self.deferred_load_patch:
build_auxiliary_coordinate(self.engine, self.cf_coord_var)

coord, _ = self.engine.provides['coordinates'][0]
self.assertEqual(coord.dtype.kind, 'f')


if __name__ == '__main__':
tests.main()