diff --git a/lib/iris/fileformats/_pp_lbproc_pairs.py b/lib/iris/fileformats/_pp_lbproc_pairs.py new file mode 100644 index 0000000000..28e35a8e86 --- /dev/null +++ b/lib/iris/fileformats/_pp_lbproc_pairs.py @@ -0,0 +1,48 @@ +# (C) British Crown Copyright 2017, Met Office +# +# This file is part of Iris. +# +# Iris is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Iris is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Iris. If not, see . + +from __future__ import (absolute_import, division, print_function) +from six.moves import (filter, input, map, range, zip) # noqa +import six + +import itertools + + +# LBPROC codes and their English equivalents +LBPROC_PAIRS = ((1, "Difference from another experiment"), + (2, "Difference from zonal (or other spatial) mean"), + (4, "Difference from time mean"), + (8, "X-derivative (d/dx)"), + (16, "Y-derivative (d/dy)"), + (32, "Time derivative (d/dt)"), + (64, "Zonal mean field"), + (128, "Time mean field"), + (256, "Product of two fields"), + (512, "Square root of a field"), + (1024, "Difference between fields at levels BLEV and BRLEV"), + (2048, "Mean over layer between levels BLEV and BRLEV"), + (4096, "Minimum value of field during time period"), + (8192, "Maximum value of field during time period"), + (16384, "Magnitude of a vector, not specifically wind speed"), + (32768, "Log10 of a field"), + (65536, "Variance of a field"), + (131072, "Mean over an ensemble of parallel runs")) + +# lbproc_map is dict mapping lbproc->English and English->lbproc +# essentially a one to one mapping +LBPROC_MAP = {x: y for x, y in + itertools.chain(LBPROC_PAIRS, ((y, x) for x, y in LBPROC_PAIRS))} diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index ad5fab1d66..f9f8069a32 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -26,7 +26,6 @@ import abc import collections from copy import deepcopy -import itertools import operator import os import re @@ -43,6 +42,11 @@ import iris.config import iris.fileformats.pp_rules from iris.fileformats.pp_save_rules import verify + +# NOTE: this is for backwards-compatitibility *ONLY* +# We could simply remove it for v2.0 ? +from iris.fileformats._pp_lbproc_pairs import (LBPROC_PAIRS, + LBPROC_MAP as lbproc_map) import iris.fileformats.rules import iris.coord_systems @@ -222,31 +226,6 @@ 'default': np.dtype('>f4'), } -# LBPROC codes and their English equivalents -LBPROC_PAIRS = ((1, "Difference from another experiment"), - (2, "Difference from zonal (or other spatial) mean"), - (4, "Difference from time mean"), - (8, "X-derivative (d/dx)"), - (16, "Y-derivative (d/dy)"), - (32, "Time derivative (d/dt)"), - (64, "Zonal mean field"), - (128, "Time mean field"), - (256, "Product of two fields"), - (512, "Square root of a field"), - (1024, "Difference between fields at levels BLEV and BRLEV"), - (2048, "Mean over layer between levels BLEV and BRLEV"), - (4096, "Minimum value of field during time period"), - (8192, "Maximum value of field during time period"), - (16384, "Magnitude of a vector, not specifically wind speed"), - (32768, "Log10 of a field"), - (65536, "Variance of a field"), - (131072, "Mean over an ensemble of parallel runs")) - -# lbproc_map is dict mapping lbproc->English and English->lbproc -# essentially a one to one mapping -lbproc_map = {x: y for x, y in - itertools.chain(LBPROC_PAIRS, ((y, x) for x, y in LBPROC_PAIRS))} - class STASH(collections.namedtuple('STASH', 'model section item')): """ diff --git a/lib/iris/fileformats/pp_rules.py b/lib/iris/fileformats/pp_rules.py index a86c5f358e..f709ac30c9 100644 --- a/lib/iris/fileformats/pp_rules.py +++ b/lib/iris/fileformats/pp_rules.py @@ -30,9 +30,10 @@ from iris.coords import AuxCoord, CellMethod, DimCoord from iris.fileformats.rules import (ConversionMetadata, Factory, Reference, ReferenceTarget) +import iris.fileformats.pp +from iris.fileformats._pp_lbproc_pairs import LBPROC_MAP from iris.fileformats.um_cf_map import (LBFC_TO_CF, STASH_TO_CF, STASHCODE_IMPLIED_HEIGHTS) -import iris.fileformats.pp ############################################################################### @@ -1027,7 +1028,7 @@ def _all_other_rules(f): if unhandled_lbproc: attributes["ukmo__process_flags"] = tuple(sorted( [name - for value, name in six.iteritems(iris.fileformats.pp.lbproc_map) + for value, name in six.iteritems(LBPROC_MAP) if isinstance(value, int) and f.lbproc & value])) if (f.lbsrce % 10000) == 1111: diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index 8457cf0191..2d11b757d8 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -24,8 +24,8 @@ import iris from iris.fileformats._ff_cross_references import STASH_TRANS from iris.aux_factory import HybridHeightFactory, HybridPressureFactory -from iris.fileformats.pp import lbproc_map from iris.fileformats.um_cf_map import CF_TO_LBFC +from iris.fileformats._pp_lbproc_pairs import LBPROC_MAP from iris.fileformats.rules import (aux_factory, has_aux_factory, scalar_cell_method, @@ -568,7 +568,7 @@ def _lbproc_rules(cube, pp): pp.lbproc = 0 if cube.attributes.get("ukmo__process_flags", None): - pp.lbproc += sum([lbproc_map[name] + pp.lbproc += sum([LBPROC_MAP[name] for name in cube.attributes["ukmo__process_flags"]]) # Zonal-mean: look for a CellMethod which is a "mean" over "longitude" or diff --git a/lib/iris/fileformats/rules.py b/lib/iris/fileformats/rules.py index 5b2dbc7603..d156fa8b9e 100644 --- a/lib/iris/fileformats/rules.py +++ b/lib/iris/fileformats/rules.py @@ -911,4 +911,3 @@ def loadcubes_user_callback_wrapper(cube, field, filename): converter=loader.converter, user_callback_wrapper=loadcubes_user_callback_wrapper): yield cube -