Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async all the things that were blocking. #90

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions env_canada/ec_radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ def compute_bounding_box(distance, latittude, longitude):
return lat_min, lon_min, lat_max, lon_max


async def _image_open(bytes, mode):
loop = asyncio.get_running_loop()
image = await loop.run_in_executor(None, Image.open, BytesIO(bytes))
return image.convert(mode)


async def _load_font(font_file):
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, ImageFont.load, font_file)


class ECRadar(object):
def __init__(self, **kwargs):
"""Initialize the radar object."""
Expand Down Expand Up @@ -163,10 +174,7 @@ def __init__(self, **kwargs):
self.legend_position = None

self.show_timestamp = kwargs["timestamp"]
if self.show_timestamp:
self.font = ImageFont.load(
os.path.join(os.path.dirname(__file__), "10x20.pil")
)
self.font = None

@property
def precip_type(self):
Expand Down Expand Up @@ -198,7 +206,7 @@ async def _get_basemap(self):
async with ClientSession(raise_for_status=True) as session:
response = await session.get(url=basemap_url, params=basemap_params)
base_bytes = await response.read()
self.map_image = Image.open(BytesIO(base_bytes)).convert("RGBA")
self.map_image = await _image_open(base_bytes, "RGBA")

except ClientConnectorError as e:
logging.warning("NRCan base map could not be retrieved: %s" % e)
Expand All @@ -209,7 +217,7 @@ async def _get_basemap(self):
url=backup_map_url, params=basemap_params
)
base_bytes = await response.read()
self.map_image = Image.open(BytesIO(base_bytes)).convert("RGBA")
self.map_image = await _image_open(base_bytes, "RGBA")
except ClientConnectorError:
logging.warning("Mapbox base map could not be retrieved")

Expand All @@ -225,7 +233,7 @@ async def _get_legend(self):
async with ClientSession(raise_for_status=True) as session:
response = await session.get(url=geomet_url, params=legend_params)
legend_bytes = await response.read()
self.legend_image = Image.open(BytesIO(legend_bytes)).convert("RGB")
self.legend_image = await _image_open(legend_bytes, "RGB")
gwww marked this conversation as resolved.
Show resolved Hide resolved
legend_width = self.legend_image.size[0]
self.legend_position = (self.width - legend_width, 0)
self.legend_layer = self.layer_key
Expand Down Expand Up @@ -256,7 +264,7 @@ async def _get_dimensions(self):
async def _combine_layers(self, radar_bytes, frame_time):
"""Add radar overlay to base layer and add timestamp."""

radar = Image.open(BytesIO(radar_bytes)).convert("RGBA")
radar = await _image_open(radar_bytes, "RGBA")

# Add transparency to radar

Expand Down Expand Up @@ -285,6 +293,10 @@ async def _combine_layers(self, radar_bytes, frame_time):
# Add timestamp

if self.show_timestamp:
if not self.font:
self.font = await _load_font(
os.path.join(os.path.dirname(__file__), "10x20.pil")
)
timestamp = (
timestamp_label[self.layer_key][self.language]
+ " @ "
Expand Down
Loading