Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions lib/iris/fileformats/pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,21 +1907,6 @@ def _field_gen(filename, read_data_bytes, little_ended=False):
yield pp_field


def _ensure_save_rules_loaded():
"""Makes sure the standard save rules are loaded."""

# Uses these module-level variables
global _save_rules

if _save_rules is None:
# Load the pp save rules
rules_filename = os.path.join(iris.config.CONFIG_PATH,
'pp_save_rules.txt')
with iris.fileformats.rules._disable_deprecation_warnings():
_save_rules = iris.fileformats.rules.RulesContainer(
rules_filename, iris.fileformats.rules.ProcedureRule)


# Stash codes not to be filtered (reference altitude and pressure fields).
_STASH_ALLOW = [STASH(1, 0, 33), STASH(1, 0, 1)]

Expand Down Expand Up @@ -2223,8 +2208,6 @@ def save_pairs_from_cube(cube, field_coords=None, target=None):
# On the flip side, record which Cube metadata has been "used" and flag up
# unused?

_ensure_save_rules_loaded()

n_dims = len(cube.shape)
if n_dims < 2:
raise ValueError('Unable to save a cube of fewer than 2 dimensions.')
Expand Down
90 changes: 0 additions & 90 deletions lib/iris/fileformats/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,96 +549,6 @@ def conditional_warning(self, condition, warning):
warnings.warn(warning)


class RulesContainer(object):
"""
A collection of :class:`Rule` instances, with the ability to read rule
definitions from files and run the rules against given fields.

.. deprecated:: 1.10

"""
def __init__(self, filepath=None, rule_type=FunctionRule):
"""Create a new rule set, optionally adding rules from the specified file.

The rule_type defaults to :class:`FunctionRule`,
e.g for CM loading actions that return objects, such as *AuxCoord(...)*

rule_type can also be set to :class:`ProcedureRule`
e.g for PP saving actions that do not return anything, such as *pp.lbuser[3] = 16203*
"""
if _enable_rules_deprecations:
warn_deprecated(
"the `iris.fileformats.rules.RulesContainer class is deprecated.")
self._rules = []
self.rule_type = rule_type
if filepath is not None:
self.import_rules(filepath)

def import_rules(self, filepath):
"""Extend the rule collection with the rules defined in the specified file."""
# Define state constants
IN_CONDITION = 1
IN_ACTION = 2

rule_file = os.path.expanduser(filepath)
conditions = []
actions = []
state = None

with open(rule_file, 'r') as file:
for line in file:
line = line.rstrip()
if line == "IF":
if conditions and actions:
self._rules.append(self.rule_type(conditions, actions))
conditions = []
actions = []
state = IN_CONDITION
elif line == "THEN":
state = IN_ACTION
elif len(line) == 0:
pass
elif line.strip().startswith('#'):
pass
elif state == IN_CONDITION:
conditions.append(line)
elif state == IN_ACTION:
actions.append(line)
else:
raise Exception('Rule file not read correctly at line: ' +
line)
if conditions and actions:
self._rules.append(self.rule_type(conditions, actions))

def verify(self, cube, field):
"""
Add to the given :class:`iris.cube.Cube` by running this set of
rules with the given field.

Args:

* cube:
An instance of :class:`iris.cube.Cube`.
* field:
A field object relevant to the rule set.

Returns: (cube, matching_rules)

* cube - the resultant cube
* matching_rules - a list of rules which matched

"""
matching_rules = []
factories = []
for rule in self._rules:
if rule.evaluates_true(cube, field):
matching_rules.append(rule)
rule_factories = rule.run_actions(cube, field)
if rule_factories:
factories.extend(rule_factories)
return RuleResult(cube, matching_rules, factories)


def scalar_coord(cube, coord_name):
"""Try to find a single-valued coord with the given name."""
found_coord = None
Expand Down
9 changes: 0 additions & 9 deletions lib/iris/tests/integration/test_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def test_soil_level_round_trip(self):
field.lbvc = 0
field.brsvd = [None] * 4
field.brlev = None
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)

# Check the vertical coordinate is as originally specified.
Expand Down Expand Up @@ -111,7 +110,6 @@ def test_soil_depth_round_trip(self):
field.lbvc = 0
field.brlev = None
field.brsvd = [None] * 4
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)

# Check the vertical coordinate is as originally specified.
Expand Down Expand Up @@ -144,7 +142,6 @@ def test_potential_temperature_level_round_trip(self):
field = iris.fileformats.pp.PPField3()
field.lbfc = 0
field.lbvc = 0
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)

# Check the vertical coordinate is as originally specified.
Expand Down Expand Up @@ -214,7 +211,6 @@ def field_with_data(scale=1):
pressure_field.lbvc = 0
pressure_field.brsvd = [None, None]
pressure_field.lbuser = [None] * 7
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(pressure_cube, pressure_field)

data_field = iris.fileformats.pp.PPField3()
Expand Down Expand Up @@ -306,7 +302,6 @@ def test_hybrid_height_with_non_standard_coords(self):
field.lbvc = 0
field.brsvd = [None, None]
field.lbuser = [None] * 7
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)

self.assertEqual(field.blev, delta)
Expand Down Expand Up @@ -343,7 +338,6 @@ def test_hybrid_pressure_with_non_standard_coords(self):
field.lbvc = 0
field.brsvd = [None, None]
field.lbuser = [None] * 7
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)

self.assertEqual(field.bhlev, delta)
Expand Down Expand Up @@ -409,7 +403,6 @@ def field_with_data(scale=1):
data_field.lbvc = 0
data_field.brsvd = [None, None]
data_field.lbuser = [None] * 7
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(data_cube, data_field)

# Check the data field has the vertical coordinate as originally
Expand Down Expand Up @@ -446,7 +439,6 @@ def convert_cube_to_field(self, cube):
field.lbfc = 0
field.lbvc = 0
field.lbtim = 0
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)
return field

Expand Down Expand Up @@ -633,7 +625,6 @@ def create_cube(self, longitude_coord='longitude'):
def convert_cube_to_field(self, cube):
field = iris.fileformats.pp.PPField3()
field.lbvc = 0
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, field)
return field

Expand Down
1 change: 0 additions & 1 deletion lib/iris/tests/test_cube_to_pp.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def test_365_calendar_export(self):
# Add an extra "fill_value" property, as used by the save rules.
cube.fill_value = None
pp_field = mock.MagicMock(spec=PPField3)
iris.fileformats.pp._ensure_save_rules_loaded()
iris.fileformats.pp._save_rules.verify(cube, pp_field)
self.assertEqual(pp_field.lbtim.ic, 4)

Expand Down