-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDownload-CulgooraData
More file actions
59 lines (49 loc) · 2.16 KB
/
Download-CulgooraData
File metadata and controls
59 lines (49 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import requests
import os
import bz2
from datetime import datetime
# Function to download and decompress Culgoora .bz2 file
def download_srs_file(date_str, save_dir="downloaded_data"):
"""
Download and decompress Culgoora SPEC file for a given date.
Parameters:
- date_str: Date string in 'YYYY-MM-DD' format (e.g., '2024-01-01')
- save_dir: Directory to save the downloaded and decompressed file
Returns:
- Path to the decompressed file
"""
date = datetime.strptime(date_str, "%Y-%m-%d")
year_short = str(date.year)[-2:] # Last two digits of year (e.g., '24' for 2024)
file_name = f"SPEC{year_short}{date.strftime('%m%d')}.bz2" # e.g., SPEC240101.bz2
url = f"https://downloads.sws.bom.gov.au/wdc/wdc_spec/data/culgoora/raw/{year_short}/{file_name}"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Path for the compressed file
compressed_path = os.path.join(save_dir, file_name)
# Path for the decompressed file (remove .bz2 extension)
decompressed_path = os.path.join(save_dir, file_name.replace('.bz2', ''))
# Download the compressed file
print(f"Downloading {url}...")
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(compressed_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print(f"Saved compressed file to {compressed_path}")
else:
raise ValueError(f"Failed to download file. Status code: {response.status_code}")
# Decompress the file
print(f"Decompressing {compressed_path}...")
with bz2.open(compressed_path, 'rb') as f_in:
with open(decompressed_path, 'wb') as f_out:
f_out.write(f_in.read())
print(f"Decompressed file saved to {decompressed_path}")
# Optionally, remove the compressed file to save space
os.remove(compressed_path)
print(f"Removed compressed file {compressed_path}")
return decompressed_path
# Download Culgoora data for January 1, 2024
date_str = "2015-11-04"
file_path = download_srs_file(date_str)