-
Notifications
You must be signed in to change notification settings - Fork 1
/
ss_download.py
133 lines (111 loc) · 3.78 KB
/
ss_download.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 2023 (C) Crown Copyright, Met Office. All rights reserved.
#
# This file is part of Weather DataHub and is released under the
# BSD 3-Clause license.
# See LICENSE in the root of the repository for full licensing details.
# (c) Met Office 2023
import requests
import argparse
import time
import sys
import logging as log
log.basicConfig(filename='ss_download.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
base_url = "https://data.hub.api.metoffice.gov.uk/sitespecific/v0/point/"
def retrieve_forecast(baseUrl, timesteps, requestHeaders, latitude, longitude, excludeMetadata, includeLocation):
url = baseUrl + timesteps
headers = {'accept': "application/json"}
headers.update(requestHeaders)
params = {
'excludeParameterMetadata' : excludeMetadata,
'includeLocationName' : includeLocation,
'latitude' : latitude,
'longitude' : longitude
}
success = False
retries = 5
while not success and retries >0:
try:
req = requests.get(url, headers=headers, params=params)
success = True
except Exception as e:
log.warning("Exception occurred", exc_info=True)
retries -= 1
time.sleep(10)
if retries == 0:
log.error("Retries exceeded", exc_info=True)
sys.exit()
req.encoding = 'utf-8'
#print(req.text)
return (req.json())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Retrieve the site-specific forecast for a single location"
)
parser.add_argument(
"-t",
"--timesteps",
action="store",
dest="timesteps",
default="hourly",
help="The frequency of the timesteps provided in the forecast. The options are hourly, three-hourly or daily",
)
parser.add_argument(
"-m",
"--metadata",
action="store",
dest="excludeMetadata",
default="FALSE",
help="Provide a boolean value for whether parameter metadata should be excluded."
)
parser.add_argument(
"-n",
"--name",
action="store",
dest="includeLocation",
default="TRUE",
help="Provide a boolean value for whether the location name should be included."
)
parser.add_argument(
"-y",
"--latitude",
action="store",
dest="latitude",
default="",
help="Provide the latitude of the location you wish to retrieve the forecast for."
)
parser.add_argument(
"-x",
"--longitude",
action="store",
dest="longitude",
default="",
help="Provide the longitude of the location you wish to retrieve the forecast for."
)
parser.add_argument(
"-k",
"--apikey",
action="store",
dest="apikey",
default="",
help="REQUIRED: Your WDH API Credentials."
)
args = parser.parse_args()
timesteps = args.timesteps
includeLocation = args.includeLocation
excludeMetadata = args.excludeMetadata
latitude = args.latitude
longitude = args.longitude
apikey = args.apikey
# Client API key must be supplied
if apikey == "":
print("ERROR: API credentials must be supplied.")
sys.exit()
else:
requestHeaders = {"apikey": apikey}
if latitude == "" or longitude == "":
print("ERROR: Latitude and longitude must be supplied")
sys.exit()
if timesteps != "hourly" and timesteps != "three-hourly" and timesteps != "daily":
print("ERROR: The available frequencies for timesteps are hourly, three-hourly or daily.")
sys.exit()
print (retrieve_forecast(base_url, timesteps, requestHeaders, latitude, longitude, excludeMetadata, includeLocation))