Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/api/aiohttp-3.8.5
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad authored Jul 20, 2023
2 parents c1ea4ff + af46371 commit 8179fd5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 93 deletions.
12 changes: 12 additions & 0 deletions api/app/tests/weather_models/test_process_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ def test_calculate_wind_direction_from_uv():
calculated_wind_direction = process_grib.calculate_wind_dir_from_u_v(
test_case['u_float'], test_case['v_float'])
assert round(calculated_wind_direction, 0) == test_case['expected_wind_direction']


def test_convert_mps_to_kph():
metres_per_second_speed = 1
kilometres_per_hour_speed = process_grib.convert_mps_to_kph(metres_per_second_speed)
assert kilometres_per_hour_speed == 3.6 # 1m/s * 3600 sec/hour / 1000m/km


def test_convert_mps_to_kph_zero_wind_speed():
metres_per_second_speed = 0
kilometres_per_hour_speed = process_grib.convert_mps_to_kph(metres_per_second_speed)
assert kilometres_per_hour_speed == 0
18 changes: 15 additions & 3 deletions api/app/weather_models/process_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get_transformer(crs_from, crs_to):


def calculate_wind_speed_from_u_v(u: float, v: float):
""" Return calculated wind speed from u and v components using formula
""" Return calculated wind speed in metres per second from u and v components using formula
wind_speed = sqrt(u^2 + v^2)
What the heck is going on here?! See
Expand All @@ -158,6 +158,12 @@ def calculate_wind_dir_from_u_v(u: float, v: float):
return calc if calc > 0 else 360 + calc


def convert_mps_to_kph(value: float):
""" Convert a value from metres per second to kilometres per hour.
"""
return value / 1000 * 3600


class GribFileProcessor():
""" Instances of this object can be used to process and ingest a grib file.
"""
Expand Down Expand Up @@ -224,10 +230,13 @@ def get_wind_dir_values(self, u_points: List[int], zipped_uv_values):
return (u_points, wind_dir_values)

def get_wind_speed_values(self, u_points: List[int], zipped_uv_values):
""" Get calculated wind speed values for list of points and zipped u,v values """
""" Get calculated wind speed values in kilometres per hour for a list of points
and zipped u,v values """
wind_speed_values = []
for u, v in zipped_uv_values:
wind_speed_values.append(calculate_wind_speed_from_u_v(u, v))
metres_per_second_speed = calculate_wind_speed_from_u_v(u, v)
kilometres_per_hour_speed = convert_mps_to_kph(metres_per_second_speed)
wind_speed_values.append(kilometres_per_hour_speed)
return (u_points, wind_speed_values)

def get_variable_name(self, grib_info: ModelRunInfo) -> str:
Expand Down Expand Up @@ -295,6 +304,9 @@ def process_env_can_grib_file(self, session: Session, dataset, grib_info: ModelR
raster_band = dataset.GetRasterBand(1)
# Iterate through stations:
for (points, values) in self.yield_data_for_stations(raster_band):
# Convert wind speed from metres per second to kilometres per hour
if grib_info.variable_name.lower().startswith("apcp_sfc"):
values = [convert_mps_to_kph(value) for value in values]
self.store_bounding_values(
points, values, prediction_run, grib_info, session)

Expand Down
Loading

0 comments on commit 8179fd5

Please sign in to comment.