Skip to content

Commit

Permalink
Merge pull request #37 from ecmwf/feature/update_vp
Browse files Browse the repository at this point in the history
Feature/update vp
  • Loading branch information
awarde96 authored Sep 11, 2024
2 parents 1f842f3 + 0b45ace commit d611f1f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
5 changes: 4 additions & 1 deletion covjsonkit/decoder/TimeSeries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pandas as pd
import xarray as xr

from .decoder import Decoder
Expand Down Expand Up @@ -67,7 +68,9 @@ def to_xarray(self):
y = coords[0]["axes"]["y"]["values"]
z = coords[0]["axes"]["z"]["values"]
steps = coords[0]["axes"]["t"]["values"]
steps = list(range(len(steps)))
steps = [step.replace("Z", "") for step in steps]
steps = pd.to_datetime(steps)
# steps = list(range(len(steps)))

num = []
datetime = []
Expand Down
6 changes: 3 additions & 3 deletions covjsonkit/decoder/VerticalProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_coordinates(self):
for ind, domain in enumerate(self.domains):
x = domain["axes"]["x"]["values"][0]
y = domain["axes"]["y"]["values"][0]
t = domain["axes"]["t"]["values"][0]
t = domain["axes"]["t"]["values"]
zs = domain["axes"]["z"]["values"]
num = self.mars_metadata[ind]["number"]
for param in self.parameters:
Expand Down Expand Up @@ -63,7 +63,7 @@ def to_xarray(self):
x = [coords[ind][0][0]]
y = [coords[ind][0][1]]
t = [coords[ind][0][4]]
num = [int(coord[0][3]) for coord in coords]
num = [coord[0][3] for coord in coords]
coords_z = coords[ind]
z = [int(coord[2]) for coord in coords_z]
param_coords = {
Expand All @@ -85,7 +85,7 @@ def to_xarray(self):
dataarraydict[dataarray.attrs["long_name"]] = dataarray

ds = xr.Dataset(dataarraydict)
print(ds)

for mars_metadata in self.mars_metadata[0]:
if mars_metadata != "date" and mars_metadata != "step":
ds.attrs[mars_metadata] = self.mars_metadata[0][mars_metadata]
Expand Down
62 changes: 37 additions & 25 deletions covjsonkit/encoder/VerticalProfile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from .encoder import Encoder


Expand Down Expand Up @@ -92,7 +94,7 @@ def from_polytope(self, result):
range_dict = {}
lat = 0
param = 0
# number = 0
number = 0
step = 0
long = 0
levels = 0
Expand All @@ -109,6 +111,7 @@ def from_polytope(self, result):
step,
levels,
dates,
number,
)

self.add_reference(
Expand All @@ -121,14 +124,24 @@ def from_polytope(self, result):
}
)

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

for date in range_dict.keys():
for param in range_dict[date].keys():
# self.coord_length = len(range_dict[date][param])
self.add_parameter(param)
for num in range_dict[date].keys():
for param in range_dict[date][num].keys():
# self.coord_length = len(range_dict[date][param])
self.add_parameter(param)
break
break

for date in range_dict.keys():
self.add_coverage(mars_metadata, coords[date], range_dict[date])
for num in range_dict[date].keys():
mm = mars_metadata.copy()
mm["number"] = num
# mm["Forecast date"] = date
del mm["date"]
self.add_coverage(mm, coords[date], range_dict[date][num])

# return json.loads(self.get_json())
return self.covjson
Expand All @@ -145,6 +158,7 @@ def func(
step,
levels,
dates,
number,
):
if len(tree.children) != 0:
# recurse while we are not a leaf
Expand All @@ -163,8 +177,14 @@ def func(
if c.axis.name == "param":
param = c.values
for date in dates:
for para in param:
range_dict[date][para] = []
for num in number:
for para in param:
range_dict[date][num][para] = []
if c.axis.name == "number":
number = c.values
for date in dates:
for num in number:
range_dict[date][num] = {}
if c.axis.name == "date":
dates = [str(date) + "Z" for date in c.values]
for date in dates:
Expand All @@ -174,34 +194,26 @@ def func(
if c.axis.name == "levelist":
levels = c.values

self.func(
c,
lat,
long,
coords,
mars_metadata,
param,
range_dict,
step,
levels,
dates,
)
self.func(c, lat, long, coords, mars_metadata, param, range_dict, step, levels, dates, number)
else:
tree.values = [float(val) for val in tree.values]
tree.result = [float(val) for val in tree.result]
# num_intervals = int(len(tree.result)/len(number))
# para_intervals = int(num_intervals/len(param))
len_paras = len(param) * len(levels)
len_paras = len(param)
len_levels = len(param)
len_nums = len_paras * len(levels)

for date in dates:

coords[date]["x"] = [lat]
coords[date]["y"] = [long]
coords[date]["y"] = [tree.values[0]]
coords[date]["z"] = list(levels)
coords[date]["t"] = date

for i, date in enumerate(dates):
for j, level in enumerate(list(levels)):
for k, para in enumerate(param):
range_dict[date][para].append(tree.result[i * len_paras + j * len_levels + k])
for j, num in enumerate(number):
for l, level in enumerate(list(levels)): # noqa: E741
for k, para in enumerate(param):
range_dict[date][num][para].append(
tree.result[i * len_paras + l * len_levels + j * len_nums + k]
)
2 changes: 2 additions & 0 deletions covjsonkit/encoder/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(self, type, domaintype):
self.domaintype = DomainType.multi_point
elif domaintype == "frame":
self.domaintype = DomainType.multi_point
elif domaintype == "verticalprofile":
self.domaintype = DomainType.vertical_profile
elif domaintype == "path":
self.domaintype = "Trajectory"

Expand Down
16 changes: 8 additions & 8 deletions tests/test_decoder_vertical_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,22 @@ def test_verticalprofile_coordinates(self):
coordinates = {
"t": [
[
["0.0", "0.0", "500", "0", "2017-01-01 12:00:00"],
["0.0", "0.0", "850", "0", "2017-01-01 12:00:00"],
["0.0", "0.0", "500", "0", ["2017-01-01 12:00:00"]],
["0.0", "0.0", "850", "0", ["2017-01-01 12:00:00"]],
],
[
["0.0", "0.0", "500", "1", "2017-01-01 12:00:00"],
["0.0", "0.0", "850", "1", "2017-01-01 12:00:00"],
["0.0", "0.0", "500", "1", ["2017-01-01 12:00:00"]],
["0.0", "0.0", "850", "1", ["2017-01-01 12:00:00"]],
],
],
"p": [
[
["0.0", "0.0", "500", "0", "2017-01-01 12:00:00"],
["0.0", "0.0", "850", "0", "2017-01-01 12:00:00"],
["0.0", "0.0", "500", "0", ["2017-01-01 12:00:00"]],
["0.0", "0.0", "850", "0", ["2017-01-01 12:00:00"]],
],
[
["0.0", "0.0", "500", "1", "2017-01-01 12:00:00"],
["0.0", "0.0", "850", "1", "2017-01-01 12:00:00"],
["0.0", "0.0", "500", "1", ["2017-01-01 12:00:00"]],
["0.0", "0.0", "850", "1", ["2017-01-01 12:00:00"]],
],
],
}
Expand Down

0 comments on commit d611f1f

Please sign in to comment.