Skip to content
Draft
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
8 changes: 5 additions & 3 deletions covjsonkit/encoder/BoundingBox.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -130,8 +130,7 @@ def from_polytope(self, result):
fields["dates"] = []
fields["levels"] = [0]

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

self.walk_tree(result, fields, coords, mars_metadata, range_dict, date_key=date_key)
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

Expand Down Expand Up @@ -203,6 +202,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
7 changes: 5 additions & 2 deletions covjsonkit/encoder/Circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjsonå

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -127,7 +127,7 @@ def from_polytope(self, result):
fields["dates"] = []
fields["levels"] = [0]

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

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
Expand Down Expand Up @@ -200,6 +200,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
7 changes: 5 additions & 2 deletions covjsonkit/encoder/Frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -127,7 +127,7 @@ def from_polytope(self, result):
fields["dates"] = []
fields["levels"] = [0]

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

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
Expand Down Expand Up @@ -195,6 +195,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
8 changes: 5 additions & 3 deletions covjsonkit/encoder/Grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -133,8 +133,7 @@ def from_polytope(self, result):
fields["dates"] = []
fields["levels"] = [0]

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

self.walk_tree(result, fields, coords, mars_metadata, range_dict, date_key=date_key)
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

Expand Down Expand Up @@ -218,6 +217,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
7 changes: 5 additions & 2 deletions covjsonkit/encoder/Path.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -129,7 +129,7 @@ def from_polytope(self, result):
fields["s"] = []
fields["l"] = []

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

if len(fields["l"]) == 0:
fields["l"] = [0]
Expand Down Expand Up @@ -225,6 +225,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
7 changes: 5 additions & 2 deletions covjsonkit/encoder/Position.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def from_xarray(self, datasets):

return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):
"""
Converts a Polytope result into an OGC CoverageJSON coverageCollection of type PointSeries
Args:
Expand All @@ -144,7 +144,7 @@ def from_polytope(self, result):

start = time.time()
logging.debug("Tree walking starts at: %s", start) # noqa: E501
self.walk_tree(result, fields, coords, mars_metadata, range_dict)
self.walk_tree(result, fields, coords, mars_metadata, range_dict, date_key=date_key)
end = time.time()
delta = end - start
logging.debug("Tree walking ends at: %s", end) # noqa: E501
Expand Down Expand Up @@ -251,6 +251,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
7 changes: 5 additions & 2 deletions covjsonkit/encoder/Shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def from_xarray(self, dataset):
# Return the generated CoverageJSON
return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):

coords = {}
mars_metadata = {}
Expand All @@ -127,7 +127,7 @@ def from_polytope(self, result):
fields["dates"] = []
fields["levels"] = [0]

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

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
Expand Down Expand Up @@ -195,6 +195,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
135 changes: 135 additions & 0 deletions covjsonkit/encoder/TimeSeries.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import logging
import time
import typing
from datetime import datetime, timedelta

import pandas as pd

from .encoder import Encoder

if typing.TYPE_CHECKING:
from polytope_feature.datacube.tensor_index_tree import TensorIndexTree


class TimeSeries(Encoder):
def __init__(self, type, domaintype):
Expand Down Expand Up @@ -471,3 +475,134 @@ def from_polytope_step(self, result):
logging.debug("Coverage creation: %s", delta) # noqa: E501

return self.covjson

def from_polytope_reforecast(self, result: "TensorIndexTree") -> dict:
"""Encode reforecast data that uses "hdate" as the time axis.

