Skip to content
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
9 changes: 5 additions & 4 deletions lib/iris/fileformats/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def load_cubes(filenames, callback=None):
def _bytes_if_ascii(string):
"""
Convert the given string to a byte string (str in py2k, bytes in py3k)
iff the given string can be encoded to ascii, else maintain the type
if the given string can be encoded to ascii, else maintain the type
of the inputted string.

Note: passing objects without an `encode` method (such as None) will
Expand Down Expand Up @@ -1690,12 +1690,13 @@ def _create_cf_grid_mapping(self, cube, cf_var_cube):
# Grid var not yet created?
if cs not in self._coord_systems:
while cs.grid_mapping_name in self._dataset.variables:
cs.grid_mapping_name = (
self._increment_name(cs.grid_mapping_name))
aname = self._increment_name(cs.grid_mapping_name)
cs.grid_mapping_name = aname

cf_var_grid = self._dataset.createVariable(
cs.grid_mapping_name, np.int32)
cf_var_grid.grid_mapping_name = cs.grid_mapping_name
_setncattr(cf_var_grid, 'grid_mapping_name',
cs.grid_mapping_name)

def add_ellipsoid(ellipsoid):
cf_var_grid.longitude_of_prime_meridian = (
Expand Down
24 changes: 12 additions & 12 deletions lib/iris/tests/unit/fileformats/netcdf/test_Saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,27 +505,27 @@ def _grid_mapping_variable(self, coord_system):

"""
cube = self._cube_with_cs(coord_system)

class NCMock(mock.Mock):
def setncattr(self, name, attr):
setattr(self, name, attr)

# Calls the actual NetCDF saver with appropriate mocking, returning
# the grid variable that gets created.
grid_variable = mock.Mock(name='NetCDFVariable')
grid_variable = NCMock(name='NetCDFVariable')
create_var_fn = mock.Mock(side_effect=[grid_variable])
dataset = mock.Mock(variables=[],
createVariable=create_var_fn)
saver = mock.Mock(spec=Saver, _coord_systems=[],
_dataset=dataset)

class NCMock(mock.Mock):
def setncattr(self, name, attr):
setattr(self, name, attr)

variable = NCMock()

# This is the method we're actually testing!
Saver._create_cf_grid_mapping(saver, cube, variable)

self.assertEqual(create_var_fn.call_count, 1)
self.assertEqual(variable.grid_mapping,
str.encode(grid_variable.grid_mapping_name))
grid_variable.grid_mapping_name)
return grid_variable

def _variable_attributes(self, coord_system):
Expand All @@ -552,7 +552,7 @@ def _test(self, coord_system, expected):

def test_rotated_geog_cs(self):
coord_system = RotatedGeogCS(37.5, 177.5, ellipsoid=GeogCS(6371229.0))
expected = {'grid_mapping_name': 'rotated_latitude_longitude',
expected = {'grid_mapping_name': b'rotated_latitude_longitude',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marqh What does this b mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b indicates a byte string type; it is a python3 ism in python2 a byte string is just ignored

'north_pole_grid_longitude': 0.0,
'grid_north_pole_longitude': 177.5,
'grid_north_pole_latitude': 37.5,
Expand All @@ -563,15 +563,15 @@ def test_rotated_geog_cs(self):

def test_spherical_geog_cs(self):
coord_system = GeogCS(6371229.0)
expected = {'grid_mapping_name': 'latitude_longitude',
expected = {'grid_mapping_name': b'latitude_longitude',
'longitude_of_prime_meridian': 0.0,
'earth_radius': 6371229.0
}
self._test(coord_system, expected)

def test_elliptic_geog_cs(self):
coord_system = GeogCS(637, 600)
expected = {'grid_mapping_name': 'latitude_longitude',
expected = {'grid_mapping_name': b'latitude_longitude',
'longitude_of_prime_meridian': 0.0,
'semi_minor_axis': 600.0,
'semi_major_axis': 637.0,
Expand All @@ -583,7 +583,7 @@ def test_lambert_conformal(self):
false_easting=-2, false_northing=-5,
secant_latitudes=(38, 50),
ellipsoid=GeogCS(6371000))
expected = {'grid_mapping_name': 'lambert_conformal_conic',
expected = {'grid_mapping_name': b'lambert_conformal_conic',
'latitude_of_projection_origin': 44,
'longitude_of_central_meridian': 2,
'false_easting': -2, 'false_northing': -5,
Expand All @@ -600,7 +600,7 @@ def test_laea_cs(self):
false_easting=100,
false_northing=200,
ellipsoid=GeogCS(6377563.396, 6356256.909))
expected = {'grid_mapping_name': 'lambert_azimuthal_equal_area',
expected = {'grid_mapping_name': b'lambert_azimuthal_equal_area',
'latitude_of_projection_origin': 52,
'longitude_of_projection_origin': 10,
'false_easting': 100,
Expand Down