Skip to content

Commit c663df5

Browse files
committed
Updates following review comments
1 parent 036fbb6 commit c663df5

5 files changed

Lines changed: 103 additions & 187 deletions

File tree

improver/calibration/ensemble_calibration.py

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,15 @@ def _add_predictor_coords(
862862
863863
Returns:
864864
A cube with the predictor_index and predictor_name
865-
coordinates added.
865+
coordinates added. Single value dimension coordinates
866+
are converted to non-dimension coordinates.
866867
"""
867868
template_cubes = iris.cube.CubeList()
868869
fp_names = []
869870
for index, fp in enumerate(forecast_predictors):
870871
template_cube_copy = template_cube.copy()
871872
predictor_index = iris.coords.DimCoord(
872-
np.array(index, dtype=np.int32), long_name="predictor_index", units="1",
873+
np.array(index, dtype=np.int8), long_name="predictor_index", units="1",
873874
)
874875
template_cube_copy.add_aux_coord(predictor_index)
875876
template_cube_copy = iris.util.new_axis(template_cube_copy, predictor_index)
@@ -882,7 +883,7 @@ def _add_predictor_coords(
882883
template_cube.add_aux_coord(
883884
predictor_name, data_dims=template_cube.coord_dims("predictor_index"),
884885
)
885-
return template_cube
886+
return iris.util.squeeze(template_cube)
886887

887888
def _create_cubelist(
888889
self,
@@ -930,7 +931,10 @@ def _create_cubelist(
930931
template_cube = self._add_predictor_coords(
931932
template_cube, forecast_predictors
932933
)
934+
optimised_coeff = np.reshape(optimised_coeff, template_cube.shape)
933935
replacements += ["predictor_index", "predictor_name"]
936+
else:
937+
optimised_coeff = np.array(optimised_coeff)
934938

935939
for coord in coords_to_replace:
936940
template_cube.replace_coord(coord)
@@ -952,9 +956,7 @@ def _create_cubelist(
952956
template_cube,
953957
generate_mandatory_attributes([historic_forecasts]),
954958
optional_attributes=self._set_attributes(historic_forecasts),
955-
data=np.reshape(optimised_coeff, template_cube.shape)
956-
if "beta" == coeff_name
957-
else np.array(optimised_coeff),
959+
data=optimised_coeff,
958960
)
959961
cubelist.append(cube)
960962
return cubelist
@@ -1021,7 +1023,6 @@ def compute_initial_guess(
10211023
truths: ndarray,
10221024
forecast_predictor: ndarray,
10231025
predictor: str,
1024-
number_of_forecast_predictors: int,
10251026
number_of_realizations: Optional[int],
10261027
) -> List[float]:
10271028
"""
@@ -1065,9 +1066,6 @@ def compute_initial_guess(
10651066
the location parameter when estimating the EMOS coefficients.
10661067
Currently the ensemble mean ("mean") and the ensemble
10671068
realizations ("realizations") are supported as the predictors.
1068-
number_of_forecast_predictors:
1069-
Number of forecast predictors. This includes all additional
1070-
fields to include in the calibration.
10711069
number_of_realizations:
10721070
Number of realizations within the forecast predictor. If no
10731071
realizations are present, this option is None.
@@ -1089,7 +1087,7 @@ def compute_initial_guess(
10891087

10901088
if predictor == "mean" and default_initial_guess:
10911089
initial_beta = np.repeat(
1092-
1.0 / number_of_forecast_predictors, number_of_forecast_predictors
1090+
1.0 / forecast_predictor.shape[0], forecast_predictor.shape[0]
10931091
).tolist()
10941092
initial_guess = [0] + initial_beta + [0, 1]
10951093
elif predictor == "realizations" and default_initial_guess:
@@ -1175,22 +1173,18 @@ def guess_and_minimise(
11751173
11761174
"""
11771175
if self.point_by_point and not self.use_default_initial_guess:
1178-
index = [
1179-
truths.coord(axis="y"),
1180-
truths.coord(axis="x"),
1181-
]
11821176
y_name = truths.coord(axis="y").name()
11831177
x_name = truths.coord(axis="x").name()
11841178

11851179
initial_guess = []
1186-
for truth_slice in truths.slices_over(index):
1180+
for truth_slice in truths.slices_over([y_name, x_name]):
11871181
constr = iris.Constraint(
11881182
coord_values={
11891183
y_name: lambda cell: any(
1190-
np.isclose(cell.point, truth_slice.coord(axis="y").points)
1184+
np.isclose(cell.point, truth_slice.coord(y_name).points)
11911185
),
11921186
x_name: lambda cell: any(
1193-
np.isclose(cell.point, truth_slice.coord(axis="x").points)
1187+
np.isclose(cell.point, truth_slice.coord(x_name).points)
11941188
),
11951189
}
11961190
)
@@ -1209,7 +1203,6 @@ def guess_and_minimise(
12091203
truth_slice.data,
12101204
forecast_predictors_data,
12111205
self.predictor,
1212-
len(forecast_predictors),
12131206
number_of_realizations,
12141207
)
12151208
)
@@ -1227,7 +1220,6 @@ def guess_and_minimise(
12271220
truths.data,
12281221
forecast_predictor_data,
12291222
self.predictor,
1230-
len(forecast_predictors),
12311223
number_of_realizations,
12321224
)
12331225
if self.point_by_point:
@@ -1334,14 +1326,14 @@ def process(
13341326
"forecast_reference_time",
13351327
"realization",
13361328
]
1337-
for af_cube in additional_fields:
1338-
if any([af_cube.coords(c) for c in disallowed_coords]):
1329+
for cube in additional_fields:
1330+
if any([cube.coords(c) for c in disallowed_coords]):
13391331
coords = [
1340-
af_cube.coord(c) for c in disallowed_coords if af_cube.coords(c)
1332+
cube.coord(c) for c in disallowed_coords if cube.coords(c)
13411333
]
13421334
msg = (
13431335
"Only static additional predictors are supported. "
1344-
f"The {af_cube.name()} cube provided contains {coords}."
1336+
f"The {cube.name()} cube provided contains {coords}."
13451337
)
13461338
raise ValueError(msg)
13471339

@@ -1360,9 +1352,8 @@ def process(
13601352

13611353
number_of_realizations = None
13621354
if self.predictor == "mean":
1363-
forecast_predictors = iris.cube.CubeList()
1364-
forecast_predictors.append(
1365-
collapsed(historic_forecasts, "realization", iris.analysis.MEAN)
1355+
forecast_predictors = iris.cube.CubeList(
1356+
[collapsed(historic_forecasts, "realization", iris.analysis.MEAN)]
13661357
)
13671358
elif self.predictor == "realizations":
13681359
number_of_realizations = len(historic_forecasts.coord("realization").points)
@@ -1417,6 +1408,7 @@ def __init__(self, predictor: str = "mean") -> None:
14171408

14181409
self.coefficients_cubelist = None
14191410
self.current_forecast = None
1411+
self.additional_fields = None
14201412

14211413
def __repr__(self) -> str:
14221414
"""Represent the configured plugin instance as a string."""
@@ -1491,29 +1483,18 @@ def _calculate_location_parameter_from_mean(self) -> ndarray:
14911483
Location parameter calculated using the ensemble mean as the
14921484
predictor.
14931485
"""
1494-
forecast_predictors = iris.cube.CubeList()
1495-
forecast_predictors.append(
1496-
collapsed(self.current_forecast, "realization", iris.analysis.MEAN)
1486+
forecast_predictors = iris.cube.CubeList(
1487+
[collapsed(self.current_forecast, "realization", iris.analysis.MEAN)]
14971488
)
14981489
if self.additional_fields:
14991490
forecast_predictors.extend(self.additional_fields)
15001491

1492+
beta_cube = self.coefficients_cubelist.extract_cube("emos_coefficient_beta")
1493+
15011494
fp_names = [fp.name() for fp in forecast_predictors]
1502-
if len(forecast_predictors) != len(
1503-
self.coefficients_cubelist.extract_cube("emos_coefficient_beta")
1504-
.coord("predictor_index")
1505-
.points
1506-
):
1507-
n_coord_points = len(
1508-
self.coefficients_cubelist.extract_cube("emos_coefficient_beta")
1509-
.coord("predictor_index")
1510-
.points
1511-
)
1512-
coord_names = (
1513-
self.coefficients_cubelist.extract_cube("emos_coefficient_beta")
1514-
.coord("predictor_name")
1515-
.points
1516-
)
1495+
if len(forecast_predictors) != len(beta_cube.coord("predictor_index").points):
1496+
n_coord_points = len(beta_cube.coord("predictor_index").points)
1497+
coord_names = beta_cube.coord("predictor_name").points
15171498
msg = (
15181499
"The number of forecast predictors must equal the number of "
15191500
"beta coefficients in order to create a calibrated forecast. "
@@ -1527,12 +1508,7 @@ def _calculate_location_parameter_from_mean(self) -> ndarray:
15271508
location_parameter = np.zeros(forecast_predictors[0].shape)
15281509
for fp in forecast_predictors:
15291510
constr = iris.Constraint(predictor_name=fp.name())
1530-
location_parameter += (
1531-
self.coefficients_cubelist.extract_cube("emos_coefficient_beta")
1532-
.extract(constr)
1533-
.data
1534-
* fp.data
1535-
)
1511+
location_parameter += beta_cube.extract(constr).data * fp.data
15361512
location_parameter += self.coefficients_cubelist.extract_cube(
15371513
"emos_coefficient_alpha"
15381514
).data

improver_tests/acceptance/SHA256SUMS

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ bee125371a9db4c80fb7f8d1b0644a2710903aad27023b5939331f7ec5f3b617 ./apply-emos-c
88
fa8efb4b54430cf6f5e4782b9ec1a0c49dd4102d228dae04bd250ec7b0b9b481 ./apply-emos-coefficients/land_sea/realizations_kgo.nc
99
4eaedd69b7d0bbc4b99fa598ffffdb39454962d8e9c33c3d75f58d1de9f25313 ./apply-emos-coefficients/normal/input.nc
1010
0c5839e3f3a9d557a24757c292099af859ff7ce27c75de7ba8b98a46828b3912 ./apply-emos-coefficients/normal/kgo.nc
11-
f2af75abf277e1c270bc4921b092d72c1d3423ac850110e372cfd745852e46ef ./apply-emos-coefficients/normal/normal_coefficients.nc
11+
7c4033340c89788ba3e55f560fbe2f47ed8bac14c7b47cdf71312e115caf7cfc ./apply-emos-coefficients/normal/normal_coefficients.nc
1212
d51fde4566a4d9695a3ba26622ef8505a879131e10f438ab19d4594a57997ad7 ./apply-emos-coefficients/percentiles/input.nc
1313
7e673a16dd80edb42f890771184a4ad8592a53bd0d0d18d7cd316c489c9b4d7d ./apply-emos-coefficients/percentiles/kgo.nc
1414
bd56f09033f7a37323e04be7b509e8e363eb2474c04fb8aa4fb5e3769acb81f7 ./apply-emos-coefficients/probabilities/input.nc
1515
8eedf1e3cbee70e33fc281f2da06f89895e68c0d8a6ad3099b6c9522b6c4ee45 ./apply-emos-coefficients/probabilities/kgo.nc
16-
6a4af7032dee64f5b9ea0e3790788814cca11d83a1a0227af08b33233c04609d ./apply-emos-coefficients/realizations/realizations_coefficients.nc
16+
12110777cf3db3c8d76daa06f78760f8e314038262f359e659d16fa5d49b98bf ./apply-emos-coefficients/realizations/realizations_coefficients.nc
1717
7c5cd2382cad998e22ef0d4210bf65d131587514de2b5cf4ad1fe3c30fede188 ./apply-emos-coefficients/realizations/realizations_kgo.nc
1818
615ddb59a838024bb055b9cbbb44b7c18ce28d5734952777774980760e0f36fc ./apply-emos-coefficients/rebadged_percentiles/input.nc
1919
0bc91af1e7003d696aa440a07d79e653bca566733cdf9bf525ca92cff821548f ./apply-emos-coefficients/sites/input.nc
20-
f3f2b6ccafab05468751e7efa9ce42c56a27b4543419bb14acb205381ca2ed1f ./apply-emos-coefficients/sites/offset/coefficients.nc
20+
a0112b5a48ba99a1a4a345d43b0c453caaf25181504fb9a13786b5722f84cc10 ./apply-emos-coefficients/sites/offset/coefficients.nc
2121
22d950494dc9d76d642b0ecc75414e95bfb78fa693e5701fe7cd2fc462a0c6a7 ./apply-emos-coefficients/sites/offset/kgo.nc
2222
2722b1d08a87cebf1f36ac914d5badb9af38859b2150da83311dc202ce9be4dd ./apply-emos-coefficients/sites/offset/offset_input.nc
23-
b587f6f057d979eebef1c2351e86ba67b99caaa2b80d2792a73b6d8691dd8eeb ./apply-emos-coefficients/sites/point_by_point/coefficients.nc
23+
63291b3435066f0b0fc56bc78b9774e0e7457d5a6b20086a67ecc6eb826d618d ./apply-emos-coefficients/sites/point_by_point/coefficients.nc
2424
833a703e14a02999a7d70e9a37ce199ab36a09b13b96f87295a0fb91739658ce ./apply-emos-coefficients/sites/point_by_point/kgo.nc
2525
965a1f0565f9192d95eb01d0a61dc7bede6902f5e1a0481d92611e677841a139 ./apply-emos-coefficients/truncated_normal/input.nc
2626
feb2d93cf96b05889571bcb348dfbb4d60cfc1f3d9b7343dcf7d85cf34339746 ./apply-emos-coefficients/truncated_normal/kgo.nc
27-
32f112547dae560e966413b1e326564adac86fda349f1b4256ac1c5910b2d90a ./apply-emos-coefficients/truncated_normal/truncated_normal_coefficients.nc
27+
d2cab1d3d8aa588be08a3d7d65e95e859fed37daa767e8d4a2bdaae25702b9a8 ./apply-emos-coefficients/truncated_normal/truncated_normal_coefficients.nc
2828
182891343e44dbccb637cc668946fad2902d25bc401bf3e539c9d8d68fe4ef39 ./apply-lapse-rate/basic/kgo.nc
2929
d72e62922cd2d97b7ad82f6b58706b9bfebf1baa0a3118a3cd26d54ca846c3ad ./apply-lapse-rate/basic/ukvx_lapse_rate.nc
3030
d81b1559d07cc9ff9086fd8fe3d72b0228701eae706be755307d18c7bfcb7df2 ./apply-lapse-rate/basic/ukvx_orography.nc
@@ -100,19 +100,19 @@ bf7e42be7897606682c3ecdaeb27bf3d3b6ab13a9a88b46c88ae6e92801c6245 ./create-grid-
100100
d84aaed836c80f55e11207c8618d556b8aaca0c9dfd4320a3ea88387de71670c ./estimate-emos-coefficients/combined_input/historic_forecast.json
101101
eeda3b2a092f3bc78bf7dc8cc22efd5e885f94b8b8e04ccc4740978dfc79e04d ./estimate-emos-coefficients/combined_input/truth.json
102102
ff3a00a16fa94697d6e529a6f98384e4602f70a7d6ce26669c3947c8ac2e6f7e ./estimate-emos-coefficients/landmask.nc
103-
cb0e56a8f4e1830f9462255bbb6e6ab5e1dd647942ec62677fa509ffad528f6d ./estimate-emos-coefficients/normal/default_initial_guess_kgo.nc
103+
4907ffd47370afdcc7aaf001e633240ec89e092463ad7ae25e910af10d6c28e4 ./estimate-emos-coefficients/normal/default_initial_guess_kgo.nc
104104
f878fca6cb6b8af8967d1bcdae28d9933b27cfd25e8e9bcf93498cae3e2163df ./estimate-emos-coefficients/normal/history/20170602T0300Z-PT0012H-temperature_at_screen_level.nc
105105
c72e48ae88fb3a9c909323376c20d9bcaecec5e1b55a42accdab7201ce8d7cc8 ./estimate-emos-coefficients/normal/history/20170603T0300Z-PT0012H-temperature_at_screen_level.nc
106106
a7bd5d01ed80297fb5b28be9a308fd8de20c162a87220be5e59c8ac3ffa800f0 ./estimate-emos-coefficients/normal/history/20170604T0300Z-PT0012H-temperature_at_screen_level.nc
107-
f2af75abf277e1c270bc4921b092d72c1d3423ac850110e372cfd745852e46ef ./estimate-emos-coefficients/normal/kgo.nc
108-
4964511af78cd064396e897b2efee748c30bdcaa925882f9d31006f23f4a14ef ./estimate-emos-coefficients/normal/land_only_kgo.nc
109-
6a4af7032dee64f5b9ea0e3790788814cca11d83a1a0227af08b33233c04609d ./estimate-emos-coefficients/normal/realizations/kgo.nc
107+
7c4033340c89788ba3e55f560fbe2f47ed8bac14c7b47cdf71312e115caf7cfc ./estimate-emos-coefficients/normal/kgo.nc
108+
67d50642e9a49a0c9115c786cc74f62fc27e10aaaff7ba7e2329d2580c63aefd ./estimate-emos-coefficients/normal/land_only_kgo.nc
109+
12110777cf3db3c8d76daa06f78760f8e314038262f359e659d16fa5d49b98bf ./estimate-emos-coefficients/normal/realizations/kgo.nc
110110
42fe8daffb997848086cb08b3d39dd366b561bb1c24fc7ed3e2e54737d772b9a ./estimate-emos-coefficients/normal/sites/history/20201209T1500Z-PT0012H00M-temperature_at_screen_level.nc
111111
3cda6a3244db09530585454d323ec13bde6b4cfae24940a8b069469f47aec60c ./estimate-emos-coefficients/normal/sites/history/20201210T1500Z-PT0012H00M-temperature_at_screen_level.nc
112112
e5dab98673996a3663093a0353b4efac7910b8176921bd0fbf10aa0f53ae5800 ./estimate-emos-coefficients/normal/sites/history/20201211T1500Z-PT0012H00M-temperature_at_screen_level.nc
113-
b587f6f057d979eebef1c2351e86ba67b99caaa2b80d2792a73b6d8691dd8eeb ./estimate-emos-coefficients/normal/sites/point_by_point/kgo.nc
114-
980b19f4e5dcc557e3e499ee884ade51e6a6052fca2d7d2d95259ea442ec99b5 ./estimate-emos-coefficients/normal/sites/point_by_point/realizations_kgo.nc
115-
cb2452279a335a09edc0fc9aced714dddd8cc1472344a67f0487e7e9b55cd32d ./estimate-emos-coefficients/normal/sites/point_by_point_default_initial_guess/kgo.nc
113+
63291b3435066f0b0fc56bc78b9774e0e7457d5a6b20086a67ecc6eb826d618d ./estimate-emos-coefficients/normal/sites/point_by_point/kgo.nc
114+
20ba2f8b50ccafb6e10af1db9ecef72f2d529afd131a652bf2cfa70c64d5f95b ./estimate-emos-coefficients/normal/sites/point_by_point/realizations_kgo.nc
115+
36412f957fb44761e389432c401d49a443d50add44fc1b40ce47456962173fba ./estimate-emos-coefficients/normal/sites/point_by_point_default_initial_guess/kgo.nc
116116
c93cb58b443c90fec46135715c322d2a4a214868f13bcb073871690c0a59aeaa ./estimate-emos-coefficients/normal/sites/truth/20201209T1500Z-PT0000H00M-temperature_at_screen_level.nc
117117
d128c7ae795bbc5c7281d4140f1d7826e82e82160ec6320317f72bccd08bcb83 ./estimate-emos-coefficients/normal/sites/truth/20201210T1500Z-PT0000H00M-temperature_at_screen_level.nc
118118
bca3f986233d44f4c008f1113d40e808f730b80428a55fe4eb4c6f9ee8241e16 ./estimate-emos-coefficients/normal/sites/truth/20201211T1500Z-PT0000H00M-temperature_at_screen_level.nc
@@ -122,7 +122,7 @@ ee8638a56d5c8866106ed0e0e9578f391300d9271b78b49e70baeae78ebb9e2d ./estimate-emo
122122
4d6f2a70e895017464765f7c9ec1df67e4fc33992783241971a4d630177b6e08 ./estimate-emos-coefficients/truncated_normal/history/20170602T0300Z-PT0012H-horizontal_wind_speed_at_10m.nc
123123
11e0ff3506e0456abd4b169846cf1f5bcc9596fcb50eb520ba9f68c988fa786d ./estimate-emos-coefficients/truncated_normal/history/20170603T0300Z-PT0012H-horizontal_wind_speed_at_10m.nc
124124
db4aa83b225fd02aa6d15b5ea1500b960d2da46378290ebba9d45b8a29c189f2 ./estimate-emos-coefficients/truncated_normal/history/20170604T0300Z-PT0012H-horizontal_wind_speed_at_10m.nc
125-
32f112547dae560e966413b1e326564adac86fda349f1b4256ac1c5910b2d90a ./estimate-emos-coefficients/truncated_normal/kgo.nc
125+
d2cab1d3d8aa588be08a3d7d65e95e859fed37daa767e8d4a2bdaae25702b9a8 ./estimate-emos-coefficients/truncated_normal/kgo.nc
126126
397b0e3712041863f9d8ac45480b67162f6e740cf9b7d2e1521a6844074146e8 ./estimate-emos-coefficients/truncated_normal/truth/20170602T1500Z-PT0000H-horizontal_wind_speed_at_10m.nc
127127
1032b6e41987c08ad4caace25e182a02701177408461c7b573f3e8b304b31c16 ./estimate-emos-coefficients/truncated_normal/truth/20170603T1500Z-PT0000H-horizontal_wind_speed_at_10m.nc
128128
fe84db49d69ea62e2e82312c7b3d860c7329c627d79d76c2543d365f69d2e0d8 ./estimate-emos-coefficients/truncated_normal/truth/20170604T1500Z-PT0000H-horizontal_wind_speed_at_10m.nc

0 commit comments

Comments
 (0)