Skip to content

Commit

Permalink
Fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
awarde96 committed Aug 6, 2024
1 parent 822477e commit 4aa43f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 59 deletions.
4 changes: 2 additions & 2 deletions covjsonkit/decoder/Path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 35 additions & 52 deletions covjsonkit/encoder/Path.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import json

import pandas as pd
from covjson_pydantic.coverage import Coverage

from .encoder import Encoder


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 = {}
Expand All @@ -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):
Expand Down Expand Up @@ -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
9 changes: 4 additions & 5 deletions covjsonkit/encoder/encoder.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4aa43f1

Please sign in to comment.