Skip to content

Commit e401a07

Browse files
authored
Some fixes to failure messages in the cc6 checks (#73)
* Some fixes to failure messages in the cc6 checks * Adding sanitization of consistency output to ensure json serializability.
1 parent f32b179 commit e401a07

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

cc_plugin_cc6/base.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from cc_plugin_cc6 import __version__
1616

1717
from ._constants import deltdic
18-
from .utils import match_pattern_or_string, to_str
18+
from .utils import match_pattern_or_string, sanitize, to_str
1919

2020
get_tseconds = lambda t: t.total_seconds() # noqa
2121
get_tseconds_vector = np.vectorize(get_tseconds)
@@ -484,17 +484,19 @@ def _write_consistency_output(self):
484484
# Write combined dictionary
485485
with open(self.consistency_output, "w") as f:
486486
json.dump(
487-
{
488-
"global_attributes": file_attrs_req,
489-
"global_attributes_non_required": file_attrs_nreq,
490-
"global_attributes_dtypes": file_attrs_dtypes,
491-
"variable_attributes": var_attrs,
492-
"variable_attributes_dtypes": var_attrs_dtypes,
493-
"variable_dtypes": var_dtypes,
494-
"dimensions": dims,
495-
"coordinates": coord_checksums,
496-
"time_info": time_info,
497-
},
487+
sanitize(
488+
{
489+
"global_attributes": file_attrs_req,
490+
"global_attributes_non_required": file_attrs_nreq,
491+
"global_attributes_dtypes": file_attrs_dtypes,
492+
"variable_attributes": var_attrs,
493+
"variable_attributes_dtypes": var_attrs_dtypes,
494+
"variable_dtypes": var_dtypes,
495+
"dimensions": dims,
496+
"coordinates": coord_checksums,
497+
"time_info": time_info,
498+
}
499+
),
498500
f,
499501
indent=4,
500502
)

cc_plugin_cc6/cc6.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,14 @@ def check_time_chunking(self, ds):
333333
# Check if the first time is equal to the expected start date
334334
if first_time != expected_start_date:
335335
messages.append(
336-
f"The first timestep conflicts with this recommendation ('{expected_start_date}'): '{first_time}'. "
337-
+ errmsg
336+
errmsg
337+
+ f" The first timestep conflicts with this recommendation ('{expected_start_date}'): '{first_time}'. "
338338
)
339339
# Check if the last time is equal to the expected end date
340340
if last_time != expected_end_date and last_time != expected_end_date_exp:
341341
messages.append(
342-
f"The last timestep conflicts with this recommendation ('{expected_end_date}'): '{last_time}'. "
343-
+ errmsg
342+
errmsg
343+
+ f" The last timestep conflicts with this recommendation ('{expected_end_date}'): '{last_time}'. "
344344
)
345345
if len(messages) == 0:
346346
score += 1
@@ -823,22 +823,19 @@ def check_lon_value_range(self, ds):
823823
else:
824824
messages.append(
825825
"The longitude coordinate should be strictly monotonically increasing."
826-
f"{increasing_0}, {increasing_1}"
827826
)
828827
elif rlon_idx == 1:
829828
if increasing_1:
830829
score += 1
831830
else:
832831
messages.append(
833832
"The longitude coordinate should be strictly monotonically increasing."
834-
f"{increasing_0}, {increasing_1}"
835833
)
836834
elif increasing_0 or increasing_1:
837835
score += 1
838836
else:
839837
messages.append(
840838
"The longitude coordinate should be strictly monotonically increasing."
841-
f"{increasing_0}, {increasing_1}"
842839
)
843840

844841
# Check if longitude coordinates are confined to the range -180 to 360

cc_plugin_cc6/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,20 @@ def to_str(val):
7373
except UnicodeDecodeError:
7474
return val
7575
return str(val)
76+
77+
78+
def sanitize(obj):
79+
"""
80+
Make sure all values are json-serializable.
81+
"""
82+
if isinstance(obj, dict):
83+
return {k: sanitize(v) for k, v in obj.items()}
84+
if isinstance(obj, list):
85+
return [sanitize(v) for v in obj]
86+
if isinstance(obj, (np.integer,)):
87+
return int(obj)
88+
if isinstance(obj, (np.floating,)):
89+
return float(obj)
90+
if isinstance(obj, (np.ndarray,)):
91+
return obj.tolist()
92+
return obj

0 commit comments

Comments
 (0)