-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CellMethods now printed in CF-compliant manner #4279
Changes from all commits
4fa484f
977a0f2
230e7e9
d3a648f
1faed11
add1eec
50e74b7
ccb526a
a964e71
21f7507
03f702e
ec9429e
10c31d8
7b19fd2
b0966e6
de1ff64
f93a490
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -257,9 +257,12 @@ def add_scalar_row(name, value=""): | |||||||||
add_scalar_row(item.name, item.content) | ||||||||||
if item.extra: | ||||||||||
add_scalar_row(item_to_extra_indent + item.extra) | ||||||||||
elif "attribute" in title or "cell method" in title: | ||||||||||
elif "attribute" in title: | ||||||||||
for title, value in zip(sect.names, sect.values): | ||||||||||
add_scalar_row(title, value) | ||||||||||
elif "cell method" in title: | ||||||||||
for title, value in zip(sect.names, sect.values): | ||||||||||
add_scalar_row(", ".join(title), value) | ||||||||||
Comment on lines
+264
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Specialised behaviour will benefit from specialised language to help understanding of what's going on. |
||||||||||
elif "scalar cell measure" in title: | ||||||||||
# These are just strings: nothing in the 'value' column. | ||||||||||
for name in sect.contents: | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,12 +222,12 @@ def __init__(self, title, cell_methods): | |
self.values = [] | ||
self.contents = [] | ||
for method in cell_methods: | ||
name = method.method | ||
# Remove "method: " from the front of the string, leaving the value. | ||
value = str(method)[len(name + ": ") :] | ||
self.names.append(name) | ||
content = str(method) | ||
names = content.split(": ")[:-1] | ||
# Remove "coord: " or "coord1: coord2: " from the front of the string, leaving the value. | ||
value = content.split(": ")[-1] | ||
Comment on lines
+226
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems risky to assume that names = method.coord_names
value = method.method_str() |
||
self.names.append(names) | ||
self.values.append(value) | ||
content = "{}: {}".format(name, value) | ||
self.contents.append(content) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2816,25 +2816,24 @@ def __init__(self, method, coords=None, intervals=None, comments=None): | |||||
self._init(method, tuple(_coords), tuple(_intervals), tuple(_comments)) | ||||||
|
||||||
def __str__(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in my Then just have |
||||||
"""Return a custom string representation of CellMethod""" | ||||||
"""Return a custom string representation of CellMethod in CF-format""" | ||||||
# Group related coord names intervals and comments together | ||||||
cell_components = zip_longest( | ||||||
self.coord_names, self.intervals, self.comments, fillvalue="" | ||||||
) | ||||||
|
||||||
collection_summaries = [] | ||||||
cm_summary = "%s: " % self.method | ||||||
coords = [] | ||||||
other_infos = [] | ||||||
|
||||||
for coord_name, interval, comment in cell_components: | ||||||
coords.append(coord_name) | ||||||
other_info = ", ".join(filter(None, chain((interval, comment)))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at one of the examples, this currently generates:
If we're aligning strictly to the conventions, should we actually be aiming for the following?
|
||||||
if other_info: | ||||||
coord_summary = "%s (%s)" % (coord_name, other_info) | ||||||
else: | ||||||
coord_summary = "%s" % coord_name | ||||||
|
||||||
collection_summaries.append(coord_summary) | ||||||
other_infos.append(other_info) | ||||||
|
||||||
return cm_summary + ", ".join(collection_summaries) | ||||||
result = f"{''.join([f'{c}: ' for c in coords])}{self.method}" | ||||||
if any(other_infos): | ||||||
result += f" ({', '.join(other_infos)})" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If we're aligning strictly to the conventions, should we actually be separating with a space only? |
||||||
return result | ||||||
|
||||||
def __add__(self, other): | ||||||
# Disable the default tuple behaviour of tuple concatenation | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -750,9 +750,9 @@ class Cube(CFVariableMixin): | |||||
time \ | ||||||
1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00) | ||||||
Cell methods: | ||||||
mean within years time | ||||||
mean over years time | ||||||
Attributes: | ||||||
time mean within years | ||||||
time mean over years | ||||||
Attributes: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
STASH m01s16i203 | ||||||
source Data from Met Office Unified Model | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.