Skip to content

Commit

Permalink
Add --api_url.
Browse files Browse the repository at this point in the history
  • Loading branch information
ways committed Jan 3, 2024
1 parent a788bbb commit 8647ad2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions app/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()


Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions app/test_initialize.py
Original file line number Diff line number Diff line change
@@ -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 = ""

Expand All @@ -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)
Expand Down

0 comments on commit 8647ad2

Please sign in to comment.