Skip to content

Commit

Permalink
Merge pull request #49 from GliderGeek/fix-bug-with-missing-task-info
Browse files Browse the repository at this point in the history
make more resilient by adding support for missing task info
  • Loading branch information
GliderGeek authored Jan 21, 2024
2 parents 2f94190 + 8ad034e commit fb44669
Show file tree
Hide file tree
Showing 4 changed files with 23,827 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Removed
~~~~~~~~~
Fixed
~~~~~~~~
* allow for soaringspot files without task info
Security
~~~~~~~~~

Expand Down
23 changes: 14 additions & 9 deletions opensoar/competition/soaringspot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import datetime
import re
from typing import List, Tuple, Union
from typing import List, Tuple, Union, Optional
from urllib.error import URLError
from urllib.parse import urljoin

Expand Down Expand Up @@ -46,7 +46,7 @@ def get_task_rules(lseeyou_tsk_line: str) -> Tuple[datetime.time, datetime.timed
return start_opening, t_min, multi_start


def get_info_from_comment_lines(parsed_igc_file: dict, start_time_buffer: int=0) -> Tuple[Task, dict, dict]:
def get_info_from_comment_lines(parsed_igc_file: dict, start_time_buffer: int=0) -> Tuple[Optional[Task], dict, dict]:
"""
There is specific contest information stored in the comment lines of the IGC files.
This function extracts this information
Expand Down Expand Up @@ -87,12 +87,16 @@ def get_info_from_comment_lines(parsed_igc_file: dict, start_time_buffer: int=0)
# convert start opening to UTC time
start_opening = subtract_times(start_opening, datetime.time(hour=timezone))

waypoints = get_waypoints(lcu_lines, lseeyou_lines)

if t_min is None:
task = RaceTask(waypoints, timezone, start_opening, start_time_buffer, multi_start)
if len(lcu_lines) == 0 or len(lseeyou_lines) == 0:
# somehow some IGC files do not contain the LCU or LSEEYOU lines with task information
task = None
else:
task = AAT(waypoints, t_min, timezone, start_opening, start_time_buffer, multi_start)
waypoints = get_waypoints(lcu_lines, lseeyou_lines)

if t_min is None:
task = RaceTask(waypoints, timezone, start_opening, start_time_buffer, multi_start)
else:
task = AAT(waypoints, t_min, timezone, start_opening, start_time_buffer, multi_start)

return task, contest_information, competitor_information

Expand Down Expand Up @@ -324,7 +328,7 @@ def generate_competition_day(self, target_directory: str, download_progress=None
# download files. skip if not valid
try:
file_path = self.download_flight(igc_url, competition_id)
except URLError:
except URLError as e:
print('{} is skipped because of invalid URL'.format(competition_id))
continue

Expand Down Expand Up @@ -356,7 +360,8 @@ def generate_competition_day(self, target_directory: str, download_progress=None
competitor = Competitor(trace, competition_id, plane_model, ranking, pilot_name)

competitors.append(competitor)
tasks.append(task)
if task is not None:
tasks.append(task)

# Select task from tasks list
task = self._select_task(tasks)
Expand Down
22 changes: 20 additions & 2 deletions tests/competition/test_soaringspot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import unittest

import os
import datetime

from opensoar.competition.soaringspot import get_lat_long, get_fixed_orientation_angle, get_sector_orientation, \
get_sector_dimensions, get_waypoint, get_waypoints, SoaringSpotDaily, get_task_rules
get_sector_dimensions, get_waypoint, get_waypoints, SoaringSpotDaily, get_task_rules, get_info_from_comment_lines
from opensoar.task.waypoint import Waypoint
from opensoar.task.task import Task

from aerofiles.igc import Reader


class TestSoaringspot(unittest.TestCase):

Expand Down Expand Up @@ -178,3 +180,19 @@ def test_get_task_rules(self):

self.assertEqual(start_opening, datetime.time(13, 29, 0))
self.assertEqual(t_min, datetime.timedelta(hours=3, minutes=30, seconds=0))

def test_get_info_from_comment_lines_no_lcu_no_lseeyou(self):
"""
Some IGC files from soaringspot seem to miss the task information written in LCU and LSEEYOU lines
This test checks that the function then return a None task.
"""

cwd = os.path.dirname(__file__)
igc_path = os.path.join(cwd, '..', 'igc_files', 'missing_lcu_lseeyou_lines.igc')

# try: # try utf-8
with open(igc_path, 'r', encoding='latin1') as f:
parsed_igc_file = Reader().read(f)

task, contest_information, competitor_information = get_info_from_comment_lines(parsed_igc_file)
self.assertIsNone(task, None)
Loading

0 comments on commit fb44669

Please sign in to comment.