Skip to content

Commit 46f6b25

Browse files
committed
Reorganisation of pp lbproc_pairs feature.
1 parent f22de6c commit 46f6b25

File tree

8 files changed

+68
-43
lines changed

8 files changed

+68
-43
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# (C) British Crown Copyright 2017, Met Office
2+
#
3+
# This file is part of Iris.
4+
#
5+
# Iris is free software: you can redistribute it and/or modify it under
6+
# the terms of the GNU Lesser General Public License as published by the
7+
# Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Iris is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from __future__ import (absolute_import, division, print_function)
19+
from six.moves import (filter, input, map, range, zip) # noqa
20+
import six
21+
22+
import itertools
23+
24+
25+
# LBPROC codes and their English equivalents
26+
LBPROC_PAIRS = ((1, "Difference from another experiment"),
27+
(2, "Difference from zonal (or other spatial) mean"),
28+
(4, "Difference from time mean"),
29+
(8, "X-derivative (d/dx)"),
30+
(16, "Y-derivative (d/dy)"),
31+
(32, "Time derivative (d/dt)"),
32+
(64, "Zonal mean field"),
33+
(128, "Time mean field"),
34+
(256, "Product of two fields"),
35+
(512, "Square root of a field"),
36+
(1024, "Difference between fields at levels BLEV and BRLEV"),
37+
(2048, "Mean over layer between levels BLEV and BRLEV"),
38+
(4096, "Minimum value of field during time period"),
39+
(8192, "Maximum value of field during time period"),
40+
(16384, "Magnitude of a vector, not specifically wind speed"),
41+
(32768, "Log10 of a field"),
42+
(65536, "Variance of a field"),
43+
(131072, "Mean over an ensemble of parallel runs"))
44+
45+
# lbproc_map is dict mapping lbproc->English and English->lbproc
46+
# essentially a one to one mapping
47+
LBPROC_MAP = {x: y for x, y in
48+
itertools.chain(LBPROC_PAIRS, ((y, x) for x, y in LBPROC_PAIRS))}

lib/iris/fileformats/pp.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
import iris.config
4343
import iris.fileformats.pp_rules
4444
from iris.fileformats.pp_save_rules import verify
45+
46+
# NOTE: this is for backwards-compatitibility *ONLY*
47+
# We could simply remove it for v2.0 ?
48+
from iris.fileformats._pp_lbproc_pairs import (LBPROC_PAIRS,
49+
LBPROC_MAP as lbproc_map)
4550
import iris.fileformats.rules
4651
import iris.coord_systems
4752

lib/iris/fileformats/pp_rules.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
from iris.aux_factory import HybridHeightFactory, HybridPressureFactory
3030
from iris.coords import AuxCoord, CellMethod, DimCoord
3131
from iris.fileformats.rules import (ConversionMetadata, Factory, Reference,
32-
ReferenceTarget, lbproc_map)
32+
ReferenceTarget)
33+
import iris.fileformats.pp
34+
from iris.fileformats._pp_lbproc_pairs import LBPROC_MAP
3335
from iris.fileformats.um_cf_map import (LBFC_TO_CF, STASH_TO_CF,
3436
STASHCODE_IMPLIED_HEIGHTS)
35-
import iris.fileformats.pp
3637

3738

3839
###############################################################################
@@ -1027,7 +1028,7 @@ def _all_other_rules(f):
10271028
if unhandled_lbproc:
10281029
attributes["ukmo__process_flags"] = tuple(sorted(
10291030
[name
1030-
for value, name in six.iteritems(lbproc_map)
1031+
for value, name in six.iteritems(LBPROC_MAP)
10311032
if isinstance(value, int) and f.lbproc & value]))
10321033

10331034
if (f.lbsrce % 10000) == 1111:

