Skip to content

Commit d9cc2b4

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

4 files changed

Lines changed: 61 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
Utilities
33
"""
44

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

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)