Skip to content

Commit

Permalink
amend_attributes can now add to an existing value, and add timestamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
MoseleyS committed Dec 13, 2023
1 parent f77f534 commit 6ce2c02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 16 additions & 0 deletions improver/metadata/amend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Module containing utilities for modifying cube metadata"""
import re
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Tuple, Union

Expand Down Expand Up @@ -76,10 +77,25 @@ def amend_attributes(cube: Cube, attributes_dict: Dict[str, Any]) -> None:
Dictionary containing items of the form {attribute_name: value}.
The "value" item is either the string "remove" or the new value
of the attribute required.
If the new value contains "{}", the existing value will be
inserted at this point (no existing value will result in the
"{}" being removed, then applied as the attribute value).
If the new value contains "{now:.*}", where the .* is a valid
date format, then this string is replaced with the current
wall-clock time, formatted as specified.
"""
for attribute_name, value in attributes_dict.items():
re_now = r"({now:.*})"
has_now = re.match(rf".*{re_now}.*", value, re.DOTALL)
if has_now:
now = has_now[1].format(now=datetime.now())
value = re.sub(re_now, now, value)
if value == "remove":
cube.attributes.pop(attribute_name, None)
elif "{}" in value:
cube.attributes[attribute_name] = value.format(
cube.attributes.get(attribute_name, "")
)
else:
cube.attributes[attribute_name] = value

Expand Down
12 changes: 9 additions & 3 deletions improver_tests/metadata/test_amend.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Tests for the improver.metadata.amend module"""

import re
import unittest
from datetime import datetime as dt

Expand Down Expand Up @@ -128,22 +128,28 @@ def setUp(self):
attributes={
"mosg__grid_version": "1.3.0",
"mosg__model_configuration": "uk_det",
"history": "20231213T09:04:23Z: StaGE Decoupler",
},
)
self.metadata_dict = {
"mosg__grid_version": "remove",
"source": "IMPROVER unit tests",
"mosg__model_configuration": "other_model",
"history": "{}\n{now:%Y%m%dT%H:%M:%SZ}: IMPROVER",
}

def test_basic(self):
"""Test function adds, removes and modifies attributes as expected"""
"""Test function adds, removes, updates and modifies attributes as expected,
including the presence of a formatted now string."""
expected_attributes = {
"source": "IMPROVER unit tests",
"mosg__model_configuration": "other_model",
"history": "20231213T09:04:23Z: StaGE Decoupler\n.*Z: IMPROVER",
}
amend_attributes(self.cube, self.metadata_dict)
self.assertDictEqual(self.cube.attributes, expected_attributes)
self.assertTrue(self.cube.attributes.keys() == expected_attributes.keys())
for k, v in expected_attributes.items():
self.assertTrue(re.match(v, self.cube.attributes[k]))


class Test_set_history_attribute(IrisTest):
Expand Down

0 comments on commit 6ce2c02

Please sign in to comment.