lib/iris/fileformats/pp_save_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
from iris.fileformats._ff_cross_references import STASH_TRANS
2626
from iris.aux_factory import HybridHeightFactory, HybridPressureFactory
2727
from iris.fileformats.um_cf_map import CF_TO_LBFC
28+
from iris.fileformats._pp_lbproc_pairs import LBPROC_MAP
2829
from iris.fileformats.rules import (aux_factory,
2930
has_aux_factory,
30-
lbproc_map,
3131
scalar_cell_method,
3232
scalar_coord,
3333
vector_coord)
@@ -568,7 +568,7 @@ def _lbproc_rules(cube, pp):
568568
pp.lbproc = 0
569569

570570
if cube.attributes.get("ukmo__process_flags", None):
571-
pp.lbproc += sum([lbproc_map[name]
571+
pp.lbproc += sum([LBPROC_MAP[name]
572572
for name in cube.attributes["ukmo__process_flags"]])
573573

574574
# Zonal-mean: look for a CellMethod which is a "mean" over "longitude" or

lib/iris/fileformats/rules.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import collections
2828
from contextlib import contextmanager
2929
import getpass
30-
import itertools
3130
import logging
3231
import logging.handlers as handlers
3332
import os
@@ -51,32 +50,6 @@
5150
('name', 'transform'))
5251

5352

54-
# LBPROC codes and their English equivalents
55-
LBPROC_PAIRS = ((1, "Difference from another experiment"),
56-
(2, "Difference from zonal (or other spatial) mean"),
57-
(4, "Difference from time mean"),
58-
(8, "X-derivative (d/dx)"),
59-
(16, "Y-derivative (d/dy)"),
60-
(32, "Time derivative (d/dt)"),
61-
(64, "Zonal mean field"),
62-
(128, "Time mean field"),
63-
(256, "Product of two fields"),
64-
(512, "Square root of a field"),
65-
(1024, "Difference between fields at levels BLEV and BRLEV"),
66-
(2048, "Mean over layer between levels BLEV and BRLEV"),
67-
(4096, "Minimum value of field during time period"),
68-
(8192, "Maximum value of field during time period"),
69-
(16384, "Magnitude of a vector, not specifically wind speed"),
70-
(32768, "Log10 of a field"),
71-
(65536, "Variance of a field"),
72-
(131072, "Mean over an ensemble of parallel runs"))
73-
74-
# lbproc_map is dict mapping lbproc->English and English->lbproc
75-
# essentially a one to one mapping
76-
lbproc_map = {x: y for x, y in
77-
itertools.chain(LBPROC_PAIRS, ((y, x) for x, y in LBPROC_PAIRS))}
78-
79-
8053
class ConcreteReferenceTarget(object):
8154
"""Everything you need to make a real Cube for a named reference."""
8255

lib/iris/tests/test_cube_to_pp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import iris.coord_systems
3333
import iris.fileformats.pp
3434
from iris.fileformats.pp import PPField3
35-
import iris.fileformats.rules
3635
from iris.tests import mock
3736
import iris.tests.pp as pp
3837
import iris.util
@@ -240,7 +239,7 @@ def lbproc_from_pp(self, filename):
240239

241240
def test_pp_save_rules(self):
242241
# Test single process flags
243-
for _, process_desc in iris.fileformats.rules.LBPROC_PAIRS[1:]:
242+
for _, process_desc in iris.fileformats.pp.LBPROC_PAIRS[1:]:
244243
# Get basic cube and set process flag manually
245244
ll_cube = stock.lat_lon_cube()
246245
ll_cube.attributes["ukmo__process_flags"] = (process_desc,)
@@ -251,15 +250,15 @@ def test_pp_save_rules(self):
251250

252251
# Check the lbproc is what we expect
253252
self.assertEqual(self.lbproc_from_pp(temp_filename),
254-
iris.fileformats.rules.lbproc_map[process_desc])
253+
iris.fileformats.pp.lbproc_map[process_desc])
255254

256255
os.remove(temp_filename)
257256

258257
# Test mutiple process flags
259258
multiple_bit_values = ((128, 64), (4096, 1024), (8192, 1024))
260259

