-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_hirlam_FMI.py
48 lines (41 loc) · 1.77 KB
/
get_hirlam_FMI.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
48
"""Python program to fetch HIRLAM FMI surface temperature data from AWS.
- Use the current date/time to fetch the most recent data
- Download the temperature only (not modular/flexible code)
- store the results in numerical-hirlam74-forecast-Temperature.grb2
"""
from datetime import datetime, timedelta
import s3fs
def get_lastfile(fs, output_prefix):
# datetime object containing current date and time
current_date = datetime.now()
print("Current date and time =", current_date)
s3path = 's3://fmi-opendata-rcrhirlam-surface-grib/' + current_date.strftime("%Y") + '/' + \
current_date.strftime("%m") + '/' + current_date.strftime("%d") + '/*/' + \
output_prefix + '-*.grb2'
remote_files = fs.glob(s3path)
if len(remote_files) <= 0:
previous_date = current_date - timedelta(days=1)
s3path = 's3://fmi-opendata-rcrhirlam-surface-grib/' + previous_date.strftime("%Y") + '/' + \
previous_date.strftime("%m") + '/' + previous_date.strftime("%d") + '/*/' + \
output_prefix + '-*.grb2'
remote_files = fs.glob(s3path)
if len(remote_files) <= 0:
latest_file = ""
else:
latest_file = remote_files[-1]
return latest_file
def main():
fs = s3fs.S3FileSystem(
anon=True,
client_kwargs={"endpoint_url": "http://s3-eu-west-1.amazonaws.com" },
)
output_prefix = 'numerical-hirlam74-forecast-Temperature'
latest_file = get_lastfile(fs, output_prefix)
if latest_file != "":
fs.download('s3://' + latest_file, output_prefix + '.grb2')
tag = "/".join(latest_file.split('/')[1:-1])
f = open("numerical-hirlam74-forecast-Temperature.txt", "w")
f.write(tag)
f.close()
if __name__ == "__main__":
main()