Skip to content

Commit 6f94c16

Browse files
#680: Enhance extract logging (add frequency values) (#765)
* #680: Add frequency value. * #680: Remove a file committed by mistake. * Revert "#680: Add frequency value." This reverts commit 6f7ade9. * #680: Attempt to correct. * #680: reapply newlines. * #680: Fix a broken test. * #680: Remove empty dicts from output {}. * #680: Add stash codes to output again. * #680: Reimplement stash codes in extract logs. * #680: Add logging for constraint without stash. Revert a change. * #680: remove a comment. --------- Co-authored-by: Lauren Boon <[email protected]>
1 parent 0571421 commit 6f94c16

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

cdds/cdds/extract/common.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def configure_mappings(mappings):
834834
else:
835835
mip_table = " - "
836836
msg += " {:<15} {:<15}\n".format(
837-
var["var"], "[ {} ]".format(mip_table))
837+
var["var"], "[ {},@{} ]".format(mip_table, var["frequency"]))
838838

839839
if len(msg) > 0:
840840
logger.info("\n ----- Embargoed Variable Mappings -----\n{} \n".format(msg))
@@ -1028,13 +1028,20 @@ def condense_constraints(variable_constraints) -> dict:
10281028
A defaultdict where the keys are hashed representations of constraints and the
10291029
values are sets of 'stash' values corresponding to those constraints.
10301030
"""
1031+
logger = logging.getLogger(__name__)
10311032
condensed_constraints = defaultdict(set)
10321033

10331034
for variable in variable_constraints:
10341035
for constraint in variable["constraint"]:
1035-
if "stash" in constraint:
1036-
stash = constraint.pop("stash")
1037-
hashed_constraint = json.dumps(constraint, sort_keys=True)
1038-
condensed_constraints[hashed_constraint].add(stash)
1036+
if "stash" not in constraint:
1037+
logger.critical("Constraint without stash found: {}".format(constraint))
1038+
continue
1039+
stash = constraint["stash"]
1040+
# Create a copy and remove stash for grouping whilst avoiding mutating the
1041+
# original (needed to ensure stash codes appear in logs).
1042+
constraint_without_stash = constraint.copy()
1043+
constraint_without_stash.pop("stash")
1044+
hashed_constraint = json.dumps(constraint_without_stash, sort_keys=True)
1045+
condensed_constraints[hashed_constraint].add(stash)
10391046

10401047
return condensed_constraints

cdds/cdds/extract/filters.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ def set_mappings(self, request):
130130
else:
131131
self.mappings[key].append(var)
132132
if var["status"] == "embargoed":
133+
# Look up frequency from original var_list
134+
frequency = None
135+
for original_var in self.var_list["variables"]:
136+
if (original_var["name"] == var["name"] and
137+
original_var["table"] == var["table"]):
138+
frequency = original_var["frequency"]
139+
break
133140
self.mappings_embargoed[key].append(
134141
{
135142
"table": var["table"],
136-
"var": var["name"]
143+
"var": var["name"],
144+
"frequency": frequency
137145
}
138146
)
139147

cdds/cdds/extract/variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def create_variables_list(self, file_content):
7070
{
7171
"name": str(var['label']),
7272
"table": str(var['miptable']),
73-
"stream": str(var["stream"])
73+
"stream": str(var["stream"]),
74+
"frequency": str(var["frequency"])
7475
})
7576
else:
7677
reason_str = "not active "

cdds/cdds/tests/test_extract/test_filters.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
class TestFilters(unittest.TestCase):
2222

2323
VAR_LIST = [
24-
{"table": "Amon", "name": "tas"},
25-
{"table": "Amon", "name": "pr"},
26-
{"table": "Amon", "name": "uo"}
24+
{"table": "Amon", "name": "tas", "frequency": "mon"},
25+
{"table": "Amon", "name": "pr", "frequency": "mon"},
26+
{"table": "Amon", "name": "uo", "frequency": "mon"}
2727
]
2828

2929
VAR_LIST_OCEAN = [
30-
{"table": "Omon", "name": "tos"},
30+
{"table": "Omon", "name": "tos", "frequency": "mon"},
3131
]
3232

3333
def setUp(self):
@@ -262,7 +262,7 @@ def test_embargo_response(self, mock_mass_filters):
262262
mock_mass_filters.return_value = model_to_mip_response
263263
filters.set_mappings(self.request)
264264

265-
self.assertDictEqual({"ap5": [{"var": "pr", "table": "Amon"}]},
265+
self.assertDictEqual({"ap5": [{"var": "pr", "table": "Amon", "frequency": "mon"}]},
266266
filters.get_embargoed_mappings())
267267
# The expected behaviour of filters.set_mappings is to add all
268268
# entries from the model_to_mip_response which have status "ok" or

0 commit comments

Comments
 (0)