261260
# Maps lbproc value to the process flags that should be created
262-
multiple_map = {sum(bits) : [iris.fileformats.rules.lbproc_map[bit] for bit in bits] for bits in multiple_bit_values}
261+
multiple_map = {sum(bits) : [iris.fileformats.pp.lbproc_map[bit] for bit in bits] for bits in multiple_bit_values}
263262

264263
for lbproc, descriptions in six.iteritems(multiple_map):
265264
ll_cube = stock.lat_lon_cube()

lib/iris/tests/test_netcdf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import iris.analysis.trajectory
4242
import iris.fileformats._pyke_rules.compiled_krb.fc_rules_cf_fc as pyke_rules
4343
import iris.fileformats.netcdf
44-
import iris.fileformats.rules
4544
import iris.std_names
4645
import iris.util
4746
import iris.coord_systems as icoord_systems
@@ -911,7 +910,7 @@ def test_std_name_lookup_fail(self):
911910
class TestNetCDFUKmoProcessFlags(tests.IrisTest):
912911
def test_process_flags(self):
913912
# Test single process flags
914-
for _, process_desc in iris.fileformats.rules.LBPROC_PAIRS[1:]:
913+
for _, process_desc in iris.fileformats.pp.LBPROC_PAIRS[1:]:
915914
# Get basic cube and set process flag manually
916915
ll_cube = stock.lat_lon_cube()
917916
ll_cube.attributes["ukmo__process_flags"] = (process_desc,)
@@ -934,7 +933,7 @@ def test_process_flags(self):
934933
multiple_bit_values = ((128, 64), (4096, 1024), (8192, 1024))
935934

936935
# Maps lbproc value to the process flags that should be created
937-
multiple_map = {bits: [iris.fileformats.rules.lbproc_map[bit] for
936+
multiple_map = {bits: [iris.fileformats.pp.lbproc_map[bit] for
938937
bit in bits] for bits in multiple_bit_values}
939938

940939
for bits, descriptions in six.iteritems(multiple_map):

lib/iris/tests/test_pp_to_cube.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def test_cell_methods(self):
158158
8192: "maximum"}
159159

160160
# Make test values as list of single bit values and some multiple bit values
161-
single_bit_values = list(iris.fileformats.rules.LBPROC_PAIRS)
161+
single_bit_values = list(iris.fileformats.pp.LBPROC_PAIRS)
162162
multiple_bit_values = [(128 + 32, ""), (4096 + 2096, ""), (8192 + 1024, "")]
163163
test_values = list(single_bit_values) + multiple_bit_values
164164

@@ -192,7 +192,7 @@ def test_process_flags(self):
192192
omit_process_flags_values = (64, 128, 4096, 8192)
193193

194194
# Test single flag values
195-
for value, _ in iris.fileformats.rules.LBPROC_PAIRS:
195+
for value, _ in iris.fileformats.pp.LBPROC_PAIRS:
196196
f = next(iris.fileformats.pp.load(orig_file))
197197
f.lbproc = value # set value
198198

@@ -209,15 +209,15 @@ def test_process_flags(self):
209209
self.assertEqual(cube.attributes.get("ukmo__process_flags", None), None)
210210
else:
211211
# Check ukmo__process_flags attribute contains correct values
212-
self.assertIn(iris.fileformats.rules.lbproc_map[value], cube.attributes["ukmo__process_flags"])
212+
self.assertIn(iris.fileformats.pp.lbproc_map[value], cube.attributes["ukmo__process_flags"])
213213

214214
os.remove(temp_filename)
215215

216216
# Test multiple flag values
217217
multiple_bit_values = ((128, 32), (4096, 1024), (8192, 1024))
218218

219219
# Maps lbproc value to the process flags that should be created
220-
multiple_map = {sum(x) : [iris.fileformats.rules.lbproc_map[y] for y in x] for x in multiple_bit_values}
220+
multiple_map = {sum(x) : [iris.fileformats.pp.lbproc_map[y] for y in x] for x in multiple_bit_values}
221221

222222
for bit_values in multiple_bit_values:
223223
f = next(iris.fileformats.pp.load(orig_file))

0 commit comments

Comments
 (0)