Each hdate produces a separate coverage (one per point × hdate).
Steps within a single hdate become that coverage's t-axis values.
"""
coords = {}
mars_metadata = {}
range_dict = {}
fields = {}
fields["lat"] = 0
fields["param"] = 0
fields["number"] = [0]
fields["step"] = 0
fields["dates"] = []
fields["levels"] = [0]

start = time.time()
logging.debug("Tree walking starts at: %s", start) # noqa: E501
self.walk_tree(result, fields, coords, mars_metadata, range_dict, date_key="hdate")
end = time.time()
delta = end - start
logging.debug("Tree walking ends at: %s", end) # noqa: E501
logging.debug("Tree walking takes: %s", delta) # noqa: E501

start = time.time()
logging.debug("Coords creation: %s", start) # noqa: E501

self.add_reference(
{
"coordinates": ["latitude", "longitude", "levelist"],
"system": {
"type": "GeographicCRS",
"id": "http://www.opengis.net/def/crs/OGC/1.3/CRS84",
},
}
)

coordinates = {}

levels = fields["levels"]
if fields["param"] == 0:
raise ValueError("No data was returned.")
for para in fields["param"]:
self.add_parameter(para)

logging.debug("The parameters added were: %s", self.parameters) # noqa: E501

points = len(coords[fields["dates"][0]]["composite"])

for date in fields["dates"]:
coordinates[date] = []
for i, point in enumerate(range(points)):
coordinates[date].append(
{
"latitude": [coords[date]["composite"][i][0]],
"longitude": [coords[date]["composite"][i][1]],
"levelist": [levels[0]],
}
)
coordinates[date][i]["t"] = []
for level in fields["levels"]:
for num in fields["number"]:
for para in fields["param"]:
for step in fields["step"]:
date_format = "%Y%m%dT%H%M%S"
new_date = pd.Timestamp(date).strftime(date_format)
start_time = datetime.strptime(new_date, date_format)
# add current date to list by converting it to iso format
if isinstance(step, timedelta):
stamp = start_time + step
else:
try:
int(step)
except ValueError:
step = step[0]
stamp = start_time + timedelta(hours=int(step))
coordinates[date][i]["t"].append(stamp.isoformat() + "Z")
break
break
break

logging.debug("Coordinates created: %s", coordinates) # noqa: E501

end = time.time()
delta = end - start
logging.debug("Coords creation: %s", end) # noqa: E501
logging.debug("Coords creation: %s", delta) # noqa: E501

start = time.time()
logging.debug("Coverage creation: %s", start) # noqa: E501

logging.debug("The points found were: %s", points) # noqa: E501
logging.debug("The fields retrieved were: %s", fields) # noqa: E501
logging.debug("The range_dict created was: %s", range_dict) # noqa: E501

for i, point in enumerate(range(points)):
for date in fields["dates"]:
for level in fields["levels"]:
for num in fields["number"]:
val_dict = {}
for para in fields["param"]:
val_dict[para] = []
for step in fields["step"]:
key = (date, level, num, para, step)
try:
val_dict[para].append(range_dict[key][i])
except IndexError:
logging.debug(
f"Index {i} out of range for key {key} in range_dict. "
f"Available keys: {list(range_dict.keys())}"
)
raise IndexError(
f"Key {key} not found in range_dict. "
f"Please ensure all axes are compressed in config"
)
mm = mars_metadata.copy()
mm["number"] = num
mm["Forecast date"] = date
mm["levelist"] = level
coordinates[date][i]["levelist"] = [level]
del mm["step"]
self.add_coverage(mm, coordinates[date][i], val_dict)

end = time.time()
delta = end - start
logging.debug("Coverage creation: %s", end) # noqa: E501
logging.debug("Coverage creation: %s", delta) # noqa: E501

return self.covjson
7 changes: 5 additions & 2 deletions covjsonkit/encoder/VerticalProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def from_xarray(self, datasets):

return self.covjson

def from_polytope(self, result):
def from_polytope(self, result, date_key="date"):
coords = {}
mars_metadata = {}
range_dict = {}
Expand All @@ -135,7 +135,7 @@ def from_polytope(self, result):

start = time.time()
logging.debug("Tree walking starts at: %s", start) # noqa: E501
self.walk_tree(result, fields, coords, mars_metadata, range_dict)
self.walk_tree(result, fields, coords, mars_metadata, range_dict, date_key=date_key)
end = time.time()
delta = end - start
logging.debug("Tree walking ends at: %s", end) # noqa: E501
Expand Down Expand Up @@ -239,6 +239,9 @@ def from_polytope(self, result):

return self.covjson

def from_polytope_reforecast(self, result):
return self.from_polytope(result, date_key="hdate")

def from_polytope_month(self, result):
coords = {}
mars_metadata = {}
Expand Down
Loading
Loading