-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmogreps.py
47 lines (36 loc) · 1.37 KB
/
mogreps.py
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
"""
Utility functions to retrieve data from the MOGREPS repository.
"""
from pathlib import Path
import boto3
import botocore
s3 = boto3.resource(
's3',
config=botocore.client.Config(signature_version=botocore.UNSIGNED))
def make_data_object_name(
dataset_name,
year, month, day, hour,
realization, forecast_period):
"""Create a string formatted to give a filename in the MOGREPS dataset."""
template_string = "prods_op_{}_{:02d}{:02d}{:02d}_{:02d}_{:02d}_{:03d}.nc"
return template_string.format(
dataset_name, year, month, day, hour, realization, forecast_period)
def download_data(bucket, name, data_folder=Path("data")):
"""Download a file from the Amazon AWS.
If the target already exists, nothing is done.
Retuns a `pathlib.Path` object pointing to the target file."""
if not data_folder.exists():
data_folder.mkdir()
target = data_folder / name
if not target.exists():
try:
print("Downloading {} ...".format(name))
s3.Bucket(bucket).download_file(name, str(target))
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
else:
print("File {} already exists.".format(target))
return target