Skip to content

Commit eed680c

Browse files
committed
Handle missing and non-string CF axis units
Guard CF axis-unit parsing against missing and non-string values and keep the upstream dataset decode regression test while rebasing onto current main.
1 parent 3b10d51 commit eed680c

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

pyresample/test/test_utils/test_cf.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,26 @@ def test_load_cf_latlon(self, file_func, kwargs, exp_lat, exp_lon, future_geomet
273273
_validate_lonlat_cf_area(adef, cf_info, exp_lon, exp_lat)
274274
assert_future_geometry(adef, future_geometries)
275275

276+
def test_load_cf_axis_without_units(self):
277+
cf_file = _prepare_cf_nh10km()
278+
del cf_file['xc'].attrs['units']
279+
del cf_file['yc'].attrs['units']
280+
281+
_, cf_info = load_cf_area(cf_file, variable='ice_conc')
282+
283+
assert cf_info['x']['unit'] is None
284+
assert cf_info['y']['unit'] is None
285+
286+
def test_load_cf_axis_with_non_string_units(self):
287+
cf_file = _prepare_cf_nh10km()
288+
cf_file['xc'].attrs['units'] = 1
289+
cf_file['yc'].attrs['units'] = 1
290+
291+
_, cf_info = load_cf_area(cf_file, variable='ice_conc')
292+
293+
assert cf_info['x']['unit'] is None
294+
assert cf_info['y']['unit'] is None
295+
276296
def test_load_cf_dataset_input_decodes_cf_coordinates(self, tmp_path):
277297
import xarray as xr
278298

pyresample/utils/cf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ def _load_cf_axis_info(nc_handle, coord_varname):
167167
unit = None
168168

169169
# some units that are valid in CF are not valid to pass to proj
170-
if unit.startswith('rad') or \
171-
unit.startswith('deg'):
170+
if not isinstance(unit, str) or unit.startswith(('rad', 'deg')):
172171
unit = None
173172

174173
# return in a dictionnary structure

0 commit comments

Comments
 (0)