From 98f9a3e5cbe43a15dd857c5fe88daf07a1e8e933 Mon Sep 17 00:00:00 2001 From: Simon Grasse <s.grasse@fz-juelich.de> Date: Wed, 10 Jan 2024 14:42:06 +0100 Subject: [PATCH] add: Subclass for archiving query results as zipfiles --- toargridding/toar_rest_client.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/toargridding/toar_rest_client.py b/toargridding/toar_rest_client.py index 28fb9fc..ec51f85 100644 --- a/toargridding/toar_rest_client.py +++ b/toargridding/toar_rest_client.py @@ -4,6 +4,7 @@ from zipfile import ZipFile from dataclasses import dataclass, asdict from contextlib import contextmanager import json +from pathlib import Path import requests import pandas as pd @@ -180,3 +181,32 @@ class AnalysisService: with zip_file.open(f"{data_file}.csv") as f: s_stream = io.StringIO(f.read().decode("utf-8")) return pd.read_csv(s_stream, comment="#", index_col=0) + + +class AnalysisServiceDownload(AnalysisService): + def __init__( + self, status_endpoint, cache_dir, sample_dir: Path, use_downloaded=True + ): + super().__init__(status_endpoint, cache_dir) + self.sample_dir = sample_dir + self.use_downloaded = use_downloaded + + def get_timeseries_and_metadata(self, metadata: Metadata): + filename = self.sample_dir / self.get_sample_file_name(metadata) + query_options = QueryOptions.from_metadata(metadata) + needs_fresh_download = (not self.use_downloaded) or (not filename.is_file()) + + if needs_fresh_download: + content = self.connection.get(query_options) + with open(filename, "w+b") as downloaded_file: + downloaded_file.write() + + with open(filename, "r+b") as data_file: + content = data_file.read() + + timeseries, timeseries_metadata = self.load_data(content, metadata) + return timeseries, timeseries_metadata + + @staticmethod + def get_sample_file_name(metadata: Metadata): + return f"data/{metadata.statistic}_{metadata.time.sampling}_{metadata.time.start.date()}_{metadata.time.end.date()}.zip" -- GitLab