-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New API gatway. Requires new API key.
- Loading branch information
Showing
3 changed files
with
144 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# 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)) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters