diff --git a/src/toargridding/contributors.py b/src/toargridding/contributors.py index 07ce3e70afd0af212f7b76a73ca5f748f16ce9a1..9837ce95fe6a123ecbbf2d5fbb9b36ed0ed0f56a 100644 --- a/src/toargridding/contributors.py +++ b/src/toargridding/contributors.py @@ -3,6 +3,9 @@ from pandas import DataFrame from pandas.core.groupby import DataFrameGroupBy from typing import Iterable from pathlib import Path +from time import sleep + +import requests #TODO: maybe create an abstract base class and separate the offline and service implementations class contributionsManager: @@ -11,20 +14,30 @@ class contributionsManager: self.timeseriesIDs = set() self.endpoint = endpoint self.runsAsService = True + self.inline_mode = False if contributors_path is not None: self.runsAsService = False self.contributors_path = contributors_path 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: return self.setup_contributors_service() 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: ext = "contributors" with open(self.contributors_path / f"{self.requestID}.{ext}", "w") as f: for id in self.timeseriesIDs: 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: # TODO: missing implementation raise NotImplementedError("This has not been implemented as this package is not yet operated as a service.") @@ -39,5 +52,40 @@ class contributionsManager: for _, table in data_grouped_by_cell: 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 diff --git a/src/toargridding/gridding.py b/src/toargridding/gridding.py index ffc0c0cd24fe41b53a5202f1f9c5adb6d465ebea..5b2e8719453cef323d8c0ab810ba8a2c19e65b4c 100644 --- a/src/toargridding/gridding.py +++ b/src/toargridding/gridding.py @@ -57,9 +57,9 @@ def get_gridded_toar_data( data = analysis_service.get_data(metadata) #TODO add processing of contributors # create contributors endpoint and write result to metadata - contrib = contributionsManager(metadata.get_id(), contributors_path) - contrib.extract_contributors_from_data_frame(data.stations_data) - metadata.contributors_metadata_field = contrib.setup_contributors_endpoint_for_metadata() + contributors_field = contributionsManager(metadata.get_id(), contributors_path) + contributors_field.extract_contributors_from_data_frame(data.stations_data) + metadata.contributors_metadata_field = contributors_field.setup_contributors_endpoint_for_metadata() ds = grid.as_xarray(data) datasets.append(ds)