Skip to content

Commit 4aaca4e

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

2 files changed

Lines changed: 54 additions & 1 deletion

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 send_result_mail, request_opengribs_file
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
22
Utilities
33
"""
4+
import os
5+
import tempfile
46

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

0 commit comments

Comments
 (0)