From 8647ad236ef573011c16f567dfce8567dbb9998b Mon Sep 17 00:00:00 2001 From: Lars Falk-Petersen Date: Wed, 3 Jan 2024 13:31:14 +0100 Subject: [PATCH] Add --api_url. --- app/initialize.py | 13 +++++++++---- app/test_initialize.py | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/initialize.py b/app/initialize.py index 6d01c00..cd9fcf2 100644 --- a/app/initialize.py +++ b/app/initialize.py @@ -11,8 +11,6 @@ from grib import ISOBARIC_LABEL, TEMPERATURE_LABEL - -API_URL = "https://api.met.no/weatherapi/isobaricgrib/1.0/grib2?area=southern_norway" dataset = xr.Dataset() logger = logging.getLogger() @@ -33,6 +31,12 @@ def parse_args() -> argparse.Namespace: default="0.0.0.0", required=False, ) + parser.add_argument( + "--api_url", + help="URL to download grib file from", + default="https://api.met.no/weatherapi/isobaricgrib/1.0/grib2?area=southern_norway", + required=False, + ) return parser.parse_args() @@ -57,7 +61,7 @@ def open_grib(): filename = DATAFILE else: if not check_gribfile_exists(data_path=get_data_path(), fname=DATAFILE): - filename = download_gribfile(data_path=get_data_path()) + filename = download_gribfile(data_path=get_data_path(), api_url=API_URL) try: dataset = xr.open_dataset(filename, engine="cfgrib") @@ -126,7 +130,7 @@ def check_gribfile_exists(data_path: str, fname: str) -> bool: return True -def download_gribfile(data_path: str, api_url: str = API_URL) -> str: +def download_gribfile(data_path: str, api_url: str) -> str: """Ensure data dir exists, download latest file. Returns filename.""" try: os.mkdir(data_path) @@ -159,3 +163,4 @@ def download_gribfile(data_path: str, api_url: str = API_URL) -> str: DATAFILE = args.file BASE_URL = args.base_url BIND_HOST = args.bind_host +API_URL = args.api_url diff --git a/app/test_initialize.py b/app/test_initialize.py index 4684d7e..3eddb76 100644 --- a/app/test_initialize.py +++ b/app/test_initialize.py @@ -1,7 +1,7 @@ import unittest import os from datetime import datetime -from initialize import build_gribfile_name, download_gribfile +from initialize import build_gribfile_name, download_gribfile, API_URL datafile = "" @@ -22,7 +22,7 @@ def test_build_gribfile_name2(self): def test_download_gribfile(self): global datafile - datafile = download_gribfile(data_path="/tmp") + datafile = download_gribfile(data_path="/tmp", api_url=API_URL) print(datafile) self.assertTrue(os.path.isfile(datafile)) os.remove(datafile)