Skip to content

Commit 831bbde

Browse files
authored
compare.py: add support for filtering fields (#250)
Small PR to add the option -f to be able to filter the fields one wants to check with giving a substr as argument. Only fields including the substrings are then checked and not all of them. Allows to also compare one field or some types of fields quickly which speeds up the workflow quite a lot (for me at least).
1 parent fed768f commit 831bbde

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Carlos Osuna (cosunae), MeteoSwiss
1414
Willem Deconinck (wdeconinck), ECMWF
1515
Yannick Boetzel (yboetz), MeteoSwiss
1616
Rhea George (rheacangeo), Vulcan Inc.
17+
Elsa Germann (elsagermann), ETH Zurich (C2SM)

src/serialbox-python/compare/compare.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def compare_fields(serializers, field, savepoint, dim_bounds):
307307
return False
308308

309309

310-
def compare(serializers, field_to_check, dim_bounds):
310+
def compare(serializers, field_to_check, dim_bounds, substr):
311311
""" Compare the data and info at every savepoint of field_1 to field_2 and returns
312312
True on success
313313
"""
@@ -350,6 +350,10 @@ def get_time(start):
350350
else:
351351
fields_to_check = [field_to_check] if field_to_check in fields_at_savepoint else []
352352

353+
# Only check fields with name including substr if argument substr given
354+
if substr != '':
355+
fields_to_check = [field for field in fields_to_check if not field.find(substr)]
356+
353357
savepoint_start_time = time()
354358
if fields_to_check:
355359
report(10 * "-", "Savepoint {}".format(savepoint))
@@ -449,6 +453,9 @@ def main(arguments=None):
449453
parser.add_argument("-q", "--info-only", dest="field_info_only", action="store_true",
450454
help="only compare field meta-info (no data comparison) "
451455
"(default: {})".format(get_config().FIELD_INFO_ONLY))
456+
parser.add_argument("-f", "--filter-string", dest="substr", type=str,
457+
default='',
458+
help="only compare field names with SUBSTR in it")
452459
for dim in ["i", "j", "k", "l"]:
453460
parser.add_argument("-{}".format(dim), dest="{}".format(dim), metavar="START[:END]",
454461
help="only compare the {} dimension 'START' or if 'END' is supplied "
@@ -492,7 +499,7 @@ def main(arguments=None):
492499
# Perform comparison
493500
ret = 1
494501
try:
495-
ret = compare([serializer_1, serializer_2], field_1, dim_bounds)
502+
ret = compare([serializer_1, serializer_2], field_1, dim_bounds, args.substr)
496503
except ser.SerialboxError as e:
497504
fatal_error(e)
498505
return ret

0 commit comments

Comments
 (0)