Skip to content
Snippets Groups Projects
Commit 29e04e74 authored by Carsten Hinz's avatar Carsten Hinz
Browse files

refactor in gridding function

added a test implementation to retrieve names from the TOAR-DB for all contributors.
parent bce987f5
No related branches found
No related tags found
3 merge requests!11Creation of first beta release version,!10change in metadata of notebook,!9change in metadata of notebook
...@@ -3,6 +3,9 @@ from pandas import DataFrame ...@@ -3,6 +3,9 @@ from pandas import DataFrame
from pandas.core.groupby import DataFrameGroupBy from pandas.core.groupby import DataFrameGroupBy
from typing import Iterable from typing import Iterable
from pathlib import Path from pathlib import Path
from time import sleep
import requests
#TODO: maybe create an abstract base class and separate the offline and service implementations #TODO: maybe create an abstract base class and separate the offline and service implementations
class contributionsManager: class contributionsManager:
...@@ -11,20 +14,30 @@ class contributionsManager: ...@@ -11,20 +14,30 @@ class contributionsManager:
self.timeseriesIDs = set() self.timeseriesIDs = set()
self.endpoint = endpoint self.endpoint = endpoint
self.runsAsService = True self.runsAsService = True
self.inline_mode = False
if contributors_path is not None: if contributors_path is not None:
self.runsAsService = False self.runsAsService = False
self.contributors_path = contributors_path self.contributors_path = contributors_path
def setup_contributors_endpoint_for_metadata(self): def setup_contributors_endpoint_for_metadata(self):
"""!create the contributors endpoint depending on the intended mode.
This either adds the timeseries IDs to the database and provides a get endpoint for the contributors or writes the timeseries IDs to a file and provides a curl command to upload the file to the database.
"""
if self.runsAsService: if self.runsAsService:
return self.setup_contributors_service() return self.setup_contributors_service()
else: else:
return self.setup_contributors_id_file() if self.inline_mode:
return self.setup_contributors_inline()
else:
return self.setup_contributors_id_file()
def setup_contributors_inline(self) -> str:
return f"{self.endpoint}" + "/?timeseriesids=" + ",".join([str(id) for id in self.timeseriesIDs])
def setup_contributors_id_file(self) -> str: def setup_contributors_id_file(self) -> str:
ext = "contributors" ext = "contributors"
with open(self.contributors_path / f"{self.requestID}.{ext}", "w") as f: with open(self.contributors_path / f"{self.requestID}.{ext}", "w") as f:
for id in self.timeseriesIDs: for id in self.timeseriesIDs:
f.write(f"{id}\n") f.write(f"{id}\n")
return f"curl {self.endpoint} -file {self.requestID}.{ext}" return f"curl -d @{self.requestID}.{ext} -X POST {self.endpoint}"
def setup_contributors_service(self) -> str: def setup_contributors_service(self) -> str:
# TODO: missing implementation # TODO: missing implementation
raise NotImplementedError("This has not been implemented as this package is not yet operated as a service.") raise NotImplementedError("This has not been implemented as this package is not yet operated as a service.")
...@@ -39,5 +52,40 @@ class contributionsManager: ...@@ -39,5 +52,40 @@ class contributionsManager:
for _, table in data_grouped_by_cell: for _, table in data_grouped_by_cell:
self.add_timeseries_ids( table.index.to_list() ) self.add_timeseries_ids( table.index.to_list() )
class contributions_manager_by_name(contributionsManager):
def add_timeseries_id(self, id : int) -> None:
names = self.id_to_names(id)
for name in names:
self.timeseriesIDs.add(name)
def add_timeseries_ids(self, ids : Iterable[int]) -> None:
for id in ids:
names = self.id_to_names(id)
for name in names:
self.timeseriesIDs.add(name)
def id_to_names(self, id : int) -> list[str]:
for _ in range(10):
req_res = requests.get(f"https://toar-data.fz-juelich.de/api/v2/timeseries/{id}")
try:
results = req_res.json()
break
except:
sleep(30)
pass
else:
raise RuntimeError(f"Could not get the response for the timeseries with id {id}.")
names = set()
for r in results["roles"]:
try:
#print("\t", r["contact"]["person"]["name"])
names.add( r["contact"]["person"]["name"] )
except:
try:
#print("\t", r["contact"]["organisation"]["name"])
names.add( r["contact"]["organisation"]["name"] )
except:
raise RuntimeError("Could not find 'person' or 'organisation' in the response.\n" + str(r))
return names
...@@ -57,9 +57,9 @@ def get_gridded_toar_data( ...@@ -57,9 +57,9 @@ def get_gridded_toar_data(
data = analysis_service.get_data(metadata) data = analysis_service.get_data(metadata)
#TODO add processing of contributors #TODO add processing of contributors
# create contributors endpoint and write result to metadata # create contributors endpoint and write result to metadata
contrib = contributionsManager(metadata.get_id(), contributors_path) contributors_field = contributionsManager(metadata.get_id(), contributors_path)
contrib.extract_contributors_from_data_frame(data.stations_data) contributors_field.extract_contributors_from_data_frame(data.stations_data)
metadata.contributors_metadata_field = contrib.setup_contributors_endpoint_for_metadata() metadata.contributors_metadata_field = contributors_field.setup_contributors_endpoint_for_metadata()
ds = grid.as_xarray(data) ds = grid.as_xarray(data)
datasets.append(ds) datasets.append(ds)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment