Skip to content

Commit a95a59f

Browse files
committed
Use opengribs.org API
1 parent e66529d commit a95a59f

5 files changed

Lines changed: 63 additions & 3 deletions

File tree

opendrift_leeway_webgui/leeway/tasks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.utils import timezone
99

1010
from .celery import app
11-
from .utils import send_result_mail
11+
from .utils import request_opengribs_file, send_result_mail
1212

1313
logger = logging.getLogger(__name__)
1414

@@ -24,6 +24,7 @@ def run_leeway_simulation(request_id):
2424
simulation = LeewaySimulation.objects.get(uuid=request_id)
2525
simulation.simulation_started = timezone.now()
2626
simulation.save()
27+
get_opengribs_data(simulation.longitude, simulation.latitude)
2728
params = [
2829
"docker",
2930
"run",
@@ -50,6 +51,7 @@ def run_leeway_simulation(request_id):
5051
str(simulation.duration),
5152
"--id",
5253
str(simulation.uuid),
54+
"--no-web",
5355
]
5456
with subprocess.Popen(
5557
params, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True

opendrift_leeway_webgui/leeway/utils.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
Utilities
33
"""
44

5+
import os
6+
import tempfile
7+
8+
import requests
59
from django.apps import apps
10+
from django.conf import settings
611
from django.contrib.auth import get_user_model
712
from django.core.mail import EmailMessage, send_mail
813
from dms2dec.dms_convert import dms2dec
@@ -235,3 +240,55 @@ def normalize_dms2dec(data):
235240
if "°" in data:
236241
return dms2dec(data)
237242
return data
243+
244+
245+
def create_opengribs_bounding_box(longitude, latitude):
246+
"""
247+
Calculate Bounding Box for GRIBS file download.
248+
"""
249+
return {
250+
"long_min": longitude - 1,
251+
"long_max": longitude + 1,
252+
"lat_min": latitude - 1,
253+
"lat_max": latitude + 1,
254+
}
255+
256+
257+
def request_opengribs_file(bounding_box):
258+
"""
259+
Request GRIBS file on opengribs.org
260+
"""
261+
request = requests.get(
262+
url=(
263+
f"http://grbsrv.opengribs.org/getmygribs2.php?osys=Unknown&ver=1.2.6&model=icon_p25_&"
264+
f"la1={bounding_box['lat_min']}&la2={bounding_box['lat_max']}&"
265+
f"lo1={bounding_box['long_min']}&lo2={bounding_box['long_max']}&"
266+
f"intv=3&days=8&cyc=last&par=W;&wmdl=none&wpar=h;d;p;"
267+
)
268+
)
269+
return request.json()["message"]["url"]
270+
271+
272+
def get_opengribs_file(url):
273+
"""
274+
Get OpenGRIBS file and save to disk.
275+
"""
276+
response = requests.get(url, stream=True)
277+
if response.status_code != 200:
278+
return
279+
total_size = int(response.headers.get("content-length", 0))
280+
os.makedir(os.path.join(settings.SIMULATION_ROOT, "input"))
281+
filename = os.path.join(settings.SIMULATION_ROOT, "input", url.split("/")[-1])
282+
with open(filename, "wb") as f:
283+
for chunk in response.iter_content(chunk_size=1024):
284+
f.write(chunk)
285+
return filename
286+
287+
288+
def get_opengribs_data(longitude, latitde):
289+
"""
290+
Get data from OpenGRIBS
291+
"""
292+
bbox = create_opengribs_bounding_box(longitude, latitude)
293+
download_url = request_opengribs_file(bbox)
294+
return get_opengribs_file(download_url)

opendrift_leeway_webgui/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def main():
8888
sources = [
8989
os.path.join(INPUTDIR, data_file)
9090
for data_file in os.listdir(INPUTDIR)
91-
if data_file.endswith(".nc")
91+
if data_file.endswith(".nc") or data_file.endswith(".grb2")
9292
]
9393

9494
if not args.no_web:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"dms2dec",
2626
"drf-spectacular",
2727
"redis",
28+
"requests",
2829
]
2930

3031
[project.urls]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
""" Setup.py """
2+
"""Setup.py"""
33
from setuptools import setup
44

55
setup()

0 commit comments

Comments
 (0)