Skip to content

Commit 2cf099a

Browse files
lbdreyerpp-mo
authored andcommitted
Revert pv level indexing change (#165)
1 parent 6c8f7dc commit 2cf099a

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed

iris_grib/_load_convert.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,14 +1508,17 @@ def hybrid_factories(section, metadata):
15081508
{'long_name': 'sigma'},
15091509
Reference('ref_surface_pressure')]
15101510

1511-
# Create the level pressure scalar coordinate.
1511+
# Create the level height/pressure scalar coordinate.
1512+
# scaledValue represents the level number, which is used to select
1513+
# the sigma and delta values as follows:
1514+
# sigma, delta = PV[i], PV[NV/2+i] : where i=1..level_number
15121515
pv = section['pv']
1513-
offset = scaledValue - 1
1516+
offset = scaledValue
15141517
coord = DimCoord(pv[offset], long_name=level_value_name,
15151518
units=level_value_units)
15161519
metadata['aux_coords_and_dims'].append((coord, None))
15171520
# Create the sigma scalar coordinate.
1518-
offset = NV // 2 + scaledValue - 1
1521+
offset = NV // 2 + scaledValue
15191522
coord = AuxCoord(pv[offset], long_name='sigma')
15201523
metadata['aux_coords_and_dims'].append((coord, None))
15211524
# Create the associated factory reference.

iris_grib/_save_rules.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,9 @@ def set_fixed_surfaces(cube, grib, full3d_cube=None):
950950
'maximum value = {}.')
951951
raise ValueError(msg.format(max_valid_nlevels, n_levels))
952952
# In sample data we have seen, there seems to be an extra missing data
953-
# value after each set of n-levels coefficients.
954-
# For now, we retain that encoding. This should not cause problems as
955-
# values are indexed according to model-level-number,
956-
# I.E. sigma, delta = PV[i], PV[NV/2+i]
953+
# value *before* each set of n-levels coefficients.
954+
# Note: values are indexed according to model_level_number,
955+
# I.E. sigma, delta = PV[i], PV[NV/2+i] : where i=1..n_levels
957956
n_coeffs = n_levels + 1
958957
coeffs_array = np.zeros(n_coeffs * 2, dtype=np.float32)
959958
for n_lev, height, sigma in zip(model_levels,
@@ -962,11 +961,11 @@ def set_fixed_surfaces(cube, grib, full3d_cube=None):
962961
# Record all the level coefficients coming from the 'full' cube.
963962
# Note: if some model levels are missing, we must still have the
964963
# coeffs at the correct index according to the model_level_number
965-
# value, i.e. at [level - 1] and [NV // 2 + level - 1].
964+
# value, i.e. at [level] and [NV // 2 + level].
966965
# Thus, we can *not* paste the values in a block: each one needs to
967966
# go in the index corresponding to its 'model_level_number' value.
968-
coeffs_array[n_lev - 1] = height
969-
coeffs_array[n_coeffs + n_lev - 1] = sigma
967+
coeffs_array[n_lev] = height
968+
coeffs_array[n_coeffs + n_lev] = sigma
970969
pv_values = [float(el) for el in coeffs_array]
971970
# eccodes does not support writing numpy.int64, cast to python int
972971
gribapi.grib_set(grib, "NV", int(n_coeffs * 2))

iris_grib/tests/integration/load_convert/test_load_hybrid_coords.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ def test_hybrid_height_coords_values(self):
3535
[1, 11, 21])
3636

3737
# check sigma values correctly loaded from indices 1, 11, 21.
38-
# NOTE: level[0] == 1, so sigma[0] == 1.0 : This makes sense !
3938
self.assertArrayAllClose(cube.coord('sigma').points,
40-
[1.0, 0.911, 0.694],
39+
[0.998, 0.894, 0.667],
4140
atol=0.001)
4241

4342
# check height values too.
4443
self.assertArrayAllClose(cube.coord('level_height').points,
45-
[0., 800., 2933.],
44+
[20., 953.3, 3220.],
4645
atol=0.5)
4746

4847

@@ -63,9 +62,9 @@ def test_hybrid_pressure_coords_values(self):
6362
self.assertArrayEqual(cube.coord('model_level_number').points,
6463
[1, 51, 91])
6564
self.assertArrayAllClose(cube.coord('sigma').points,
66-
[0., 0.036, 0.998], atol=0.001)
65+
[0., 0.045, 1.], atol=0.001)
6766
self.assertArrayAllClose(cube.coord('level_pressure').points,
68-
[0., 18191.0, 0.00316], rtol=0.001)
67+
[2.00004, 18716.9688, 0.], rtol=0.0001)
6968
self.assertArrayAllClose(
7069
cube.coord('surface_air_pressure')[:2, :3].points,
7170
[[103493.8, 103493.8, 103493.8],
@@ -74,9 +73,9 @@ def test_hybrid_pressure_coords_values(self):
7473
# Also check a few values from the derived coord.
7574
self.assertArrayAllClose(
7675
cube.coord('air_pressure')[:, :3, 0].points,
77-
[[0., 0., 0.],
78-
[21940.3, 21936.9, 21932.8],
79-
[103248.5, 103156.0, 103041.0]], atol=0.1)
76+
[[2., 2., 2.],
77+
[23389.3, 23385.1, 23379.9],
78+
[103493.8, 103401.0, 103285.8]], atol=0.1)
8079

8180

8281
if __name__ == '__main__':

iris_grib/tests/integration/save_rules/test_save_hybrid_coords.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def test_save(self):
5757
# Note: gaps here are because we took model levels = (1, 3, 5).
5858
self.assertArrayAllClose(
5959
msgs[0].sections[4]['pv'],
60-
[5., 0, 45., 0, 111.667, 0,
61-
0.999, 0, 0.995, 0, 0.987, 0],
60+
[0, 5., 0, 45., 0, 111.667, 0, 0.999, 0, 0.995, 0, 0.987],
6261
atol=0.0015)
6362

6463
# Check message #2-of-3 has the correctly encoded hybrid height.
@@ -114,8 +113,8 @@ def test_save(self):
114113
# Note: HUGE gaps here because we took model levels = (1, 51, 91).
115114
self.assertEqual(msgs[0].sections[4]['NV'], 184)
116115
pv_expected = np.zeros(184, dtype=np.float64)
117-
pv_expected[[0, 50, 90]] = [0., 18191.03, 0.003]
118-
pv_expected[[92, 142, 182]] = [0., 0.036, 0.998]
116+
pv_expected[[1, 51, 91]] = [0., 18191.03, 0.003]
117+
pv_expected[[93, 143, 183]] = [0., 0.036, 0.998]
119118
self.assertArrayAllClose(
120119
msgs[0].sections[4]['pv'], pv_expected, atol=0.001)
121120

iris_grib/tests/results/unit/load_cubes/load_cubes/reduced_raw.cml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</auxCoord>
1919
</coord>
2020
<coord>
21-
<dimCoord id="c9091dc6" long_name="level_pressure" points="[0.0]" shape="(1,)" units="Unit('Pa')" value_type="float64"/>
21+
<dimCoord id="c9091dc6" long_name="level_pressure" points="[2.00004005432]" shape="(1,)" units="Unit('Pa')" value_type="float64"/>
2222
</coord>
2323
<coord datadims="[0]">
2424
<auxCoord id="f913a8b3" points="[0.0, 18.0, 36.0, ..., 306.0, 324.0, 342.0]" shape="(13280,)" standard_name="longitude" units="Unit('degrees')" value_type="float64">

0 commit comments

Comments
 (0)