Skip to content

Commit 8be6401

Browse files
committed
#1 Add download command
1 parent 15dd089 commit 8be6401

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

deshima_rawdata/cli.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1-
__all__ = []
1+
__all__ = ["download"]
22

3+
4+
# standard library
5+
from pathlib import Path
6+
7+
8+
# dependencies
9+
from fire import Fire
10+
from requests import get
11+
from tqdm import tqdm
12+
from . import __version__
13+
14+
15+
# constants
16+
CHUNK_SIZE = 1024
17+
GITHUB_URL = "https://raw.githubusercontent.com/deshima-dev/rawdata"
18+
19+
20+
def download(
21+
obsid: str,
22+
/,
23+
*,
24+
dir: Path = Path(),
25+
progress: bool = True,
26+
) -> Path:
27+
"""Download DESHIMA raw data for given observation ID.
28+
29+
Args:
30+
obsid: Observation ID (YYYYmmddHHMMSS).
31+
dir: Directory where the raw data is saved.
32+
progress: Whether to show a progress bar.
33+
34+
Returns:
35+
Path of the downloaded raw data.
36+
37+
"""
38+
url = f"{GITHUB_URL}/v{__version__}/data/{obsid}.tar.gz"
39+
40+
if not (response := get(url, stream=True)).ok:
41+
response.raise_for_status()
42+
43+
bar_options = {
44+
"disable": not progress,
45+
"total": int(response.headers.get("content-length", 0)),
46+
"unit": "B",
47+
"unit_scale": True,
48+
}
49+
data_path = Path(dir) / response.url.split("/")[-1]
50+
51+
with tqdm(**bar_options) as bar, open(data_path, "wb") as f:
52+
for data in response.iter_content(CHUNK_SIZE):
53+
bar.update(len(data))
54+
f.write(data)
55+
56+
return data_path
57+
58+
59+
def main() -> None:
60+
"""Entry point of the deshima-rawdata command."""
61+
Fire({"download": download})

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ ipython = "^8.17"
1818
pyright = "^1.1"
1919
pytest = "^7.4"
2020

21+
22+
[tool.poetry.scripts]
23+
deshima-rawdata = "deshima_rawdata.cli:main"
24+
2125
[build-system]
2226
requires = ["poetry-core"]
2327
build-backend = "poetry.core.masonry.api"

0 commit comments

Comments
 (0)