Skip to content

Commit

Permalink
Merge pull request #27 from ecmwf/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
awarde96 authored Aug 13, 2024
2 parents f07a035 + 86ba62e commit 00e5bd6
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 340 deletions.
51 changes: 33 additions & 18 deletions covjsonkit/decoder/BoundingBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,62 @@ def to_geopandas(self):
pass

def to_xarray(self):
dims = ["number", "steps", "points"]
dims = ["datetimes", "number", "steps", "points"]
dataarraydict = {}

# Get coordinates
x = []
y = []
datetimes = []
for coord in self.get_coordinates()["composite"]["values"]:
x.append(float(coord[0]))
y.append(float(coord[1]))

"""
# Get values
for parameter in self.parameters:
dataarray = xr.DataArray(self.get_values()[parameter][0], dims=dims)
dataarray.attrs["type"] = self.get_parameter_metadata(parameter)["type"]
dataarray.attrs["units"] = self.get_parameter_metadata(parameter)["unit"]["symbol"]
dataarray.attrs["long_name"] = self.get_parameter_metadata(parameter)["observedProperty"]["id"]
dataarraydict[dataarray.attrs["long_name"]] = dataarray
"""
for datetime in self.get_coordinates()["t"]["values"]:
datetimes.append(datetime)

values = {}
for parameter in self.parameters:
values[parameter] = []
values[parameter] = {}

datetimes = []
numbers = []
steps = []
for coverage in self.coverages:
if "number" not in coverage["mars:metadata"]:
coverage["mars:metadata"]["number"] = 0
numbers.append(coverage["mars:metadata"]["number"])
if "step" not in coverage["mars:metadata"]:
coverage["mars:metadata"]["step"] = 0
steps.append(coverage["mars:metadata"]["step"])
datetimes.append(coverage["domain"]["axes"]["t"]["values"][0])
for parameter in self.parameters:
values[parameter].append(coverage["ranges"][parameter]["values"])

# values[parameter].append(coverage["ranges"][parameter]["values"])
if coverage["domain"]["axes"]["t"]["values"][0] not in values[parameter]:
values[parameter][coverage["domain"]["axes"]["t"]["values"][0]] = {}
if (
coverage["mars:metadata"]["number"]
not in values[parameter][coverage["domain"]["axes"]["t"]["values"][0]]
):
values[parameter][coverage["domain"]["axes"]["t"]["values"][0]][
coverage["mars:metadata"]["number"]
] = {}
values[parameter][coverage["domain"]["axes"]["t"]["values"][0]][coverage["mars:metadata"]["number"]][
coverage["mars:metadata"]["step"]
] = coverage["ranges"][parameter]["values"]

datetimes = list(set(datetimes))
numbers = list(set(numbers))
steps = list(set(steps))

new_values = {}
for parameter in self.parameters:
for parameter in values.keys():
new_values[parameter] = []
for i, num in enumerate(numbers):
for i, datetime in enumerate(datetimes):
new_values[parameter].append([])
for j, step in enumerate(steps):
new_values[parameter][i].append(values[parameter][i * len(steps) + j])
for j, number in enumerate(numbers):
new_values[parameter][i].append([])
for k, step in enumerate(steps):
new_values[parameter][i][j].append(values[parameter][datetime][number][step])

