22
33
44# standard library
5+ import tarfile
56from pathlib import Path
67
78
1415
1516# constants
1617CHUNK_SIZE = 1024
18+ DEFAULT_TAG = f"v{ __version__ } "
1719GITHUB_URL = "https://raw.githubusercontent.com/deshima-dev/rawdata"
1820
1921
@@ -22,27 +24,31 @@ def download(
2224 / ,
2325 * ,
2426 dir : Path = Path (),
25- progress : bool = True ,
27+ extract : bool = False ,
28+ progress : bool = False ,
29+ tag : str = DEFAULT_TAG ,
2630) -> Path :
2731 """Download DESHIMA raw data for given observation ID.
2832
2933 Args:
3034 obsid: Observation ID (YYYYmmddHHMMSS).
3135 dir: Directory where the raw data is saved.
36+ extract: Whether to extract the raw data.
3237 progress: Whether to show a progress bar.
38+ tag: Git tag (or branch) of the raw data.
3339
3440 Returns:
3541 Path of the downloaded raw data.
3642
3743 """
38- url = f"{ GITHUB_URL } /v { __version__ } /data/{ obsid } .tar.gz"
44+ url = f"{ GITHUB_URL } /{ tag } /data/{ obsid } .tar.gz"
3945
4046 if not (response := get (url , stream = True )).ok :
4147 response .raise_for_status ()
4248
4349 bar_options = {
4450 "disable" : not progress ,
45- "total" : int (response .headers . get ( "content-length" , 0 ) ),
51+ "total" : int (response .headers [ "content-length" ] ),
4652 "unit" : "B" ,
4753 "unit_scale" : True ,
4854 }
@@ -53,7 +59,15 @@ def download(
5359 bar .update (len (data ))
5460 f .write (data )
5561
56- return data_path
62+ if not extract :
63+ return data_path
64+
65+ with tarfile .open (data_path , "r:gz" ) as tar :
66+ tar .extractall (data_path .parent )
67+ dir_name = tar .getnames ()[0 ]
68+
69+ data_path .unlink (True )
70+ return data_path .parent / dir_name
5771
5872
5973def main () -> None :
0 commit comments