From 2ca9e234048634bfc4386f649d3b2cc17bce781d Mon Sep 17 00:00:00 2001 From: Michaela Date: Wed, 7 Apr 2021 18:34:52 +1000 Subject: [PATCH] Add in download option --- README.md | 8 +++++++- pyproject.toml | 2 +- sondehub/__init__.py | 8 +++++--- sondehub/__main__.py | 23 ++++++++++++++++++++--- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 711c9a3..8aeff2b 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ while 1: ``` -CLI Usage +### CLI Usage +#### Live streaming data ```sh # all radiosondes sondehub @@ -68,6 +69,11 @@ sondehub | jq . ``` +#### Downloading data +``` +sondehub --download S2810113 # Note this may take a long time to finish! +``` + Open Data Access == diff --git a/pyproject.toml b/pyproject.toml index db94dc6..9ff269c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sondehub" -version = "0.1.8" +version = "0.1.9" description = "SDK to access SondeHub open data" authors = ["Michaela "] readme = "README.md" diff --git a/sondehub/__init__.py b/sondehub/__init__.py index 22deb63..ea4cf7a 100644 --- a/sondehub/__init__.py +++ b/sondehub/__init__.py @@ -126,6 +126,8 @@ class Downloader(threading.Thread): task = self.tasks_to_accomplish.get_nowait() except queue.Empty: return + if self.debug: + print(task[1]) data = s3.get_object(Bucket=task[0], Key=task[1]) response = json.loads(data["Body"].read()) if self.debug: @@ -134,11 +136,11 @@ class Downloader(threading.Thread): self.tasks_to_accomplish.task_done() -def download(serial=None, datetime_prefix=None, debug=False): +def download(serial=None, datetime_prefix=None, hashed=True, debug=False): if serial: - prefix_filter = f"serial/{serial}/" + prefix_filter = f"serial{'-hashed' if hashed else ''}/{serial}/" elif serial and datetime_prefix: - prefix_filter = f"serial/{serial}/{datetime_prefix}" + prefix_filter = f"serial{'-hashed' if hashed else ''}/{serial}/{datetime_prefix}" elif datetime_prefix: prefix_filter = f"date/{datetime_prefix}" else: diff --git a/sondehub/__main__.py b/sondehub/__main__.py index 422f985..e89e1aa 100644 --- a/sondehub/__main__.py +++ b/sondehub/__main__.py @@ -3,6 +3,7 @@ import sondehub import argparse import os import sys +import time unbuffered = os.fdopen(sys.stdout.fileno(), "wb", 0) @@ -15,7 +16,8 @@ def on_message(message): def main(): parser = argparse.ArgumentParser(description="Sondehub CLI") - parser.add_argument( + group = parser.add_mutually_exclusive_group() + group.add_argument( "--serial", dest="sondes", default=["#"], @@ -24,15 +26,30 @@ def main(): type=str, action="append", ) + group.add_argument( + "--download", + dest="download", + default=[], + nargs="*", + help="Sondes to download from open data", + type=str, + action="append", + ) args = parser.parse_args() + if (args.download): + serials = [item for sublist in args.download for item in sublist] + for serial in serials: + for frame in sondehub.download(serial=serial): + on_message(frame) + return if ( len(args.sondes) > 1 - ): # we need to drop the default value if the user specifies sepcific sondes + ): # we need to drop the default value if the user specifies specific sondes args.sondes = args.sondes[1:] sondes = [item for sublist in args.sondes for item in sublist] test = sondehub.Stream(on_message=on_message, sondes=sondes) while 1: - pass + time.sleep(0.01) # don't overwork the CPU waiting for events if __name__ == "__main__":