for parameter in self.parameters:
dataarray = xr.DataArray(new_values[parameter], dims=dims)
Expand All @@ -92,6 +106,7 @@ def to_xarray(self):
ds = xr.Dataset(
dataarraydict,
coords=dict(
datetimes=(["datetimes"], datetimes),
number=(["number"], numbers),
steps=(["steps"], steps),
points=(["points"], list(range(0, len(x)))),
Expand Down
60 changes: 56 additions & 4 deletions covjsonkit/decoder/Path.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def to_geopandas(self):
pass

def to_xarray(self):
dims = ["points"]
dims = ["datetimes", "number", "steps", "points"]
dataarraydict = {}

# Get coordinates
Expand All @@ -48,9 +48,52 @@ def to_xarray(self):
y.append(float(coord[2]))
t.append(coord[0])

# Get values
values = {}
for parameter in self.parameters:
dataarray = xr.DataArray(self.get_values()[parameter][0], dims=dims)
values[parameter] = {}

datetimes = []
numbers = []
steps = []
for coverage in self.coverages:
if "number" not in coverage["mars:metadata"]:
coverage["mars:metadata"]["number"] = 0
numbers.append(coverage["mars:metadata"]["number"])
if "step" not in coverage["mars:metadata"]:
coverage["mars:metadata"]["step"] = 0
steps.append(coverage["mars:metadata"]["step"])
datetimes.append(coverage["mars:metadata"]["Forecast date"])
for parameter in self.parameters:
# values[parameter].append(coverage["ranges"][parameter]["values"])
if coverage["mars:metadata"]["Forecast date"] not in values[parameter]:
values[parameter][coverage["mars:metadata"]["Forecast date"]] = {}
if (
coverage["mars:metadata"]["number"]
not in values[parameter][coverage["mars:metadata"]["Forecast date"]]
):
values[parameter][coverage["mars:metadata"]["Forecast date"]][
coverage["mars:metadata"]["number"]
] = {}
values[parameter][coverage["mars:metadata"]["Forecast date"]][coverage["mars:metadata"]["number"]][
coverage["mars:metadata"]["step"]
] = coverage["ranges"][parameter]["values"]

datetimes = list(set(datetimes))
numbers = list(set(numbers))
steps = list(set(steps))

new_values = {}
for parameter in values.keys():
new_values[parameter] = []
for i, datetime in enumerate(datetimes):
new_values[parameter].append([])
for j, number in enumerate(numbers):
new_values[parameter][i].append([])
for k, step in enumerate(steps):
new_values[parameter][i][j].append(values[parameter][datetime][number][step])

for parameter in self.parameters:
dataarray = xr.DataArray(new_values[parameter], dims=dims)
dataarray.attrs["type"] = self.get_parameter_metadata(parameter)["type"]
dataarray.attrs["units"] = self.get_parameter_metadata(parameter)["unit"]["symbol"]
dataarray.attrs["long_name"] = self.get_parameter_metadata(parameter)["observedProperty"]["id"]
Expand All @@ -59,10 +102,19 @@ def to_xarray(self):
ds = xr.Dataset(
dataarraydict,
coords=dict(
points=(["points"], list(range(0, len(x)))), x=(["points"], x), y=(["points"], y), t=(["points"], t)
datetimes=(["datetimes"], datetimes),
number=(["number"], numbers),
steps=(["steps"], steps),
points=(["points"], list(range(0, len(x)))),
x=(["points"], x),
y=(["points"], y),
t=(["points"], t),
),
)
for mars_metadata in self.mars_metadata[0]:
ds.attrs[mars_metadata] = self.mars_metadata[0][mars_metadata]

# Add date attribute
# ds.attrs["date"] = self.get_coordinates()["t"]["values"][0]

return ds
81 changes: 50 additions & 31 deletions covjsonkit/decoder/TimeSeries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import datetime as dt

import xarray as xr

from .decoder import Decoder
Expand Down Expand Up @@ -40,9 +38,12 @@ def get_coordinates(self):
x = domain["axes"]["x"]["values"][0]
y = domain["axes"]["y"]["values"][0]
z = domain["axes"]["z"]["values"][0]
fct = self.mars_metadata[ind]["date"]
fct = domain["axes"]["t"]["values"][0]
ts = domain["axes"]["t"]["values"]
num = self.mars_metadata[ind]["number"]
if "number" in self.mars_metadata[ind]:
num = self.mars_metadata[ind]["number"]
else:
num = 0
for param in self.parameters:
coords = []
for t in ts:
Expand All @@ -57,37 +58,55 @@ def to_geopandas(self):

# function to convert covjson to xarray dataset
def to_xarray(self):
dims = ["x", "y", "z", "number", "t"]
dims = ["x", "y", "z", "number", "datetime", "t"]
dataarraydict = {}

# Get coordinates
coords = self.get_domains()
x = coords[0]["axes"]["x"]["values"]
y = coords[0]["axes"]["y"]["values"]
z = coords[0]["axes"]["z"]["values"]
steps = coords[0]["axes"]["t"]["values"]
steps = list(range(len(steps)))

num = []
datetime = []
for coverage in self.covjson["coverages"]:
num.append(coverage["mars:metadata"]["number"])
datetime.append(coverage["mars:metadata"]["Forecast date"])

nums = list(set(num))
datetime = list(set(datetime))

param_values = {}

for parameter in ["ssrd", "2t"]:
param_values[parameter] = []
for i, num in enumerate(nums):
param_values[parameter].append([])
for j, date in enumerate(datetime):
param_values[parameter][i].append([])
for k, step in enumerate(steps):
for coverage in self.covjson["coverages"]:
if (
coverage["mars:metadata"]["number"] == num
and coverage["mars:metadata"]["Forecast date"] == date
):
param_values[parameter][i][j] = coverage["ranges"][parameter]["values"]

for parameter in self.parameters:
param_values = [[[self.get_values()[parameter]]]]
for ind, fc_time_vals in enumerate(self.get_values()[parameter]):
coords = self.get_coordinates()[parameter]
x = [coords[ind][0][0]]
y = [coords[ind][0][1]]
z = [coords[ind][0][2]]

num = [int(coord[0][5]) for coord in coords]
coords_fc = coords[ind]
try:
t = [dt.datetime.strptime(coord[4], "%Y-%m-%d %H:%M:%S") for coord in coords_fc]
except ValueError:
t = [dt.datetime.strptime(coord[4], "%Y-%m-%dT%H:%M:%SZ") for coord in coords_fc]

param_coords = {"x": x, "y": y, "z": z, "number": num, "t": t}
dataarray = xr.DataArray(
param_values,
dims=dims,
coords=param_coords,
name=parameter,
)

dataarray.attrs["type"] = self.get_parameter_metadata(parameter)["type"]
dataarray.attrs["units"] = self.get_parameter_metadata(parameter)["unit"]["symbol"]
dataarray.attrs["long_name"] = self.get_parameter_metadata(parameter)["observedProperty"]["id"]
dataarraydict[dataarray.attrs["long_name"]] = dataarray
param_coords = {"x": x, "y": y, "z": [z], "number": nums, "datetime": datetime, "t": steps}
dataarray = xr.DataArray(
[[[param_values[parameter]]]],
dims=dims,
coords=param_coords,
name=parameter,
)

dataarray.attrs["type"] = self.get_parameter_metadata(parameter)["type"]
dataarray.attrs["units"] = self.get_parameter_metadata(parameter)["unit"]["symbol"]
dataarray.attrs["long_name"] = self.get_parameter_metadata(parameter)["observedProperty"]["id"]
dataarraydict[dataarray.attrs["long_name"]] = dataarray

ds = xr.Dataset(dataarraydict)
for mars_metadata in self.mars_metadata[0]:
Expand Down
Loading

0 comments on commit 00e5bd6

Please sign in to comment.