Skip to content
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

Fix path encoder #49

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions covjsonkit/encoder/Path.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,17 @@ def from_polytope(self, result):
fields["step"] = 0
fields["dates"] = []
fields["levels"] = [0]
fields["s"] = []
fields["l"] = []

self.walk_tree(result, fields, coords, mars_metadata, range_dict)

if len(fields["l"]) == 0:
fields["l"] = [0]

logging.debug("The values returned from walking tree: %s", range_dict) # noqa: E501
logging.debug("The coordinates returned from walking tree: %s", coords) # noqa: E501
logging.debug("The fields: %s", fields)

self.add_reference(
{
Expand All @@ -104,27 +110,35 @@ def from_polytope(self, result):
)

combined_dict = {}
for k in range_dict.keys():
print(k, range_dict[k])

for date in fields["dates"]:
if date not in combined_dict:
combined_dict[date] = {}
for level in fields["levels"]:
for level in fields["l"]:
for num in fields["number"]:
if num not in combined_dict[date]:
combined_dict[date][num] = {}
for para in fields["param"]:
if para not in combined_dict[date][num]:
combined_dict[date][num][para] = {}
# for s, value in range_dict[date][level][num][para].items():
for s in fields["step"]:
for s in fields["s"]:
key = (date, level, num, para, s)
# for k, v in range_dict.items():
# if k == key:
if s not in combined_dict[date][num][para]:
combined_dict[date][num][para][s] = range_dict[key]
if key in range_dict:
combined_dict[date][num][para][s] = range_dict[key]
# combined_dict[date][num][para][s] = range_dict[key]
else:
# Cocatenate arrays
combined_dict[date][num][para][s] += range_dict[key]
if key in range_dict:
combined_dict[date][num][para][s] += range_dict[key]
# for s in fields["s"]:

logging.debug("The values returned from combined dicts: %s", combined_dict) # noqa: E501

levels = fields["levels"]
if fields["param"] == 0:
Expand All @@ -138,27 +152,39 @@ def from_polytope(self, result):
coord = coords[date]["composite"]
coords[date]["composite"] = []
for level in levels:
for cor in coord:
coords[date]["composite"].append([cor[0], cor[1], level])
start = 0
for i, s in enumerate(fields["s"]):
end = start + len(coord) / len(fields["s"])
for cor in coord[int(start) : int(end)]:
if len(fields["l"]) == 1:
coords[date]["composite"].append([s, cor[0], cor[1], fields["l"][0]])
elif len(fields["l"]) == 0:
coords[date]["composite"].append([s, cor[0], cor[1], level])
else:
coords[date]["composite"].append([s, cor[0], cor[1], fields["l"][i]])
start = end
logging.debug("The coordinates returned from walking tree: %s", coords) # noqa: E501

for date in combined_dict.keys():
for num in combined_dict[date].keys():
val_dict = {}
for step in combined_dict[date][num][self.parameters[0]].keys():
val_dict[step] = {}
# for step in combined_dict[date][num][self.parameters[0]].keys():
# val_dict[step] = {}
for para in combined_dict[date][num].keys():
if para not in val_dict:
val_dict[para] = []
for step in combined_dict[date][num][para].keys():
val_dict[step][para] = combined_dict[date][num][para][step]
for step in val_dict.keys():
mm = mars_metadata.copy()
mm["number"] = num
mm["step"] = step
temp = []
for coord in coords[date]["composite"]:
temp.append([step] + coord)
coords[date]["composite"] = temp
mm["Forecast date"] = date
self.add_coverage(mm, coords[date], val_dict[step])
val_dict[para].extend(combined_dict[date][num][para][step])
# for step in val_dict.keys():
mm = mars_metadata.copy()
mm["number"] = num
# mm["step"] = step
# temp = []
# for coord in coords[date]["composite"]:
# temp.append([step] + coord)
# coords[date]["composite"] = temp
mm["Forecast date"] = date
self.add_coverage(mm, coords[date], val_dict)

# self.add_coverage(mars_metadata, coords, range_dict)
# return self.covjson
Expand Down
5 changes: 4 additions & 1 deletion covjsonkit/encoder/VerticalProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,13 @@ def func(
tree.values = [float(val) for val in tree.values]
tree.result = [float(val) for val in tree.result]
# para_intervals = int(num_intervals/len(param))
try:
len(param)
except TypeError:
raise ValueError("No parameters were returned, date requested may be out of range")
len_paras = len(param)
len_levels = len(param)
len_nums = len_paras * len(levels)

for date in dates:

coords[date]["x"] = [lat]
Expand Down
4 changes: 4 additions & 0 deletions covjsonkit/encoder/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def append_composite_coords(dates, tree_values, lat, coords):
fields["lat"] = result
elif child.axis.name == "levelist":
fields["levels"] = result
if "l" in fields:
fields["l"].extend(result)
elif child.axis.name == "param":
fields["param"] = result
elif child.axis.name in ["date", "time"]:
Expand All @@ -150,6 +152,8 @@ def append_composite_coords(dates, tree_values, lat, coords):
fields["number"] = result
elif child.axis.name == "step":
fields["step"] = result
if "s" in fields:
fields["s"].extend(result)

self.walk_tree(child, fields, coords, mars_metadata, range_dict)
else:
Expand Down
Loading