Skip to content

Commit

Permalink
Merge pull request #49 from ecmwf/feature/fix_trajectory
Browse files Browse the repository at this point in the history
Fix path encoder
  • Loading branch information
awarde96 authored Oct 30, 2024
2 parents c024fda + a29d489 commit 8cb216e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
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

0 comments on commit 8cb216e

Please sign in to comment.