Skip to content

Commit e99f960

Browse files
Update default_units feature branch base1873ce14 (#3792)
* Change default units to "unknown" for all DimensionalMetadata (#3713) * Change default loading unit from "1" to "unknown" (correct branch) (#3709) * Unify saving behaviour of "unknown" and "no_unit" (#3711) * fix test (#3732) Co-authored-by: stephenworsley <[email protected]>
1 parent 1873ce1 commit e99f960

File tree

59 files changed

+265
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+265
-153
lines changed

docs/iris/gallery_code/meteorology/plot_lagged_ensemble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def realization_metadata(cube, field, fname):
4141
import iris.coords
4242

4343
realization_coord = iris.coords.AuxCoord(
44-
np.int32(realization_number), "realization"
44+
np.int32(realization_number), "realization", units="1"
4545
)
4646
cube.add_aux_coord(realization_coord)
4747

docs/iris/src/userguide/navigating_a_cube.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ by field basis *before* they are automatically merged together:
229229
# Add our own realization coordinate if it doesn't already exist.
230230
if not cube.coords('realization'):
231231
realization = np.int32(filename[-6:-3])
232-
ensemble_coord = icoords.AuxCoord(realization, standard_name='realization')
232+
ensemble_coord = icoords.AuxCoord(realization, standard_name='realization', units="1")
233233
cube.add_aux_coord(ensemble_coord)
234234

235235
filename = iris.sample_data_path('GloSea4', '*.pp')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* When loading data from netcdf-CF files, where a variable has no "units" property, the corresponding Iris object will have "units='unknown'". Prior to Iris 3.0, these cases defaulted to "units='1'".

lib/iris/analysis/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,9 @@ def post_process(self, collapsed_cube, data_result, coords, **kwargs):
803803
# order cube.
804804
for point in points:
805805
cube = collapsed_cube.copy()
806-
coord = iris.coords.AuxCoord(point, long_name=coord_name)
806+
coord = iris.coords.AuxCoord(
807+
point, long_name=coord_name, units="percent"
808+
)
807809
cube.add_aux_coord(coord)
808810
cubes.append(cube)
809811

lib/iris/coords.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(
6565
standard_name=None,
6666
long_name=None,
6767
var_name=None,
68-
units="no-unit",
68+
units=None,
6969
attributes=None,
7070
):
7171
"""
@@ -687,7 +687,7 @@ def __init__(
687687
standard_name=None,
688688
long_name=None,
689689
var_name=None,
690-
units="no-unit",
690+
units=None,
691691
attributes=None,
692692
):
693693
"""
@@ -793,7 +793,7 @@ def __init__(
793793
standard_name=None,
794794
long_name=None,
795795
var_name=None,
796-
units="1",
796+
units=None,
797797
attributes=None,
798798
measure=None,
799799
):
@@ -1283,7 +1283,7 @@ def __init__(
12831283
standard_name=None,
12841284
long_name=None,
12851285
var_name=None,
1286-
units="1",
1286+
units=None,
12871287
bounds=None,
12881288
attributes=None,
12891289
coord_system=None,
@@ -2262,7 +2262,7 @@ def from_regular(
22622262
standard_name=None,
22632263
long_name=None,
22642264
var_name=None,
2265-
units="1",
2265+
units=None,
22662266
attributes=None,
22672267
coord_system=None,
22682268
circular=False,
@@ -2325,7 +2325,7 @@ def __init__(
23252325
standard_name=None,
23262326
long_name=None,
23272327
var_name=None,
2328-
units="1",
2328+
units=None,
23292329
bounds=None,
23302330
attributes=None,
23312331
coord_system=None,

lib/iris/fileformats/_pyke_rules/fc_rules_cf.krb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ fc_extras
11951195
UD_UNITS_LON = ['degrees_east', 'degree_east', 'degree_e', 'degrees_e',
11961196
'degreee', 'degreese', 'degrees', 'degrees east',
11971197
'degree east', 'degree e', 'degrees e']
1198+
UNKNOWN_UNIT_STRING = "?"
1199+
NO_UNIT_STRING = "-"
11981200

11991201
#
12001202
# CF Dimensionless Vertical Coordinates
@@ -1651,9 +1653,9 @@ fc_extras
16511653

16521654
################################################################################
16531655
def get_attr_units(cf_var, attributes):
1654-
attr_units = getattr(cf_var, CF_ATTR_UNITS, cf_units._UNIT_DIMENSIONLESS)
1656+
attr_units = getattr(cf_var, CF_ATTR_UNITS, UNKNOWN_UNIT_STRING)
16551657
if not attr_units:
1656-
attr_units = '1'
1658+
attr_units = UNKNOWN_UNIT_STRING
16571659

16581660
# Sanitise lat/lon units.
16591661
if attr_units in UD_UNITS_LAT or attr_units in UD_UNITS_LON:
@@ -1668,10 +1670,10 @@ fc_extras
16681670
cf_var.cf_name, attr_units)
16691671
warnings.warn(msg)
16701672
attributes['invalid_units'] = attr_units
1671-
attr_units = cf_units._UNKNOWN_UNIT_STRING
1673+
attr_units = UNKNOWN_UNIT_STRING
16721674

16731675
if np.issubdtype(cf_var.dtype, np.str_):
1674-
attr_units = cf_units._NO_UNIT_STRING
1676+
attr_units = NO_UNIT_STRING
16751677

16761678
# Get any assoicated calendar for a time reference coordinate.
16771679
if cf_units.as_unit(attr_units).is_time_reference():

lib/iris/fileformats/netcdf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import warnings
2323

2424
import dask.array as da
25+
import cf_units
2526
import netCDF4
2627
import numpy as np
2728
import numpy.ma as ma
@@ -1760,7 +1761,7 @@ def _inner_create_cf_cellmeasure_or_ancil_variable(
17601761
# Add the data to the CF-netCDF variable.
17611762
cf_var[:] = data
17621763

1763-
if dimensional_metadata.units != "unknown":
1764+
if dimensional_metadata.units.is_udunits():
17641765
_setncattr(cf_var, "units", str(dimensional_metadata.units))
17651766

17661767
if dimensional_metadata.standard_name is not None:
@@ -1926,7 +1927,7 @@ def _create_cf_coord_variable(self, cube, dimension_names, coord):
19261927
# Deal with CF-netCDF units and standard name.
19271928
standard_name, long_name, units = self._cf_coord_identity(coord)
19281929

1929-
if units != "unknown":
1930+
if cf_units.as_unit(units).is_udunits():
19301931
_setncattr(cf_var, "units", units)
19311932

19321933
if standard_name is not None:
@@ -2371,7 +2372,7 @@ def store(data, cf_var, fill_value):
23712372
if cube.long_name:
23722373
_setncattr(cf_var, "long_name", cube.long_name)
23732374

2374-
if cube.units != "unknown":
2375+
if cube.units.is_udunits():
23752376
_setncattr(cf_var, "units", str(cube.units))
23762377

23772378
# Add the CF-netCDF calendar attribute.

lib/iris/fileformats/pp_load_rules.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def _convert_vertical_coords(
147147
model_level_number,
148148
standard_name="model_level_number",
149149
attributes={"positive": "down"},
150+
units="1",
150151
)
151152
coords_and_dims.append((coord, dim))
152153

@@ -197,6 +198,7 @@ def _convert_vertical_coords(
197198
model_level_number,
198199
long_name="soil_model_level_number",
199200
attributes={"positive": "down"},
201+
units="1",
200202
)
201203
coords_and_dims.append((coord, dim))
202204
elif np.any(brsvd1 != brlev):
@@ -235,6 +237,7 @@ def _convert_vertical_coords(
235237
model_level_number,
236238
standard_name="model_level_number",
237239
attributes={"positive": "up"},
240+
units="1",
238241
)
239242
level_pressure = _dim_or_aux(
240243
bhlev,
@@ -243,7 +246,10 @@ def _convert_vertical_coords(
243246
bounds=np.vstack((bhrlev, brsvd2)).T,
244247
)
245248
sigma = AuxCoord(
246-
blev, long_name="sigma", bounds=np.vstack((brlev, brsvd1)).T
249+
blev,
250+
long_name="sigma",
251+
bounds=np.vstack((brlev, brsvd1)).T,
252+
units="1",
247253
)
248254
coords_and_dims.extend(
249255
[(model_level_number, dim), (level_pressure, dim), (sigma, dim)]
@@ -265,6 +271,7 @@ def _convert_vertical_coords(
265271
model_level_number,
266272
standard_name="model_level_number",
267273
attributes={"positive": "up"},
274+
units="1",
268275
)
269276
level_height = _dim_or_aux(
270277
blev,
@@ -274,7 +281,10 @@ def _convert_vertical_coords(
274281
attributes={"positive": "up"},
275282
)
276283
sigma = AuxCoord(
277-
bhlev, long_name="sigma", bounds=np.vstack((bhrlev, brsvd2)).T
284+
bhlev,
285+
long_name="sigma",
286+
bounds=np.vstack((bhrlev, brsvd2)).T,
287+
units="1",
278288
)
279289
coords_and_dims.extend(
280290
[(model_level_number, dim), (level_height, dim), (sigma, dim)]
@@ -846,7 +856,7 @@ def _convert_scalar_realization_coords(lbrsvd4):
846856
coords_and_dims = []
847857
if lbrsvd4 != 0:
848858
coords_and_dims.append(
849-
(DimCoord(lbrsvd4, standard_name="realization"), None)
859+
(DimCoord(lbrsvd4, standard_name="realization", units="1"), None)
850860
)
851861
return coords_and_dims
852862

@@ -1078,7 +1088,7 @@ def _all_other_rules(f):
10781088
and f.lbmon == f.lbmond
10791089
):
10801090
aux_coords_and_dims.append(
1081-
(AuxCoord(f.lbmon, long_name="month_number"), None)
1091+
(AuxCoord(f.lbmon, long_name="month_number", units="1"), None)
10821092
)
10831093
aux_coords_and_dims.append(
10841094
(

lib/iris/tests/integration/test_netcdf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def test_hybrid_height_and_pressure(self):
8181
1200.0, long_name="level_pressure", units="hPa"
8282
)
8383
)
84-
cube.add_aux_coord(iris.coords.DimCoord(0.5, long_name="other sigma"))
84+
cube.add_aux_coord(
85+
iris.coords.DimCoord(0.5, long_name="other sigma", units="1")
86+
)
8587
cube.add_aux_coord(
8688
iris.coords.DimCoord(
8789
1000.0, long_name="surface_air_pressure", units="hPa"

lib/iris/tests/integration/test_pp.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def test_hybrid_height_with_non_standard_coords(self):
299299
delta_lower, delta, delta_upper = 150, 200, 250
300300

301301
cube = Cube(np.zeros((ny, nx)), "air_temperature")
302-
level_coord = AuxCoord(0, "model_level_number")
302+
level_coord = AuxCoord(0, "model_level_number", units="1")
303303
cube.add_aux_coord(level_coord)
304304
delta_coord = AuxCoord(
305305
delta,
@@ -308,7 +308,10 @@ def test_hybrid_height_with_non_standard_coords(self):
308308
units="m",
309309
)
310310
sigma_coord = AuxCoord(
311-
sigma, bounds=[[sigma_lower, sigma_upper]], long_name="mavis"
311+
sigma,
312+
bounds=[[sigma_lower, sigma_upper]],
313+
long_name="mavis",
314+
units="1",
312315
)
313316
surface_altitude_coord = AuxCoord(
314317
np.zeros((ny, nx)), "surface_altitude", units="m"
@@ -343,7 +346,7 @@ def test_hybrid_pressure_with_non_standard_coords(self):
343346
delta_lower, delta, delta_upper = 0.15, 0.2, 0.25
344347

345348
cube = Cube(np.zeros((ny, nx)), "air_temperature")
346-
level_coord = AuxCoord(0, "model_level_number")
349+
level_coord = AuxCoord(0, "model_level_number", units="1")
347350
cube.add_aux_coord(level_coord)
348351
delta_coord = AuxCoord(
349352
delta,
@@ -352,7 +355,10 @@ def test_hybrid_pressure_with_non_standard_coords(self):
352355
units="Pa",
353356
)
354357
sigma_coord = AuxCoord(
355-
sigma, bounds=[[sigma_lower, sigma_upper]], long_name="mavis"
358+
sigma,
359+
bounds=[[sigma_lower, sigma_upper]],
360+
long_name="mavis",
361+
units="1",
356362
)
357363
surface_air_pressure_coord = AuxCoord(
358364
np.zeros((ny, nx)), "surface_air_pressure", units="Pa"

0 commit comments

Comments
 (0)