File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 })
Original file line number Diff line number Diff line change @@ -18,6 +18,10 @@ ipython = "^8.17"
1818pyright = " ^1.1"
1919pytest = " ^7.4"
2020
21+
22+ [tool .poetry .scripts ]
23+ deshima-rawdata = " deshima_rawdata.cli:main"
24+
2125[build-system ]
2226requires = [" poetry-core" ]
2327build-backend = " poetry.core.masonry.api"
You can’t perform that action at this time.
0 commit comments