Skip to content

Commit 3fb5099

Browse files
authored
#2 Merge pull request from deshima-dev/astropenguin/issue1
#1 Add downloader
2 parents 3e294d3 + 8be6401 commit 3fb5099

5 files changed

Lines changed: 849 additions & 0 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "deshima-rawdata",
3+
"image":"python:3.11",
4+
"onCreateCommand": "pip install poetry==1.6.1",
5+
"postCreateCommand": "poetry install",
6+
"containerEnv": {
7+
"POETRY_VIRTUALENVS_CREATE": "false"
8+
},
9+
"customizations": {
10+
"vscode": {
11+
"extensions": [
12+
"github.vscode-pull-request-github",
13+
"mhutchie.git-graph",
14+
"ms-python.black-formatter",
15+
"ms-python.python",
16+
"streetsidesoftware.code-spell-checker",
17+
"tamasfe.even-better-toml"
18+
],
19+
"settings": {
20+
"files.insertFinalNewline": true,
21+
"files.trimTrailingWhitespace": true,
22+
"[python]": {
23+
"editor.defaultFormatter": "ms-python.black-formatter",
24+
"editor.formatOnSave": true,
25+
"editor.insertSpaces": true,
26+
"editor.tabSize": 4,
27+
"python.languageServer": "Pylance"
28+
}
29+
}
30+
}
31+
}
32+
}

deshima_rawdata/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__all__ = ["cli"]
2+
__version__ = "2023.11.0"
3+
4+
5+
# submodules
6+
from . import cli

deshima_rawdata/cli.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
__all__ = ["download"]
2+
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})

0 commit comments

Comments
 (0)