Skip to content
Snippets Groups Projects
Commit 98f9a3e5 authored by Simon Grasse's avatar Simon Grasse
Browse files

add: Subclass for archiving query results as zipfiles

parent 54d6e591
No related branches found
No related tags found
1 merge request!11Creation of first beta release version
......@@ -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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment