From 4aa43f10c0808abbdfa335b9aa7be291d5abf34a Mon Sep 17 00:00:00 2001 From: awarde96 Date: Tue, 6 Aug 2024 13:35:23 +0000 Subject: [PATCH] Fix path --- covjsonkit/decoder/Path.py | 4 +- covjsonkit/encoder/Path.py | 87 ++++++++++++++--------------------- covjsonkit/encoder/encoder.py | 9 ++-- 3 files changed, 41 insertions(+), 59 deletions(-) diff --git a/covjsonkit/decoder/Path.py b/covjsonkit/decoder/Path.py index 8290bf6..0205567 100644 --- a/covjsonkit/decoder/Path.py +++ b/covjsonkit/decoder/Path.py @@ -44,8 +44,8 @@ def to_xarray(self): y = [] t = [] for coord in self.get_coordinates()["composite"]["values"]: - x.append(float(coord[1])) - y.append(float(coord[2])) + x.append(float(coord[0])) + y.append(float(coord[1])) t.append(coord[0]) # Get values diff --git a/covjsonkit/encoder/Path.py b/covjsonkit/encoder/Path.py index 3a0d9ee..a8049c3 100644 --- a/covjsonkit/encoder/Path.py +++ b/covjsonkit/encoder/Path.py @@ -1,8 +1,3 @@ -import json - -import pandas as pd -from covjson_pydantic.coverage import Coverage - from .encoder import Encoder @@ -10,6 +5,7 @@ class Path(Encoder): def __init__(self, type, domaintype): super().__init__(type, domaintype) self.covjson["domainType"] = "Trajectory" + self.covjson["coverages"] = [] def add_coverage(self, mars_metadata, coords, values): new_coverage = {} @@ -20,15 +16,16 @@ def add_coverage(self, mars_metadata, coords, values): self.add_mars_metadata(new_coverage, mars_metadata) self.add_domain(new_coverage, coords) self.add_range(new_coverage, values) - cov = Coverage.model_validate_json(json.dumps(new_coverage)) - self.pydantic_coverage.coverages.append(cov) + self.covjson["coverages"].append(new_coverage) + # cov = Coverage.model_validate_json(json.dumps(new_coverage)) + # self.pydantic_coverage.coverages.append(cov) def add_domain(self, coverage, coords): coverage["domain"]["type"] = "Domain" coverage["domain"]["axes"] = {} coverage["domain"]["axes"]["composite"] = {} coverage["domain"]["axes"]["composite"]["dataType"] = "tuple" - coverage["domain"]["axes"]["composite"]["coordinates"] = self.referencing + coverage["domain"]["axes"]["composite"]["coordinates"] = self.covjson["referencing"] coverage["domain"]["axes"]["composite"]["values"] = coords["composite"] def add_range(self, coverage, values): @@ -77,58 +74,44 @@ def from_xarray(self, dataset): return self.covjson def from_polytope(self, result): - ancestors = [val.get_ancestors() for val in result.leaves] - values = [val.result for val in result.leaves] - - columns = [] - df_dict = {} - # Create empty dataframe - for feature in ancestors[0]: - columns.append(str(feature).split("=")[0]) - df_dict[str(feature).split("=")[0]] = [] - # populate dataframe - for ancestor in ancestors: - for feature in ancestor: - df_dict[str(feature).split("=")[0]].append(str(feature).split("=")[1]) - values = [val.result for val in result.leaves] - df_dict["values"] = values - df = pd.DataFrame(df_dict) - - params = df["param"].unique() + coords = {} + # coords['composite'] = [] + mars_metadata = {} + range_dict = {} + lat = 0 + param = 0 + number = [0] + step = 0 + dates = [0] - for param in params: - self.add_parameter(param) + self.walk_tree(result, lat, coords, mars_metadata, param, range_dict, number, step, dates) self.add_reference( { - "coordinates": ["t", "x", "y"], + "coordinates": ["x", "y", "z"], "system": { "type": "GeographicCRS", "id": "http://www.opengis.net/def/crs/OGC/1.3/CRS84", }, } ) - - mars_metadata = {} - mars_metadata["class"] = df["class"].unique()[0] - mars_metadata["expver"] = df["expver"].unique()[0] - mars_metadata["levtype"] = df["levtype"].unique()[0] - mars_metadata["type"] = df["type"].unique()[0] - mars_metadata["domain"] = df["domain"].unique()[0] - mars_metadata["stream"] = df["stream"].unique()[0] - - range_dict = {} - coords = {} - coords["composite"] = [] - - for param in params: - df_param = df[df["param"] == param] - range_dict[param] = df_param["values"].values.tolist() - - df_param = df[df["param"] == params[0]] - for row in df_param.iterrows(): - coords["composite"].append([row[1]["date"], row[1]["latitude"], row[1]["longitude"]]) - - self.add_coverage(mars_metadata, coords, range_dict) - return json.loads(self.get_json()) + print(coords) + print(range_dict) + + for date in range_dict.keys(): + for num in range_dict[date].keys(): + val_dict = {} + for step in range_dict[date][num][self.parameters[0]].keys(): + val_dict[step] = {} + for para in range_dict[date][num].keys(): + for step in range_dict[date][num][para].keys(): + val_dict[step][para] = range_dict[date][num][para][step] + for step in val_dict.keys(): + mm = mars_metadata.copy() + mm["number"] = num + mm["step"] = step + self.add_coverage(mm, coords[date], val_dict[step]) + + # self.add_coverage(mars_metadata, coords, range_dict) + return self.covjson diff --git a/covjsonkit/encoder/encoder.py b/covjsonkit/encoder/encoder.py index f598403..b6e26ac 100644 --- a/covjsonkit/encoder/encoder.py +++ b/covjsonkit/encoder/encoder.py @@ -1,7 +1,6 @@ from abc import ABC, abstractmethod import orjson -from covjson_pydantic.coverage import CoverageCollection from covjson_pydantic.domain import DomainType from covjsonkit.param_db import get_param_ids, get_params, get_units @@ -35,11 +34,11 @@ def __init__(self, type, domaintype): elif domaintype == "frame": self.domaintype = DomainType.multi_point elif domaintype == "path": - self.domaintype = DomainType.trajectory + self.domaintype = "Trajectory" - self.pydantic_coverage = CoverageCollection( - type=type, coverages=[], domainType=self.domaintype, parameters={}, referencing=[] - ) + # self.pydantic_coverage = CoverageCollection( + # type=type, coverages=[], domainType=self.domaintype, parameters={}, referencing=[] + # ) self.parameters = [] def add_parameter(self, param):